Stackdriver error reporting with gcloud

2k Views Asked by At

I'm retying to use the gcloud cli to send events to StackDriver Error Reporting.
The (very limited) documentation is here: https://cloud.google.com/sdk/gcloud/reference/beta/error-reporting/events/report

Regardless of what I send as a message I seem to get this error:

ERROR: (gcloud.beta.error-reporting.events.report) INVALID_ARGUMENT: ReportedErrorEvent.context must contain a location unless message contain an exception or stacktrace.

I have tried formatting the message as a JSON representation of an error report: https://cloud.google.com/error-reporting/docs/formatting-error-messages but the message seems to be the same. Here's an example command and JSON:

gcloud beta error-reporting events report --service foo --message-file err.json

{
    "serviceContext": {
        "service": "foo"
    },
    "message": "Whoops!",
    "context": {
        "reportLocation": {
            "filePath": "/usr/local/bin/test",
            "lineNumber": 123,
            "functionName": "main"
        }
    }
}
4

There are 4 best solutions below

0
On BEST ANSWER

The gcloud --message or --message-field argument is just the message field of the reported error, not the entire JSON. Since you can't provide the reportLocation via gcloud, message has to be a stack trace or exception.

Using the API explorer with the original request works. Logging the error also makes its way into Error Reporting via:

gcloud beta logging write --payload-type=json test-errors-log '
{
    "serviceContext": {
        "service": "foo"
    },                        
    "message": "Whoops!",
    "context": {
        "reportLocation": {
            "filePath": "/usr/local/bin/test",
            "lineNumber": 123,
            "functionName": "main"
        }
    }
}'
0
On

gcloud beta error-reporting events report requires stacktrace from programming language to report it. It is artificial (and actually quite stupid requirement).

Minimal Java like stacktrace:

gcloud beta error-reporting events report --verbosity=debug \
  --service test --service-version test1 \
  --message "at a()"

You can append interesting data if you keep at<SP><CHARS>() inside message on a separate line (thanks for Bash $'\n' magic!):

gcloud beta error-reporting events report --verbosity=debug \
  --service test --service-version test1 \
  --message $'Hello world!\nat a()'
1
On

Yet another way.

gcloud logging write --payload-type=json test-errors-log '
{
  "serviceContext": {"service": "foo"},                        
  "message": "message with stacktrace\n at /test.js"
}
'
gcloud logging write --payload-type=json test-errors-log '
{
  "@type": "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent",
  "message": "message without stacktrace"
}
'
gcloud beta error-reporting events report --service foo --message 'message with stacktrace
at /test.js'
0
On

Try adding httpRequest details under context.

You can find details of the json payload in the documentation. The parent object in the json is ErrorEvent

You can start from there and find links to its sub-elements ServiceContext and ErrorContext

Though I have not tried the command line option, I debugged the stackdriver logging classes which we use in our app, to find out what json payload we are sending and here is the sample json fired from our application.

{
  "context": {
    "httpRequest": {
      "responseStatusCode": 500,
      "method": "GET",
      "url": "http://localhost:16500/product"
    },
    "user": "2247177"
  },
  "message": "org.springframework.web.client.HttpServerErrorException:  500 Server Error
  at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java: 94)
  ",
  "serviceContext": {
    "service": "cart",
    "version": ""
  }