opentelemetry-js icon indicating copy to clipboard operation
opentelemetry-js copied to clipboard

[@opentelemetry/instrumentation-http] Default traces have just "GET" as their name

Open alexandros-megas opened this issue 6 months ago • 2 comments

What happened?

Steps to Reproduce

I'm using otel instrumentation in a next.js 12 app, with a basic HttpInstrumentation. All traces that are captured show up with the same name: "GET". It would be nice to see different traces based on path, or maybe even route if possible.

I know that nextjs uses express under the hood to serve my app, so maybe the express instrumentation might be more helpful?

Expected Result

I should be able to see different traces based on method/path, or maybe have a hook called traceNameHook that lets me generate the name, and maybe has type (req: IncomingMessage) => string or something like that.

Actual Result

image

Additional Details

I also tried customizing the span with the following requestHook implementation, but no luck...

      new HttpInstrumentation({
        requestHook(span, req) {
          span.updateName(`${req.method} ${req.path}`);
        },
      }),

OpenTelemetry Setup Code

const util = require('util');
const process = require('process');
const opentelemetry = require('@opentelemetry/sdk-node');
const { BatchSpanProcessor } = require('@opentelemetry/sdk-trace-node');
const SemanticAttrs = require('@opentelemetry/semantic-conventions');
const { Resource } = require('@opentelemetry/resources');
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http');
const { publicRuntimeConfig: configuration } = require('./configuration');
const { diag, DiagLogLevel, DiagConsoleLogger } = require('@opentelemetry/api');

const traceEndpoint = process.env.OTEL_TRACE_EXPORTER_ENDPOINT;

/**
 * Set default options for util.inspect, so that console logging of objects
 * prints on a single line, and doesn't truncate anything.
 */
util.inspect.defaultOptions = {
  ...(util.inspect.defaultOptions || {}),
  breakLength: Number.POSITIVE_INFINITY,
  compact: true,
};

if (!traceEndpoint) {
  console.info('OTEL_TRACE_EXPORTER_ENDPOINT not set. Skipping OTEL SDK registration');
}
else {
  console.info('Registering OTEL SDK...');

  diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO);

  registerInstrumentations({
    instrumentations: [
      new HttpInstrumentation(),
    ],
  });

  const traceExporter = new OTLPTraceExporter({
    url: traceEndpoint,
  });
  console.info(`OTLPTraceExporter created with url ${traceEndpoint}`);

  const resource = Resource.default().merge(
    new Resource({
      [SemanticAttrs.SEMRESATTRS_SERVICE_NAME]: configuration.NAME,
      [SemanticAttrs.SEMRESATTRS_SERVICE_VERSION]: configuration.VERSION,
    }),
  );

  const spanProcessor = new BatchSpanProcessor(traceExporter);

  // Create and register an SDK
  const sdk = new opentelemetry.NodeSDK({
    serviceName: configuration.NAME,
    autoDetectResources: true,
    resource,
    traceExporter,
    spanProcessors: [spanProcessor],
  });

  sdk.start();

  // Handle shutdown
  const shutdown = () => {
    return sdk
      .shutdown().then(
        () => console.log('SDK shut down successfully'),
        (err) => console.log('Error shutting down SDK', err),
      ).finally(() => process.exit(0));
  };
  process.on('exit', shutdown);
  process.on('SIGINT', shutdown);
  process.on('SIGTERM', shutdown);

  console.info('OTEL SDK Ready.');
}

package.json

"@opentelemetry/api": "^1.9.0",
    "@opentelemetry/exporter-metrics-otlp-http": "^0.52.1",
    "@opentelemetry/exporter-trace-otlp-http": "^0.52.1",
    "@opentelemetry/instrumentation": "^0.52.1",
    "@opentelemetry/instrumentation-http": "^0.52.1",
    "@opentelemetry/resources": "^1.25.1",
    "@opentelemetry/sdk-node": "^0.52.1",
    "@opentelemetry/sdk-trace-base": "^1.25.1",
    "@opentelemetry/sdk-trace-node": "^1.25.1",
    "@opentelemetry/semantic-conventions": "^1.25.1",

Relevant log output

No response

alexandros-megas avatar Aug 28 '24 01:08 alexandros-megas