Skip to content

Export getCurrentTraceFromAgent for correlation with traces #459

@draperunner

Description

@draperunner

I'd like to correlate logs with traces. Your example in the README explains how to get the key (LoggingWinston.LOGGING_TRACE_KEY), but not how to specify the correct value (your example: custom-trace-value). I checked the code for your middleware, and found getCurrentTraceFromAgent. I copied this, modified it a little to avoid the global variable, and it works perfectly.

My modified version of getCurrentTraceFromAgent + implementation:

import { LoggingWinston } from '@google-cloud/logging-winston'
import { get as getTracerAgent } from '@google-cloud/trace-agent'

function getCurrentTraceFromAgent(): string | null {
    const agent = getTracerAgent()
    if (!agent || !agent.getCurrentContextId || !agent.getWriterProjectId) {
        return null
    }

    const traceId = agent.getCurrentContextId()
    if (!traceId) {
        return null
    }

    const traceProjectId = agent.getWriterProjectId()
    if (!traceProjectId) {
        return null
    }

    return `projects/${traceProjectId}/traces/${traceId}`
}

logger.info(`Response ${req.method} ${req.url}`, {
      [LoggingWinston.LOGGING_TRACE_KEY]: getCurrentTraceFromAgent(),
      ...myLogData
})

Describe the solution you'd like

I would like this getCurrentTraceFromAgent function to be exported as well, so I could do something like this:

import { LoggingWinston } from '@google-cloud/logging-winston'

logger.info(`Response ${req.method} ${req.url}`, {
      [LoggingWinston.LOGGING_TRACE_KEY]: LoggingWinston.getCurrentTraceFromAgent(),
      ...myLogData
})

Metadata

Metadata

Assignees

Labels

api: loggingIssues related to the googleapis/nodejs-logging-winston API.priority: p2Moderately-important priority. Fix may not be included in next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions