pinojs / pino-opentelemetry-transport

OpenTelemetry transport for Pino
MIT License
24 stars 3 forks source link

About traceId and spanId #189

Closed clifinger closed 3 weeks ago

clifinger commented 1 month ago

Just to let you know,

To have the traceId and spanId with @opentelemetry/auto-instrumentations-node I had to specify it:

import { pino } from "pino";
import { context, trace } from "@opentelemetry/api";

const transport = pino.transport({
    target: "pino-opentelemetry-transport",
});

const logger = pino(
    {
        mixin() {
            const span = trace.getSpan(context.active());
            if (span) {
                return {
                    traceId: span.spanContext().traceId,
                    spanId: span.spanContext().spanId,
                };
            }
            return {};
        },
    },
    transport,
);

export default logger;

export type Logger = typeof logger;

Best regards

Vunovati commented 3 weeks ago

Hello @clifinger, did you try the trace-context example? This is the most convenient way to have spanId and traceId in your logs.

If that's not acceptable for some reason, check this issue where we discussed an alternative to using pino instrumentation: https://github.com/pinojs/pino-opentelemetry-transport/issues/160

clifinger commented 3 weeks ago

Hello @Vunovati,

To be honest, I like the way we currently handle observability in our project. I don't want my team to spend too much time implementing observability in new projects or functions.

For now, we just use:

 --require '@opentelemetry/auto-instrumentations-node/register'

and

import { pino } from "pino";
import { context, trace } from "@opentelemetry/api";

const transport = pino.transport({
    target: "pino-opentelemetry-transport",
});

const logger = pino(
    {
        mixin() {
            const span = trace.getSpan(context.active());
            if (span) {
                return {
                    traceId: span.spanContext().traceId,
                    spanId: span.spanContext().spanId,
                };
            }
            return {};
        },
    },
    transport,
);

export default logger;

export type Logger = typeof logger;

We inject it, and everything works automatically.

Screenshot 2024-09-27 151823 (1)