Unable to create a Synthetic Monitor that runs successfully with a Java Cloud Function

I am trying to create a Synthetic Monitor, with Java 17 Runtime.

The function is a simple one, and the error I am seeing is

Failed to extract test result(s) from the cloud function response.
I tried various responses, however I don’t seem to find the right one. So what should the HttpResponse contain?

public class SyntheticFunction implements HttpFunction {

    @Override
    public void service(HttpRequest request, HttpResponse response) throws Exception {
        response.setContentType("text/plain");
        response.setStatusCode(200);
        response.getWriter().write("OK");
    }

Hi @rroxanaioana ,

Welcome to Google Cloud Community!

To fix the error “Failed to extract test result(s) from the cloud function response”, your Cloud Function needs to return a JSON object in the following format:

{
  "result": "success",
  "latency": <latency in milliseconds>,
  "details": {
    // additional information
  }
}

A updated version of your Cloud Function that yields a legitimate JSON object is as follows:

public class SyntheticFunction implements HttpFunction{
    @Override 
    public void service(HttpRequest request, HttpResponse response) throws Exception {
        response.setContentType("application/json");
        response.setStatusCode(200);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", "success");
        jsonObject.put("latency", System.currentTimeMillis() - request.getStartTime());
        response.getWriter().write(jsonObject.toString());
    }
}

As a result, Cloud Monitoring will be able to retrieve the test results from a valid JSON object.

You ought to be able to successfully establish a Synthetic Monitor after making the necessary modifications to your Cloud Function.

Reference:

Hi Christian,

Is Synthetic Monitor and the response payload workable in python too? I am also getting the same error in Python.

genericError: {
errorMessage: "Failed to extract test result(s) from the cloud function response."
errorType: "SYNTHETIC_INVALID_RESPONSE"
}