pino-instrumentation not injecting trace_id into logs when used with TypeScript

See original GitHub issue

What version of OpenTelemetry are you using?

    "@opentelemetry/auto-instrumentations-node": "^0.25.0",
    "@opentelemetry/core": "^1.0.1",
    "@opentelemetry/exporter-collector": "^0.24.0",
    "@opentelemetry/exporter-prometheus": "^0.18.2",
    "@opentelemetry/instrumentation": "^0.24.0",
    "@opentelemetry/instrumentation-pino": "^0.28.1",
    "@opentelemetry/metrics": "^0.18.2",
    "@opentelemetry/node": "^0.24.0",
    "@opentelemetry/propagator-b3": "^1.0.0",
    "@opentelemetry/resources": "^0.18.2",
    "@opentelemetry/sdk-trace-node": "^1.0.1",
    "@opentelemetry/semantic-conventions": "^1.0.0",
    "pino": "^7.6.4",
   "express": "^4.17.1",
  "my-logger": "file: ../my-logger"

What version of Node are you using?

v16.6.1

What did you do?

“my-logger” is a separate typescript project on my machine, here is the simple implementation which instantiates an instance of pino and exports as default. https://gist.github.com/armandAkop/3e064c2e378cdd8a442b339f3b5e28f6

I am running “tsc” in the repo and outputting the transpiled file to “dist/index.js”, which contains

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const pino_1 = require("pino");
const logger = pino_1.pino;
exports.default = (0, pino_1.pino)();
//# sourceMappingURL=index.js.map

Here is the script I am testing with auto-instrumentation-node and pino-instrumentation. This script lives in a folder called project-A and has a local reference to “my-logger” project on line 157 https://gist.github.com/armandAkop/d2fb6f806b31b6844262918489bf0679

What did you expect to see?

I expected my test scripts log entry to contain the trace_id field

What did you see instead?

trace_id is not present in the log message. When I change the my-logger project to be a regular Javascript project with an index.js which looks like

module.exports = require('pino')();

AND changed package.json to “main” property to “main”: “index.js”

The trace_id appears in the log as expected

Additional context

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:15 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
Flarnacommented, Jan 28, 2022

Ported to typescript and there seems to be a bug in the instrumentation resulting in non working setups depending on how you import and if esModuleInterop is set in tsconfig.js. see below for details.

import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { PinoInstrumentation } from '@opentelemetry/instrumentation-pino';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import * as opentelemetry from '@opentelemetry/api';

const provider = new NodeTracerProvider();
provider.register();

registerInstrumentations({
  instrumentations: [
    new PinoInstrumentation({
      // Optional hook to insert additional context to log object.
      logHook: (span, record) => {
        record['resource.service.name'] = provider.resource.attributes['service.name'];
      },
    }),
    // other instrumentations
  ],
});

// ok
import pino = require('pino');
const logger = (pino as any)();

// not ok
//import { pino } from 'pino';
//const logger = pino();

// ok if "esModuleInterop": true is in tsconfig.js
//import pino from 'pino';
//const logger = pino();

const tracer = opentelemetry.trace.getTracer('example-basic-tracer-node');
tracer.startActiveSpan('init', (span) => {
    for (let i = 0; i < 3; i++) {
        logger.info(`expect trace_id to be in this log ${i}`);
    }
    span.end();
});

root cause is that pino exports itself several times:

  • the export object itself (case const pino = require('pino')
  • as pino property on the export (case `import { pino } from ‘pino’)
  • as default property on the export (not sure when this is used, maybe by import pino from 'pino' if esModuleInterop: false)

The instrumentation covers only the first case above.

0reactions
Rauno56commented, Jun 28, 2022

We don’t have a component owner for the pino instrumentation, but it was contributed by @seemk. Would you care to take a look, Siim?

Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript TraceId and SpanId injection into logs
Configuring traceId, spanId, and trace flags data injection into user logs in JavaScript applications is simple.
Read more >
Log injection doesn't seem to be working with pino · Issue #2189
I am pretty new to both dd-trace and pino, but I cannot figure out why the dd object is not showing up in...
Read more >
Connecting Node.js Logs and Traces - Datadog Docs
Connect your Node.js logs and traces to correlate them in Datadog. ... If you are using a logging library not supported for automatic...
Read more >
@opentelemetry/instrumentation-pino - npm
Start using @opentelemetry/instrumentation-pino in your project by running `npm i @opentelemetry/instrumentation-pino`.
Read more >
Logging with Pino and AsyncLocalStorage in Node.js
In this post, you can learn what a logging library is, why you should use it, and how to log with Pino and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found