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

Select SpanProcessor to use in AutoConfiguration

Open radcortez opened this issue 2 years ago • 8 comments

Is your feature request related to a problem? Please describe. When using SDK AutoConfiguration, we have the ability to add our own SpanExporter via ServiceLoader and ConfigurableSpanExporterProvider, but there is no way to select which SpanProcessor to use:

https://github.com/open-telemetry/opentelemetry-java/blob/1e073fcff20697fd5f2eb39bd6246d06a1231089/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/TracerProviderConfiguration.java#L70-L87

Describe the solution you'd like The ConfigurableSpanExporterProvider could expose a new method that provides the SpanProcessor to use (either instance or name). The downside of an instance is that if you want to reuse the same processor type (like the batch one), you need to create one instance per exporter. If using a name to associate, then it requires to add the ability to auto register and configure SpanExporter via ServiceLoader and do the association by name.

I'm available to submit a PR for this.

radcortez avatar Nov 25 '21 13:11 radcortez

This issue probably belongs in https://github.com/open-telemetry/opentelemetry-java. @anuraaga @jkwatson (since you have powers in both repos) can you move it?

trask avatar Nov 28 '21 21:11 trask

Can you describe what your use case for needing to be able to select a span processor other than the batch span processor? The simple span processor isn't really meant for production use-cases outside of perhaps logging.

jkwatson avatar Nov 28 '21 21:11 jkwatson

For reference it looks like there is some related discussion in the specification

https://github.com/open-telemetry/opentelemetry-specification/pull/2172

But agree with @jkwatson that it'd be good to hear your use case - indeed we recommend avoiding the simple span processor for most apps.

anuraaga avatar Nov 29 '21 02:11 anuraaga

Can you describe what your use case for needing to be able to select a span processor other than the batch span processor? The simple span processor isn't really meant for production use-cases outside of perhaps logging.

The use case I have now is for testing. My exporter is always forced on the batch, which is fine for prod environments, but it would be great if you could have more control over it and either have a way to change the associated processor to the logging one (or even something else).

This is just if you use auto-configuration because if you use the SdkTracerProviderBuilder, you can wrap your SpanExporter in a SpanProcessor via https://github.com/open-telemetry/opentelemetry-java/blob/f90040579e10a4ab2c256501b04dffd4bbbb6518/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracerProviderBuilder.java#L132 and SimpleSpanProcessor.create().

Of course, I would prefer to have the ability to do it with auto-configuration instead of manually creating my own :)

radcortez avatar Nov 29 '21 12:11 radcortez

We actually special case when logging is set for e.g. OTLP_TRACES_EXPORTER

https://github.com/open-telemetry/opentelemetry-java/blob/main/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/TracerProviderConfiguration.java#L79

Does that not work for you? Or was it not clear that is the behavior for that variable? It is a bit magical

anuraaga avatar Nov 29 '21 12:11 anuraaga

Yes, I was aware of that. It does work. I just find it a little bit weird to use this magic name in a custom SpanExporter :)

BTW, another workaround I use is to call forceFlush in SdkTracerProvider if you are on a BatchSpanExporter. This requires some nasty reflection code due to the unobfuscate of the TracerProvider. Maybe this could be another solution, to provide a public access to forceFlush.

radcortez avatar Nov 29 '21 12:11 radcortez

I don't think we should add this ability until the specification has sorted out how all of this should work, given that you could have multiple exporters, processors, etc, and just having a single value doesn't make sense given that.

jkwatson avatar Nov 29 '21 21:11 jkwatson

To make it clear, the idea was not to select the SpanProcessor to use as suggested in https://github.com/open-telemetry/opentelemetry-specification/pull/2172, but to be able to influence in which SpanProcessor a SpanExporter will end up.

Even if you use the magic constant logging in your custom exporter, the auto configuration implementation will try to instantiate the logging exporter and not your own:

https://github.com/open-telemetry/opentelemetry-java/blob/1e073fcff20697fd5f2eb39bd6246d06a1231089/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/SpanExporterConfiguration.java#L95-L117

I believe it would be useful to be able to choose where do you want your custom exported to end up with (either batch, or logging, where batch is the default). Right now, any custom exporter loaded by the ServiceLoader in auto configuration will always go into the Batch exporter.

radcortez avatar Nov 30 '21 11:11 radcortez