gapic-generator-java icon indicating copy to clipboard operation
gapic-generator-java copied to clipboard

Support OpenTelemetry for Java

Open electrum opened this issue 2 years ago • 2 comments

Currently, it seems the only way to add tracing using OpenTelemetry is via the legacy OpenCensus shim. It would be great to have native OpenTelemetry support with the ability to provide the OpenTelemetry or Tracer instance.

electrum avatar Nov 26 '23 17:11 electrum

@electrum Thanks for opening this issue! Can you please provide more info regarding this request? Like how you are using OpenCensus tracing in your project for the SDKs right now? What features are you expecting with OpenTelemetry? Do you need metrics or traces or both? I'm assuming traces first since you mentioned Tracer? It would be great if you can provide a sample project with your use cases as well.

blakeli0 avatar Nov 27 '23 22:11 blakeli0

@blakeli0 I've done this exact thing at a client of mine. I am unfortunately not at liberty to share their program code. It's a fairly simple change: the existing OpenCensusTracer{,Factory}{,Test} classes have to be parallel-implemented using the OpenTelemetry Tracing API's instead of the OpenCensus ones. It's entirely drop-in: any existing code that news up an OpenCensusTracerFactory as part of the sdk setup can new up an OpenTelemetryTracerFactory instead.

The upshot is that OpenTelemetry-native projects (such as the one at my client) can drop the OpenTelemetry-OpenCensus-Shim dependency from their build entirely, which greatly simplifies application startup. Because of the reliance on singleton globals, it's prone to init-order problems.

Both families of tracing API's are very close; it's not quite a case of copying the code and changing the imports, but it's not far off either.

One of the avoidable traps in implementing this is to provide enough constructors. It's convenient for a default constructor to reference the GlobalOpenTelemetry singleton, but please also provide a constructor that accepts a TracerProvider instance provided explicitly.

barend-xebia avatar Feb 16 '24 10:02 barend-xebia