Skip to content

Pino instrumentation not working when using ES modules #2838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ayoisaiah opened this issue May 20, 2025 · 5 comments
Open

Pino instrumentation not working when using ES modules #2838

ayoisaiah opened this issue May 20, 2025 · 5 comments
Labels
bug Something isn't working needs:author-response needs:reproducer This bug/feature is in need of a minimal reproducer pkg:instrumentation-pino

Comments

@ayoisaiah
Copy link

ayoisaiah commented May 20, 2025

What version of OpenTelemetry are you using?

What version of Node are you using?

Node.js 24

What did you do?

When I converted the example code in the @opentelemetry/instrumentation-pino package to ES module syntax, the instrumentation stopped working. But it works as expected when run using the original CommonJS format.

import { NodeSDK, tracing, logs, api } from "@opentelemetry/sdk-node";
import { PinoInstrumentation } from "@opentelemetry/instrumentation-pino";
import pino from "pino";

const sdk = new NodeSDK({
  spanProcessor: new tracing.SimpleSpanProcessor(
    new tracing.ConsoleSpanExporter(),
  ),
  logRecordProcessor: new logs.SimpleLogRecordProcessor(
    new logs.ConsoleLogRecordExporter(),
  ),
  instrumentations: [
    new PinoInstrumentation({
      // See below for Pino instrumentation options.
    }),
  ],
});
sdk.start();

const logger = pino();

logger.info("hi");
// 1. Log records will be sent to the SDK-registered log record processor, if any.
//    This is called "log sending".

const tracer = api.trace.getTracer("example");
tracer.startActiveSpan("manual-span", (span) => {
  logger.info("in a span");
  // 2. Fields identifying the current span will be added to log records:
  //    {"level":30,...,"msg":"in a span","trace_id":"d61b4e4af1032e0aae279d12f3ab0159","span_id":"d140da862204f2a2","trace_flags":"01"}
  //    This feature is called "log correlation".
});

What did you expect to see?

The trace_id and span_id in the logs, alongside their OTLP representation as is the case with the original CommonJS format:

{"level":30,"time":1747735809772,"pid":334794,"hostname":"Falcon","trace_id":"78765079501acd3516ae627d6c311455","span_id":"f67c2314b2cd42db","trace_flags":"01","msg":"in a span"}
{
  resource: {
    attributes: {
      'host.name': 'Falcon',
      'host.arch': 'amd64',
      'host.id': 'b8c8a123fc8c4017a34ac35736984212',
      'process.pid': 334794,
      'process.executable.name': '/home/ayo/.local/share/mise/installs/node/24.0.0/bin/node',
      'process.executable.path': '/home/ayo/.local/share/mise/installs/node/24.0.0/bin/node',
      'process.command_args': [
        '/home/ayo/.local/share/mise/installs/node/24.0.0/bin/node',
        '/home/ayo/dev/dash0/demo/pinojs-logging/pino.js'
      ],
      'process.runtime.version': '24.0.0',
      'process.runtime.name': 'nodejs',
      'process.runtime.description': 'Node.js',
      'process.command': '/home/ayo/dev/dash0/demo/pinojs-logging/pino.js',
      'process.owner': 'ayo',
      'service.name': 'unknown_service:/home/ayo/.local/share/mise/installs/node/24.0.0/bin/node',
      'telemetry.sdk.language': 'nodejs',
      'telemetry.sdk.name': 'opentelemetry',
      'telemetry.sdk.version': '2.0.1'
    }
  },
  instrumentationScope: {
    name: '@opentelemetry/instrumentation-pino',
    version: '0.48.0',
    schemaUrl: undefined
  },
  timestamp: 1747735809772000,
  traceId: '78765079501acd3516ae627d6c311455',
  spanId: 'f67c2314b2cd42db',
  traceFlags: 1,
  severityText: 'info',
  severityNumber: 9,
  body: 'in a span',
  attributes: {}
}

What did you see instead?

No trace_id or span_id. The OTLP console representation was also absent:

{"level":30,"time":1747735419210,"pid":334524,"hostname":"Falcon","msg":"in a span"}

Additional context

@ayoisaiah ayoisaiah added the bug Something isn't working label May 20, 2025
@pichlermarc
Copy link
Member

Hi @ayoisaiah, thanks for reaching out. Could it be that the loader is not registered that's required for ESM instrumentation?
For more info, please see https://github.com/open-telemetry/opentelemetry-js/blob/main/doc/esm-support.md

@ofekd
Copy link

ofekd commented May 22, 2025

I'm having similar problems with express. I am directly using import-in-the-middle, though, since the hook included with opentelemetry does not work with type stripping

@trentm
Copy link
Contributor

trentm commented May 28, 2025

@ofekd Could you please open a separate issue for the import-in-the-middle thing, and if you can show some of your code that would be very helpful.

@ayoisaiah
Copy link
Author

@pichlermarc It doesn't work even after registering the loader hook. Winston's instrumentation works fine though.

@pichlermarc
Copy link
Member

@ayoisaiah hmm, this works fine for me:
https://github.com/pichlermarc/repro-contrib-2838

Can you check if there are any differences from what I'm doing in the linked example and what you're doing?

@pichlermarc pichlermarc added needs:author-response needs:reproducer This bug/feature is in need of a minimal reproducer labels Jun 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs:author-response needs:reproducer This bug/feature is in need of a minimal reproducer pkg:instrumentation-pino
Projects
None yet
Development

No branches or pull requests

4 participants