1
votes

I'm trying to set up Stackdriver Winston logging in a node.js application (running on localhost, to be deployed to Docker containers in Kubernetes). When running normally, an "Invalid response from metadata service: incorrect Metadata-Flavor header" exception is thrown.

Debugging locally on http://localhost:8080. I'm using a service account in GCP with Log-Writer, Admin, and Log-Reader permissions. I've tried both setting the GOOGLE_APPLICATION_CREDENTIALS env variable and explicit auth when setting up the logger.

Package versions are "winston": "^3.2.1" and "@google-cloud/logging-winston": "^0.11.0".

const { createLogger, format, transports } = require('winston');
const { LoggingWinston } = require('@google-cloud/logging-winston');

const loggingWinston = new LoggingWinston({
    projectId: 'projectid',
    keyFilename: 'path_to_key"
  });

module.exports = createLogger({
    transports:[
        new transports.Console({
            format: format.combine(
                format.timestamp(),
                format.json()
            )
        }),
        loggingWinston
    ]
})

log.info('Test our logging');
log.error('Test logging again');

No logged messages in GCP Stackdriver log viewer, winston_log not available in log type dropdown list, and "Invalid response from metadata service: incorrect Metadata-Flavor header" exception thrown at "node_modules\gcp-metadata\build\src\index.js:65:23" when trying to log messages.

1

1 Answers

0
votes

TL;DR: If you test your code on a GCE VM, you should be able to get further.

Logging to Stackdriver requires what's called a monitored resource. Normally, the Stackdriver logging clients will attempt to auto-detect the attributes needed to construct the monitored resource by querying the GCE instance metadata from the metadata server, which requires the Metadata-Flavor header. The code expects the response to have the same Metadata-Flavor header as the one that was sent in.

When you run outside of a GCE VM, the metadata server will normally be unreachable. However, you seem to be actually getting a response that doesn't contain the Metadata-Flavor header. Since the presence of the header is checked first, that is the error you see.