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

Context propagation update (OTEP 66) #720

Merged
merged 47 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
e2f642b
Initial Propagators refactor.
carlosalberto Dec 16, 2019
d5d3e02
Add tests for the new propagators changes.
carlosalberto Dec 17, 2019
b60b02d
Make the SDK compile/pass.
carlosalberto Dec 17, 2019
9160417
Make the OT shim compile/pass.
carlosalberto Dec 17, 2019
b97d700
Make contrib components compile/pass.
carlosalberto Dec 17, 2019
0b109f9
Improvement over Span/SpanContext handling in Context.
carlosalberto Dec 18, 2019
51732a9
Add Span.setParent(Context) overload.
carlosalberto Dec 19, 2019
d6894d4
Do not provide default values for Span/SpanContext keys in Context.
carlosalberto Dec 19, 2019
d5acde5
Improve the Context's active state handling.
carlosalberto Dec 19, 2019
71f04d4
Rename DistributedContext to CorrelationContext.
carlosalberto Dec 19, 2019
eb1e411
Improve names for correlationcontext's ContextUtils methods.
carlosalberto Dec 19, 2019
bbfa035
Don't provide an automatic default for current CorrelationContext.
carlosalberto Dec 19, 2019
b9f9b93
Improve the client-server example after the recent changes.
carlosalberto Dec 19, 2019
fdf644b
Adds CorrelationContext.Builder.setParent(Context) overload.
carlosalberto Dec 20, 2019
fd2b4a9
s/be/become.
carlosalberto Dec 20, 2019
24fecd1
Fix javadoc.
carlosalberto Dec 20, 2019
3f7c227
Merge branch 'master' into context_prop_update
carlosalberto Jan 9, 2020
b56ac60
No need to use diamond.
carlosalberto Jan 9, 2020
08297c6
Simply import withScopedContext().
carlosalberto Jan 9, 2020
be3dd73
Merge branch 'master' into context_prop_update
carlosalberto Jan 22, 2020
e06c32f
Fix the API/SDK build.
carlosalberto Jan 24, 2020
4c48266
Remove the builder from the Propagators interface.
carlosalberto Jan 24, 2020
1328ec5
Fix name.
carlosalberto Jan 24, 2020
d35f19e
Use Collections.emptyList() directly.
carlosalberto Jan 24, 2020
14acd5c
Rename Propagators to ContextPropagators.
carlosalberto Jan 24, 2020
b0649da
Move context/ members in api/ to context_prop/
carlosalberto Feb 20, 2020
762408e
Add check/tests for null scoped Contexts.
carlosalberto Feb 20, 2020
05b7628
Rename ContextUtils classes to better alternatives.
carlosalberto Feb 21, 2020
1113f61
Update the context* util classes.
carlosalberto Mar 9, 2020
d271bb6
Merge branch 'master' into context_prop_update
carlosalberto Mar 10, 2020
43e42d4
Make the code compile after the latest master merge.
carlosalberto Mar 10, 2020
014b3c4
Cache the fields() in our default composite propagator.
carlosalberto Mar 10, 2020
f3d29a3
Remove the overloads of setParent(Context) for now.
carlosalberto Mar 10, 2020
555095b
Use DefaultSpan for the tests instead of calling getTracerProvider()
carlosalberto Mar 10, 2020
3c1f781
Fix the sdk testbed artifact build.
carlosalberto Mar 10, 2020
64b5b1e
Merge branch 'master' into context_prop_update
carlosalberto Mar 13, 2020
136f0aa
Make the B3 propagator comply with the new propagator API.
carlosalberto Mar 14, 2020
ddc370a
Simplify the HttpTraceContextTest tests.
carlosalberto Mar 14, 2020
a1d93ec
Simplify the ContextUtils* classes handling of default values.
carlosalberto Mar 14, 2020
3c96627
Minor nit.
carlosalberto Mar 14, 2020
5f820cd
Merge branch 'master' into context_prop_update
carlosalberto Mar 15, 2020
9a1eb5b
Update api/src/main/java/io/opentelemetry/OpenTelemetry.java
carlosalberto Mar 17, 2020
268b546
Annotate ContextPropagators with ThreadSafe instead of Immutable.
carlosalberto Mar 19, 2020
a0ec628
Do not use the fully qualified ContextUtils identifier.
carlosalberto Mar 19, 2020
7a3cbeb
Remove SpanContext support from TracingContextUtils.
carlosalberto Mar 20, 2020
88fdcbb
Rever to using non-defaulted key for TracingContextUtils.
carlosalberto Mar 20, 2020
37ba00b
Revert the default keys for CorrelationsContextUtils.
carlosalberto Mar 20, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package io.opentelemetry.trace.propagation;

import io.grpc.Context;
import io.opentelemetry.context.propagation.HttpTextFormat.Getter;
import io.opentelemetry.trace.SpanContext;
import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.TracingContextUtils;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -69,8 +71,9 @@ public void setup() {
@Measurement(iterations = 15, time = 1)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 1)
public SpanContext measureExtract() {
return httpTraceContext.extract(carrier, getter);
public Span measureExtract() {
return TracingContextUtils.getSpan(
httpTraceContext.extract(Context.current(), carrier, getter));
}

@TearDown(Level.Iteration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

package io.opentelemetry.trace.propagation;

import io.grpc.Context;
import io.opentelemetry.context.propagation.HttpTextFormat.Setter;
import io.opentelemetry.trace.DefaultSpan;
import io.opentelemetry.trace.SpanContext;
import io.opentelemetry.trace.SpanId;
import io.opentelemetry.trace.TraceFlags;
import io.opentelemetry.trace.TraceId;
import io.opentelemetry.trace.TraceState;
import io.opentelemetry.trace.TracingContextUtils;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -69,7 +72,9 @@ public void set(Map<String, String> carrier, String key, String value) {
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 1)
public Map<String, String> measureInject() {
httpTraceContext.inject(contextToTest, carrier, setter);
Context context =
TracingContextUtils.withSpan(DefaultSpan.create(contextToTest), Context.current());
httpTraceContext.inject(context, carrier, setter);
return carrier;
}

Expand Down
36 changes: 36 additions & 0 deletions api/src/main/java/io/opentelemetry/OpenTelemetry.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

package io.opentelemetry;

import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.DefaultContextPropagators;
import io.opentelemetry.correlationcontext.CorrelationContextManager;
import io.opentelemetry.correlationcontext.DefaultCorrelationContextManager;
import io.opentelemetry.correlationcontext.DefaultCorrelationContextManagerProvider;
import io.opentelemetry.correlationcontext.spi.CorrelationContextManagerProvider;
import io.opentelemetry.internal.Utils;
import io.opentelemetry.metrics.DefaultMeterProvider;
import io.opentelemetry.metrics.DefaultMetricsProvider;
import io.opentelemetry.metrics.Meter;
Expand All @@ -29,6 +32,7 @@
import io.opentelemetry.trace.DefaultTracerProvider;
import io.opentelemetry.trace.Tracer;
import io.opentelemetry.trace.TracerProvider;
import io.opentelemetry.trace.propagation.HttpTraceContext;
import io.opentelemetry.trace.spi.TraceProvider;
import java.util.ServiceLoader;
import javax.annotation.Nullable;
Expand All @@ -53,6 +57,9 @@ public final class OpenTelemetry {
private final MeterProvider meterProvider;
private final CorrelationContextManager contextManager;

private volatile ContextPropagators propagators =
DefaultContextPropagators.builder().addHttpTextFormat(new HttpTraceContext()).build();

/**
* Returns a singleton {@link TracerProvider}.
*
Expand Down Expand Up @@ -90,6 +97,35 @@ public static CorrelationContextManager getCorrelationContextManager() {
return getInstance().contextManager;
}

/**
* Returns a {@link ContextPropagators} object, which can be used to access the set of registered
* propagators for each supported format.
*
* @return registered propagators container, defaulting to a {@link ContextPropagators} object
* with {@link HttpTraceContext} registered.
* @throws IllegalStateException if a specified manager (via system properties) could not be
* found.
* @since 0.3.0
*/
public static ContextPropagators getPropagators() {
return getInstance().propagators;
}

/**
* Sets the {@link ContextPropagators} object, which can be used to access the set of registered
* propagators for each supported format.
*
* @param propagators the {@link ContextPropagators} object to be registered.
* @throws IllegalStateException if a specified manager (via system properties) could not be
* found.
* @throws NullPointerException if {@code propagators} is {@code null}.
* @since 0.3.0
*/
public static void setPropagators(ContextPropagators propagators) {
Utils.checkNotNull(propagators, "propagators");
getInstance().propagators = propagators;
}

bogdandrutu marked this conversation as resolved.
Show resolved Hide resolved
/** Lazy loads an instance. */
private static OpenTelemetry getInstance() {
if (instance == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package io.opentelemetry.correlationcontext;

import io.opentelemetry.context.Scope;
import io.opentelemetry.context.propagation.HttpTextFormat;
import javax.annotation.concurrent.ThreadSafe;

/**
Expand Down Expand Up @@ -63,53 +62,4 @@ public interface CorrelationContextManager {
* @since 0.1.0
*/
Scope withContext(CorrelationContext distContext);

/**
* Returns the {@link HttpTextFormat} for this implementation.
*
* <p>Usually this will be the W3C Correlation Context as the HTTP text format. For more details,
* see <a href="https://github.com/w3c/correlation-context">correlation-context</a>.
*
* <p>Example of usage on the client:
*
* <pre>{@code
* private static final CorrelationContextManager contextManager =
* OpenTelemetry.getCorrelationContextManager();
* private static final HttpTextFormat textFormat = contextManager.getHttpTextFormat();
*
* private static final HttpTextFormat.Setter setter =
* new HttpTextFormat.Setter<HttpURLConnection>() {
* public void put(HttpURLConnection carrier, String key, String value) {
* carrier.setRequestProperty(field, value);
* }
* };
*
* void makeHttpRequest() {
* HttpURLConnection connection =
* (HttpURLConnection) new URL("http://myserver").openConnection();
* textFormat.inject(contextManager.getCurrentContext(), connection, httpURLConnectionSetter);
* // Send the request, wait for response and maybe set the status if not ok.
* }
* }</pre>
*
* <p>Example of usage on the server:
*
* <pre>{@code
* private static final CorrelationContextManager contextManager =
* OpenTelemetry.getCorrelationContextManager();
* private static final HttpTextFormat textFormat = contextManager.getHttpTextFormat();
* private static final HttpTextFormat.Getter<HttpRequest> getter = ...;
*
* void onRequestReceived(HttpRequest request) {
* CorrelationContext distContext = textFormat.extract(request, getter);
* try (Scope s = contextManager.withContext(distContext)) {
* // Handle request and send response back.
* }
* }
* }</pre>
*
* @return the {@code HttpTextFormat} for this implementation.
* @since 0.1.0
*/
HttpTextFormat<CorrelationContext> getHttpTextFormat();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
package io.opentelemetry.correlationcontext;

import io.opentelemetry.context.Scope;
import io.opentelemetry.context.propagation.HttpTextFormat;
import io.opentelemetry.internal.Utils;
import java.util.Collections;
import java.util.List;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.ThreadSafe;

Expand All @@ -33,8 +30,6 @@
public final class DefaultCorrelationContextManager implements CorrelationContextManager {
private static final DefaultCorrelationContextManager INSTANCE =
new DefaultCorrelationContextManager();
private static final HttpTextFormat<CorrelationContext> HTTP_TEXT_FORMAT =
new NoopHttpTextFormat();

/**
* Returns a {@code CorrelationContextManager} singleton that is the default implementation for
Expand Down Expand Up @@ -62,11 +57,6 @@ public Scope withContext(CorrelationContext distContext) {
return CorrelationsContextUtils.currentContextWith(distContext);
}

@Override
public HttpTextFormat<CorrelationContext> getHttpTextFormat() {
return HTTP_TEXT_FORMAT;
}

@Immutable
private static final class NoopCorrelationContextBuilder implements CorrelationContext.Builder {
@Override
Expand All @@ -82,10 +72,10 @@ public CorrelationContext.Builder setNoParent() {

@Override
public CorrelationContext.Builder put(
EntryKey key, EntryValue value, EntryMetadata tagMetadata) {
EntryKey key, EntryValue value, EntryMetadata entryMetadata) {
Utils.checkNotNull(key, "key");
Utils.checkNotNull(value, "value");
Utils.checkNotNull(tagMetadata, "tagMetadata");
Utils.checkNotNull(entryMetadata, "entryMetadata");
return this;
}

Expand All @@ -100,26 +90,4 @@ public CorrelationContext build() {
return EmptyCorrelationContext.getInstance();
}
}

@Immutable
private static final class NoopHttpTextFormat implements HttpTextFormat<CorrelationContext> {
@Override
public List<String> fields() {
return Collections.emptyList();
}

@Override
public <C> void inject(CorrelationContext distContext, C carrier, Setter<C> setter) {
Utils.checkNotNull(distContext, "distContext");
Utils.checkNotNull(carrier, "carrier");
Utils.checkNotNull(setter, "setter");
}

@Override
public <C> CorrelationContext extract(C carrier, Getter<C> getter) {
Utils.checkNotNull(carrier, "carrier");
Utils.checkNotNull(getter, "getter");
return EmptyCorrelationContext.getInstance();
}
}
}
23 changes: 7 additions & 16 deletions api/src/main/java/io/opentelemetry/trace/DefaultTracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@

import io.opentelemetry.common.AttributeValue;
import io.opentelemetry.context.Scope;
import io.opentelemetry.context.propagation.HttpTextFormat;
import io.opentelemetry.internal.Utils;
import io.opentelemetry.trace.propagation.HttpTraceContext;
import java.util.Map;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;

/**
Expand All @@ -32,7 +31,6 @@
@ThreadSafe
public final class DefaultTracer implements Tracer {
private static final DefaultTracer INSTANCE = new DefaultTracer();
private static final HttpTextFormat<SpanContext> HTTP_TEXT_FORMAT = new HttpTraceContext();

/**
* Returns a {@code Tracer} singleton that is the default implementations for {@link Tracer}.
Expand All @@ -56,30 +54,24 @@ public Scope withSpan(Span span) {

@Override
public Span.Builder spanBuilder(String spanName) {
return NoopSpanBuilder.create(this, spanName);
}

@Override
public HttpTextFormat<SpanContext> getHttpTextFormat() {
return HTTP_TEXT_FORMAT;
return NoopSpanBuilder.create(spanName);
bogdandrutu marked this conversation as resolved.
Show resolved Hide resolved
}

private DefaultTracer() {}

// Noop implementation of Span.Builder.
private static final class NoopSpanBuilder implements Span.Builder {
static NoopSpanBuilder create(Tracer tracer, String spanName) {
return new NoopSpanBuilder(tracer, spanName);
static NoopSpanBuilder create(String spanName) {
return new NoopSpanBuilder(spanName);
}

private final Tracer tracer;
private boolean isRootSpan;
private SpanContext spanContext;
@Nullable private SpanContext spanContext;

@Override
public Span startSpan() {
if (spanContext == null && !isRootSpan) {
spanContext = tracer.getCurrentSpan().getContext();
spanContext = TracingContextUtils.getCurrentSpan().getContext();
}

return spanContext != null && !SpanContext.getInvalid().equals(spanContext)
Expand Down Expand Up @@ -165,9 +157,8 @@ public NoopSpanBuilder setStartTimestamp(long startTimestamp) {
return this;
}

private NoopSpanBuilder(Tracer tracer, String name) {
private NoopSpanBuilder(String name) {
Utils.checkNotNull(name, "name");
this.tracer = tracer;
}
}
}
57 changes: 0 additions & 57 deletions api/src/main/java/io/opentelemetry/trace/Tracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import com.google.errorprone.annotations.MustBeClosed;
import io.opentelemetry.context.Scope;
import io.opentelemetry.context.propagation.HttpTextFormat;
import io.opentelemetry.trace.propagation.HttpTraceContext;
import javax.annotation.concurrent.ThreadSafe;

/**
Expand Down Expand Up @@ -152,59 +150,4 @@ public interface Tracer {
* @since 0.1.0
*/
Span.Builder spanBuilder(String spanName);

/**
* Returns the {@link HttpTextFormat} for this tracer implementation.
*
* <p>If no tracer implementation is provided, this defaults to the W3C Trace Context HTTP text
* format ({@link HttpTraceContext}). For more details see <a
* href="https://w3c.github.io/trace-context/">W3C Trace Context</a>.
*
* <p>Example of usage on the client:
*
* <pre>{@code
* private static final Tracer tracer = OpenTelemetry.getTracer();
* private static final HttpTextFormat textFormat = tracer.getHttpTextFormat();
* private static final HttpTextFormat.Setter setter =
* new HttpTextFormat.Setter<HttpURLConnection>() {
* public void put(HttpURLConnection carrier, String key, String value) {
* carrier.setRequestProperty(field, value);
* }
* }
*
* void makeHttpRequest() {
* Span span = tracer.spanBuilder("MyRequest").setSpanKind(Span.Kind.CLIENT).startSpan();
* try (Scope s = tracer.withSpan(span)) {
* HttpURLConnection connection =
* (HttpURLConnection) new URL("http://myserver").openConnection();
* textFormat.inject(span.getContext(), connection, httpURLConnectionSetter);
* // Send the request, wait for response and maybe set the status if not ok.
* }
* span.end(); // Can set a status.
* }
* }</pre>
*
* <p>Example of usage on the server:
*
* <pre>{@code
* private static final Tracer tracer = OpenTelemetry.getTracer();
* private static final HttpTextFormat textFormat = tracer.getHttpTextFormat();
* private static final HttpTextFormat.Getter<HttpRequest> getter = ...;
*
* void onRequestReceived(HttpRequest request) {
* SpanContext spanContext = textFormat.extract(request, getter);
* Span span = tracer.spanBuilder("MyRequest")
* .setParent(spanContext)
* .setSpanKind(Span.Kind.SERVER).startSpan();
* try (Scope s = tracer.withSpan(span)) {
* // Handle request and send response back.
* }
* span.end()
* }
* }</pre>
*
* @return the {@code HttpTextFormat} for this implementation.
* @since 0.1.0
*/
HttpTextFormat<SpanContext> getHttpTextFormat();
}
Loading