googleapis / nodejs-logging

Node.js client for Stackdriver Logging: Store, search, analyze, monitor, and alert on log data and events from Google Cloud Platform and Amazon Web Services (AWS).
https://cloud.google.com/logging/
Apache License 2.0
170 stars 62 forks source link

Keep original labels like "instanceId" #1451

Open JudithvW opened 11 months ago

JudithvW commented 11 months ago

Is your feature request related to a problem? Please describe. A Cloud Run instance can handle multiple requests. Also, multiple Cloud Run instances could be running. It is currently not visible which Cloud Run instance is logging.

Describe the solution you'd like If I use console.log in my NodeJS application, the labels in the log will show the labels of the Cloud Run Service, as well as an "instanceId". When I use this package, those 'default' labels in the logging disappear. I want those labels to be present, even when I add other labels.

Additional context image Example of default google logging (console.log), which shows the Cloud Run labels and an instanceId.

image Example of logging with this package, which removed the original labels and only added the labels set by this package.

cindy-peng commented 7 months ago

Hi @JudithvW, looks like I don't have access to your screenshot. Could you provide a sample code how you write log so I can try repro?

JudithvW commented 7 months ago
const { Logging } = require("@google-cloud/logging");

class Logger {
  constructor() {
    this.logging = new Logging({
      projectId: process.env.GCP_PROJECT,
    });

    this.log = this.logging.log("microservices");

    this.metadata = {
      labels: {},
    };
  }

  setMetadataField(fieldName, fieldValue) {
    this.metadata.labels[fieldName] = fieldValue;
  }

  // NB: message can be a string, but can also be a JSON object (not an Array!)

  async debug(message) {
    const entry = this.log.entry(this.metadata, message);

    await this.log.debug(entry);
  }

  async info(message) {
    const entry = this.log.entry(this.metadata, message);

    await this.log.info(entry);
  }

  async warn(message) {
    const entry = this.log.entry(this.metadata, message);

    await this.log.warning(entry);
  }

  async error(message) {
    const entry = this.log.entry(this.metadata, message);

    await this.log.error(entry);
  }
}

module.exports = Logger;
ali-yunes commented 2 weeks ago

What was the solution to this? I am facing a similar issue