Is there a way to add custom attributes to the automatically generated logs for Cloud Run services?
const app = express();
app.use((req: any, res, next) => {
const myCustomHeader = req.get('x-my-custom-header');
// TODO: Add my custom header to Cloud Run automatic logs
next();
});
I would like to add myCustomHeader
to the Cloud Logging output (e.g. to its httpRequest
object):
{
httpRequest: {
myCustomHeader: "my custom value here",
… // Default attributes set by Cloud Run
},
insertId: "5c82b3d1000ece0000000000",
labels: {
instanceId: "someid",
mylabel: "mylabelvalue",
mysecondlabel: "mysecondlabelvalue",
},
logName: "projects/my-project/logs/run.googleapis.com%2Frequests",
receiveTimestamp: "2019-03-08T18:26:25.981686167Z",
resource: {
labels: {
configuration_name: "myservice",
location: "us-central1",
project_id: "my-project",
revision_name: "myservice-00002",
service_name: "myservice",
}
type: "cloud_run_revision",
},
severity: "INFO",
timestamp: "2019-03-08T18:26:25.970397Z",
}
As per this official documentation
When you write logs from your service or job, they will be picked up automatically by Cloud Logging so long as the logs are written to any of these locations:
Standard output (stdout) or standard error (stderr) streams
Any files under the /var/log directory
syslog (/dev/log)
Logs written using Cloud Logging client libraries, which are available for many popular languages
Most developers are expected to write logs using standard output and standard error.
So if you want to add some custom attributes to the logs follow the steps given in the documentation for generating logs.