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

Alternative to deprecated `addSpanProcessor`

Open satazor opened this issue 10 months ago • 6 comments
trafficstars

Is your feature request related to a problem? Please describe

I'm using the OpenTelemetry Node SDK to initialize OpenTelemetry because it does most of the work of setting it up. Besides the default span processor, I want to add some more span processors. I was using addSpanProcessor but it was deprecated here: https://github.com/open-telemetry/opentelemetry-js/pull/5134

At first, spanProcessors constructor option of NodeSDK seems designed for this use-case, but it has a shortcoming: you must specify the extra ones plus the default BatchSpanProcessor. However, the BatchSpanProcessor requires an instance of exporter, that it's created automatically for us via environment variables. Currently, it's not feasible to create an exporter based on environment variables without duplicating code, since the code that does it is hidden via private functions, e.g.: _buildExporterFromEnv and _getSpanExporter within BasicTracerProvider class.

Describe the solution you'd like to see

I would like an easy way to add span processors without having to duplicate the initialization of BatchSpanProcessor and the initialization of the exporter based on environment variables.

Approach 1: Allow spanProcessors to be a function in order to append / prepend processors:

new NodeSDK({
  spanProcessors: (spanProcessors) => [mySpanProcessor, ...spanProcessors],
});

The same logic could be applied to logRecordProcessors .

Approach 2: Export the necessary functionality to create stuff from env, such as exposing _buildExporterFromEnv, _buildPropagatorFromEnv, etc.

Describe alternatives you've considered

  • Keep using the deprecated addSpanProcessor for now.
  • Duplicate the code necessary to build an exporter from env to set up BatchSpanProcessor

satazor avatar Dec 26 '24 02:12 satazor

I think the approach 2 would be quite straight forward. The utility function to get processors from the environment vars is located at https://github.com/open-telemetry/opentelemetry-js/blob/fc0edd82f6749bfe1ca733ed1f80ad4a6015285f/experimental/packages/opentelemetry-sdk-node/src/utils.ts#L125

With that function being exported you could do

new NodeSDK({
  spanProcessors: [mySpanProcessor, ...getSpanProcessorsFromEnv()],
});

cc: @open-telemetry/javascript-maintainers

david-luna avatar Jan 16 '25 17:01 david-luna

@david-luna yes that was also my first thought when reading this - but it adds to the public API though which I'd like to avoid wherever possible.

I'd like to gauge how many people would actually need this first before exporting this. We're overwhelmed with the amount of features we offer today and I think we might want to focus on features first that are likely used by a significant chunk of users.

That being said, I don't want to block this feature for no reason, so if anyone reading this wants/needs this feature, please react with a "👍" emoji to the original issue above so that we can get a rough idea of how much interest there is to get this in.

@satazor I'm curious, since the NodeSDK auto-setup does not offer public access to TracerProvider, how did you add a SpanProcessor before addSpanProcessor was deprecated?

pichlermarc avatar Jan 17 '25 13:01 pichlermarc

@pichlermarc after calling start(), we are doing this:

const proxyTracerProvider = trace.getTracerProvider() as ProxyTracerProvider;
const tracerProvider = proxyTracerProvider.getDelegate() as NodeTracerProvider;

spanProcessors.forEach(spanProcessor => tracerProvider.addSpanProcessor(spanProcessor));

Where spanProcessors is an array of extra processors.

satazor avatar Jan 17 '25 20:01 satazor

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.

github-actions[bot] avatar Apr 07 '25 06:04 github-actions[bot]

Hey everyone.

This issue became now important for me since I'm upgrading to otel v2 and the workaround of using the deprecated addSpanProcessor is no longer viable since it got removed.

There's another workaround that involves replacing _activeSpanProcessor on the tracer provider but it's even uglier.

Would you be ok to expose getSpanProcessorsFromEnv from utils.ts so we can build the span processors from the outside, considering the default ones based on ENV vars?

satazor avatar May 04 '25 15:05 satazor

As a more okeish workaround, I'm importing

import { getSpanProcessorsFromEnv } from '@opentelemetry/sdk-node/build/src/utils';

and using it like so:

const sdk = new NodeSDK({
  spanProcessors: [...getSpanProcessorsFromEnv(), new FooProcessor()],
  // ... other stuff
});

I confirm everything works as expected, so having getSpanProcessorsFromEnv exported directly would be great. I can follow up with a PR if you everyone agrees.

satazor avatar May 07 '25 17:05 satazor

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.

github-actions[bot] avatar Aug 18 '25 06:08 github-actions[bot]

This issue was closed because it has been stale for 14 days with no activity.

github-actions[bot] avatar Sep 08 '25 06:09 github-actions[bot]