I am using Error Reporting to report any error in my Cloud Run services and Cloud Run jobs.
In a Node.js application, you can instantiate Error Reporting like this:
const error_reporting = new ErrorReporting()
Which is equivalent to this, since logLevel defaults to 2 and reportMode to 'production'.
const error_reporting = new ErrorReporting({
logLevel: 2,
reportMode: 'production'
})
In my error handler I am constructing an error event manually, so I can add some additional information (user, user agent, etc).
const reportErrorAndExit = (err) => {
const event = error_reporting.event()
event.setMessage(err.message)
event.setUser('the service account used by this Cloud Run service/job')
error_reporting.report(event, () => {
// exit with an exit code !=0 so the Cloud Run job marks
// this particular task run as a failure
process.exit(1)
})
}
With this configuration, errors in a Cloud Run job show the user that encountered the error, but contain little information about the service that generated the error event:
Luckily, you can also instantiate Error Reporting with a serviceContext:
const error_reporting = new ErrorReporting({
logLevel: 2,
reportMode: 'production',
serviceContext: {
service: 'invoice-generator',
version: 'latest'
}
})
When you do that, the generic Service node will be replaced by a more descriptive invoice-generator:latest.
I thought that the Error Reporting client would have been able to authenticate with Application Default Credentials (ADC) and retrieve the service/job name and revision automatically from its execution environment. But apparently it's not.
I think the only way to configure the Error Reporting serviceContext with the currently active Cloud Run service/job revision, is to retrieve the revision from the service/job itself. What's the recommended approach to do that?

With Cloud Run jobs, you haven't revisions, it's not a Knative service.
However, I listed all the env vars and I got this:
The
CLOUD_RUN_JOBis the name of my Job. You could use it as service name. You have also the version of the executionCLOUD_RUN_EXECUTION; it changes to all run of your jobs.They might help you to configure your error reporting as you wish.