Skip to content
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

Undefined traceId and spanId with ECMAScript? #172

Open
c0j0s opened this issue May 20, 2024 · 2 comments
Open

Undefined traceId and spanId with ECMAScript? #172

c0j0s opened this issue May 20, 2024 · 2 comments

Comments

@c0j0s
Copy link

c0j0s commented May 20, 2024

Hi,

I am trying to get pino to report logs back to OTelCollector. The pipeline is working except for the traceId and spanId fields being undefined.

This only happens in ECMAScript modules, cjs works as expected.

Not an expert on this, please enlighted me.

// test.mjs
'use strict';

import process from 'process';
import * as opentelemetry from '@opentelemetry/sdk-node';
import { PinoInstrumentation } from '@opentelemetry/instrumentation-pino';
import { Resource } from '@opentelemetry/resources';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
import {
  SemanticResourceAttributes
} from '@opentelemetry/semantic-conventions';
import { trace } from '@opentelemetry/api';
import pino from 'pino';

const sdk = new opentelemetry.NodeSDK({
  resource: new Resource({
    [SemanticResourceAttributes.SERVICE_NAME]: 'Pino OpenTelemetry Example'
  }),
  traceExporter: new OTLPTraceExporter(),
  instrumentations: [new PinoInstrumentation()]
});

sdk.start();
console.log("=========SDK Started=========");

process.on('SIGTERM', () => {
  sdk
    .shutdown()
    .then(() => console.log('Tracing terminated'))
    .catch(error => console.log('Error terminating tracing', error))
    .finally(() => process.exit(0));
});

const transport = pino.transport({
  target: 'pino-opentelemetry-transport',
  options: {
    logRecordProcessorOptions: [
      {
        recordProcessorType: 'simple',
        exporterOptions: { protocol: 'console' }
      }
    ],
    loggerName: 'test',
    serviceVersion: '0.0.1'
  }
});

const logger = pino(transport);

setInterval(() => {
  const tracer = trace.getTracer("test-tracer");
  tracer.startActiveSpan('span-name', span => {
    logger.info('Hello time now: ' + new Date().toISOString());
    span.end();
  });
}, 5000);
// test.cjs
'use strict';

const process = require('process');
const opentelemetry = require('@opentelemetry/sdk-node');
const { PinoInstrumentation } = require('@opentelemetry/instrumentation-pino');
const { Resource } = require('@opentelemetry/resources');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http');
const {
  SemanticResourceAttributes
} = require('@opentelemetry/semantic-conventions');
const { trace } = require('@opentelemetry/api');

const sdk = new opentelemetry.NodeSDK({
  resource: new Resource({
    [SemanticResourceAttributes.SERVICE_NAME]: 'Pino OpenTelemetry Example'
}),
traceExporter: new OTLPTraceExporter(),
  instrumentations: [new PinoInstrumentation()]
});

sdk.start();
console.log("=========SDK Started=========");

process.on('SIGTERM', () => {
    sdk
    .shutdown()
    .then(() => console.log('Tracing terminated'))
    .catch(error => console.log('Error terminating tracing', error))
    .finally(() => process.exit(0));
});

const pino = require('pino');
const transport = pino.transport({
    target: 'pino-opentelemetry-transport',
    options: {
        logRecordProcessorOptions: [
            {
                recordProcessorType: 'simple',
                exporterOptions: { protocol: 'console' }
      }
    ],
    loggerName: 'test',
    serviceVersion: '0.0.1'
  }
});

const logger = pino(transport);

setInterval(() => {
  const tracer = trace.getTracer("test-tracer");
  tracer.startActiveSpan('span-name', span => {
    logger.info('Hello time now: ' + new Date().toISOString());
    span.end();
  });
}, 5000);

Thank you.

@Vunovati
Copy link
Collaborator

Hello, my first hunch is that this is possibly due to pino-instrumentation and the way it works with ESM. There is nothing ESM/CJS specific in the pino-opentelemetry-transport code. I'll try to debug this to confirm this with certainty.

@Vunovati
Copy link
Collaborator

Vunovati commented May 22, 2024

This seems to be a tough problem. I was not able to get pino-instrumentation to work when using ESM. I converted the trace-context into .mjs and I stopped getting trace and span ids. I also had this problem when I did not use pino-opentelemetry-transport at all.

It seems like getting ESM to work is not straightforward. I guess that's why it is not documented. Currently, most instrumentations when using ESM are working via import-in-the-middle package. This issue (open-telemetry/opentelemetry-js#4553) might be connected to the problem we are experiencing here.

It might be worth creating an issue in the pino-instrumentation repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants