Stackdriver execution_id and custom labels
Asked Answered
D

1

11

I'm using Google Cloud Functions and the module @google-cloud/logging-winston.

My first issue is when using this module the property

labels: {
  execution_id: "idHere"
}

is not in the Stackdriver logs, like it is when I use the regular console.log(''); is it possible to get that execution_id back into the logs?

Also, can I add custom labels to a log, for instance a user ID. I'm trying to make my logging process easier for myself so I can see the flow for each API endpoint request via execution_id and also be able to see all logs by a certain user via adding a user ID label.

My current logging code is

'use strict'

import * as winston from 'winston';
const Logger = winston.Logger;
const Console = winston.transports.Console;

import { LoggingWinston } from '@google-cloud/logging-winston'
const loggingWinston = new LoggingWinston();

const logger = new Logger({
    level: 'debug', // log at 'debug' and above
    transports: [
        // Log to the console
        new Console(),
        // And log to Stackdriver Logging
        loggingWinston,
    ],
});

function formatMessage(message) {
    return message;
};

export const error = (message) => {
    logger.error(formatMessage(message));
}
export const warn = (message) => {
    logger.warn(formatMessage(message));
}
export const verbose = (message) => {
    logger.verbose(formatMessage(message));
}
export const info = (message) => {
    logger.info(formatMessage(message));
}
export const debug = (message) => {
    logger.debug(formatMessage(message));
}
Delacroix answered 15/2, 2018 at 12:24 Comment(1)
Did you find an answer for this @Delacroix ?Callimachus
D
-2

From the GCP docs, you should be able to write a structured log line like these examples and Winston specific example.

It looks something like this

// A json log entry with additional context
const metadata = {
  severity: 'WARNING',
  labels: {
    baz: 'bax',
    'one-fish': 'two-fish',
  },
};

log.info('Hello log line', metadata);
Dosser answered 6/10, 2021 at 17:28 Comment(1)
this is really not helpful - the question is - how do we keep the execution_id label from beforeQuerida

© 2022 - 2024 — McMap. All rights reserved.