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

Custom context manager not respected when span processors / exporters are configured via env vars #4776

Closed
plengauer opened this issue Jun 8, 2024 · 1 comment · Fixed by #4781
Assignees
Labels
bug Something isn't working pkg:sdk-node priority:p2 Bugs and spec inconsistencies which cause telemetry to be incomplete or incorrect

Comments

@plengauer
Copy link

plengauer commented Jun 8, 2024

What happened?

Steps to Reproduce

Configure env vars to any kind of trace exporter.
Use the NodeSDK and configure a custom context manager via the ctor.

Expected Result

custom context manager is used

Actual Result

custom context manager is not used

Additional Details

When setting a span processor or trace exporter via the NodeSDK ctor, this causes an internal TracerProvider config to be eagerly created. In this case, the context manager is copied over from the SDK config to the created TracerProvider config. Later, in the start call, this config is respected when registering the tracer provider by getting the context manager from the config object and passing it on to the register call.

When NOT setting a span processor or trace exporter, no config object is eagerly created. In this case, the register call does not check the config of the SDK (where the context manager would be) but passes on no context manager at all.

OpenTelemetry Setup Code

class EnvVarContextManager {
  inner;
  
  constructor(inner) {
    this.inner = inner;
  }

  enable() {
    return this;
  }

  disable() {
    return this;
  }

  active() {
    let context = this.inner.active();
    if (opentelemetry_api.ROOT_CONTEXT == context) {
      context = new opentelemetry_sdk.core.W3CTraceContextPropagator().extract(context, { traceparent: process.env.OTEL_TRACEPARENT }, opentelemetry_api.defaultTextMapGetter);
    }
    return context;
  }

  bind(...args) {
    return this.inner.bind(...args);
  }

  with(...args) {
    return this.inner.with(...args);
  }

}

let sdk = new opentelemetry_sdk.NodeSDK({
    contextManager: new EnvVarContextManager(new context_async_hooks.AsyncLocalStorageContextManager()),
    //spanProcessor: new BatchSpanProcessor(new OTLPTraceExporter()) // add this back in to fix the custom context manager
    instrumentations: [getNodeAutoInstrumentations({'@opentelemetry/instrumentation-fs': { enabled: false }})]
  });

sdk.start()

package.json

"@opentelemetry/resources": "1.25.0",
    "@opentelemetry/semantic-conventions": "1.25.0",
    "@opentelemetry/api": "1.9.0",
    "@opentelemetry/core": "1.25.0",
    "@opentelemetry/sdk-node": "0.52.0",
    "@opentelemetry/sdk-trace-base": "1.25.0",
    "@opentelemetry/exporter-trace-otlp-proto": "0.52.0",
    "@opentelemetry/auto-instrumentations-node": "0.47.0",
    "@opentelemetry/exporter-metrics-otlp-proto": "0.52.0",
    "opentelemetry-resource-detector-git": "0.30.0",
    "@opentelemetry/resource-detector-github": "0.28.2",
    "@opentelemetry/resource-detector-container": "0.3.10",
    "@opentelemetry/resource-detector-aws": "1.5.1",
    "@opentelemetry/resource-detector-gcp": "0.29.10",
    "@opentelemetry/resource-detector-alibaba-cloud": "0.28.10",

Relevant log output

No response

@pichlermarc
Copy link
Member

Yep, that's a bug. Thanks for the detailed report @plengauer.
I opened a PR (#4781) to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pkg:sdk-node priority:p2 Bugs and spec inconsistencies which cause telemetry to be incomplete or incorrect
Projects
None yet
2 participants