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

[Prototype] Move Context interaction to Tracer #1753

Closed

Conversation

carlosalberto
Copy link
Contributor

Prototype PR for open-telemetry/opentelemetry-specification#1019, specifically inspired by @mwear's proposal - mostly as an alternative presented by @bogdandrutu.

Changes are:

  • TracingContextUtils is hidden, and remains as an implementation specific detail.
  • All the interaction happens through the Tracer (and it would happen through BaggageManager for the baggage part). This means we end up with the following default methods in Tracer:
    • Span Tracer.getCurrentSpan()
    • Span Tracer.getCurrentSpan(Context context)
    • Scope Tracer.withSpan(Span span)
    • Context Tracer.contextWithSpan(Span span)
  • A notion of a 'default' tracer is added (not to confuse with the current DefaultTracer class) - that is, OpenTelemetry.getTracer() which gets a Tracer with a default name.

Under this case, most of the interaction would happen through such default tracer:

Span span = OpenTelemetry.getTracer().getCurrentSpan(ctx);

Which is slightly odd, as any code that calls this will automatically force the loading of tracer/meter/baggage, even if they didn't intend to (for example, a Propagator having its inject/extract facilities could run into this). Not sure that's an actual problem, but wanted to mention it anyway.

For your consideration ;)

@codecov
Copy link

codecov bot commented Oct 5, 2020

Codecov Report

Merging #1753 into master will decrease coverage by 0.31%.
The diff coverage is 93.54%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #1753      +/-   ##
============================================
- Coverage     85.55%   85.24%   -0.32%     
+ Complexity     1372     1369       -3     
============================================
  Files           163      164       +1     
  Lines          5316     5319       +3     
  Branches        554      554              
============================================
- Hits           4548     4534      -14     
- Misses          567      584      +17     
  Partials        201      201              
Impacted Files Coverage Δ Complexity Δ
...ain/java/io/opentelemetry/trace/DefaultTracer.java 96.96% <ø> (-0.18%) 3.00 <0.00> (-2.00)
...va/io/opentelemetry/trace/TracingContextUtils.java 87.50% <ø> (-12.50%) 6.00 <0.00> (-1.00)
...xt/src/main/java/io/opentelemetry/Application.java 0.00% <0.00%> (ø) 0.00 <0.00> (ø)
...ain/java/io/opentelemetry/sdk/trace/TracerSdk.java 100.00% <ø> (ø) 4.00 <0.00> (-2.00)
...i/src/main/java/io/opentelemetry/trace/Tracer.java 80.00% <80.00%> (ø) 4.00 <4.00> (?)
.../src/main/java/io/opentelemetry/OpenTelemetry.java 98.18% <100.00%> (+0.03%) 23.00 <1.00> (+1.00)
...ntelemetry/trace/propagation/HttpTraceContext.java 96.46% <100.00%> (ø) 32.00 <0.00> (ø)
...xtensions/trace/propagation/AwsXRayPropagator.java 87.20% <100.00%> (ø) 26.00 <0.00> (ø)
...pagation/B3PropagatorExtractorMultipleHeaders.java 100.00% <100.00%> (ø) 8.00 <0.00> (ø)
...propagation/B3PropagatorExtractorSingleHeader.java 84.61% <100.00%> (ø) 8.00 <0.00> (ø)
... and 15 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 71ba8e1...aeec764. Read the comment docs.

Comment on lines +95 to +97
default Span getCurrentSpan(Context context) {
return TracingContextUtils.getSpan(context);
}
Copy link
Member

Choose a reason for hiding this comment

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

Why not static methods for all of them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was the alternative, yes. @mwear mentioned it feels better on the actual Tracer instance, but static methods would be an option.

Copy link
Member

Choose a reason for hiding this comment

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

By requiring to be on the instance you really need that global tracer if static the need of the global tracer is limited

Copy link
Contributor

Choose a reason for hiding this comment

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

One general thought is I would expect the "job" of managing the active span to be in the TracerProvider - that is the concept of tracing in OpenTelemetry, so it needs to make sure the active span is managed properly. Having Tracer shortcut to it for convenience sounds nice though.

Also, for static I'd expect Span.current to be simpler than requiring a global tracer, it could shortcut to the global TracerProvider, and no need to introduce the "global tracer". That doesn't mean we may not want an anonymous tracer in the API for users to use in their app, that discussion has come up, but it should be orthogonal to context management.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Given that Spans are handled by Tracer, I'm more inclined to put it there directly, instead of being a shortcut.

Making this a static set of members in Tracer (e.g. Tracer.getCurrentSpan()) sounds fine by me (if we decide to go this route).

Copy link
Member

Choose a reason for hiding this comment

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

+1 for Span.current(), which nicely parallels Context.current()

@@ -17,7 +17,7 @@
* @since 0.1.0
*/
@Immutable
public final class TracingContextUtils {
final class TracingContextUtils {
Copy link
Member

Choose a reason for hiding this comment

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

This class can be moved to the Tracer class. No need to have it as a separate class

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. For the sake of simplicity I only made TracingContextUtils non-public but if we were to go down this road, we could entirely remove it (and have the keys and all that in Tracer).

@carlosalberto
Copy link
Contributor Author

Closing this as we won't go this route for Java.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants