google-cloud-platformgoogle-cloud-rungoogle-cloud-error-reporting

How to retrieve name and revision of a Cloud Run service, from the service itself?


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:

enter image description here

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?


Solution

  • 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:

    List all the env vars of the service
    
    The value of "HOME" env var is: "/home"
    The value of "PATH" env var is: "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
    The value of "CLOUD_RUN_TASK_INDEX" env var is: "0"
    The value of "CLOUD_RUN_TASK_COUNT" env var is: "1"
    The value of "CLOUD_RUN_TASK_ATTEMPT" env var is: "0"
    The value of "CLOUD_RUN_EXECUTION" env var is: "job-list-env-hnpq5"
    The value of "CLOUD_RUN_JOB" env var is: "job-list-env"
    

    The CLOUD_RUN_JOB is the name of my Job. You could use it as service name. You have also the version of the execution CLOUD_RUN_EXECUTION; it changes to all run of your jobs.

    They might help you to configure your error reporting as you wish.