Skip to content

Add java guidance for native instrumentation and manual instrumentation#8618

Merged
cartermp merged 8 commits into
open-telemetry:mainfrom
jack-berg:native-instrumentation-advice
Dec 19, 2025
Merged

Add java guidance for native instrumentation and manual instrumentation#8618
cartermp merged 8 commits into
open-telemetry:mainfrom
jack-berg:native-instrumentation-advice

Conversation

@jack-berg
Copy link
Copy Markdown
Member

@jack-berg jack-berg commented Dec 9, 2025

@jack-berg
Copy link
Copy Markdown
Member Author

Please take a look @open-telemetry/java-approvers, @open-telemetry/java-instrumentation-approvers.

Comment thread content/en/docs/languages/java/api.md Outdated
Comment thread content/en/docs/languages/java/api.md Outdated
Comment thread content/en/docs/languages/java/api.md Outdated
Comment on lines +107 to +115
Native instrumentation has an interesting interaction with the OpenTelemetry
Java agent: On startup, the Java agent initializes an
[OpenTelemetry](../api/#opentelemetry)) instance and installs
[zero-code](#zero-code-java-agent) instrumentation. Libraries adding native
instrumentation should allow users to customize the `OpenTelemetry` instance
used, but ideally should automatically use the instance initialized by the Java
agent (if present). See [GlobalOpenTelemetry](../api/#globalopentelemetry) for
guidance on how to accomplish this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@JonasKunz @SylvainJuge would be great to get this into the Elasticsearch native instrumentation!

Comment thread content/en/docs/languages/java/instrumentation.md Outdated
Comment thread content/en/docs/languages/java/instrumentation.md Outdated
Comment thread content/en/docs/languages/java/sdk.md Outdated
Copy link
Copy Markdown
Member

@theletterf theletterf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this one! An initial review.

Comment thread content/en/docs/languages/java/api.md Outdated

The following code snippet explores `GlobalOpenTelemetry` API context
propagation:
`GlobalOpenTelemetry` is designed in a very particular way to avoid
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "particular" imply here? Can we be more specific? Or just say "is designed to avoid..."

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Particular implies something like "surprising" or "make sure you read it twice". We're in a big of a bind with GlobalOpenTelemetry with there being no design that isn't surprising in some cases to some people.

The next sentence starting with "Specifically, " elaborates on what is particular about the design.

Comment on lines +107 to +109
Native instrumentation has an interesting interaction with the OpenTelemetry
Java agent: On startup, the Java agent initializes an
[OpenTelemetry](../api/#opentelemetry)) instance and installs
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Native instrumentation has an interesting interaction with the OpenTelemetry
Java agent: On startup, the Java agent initializes an
[OpenTelemetry](../api/#opentelemetry)) instance and installs
Native instrumentation interacts with the OpenTelemetry Java agent
in the following way: On startup, the Java agent initializes an
[OpenTelemetry](../api/#opentelemetry)) instance and installs

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rephrased but went in a slightly different direction. Let me know what you think.

written by application authors, and typically specific to the application
domain.

Manual instrumentation has an interesting interaction with the OpenTelemetry
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto (previous comment).

@jack-berg jack-berg marked this pull request as ready for review December 10, 2025 15:26
@otelbot-docs otelbot-docs Bot requested review from a team December 10, 2025 15:26
@jack-berg jack-berg requested a review from a team as a code owner December 10, 2025 15:26
@jack-berg
Copy link
Copy Markdown
Member Author

Ping @open-telemetry/docs-maintainers. Anything still blocking merging this?


### GlobalOpenTelemetry

{{% alert title="Java agent" %}} The Java agent is a special case where
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a replacement tl;dr for the impacient?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT of something like:

Use GlobalOpenTelemetry.getOrNoop() if your native instrumentation trying to integrate with the otel java agent. Use GlobalOpenTelemetry.isSet() ? GlobalOpenTelemetry.get() : initializeSdk() if your an application owner writing manual instrumentation and trying to integrate with the otel java agent. Otherwise, don't use GlobalOpenTelemetry. See below for more details.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would first mention to use *get" when you want to add manual instrumentation, because I believe this is what most users are trying to do.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I think its wrong to do so. Or at least not recommended. If there's manual instrumentation in an app that never uses the javaagent, just don't use the global and pass around OpenTelemetry using constructors or dependency injection. If there's manual instrumentation in an app that uses the java agent, use the GlobalOpenTelemetry.isSet() ? GlobalOpenTelemetry.get() : initializeSdk() pattern so you don't inadvertently cause side affects with GlobalOpenTelemetry.get().

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A very common case is to have the agent always - packaged in the docker image.
Those users should not have to think about sdk questions at all.
I should have been more specific about that.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. I think my guidance to these users would still to be use GlobalOpenTelemetry.getOrNoop().

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea - then we can discourage get() altogether.

I created #8690

Copy link
Copy Markdown
Contributor

@cartermp cartermp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving for merge despite some continuing discussion. We can potentially tweak guidance further in another PR if folks feel it's needed.

@cartermp cartermp dismissed theletterf’s stale review December 19, 2025 14:55

Comments addressed

@cartermp cartermp added this pull request to the merge queue Dec 19, 2025
Merged via the queue into open-telemetry:main with commit 53114ad Dec 19, 2025
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Decide / document advice for native instrumentations

6 participants