I am using Google Compute Engine Instance running on Debian for running a service written in Node.js.
For logging purpose, I am using Stackdriver logging. The logs are getting generated, however, The log entries are not getting filtered under proper GCE Instance name. Here's how the resource flags are in a log entry
resource: {
labels: {
instance_id: "6000000000000000000" //A numeric id
project_id: "my-project"
zone: ""
}
type: "gce_instance"
}
The instance id is getting generated as a numeric id, but the name of the instance is not generated inside the resource labels, thus, I have to check for numeric id after selecting GCE Instance in Stackdriver and it doesn't come under the name of the instance. Also if I select the instance and click on the View logs option, it set the stackdriver flag filter for the instance name, not the instance id, thus I don't get proper logs too. The name parameter is not getting generated automatically. Here's what it should be
resource: {
labels: {
instance_id: "6000000000000000000" //A numeric id
name: "instance-name"
project_id: "my-project"
zone: ""
}
type: "gce_instance"
}
I am not adding any custom labels while writing a log entry, thus I am assuming it should generate automatically.
Here's my logging service code.
const Logging = require('@google-cloud/logging');
function write(data) {
const logging = new Logging();
const logObj = logging.log('my-service');
const logData = data.logData;
const logText = data.logText;
const severity = data.severity || 'DEFAULT';
const httpRequest = data.httpRequest;
// The metadata associated with the entry
const metadata = {
severity: severity,
httpRequest: httpRequest
};
const logPayload = {
text: logText,
data: logData
};
// Prepares a log entry
const entry = logObj.entry(metadata, logPayload);
await logObj.write(entry);
}
Here's how I call it -
loggingService.write({
httpRequest: httpRequest,
logText: 'Text Data',
logData: logDataDump.dump,
severity: loggingService.DEBUG
});
So, is there any way to auto-generate the instance name in the resource flag while logging to Stackdriver?