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
172 stars 62 forks source link

getEntries() always returns an empty result #1379

Closed Shnatsel closed 10 months ago

Shnatsel commented 1 year ago

Environment details

Steps to reproduce

  1. Copy the official sample to a JS file that can be run separately
  2. Fulfill the TODO in the sample by setting a log name
  3. Wrap the async function in a synchronous function that gets arguments from CLI
  4. Run it

For me this resulted in the following code:

'use strict';

function main(projectId) {
  const {Logging} = require('@google-cloud/logging');

  // Creates a client
  const logging = new Logging();

  /**
   * TODO(developer): Uncomment the following line to run the code.
   */
  // I have logs from this log name that I can see in Cloud Logging and fetch via Go samples,
  // but you can set this to any valid log name
  const log = logging.log(`projects/${projectId}/logs/batch_task_logs`);

  async function printEntryMetadata() {
    // List the most recent entries for a given log
    // See https://googleapis.dev/nodejs/logging/latest/Logging.html#getEntries
    const [entries] = await log.getEntries();
    console.log('Logs:');
    entries.forEach(entry => {
      const metadata = entry.metadata;
      console.log(`${metadata.timestamp}:`, metadata[metadata.payload]);
    });
  }
  printEntryMetadata();
  // [END batch_job_logs]
}

process.on('unhandledRejection', err => {
  console.error(err.message);
  process.exitCode = 1;
});
main(...process.argv.slice(2));

which can be run with node logging-bug-repro.js YOUR_PROJECT_ID

I have tried writing this myself before discovering the sample, and converged on basically the same code as the sample, which still always returns no elements. So I suspect the issue is with the client library rather than the sample.

The official sample is also not covered by tests.

Similar code in Go and Python works fine.

RastogiAbhijeet commented 1 year ago

Hey, I faced this similar issue but for me the issue was that I was deploying my code to cloud run which was basically overwriting the logName in my case and the Logname that I passed to the logging.log("log-name") was buried in the jsonPayload.

Eg:

image
losalex commented 1 year ago

Thanks @Shnatsel for opening this issue and really sorry for late reply! Apparently, it seems that printEntryMetadata is async function and I believe that no results shown since your program exits before the printEntryMetadata is executed. In order to make this work, I believe you could use printEntryMetadata().then() for example, so for me it worked as expected. We definitely need to fix a sample though.

@RastogiAbhijeet, unfortunately for serverless environments logName cannot be overriden if you use LogSync - we have an open issue for this and awaiting for a fix. As of now the only way to see "original" logName is embedded to jsonPayload.

RastogiAbhijeet commented 1 year ago

@losalex Any specific reason for doing so? Also Can I attempt to fix that one? ( If you can please point me to the issue ?

Thanks 😁

losalex commented 1 year ago

@RastogiAbhijeet, sorry for late response - when LogSync is used, it prints log records to stdout. All stdout console output is picked up by serverless environments automatically and during the process logName is changed - thats why a fix for this issue cannot be done in logging library.

cindy-peng commented 10 months ago

Closing the issue as the empty entries issue is related to asynchronous tasks not called properly, and LogName overriding is the expected behavior when using LogSync.