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

Fully initialize OpenTelemetry items during start-up #9489

Merged
merged 2 commits into from
Nov 13, 2024

Conversation

tjquinno
Copy link
Member

@tjquinno tjquinno commented Nov 12, 2024

Description

Resolves #9488

The helidon-microprofile-telemetry component includes CDI producer methods for OpenTelemetry tracing types and the Helidon tracing counterparts backed by our OTel implementation.

Helidon provides these producer methods on an app-scoped bean. This works fine in a pure CDI world; the first time CDI needs to produce a tracer (for example), it prepares the OpenTelemetryProducer app-scoped bean which contains the producer methods. Part of that bean's post-construct code assigns the newly-created OTel tracer as the Helidon global tracer and subsequent retrievals of the global tracer return the correct, fully-featured tracer.

A problem can arise if user code accesses the global tracer before CDI has had to prepare the producer. In that case the code that accesses the global tracer triggers the creation of the default, no-op OTel tracer and assigns that to the global tracer.

This PR adds an observer method to the existing Helidon OTel telemetry CDI extension which forces CDI to create the producer bean during start-up. This ensures that the global tracer is set correctly before any user code gains control and potentially accesses the global tracer.

The PR also includes a test which reproduced the problem and ensures that the global tracer is set correctly as part of start-up, before user code gains control.

Documentation

No impact.

@tjquinno tjquinno self-assigned this Nov 12, 2024
@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Nov 12, 2024
@@ -62,4 +68,12 @@ void processAnnotations(@Observes @WithAnnotations(WithSpan.class) ProcessAnnota
}
}
}

void finish(@Observes @Priority(Interceptor.Priority.LIBRARY_BEFORE) @Initialized(ApplicationScoped.class) Object startup,
Tracer tracer) {
Copy link
Member

Choose a reason for hiding this comment

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

Have to refresh my memory here but I'm not fully sure whether the mere appearance of a Tracer client proxy here will cause its underlying target instance to be created. You may have to invoke a business method on it to force proxy "inflation". toString shouldn't do the trick, but IIRC it does.

Copy link
Member

Choose a reason for hiding this comment

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

(The relevant section of the specification is probably the one on client proxy invocations, not business methods; sorry about that.)

Copy link
Member Author

Choose a reason for hiding this comment

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

This is interesting. TRACE-level logging is not turned on during the build (and therefore when the unit test runs) nor would it likely be at runtime, so even toString is not likely to be invoked.

That said, during the build the new unit test failed before I added that observer method to the extension and passed afterwards.

To be safer I can revise the observer method to invoke enabled--not inherited from Object as the spec warns against--on the Tracer object.

Copy link
Member

Choose a reason for hiding this comment

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

Right; for posterity, you can call toString. It is both the only method that has defined semantics for client proxies and fulfills the proxy inflation conditions.

@tjquinno tjquinno merged commit 51b942a into helidon-io:main Nov 13, 2024
46 checks passed
@tjquinno tjquinno deleted the 4.x-otel-mp-init branch November 13, 2024 14:26
@barchetta barchetta mentioned this pull request Nov 13, 2024
12 tasks
barchetta pushed a commit to barchetta/helidon that referenced this pull request Dec 3, 2024
* Fully initialize OpenTelemetry items during start-up

* Invoke method on CDI proxy for tracer from extension observer method to ensure the bean is fully created

---------

Signed-off-by: Tim Quinn <[email protected]>
barchetta added a commit that referenced this pull request Dec 4, 2024
* Fully initialize OpenTelemetry items during start-up
* Invoke method on CDI proxy for tracer from extension observer method to ensure the bean is fully created

---------

Signed-off-by: Tim Quinn <[email protected]>
Co-authored-by: Tim Quinn <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OCA Verified All contributors have signed the Oracle Contributor Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4.x Global tracer not set as part of Helidon MP start-up with OpenTelemetry
2 participants