From c673f991e43e902dafc66ce4c1ed2cf6c0cc29e3 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Thu, 7 May 2020 16:07:36 -0700 Subject: [PATCH 01/22] Add a simple purpose-built Context --- .../HttpTraceContextExtractBenchmark.java | 4 +- .../HttpTraceContextInjectBenchmark.java | 5 +- .../io/opentelemetry/context/Context.java | 84 ++++++++++++++++ .../opentelemetry/context/CurrentContext.java | 84 ++++++++++++++++ .../opentelemetry/context/DefaultContext.java | 64 ++++++++++++ .../context/DefaultContextStorage.java | 70 +++++++++++++ .../java/io/opentelemetry/context/Scope.java | 0 .../propagation/ContextPropagators.java | 7 +- .../DefaultContextPropagators.java | 2 +- .../context/propagation/HttpTextFormat.java | 2 +- .../CorrelationsContextUtils.java | 99 ------------------- .../DefaultCorrelationContextManager.java | 11 ++- .../correlationcontext/package-info.java | 10 +- .../io/opentelemetry/trace/DefaultTracer.java | 11 ++- .../java/io/opentelemetry/trace/Tracer.java | 7 +- .../trace/TracingContextUtils.java | 97 ------------------ .../io/opentelemetry/trace/package-info.java | 6 +- .../trace/propagation/HttpTraceContext.java | 7 +- .../propagation/DefaultPropagatorsTest.java | 7 +- .../CorrelationsContextUtilsTest.java | 83 ---------------- .../DefaultCorrelationContextManagerTest.java | 33 +------ .../trace/DefaultTracerTest.java | 7 +- .../trace/TracingContextUtilsTest.java | 72 -------------- .../propagation/HttpTraceContextTest.java | 7 +- build.gradle | 1 - context_prop/build.gradle | 2 - .../opentelemetry/context/ContextUtils.java | 61 ------------ .../context/ContextUtilsTest.java | 65 ------------ .../PropagatorContextExtractBenchmark.java | 5 +- .../PropagatorContextInjectBenchmark.java | 7 +- .../trace/propagation/B3Propagator.java | 2 +- .../propagation/B3PropagatorExtractor.java | 5 +- .../propagation/B3PropagatorInjector.java | 5 +- .../trace/propagation/JaegerPropagator.java | 7 +- .../trace/propagation/B3PropagatorTest.java | 7 +- .../propagation/JaegerPropagatorTest.java | 7 +- .../contrib/trace/CurrentSpanUtils.java | 17 ++-- .../contrib/trace/CurrentSpanUtilsTest.java | 4 +- .../example/HelloWorldClient.java | 2 +- .../example/HelloWorldClientStream.java | 2 +- .../example/HelloWorldServer.java | 2 +- .../example/http/HttpClient.java | 2 +- .../example/http/HttpServer.java | 2 +- .../opentracingshim/Propagation.java | 18 ++-- .../CorrelationContextManagerSdk.java | 12 ++- .../sdk/trace/SpanBuilderSdk.java | 7 +- .../io/opentelemetry/sdk/trace/TracerSdk.java | 10 +- .../CorrelationContextManagerSdkTest.java | 32 +----- .../sdk/trace/TracerSdkTest.java | 9 +- .../trace/testbed/clientserver/Client.java | 2 +- .../trace/testbed/clientserver/Server.java | 5 +- 51 files changed, 423 insertions(+), 656 deletions(-) create mode 100644 api/src/main/java/io/opentelemetry/context/Context.java create mode 100644 api/src/main/java/io/opentelemetry/context/CurrentContext.java create mode 100644 api/src/main/java/io/opentelemetry/context/DefaultContext.java create mode 100644 api/src/main/java/io/opentelemetry/context/DefaultContextStorage.java rename {context_prop => api}/src/main/java/io/opentelemetry/context/Scope.java (100%) rename {context_prop => api}/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java (90%) rename {context_prop => api}/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java (99%) rename {context_prop => api}/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java (99%) delete mode 100644 api/src/main/java/io/opentelemetry/correlationcontext/CorrelationsContextUtils.java delete mode 100644 api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java rename {context_prop => api}/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java (98%) delete mode 100644 api/src/test/java/io/opentelemetry/correlationcontext/CorrelationsContextUtilsTest.java delete mode 100644 api/src/test/java/io/opentelemetry/trace/TracingContextUtilsTest.java delete mode 100644 context_prop/src/main/java/io/opentelemetry/context/ContextUtils.java delete mode 100644 context_prop/src/test/java/io/opentelemetry/context/ContextUtilsTest.java diff --git a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextExtractBenchmark.java b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextExtractBenchmark.java index 86febd0a0bc..72b4650077b 100644 --- a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextExtractBenchmark.java +++ b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextExtractBenchmark.java @@ -16,7 +16,7 @@ package io.opentelemetry.trace.propagation; -import io.grpc.Context; +import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat.Getter; import java.util.ArrayList; import java.util.Arrays; @@ -71,7 +71,7 @@ public String get(Map carrier, String key) { public Context measureExtract() { Context result = null; for (int i = 0; i < COUNT; i++) { - result = httpTraceContext.extract(Context.ROOT, carriers.get(i), getter); + result = httpTraceContext.extract(Context.empty(), carriers.get(i), getter); } return result; } diff --git a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java index aea70cef8c2..1561f110619 100644 --- a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java +++ b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java @@ -16,7 +16,7 @@ package io.opentelemetry.trace.propagation; -import io.grpc.Context; +import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.SpanContext; @@ -24,7 +24,6 @@ import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; -import io.opentelemetry.trace.TracingContextUtils; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -93,7 +92,7 @@ private static SpanContext createTestSpanContext(String traceId, String spanId) private static List createContexts(List spanContexts) { List contexts = new ArrayList<>(); for (SpanContext context : spanContexts) { - contexts.add(TracingContextUtils.withSpan(DefaultSpan.create(context), Context.ROOT)); + contexts.add(Context.empty().withSpan(DefaultSpan.create(context))); } return contexts; } diff --git a/api/src/main/java/io/opentelemetry/context/Context.java b/api/src/main/java/io/opentelemetry/context/Context.java new file mode 100644 index 00000000000..348f8e682ad --- /dev/null +++ b/api/src/main/java/io/opentelemetry/context/Context.java @@ -0,0 +1,84 @@ +/* + * Copyright 2020, OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opentelemetry.context; + +import io.opentelemetry.correlationcontext.CorrelationContext; +import io.opentelemetry.trace.Span; + +// TODO (trask) once the current() method is removed (see below) +// this could be turned into an interface +// which improves interop possibilities with other libraries' contexts +// but also limits the ability to evolve this class with additional methods, +// e.g. withLoggingContext??? (insert sad Java 7 face here) +// +// TODO (trask) does this also need to support arbitrary key-values? +// +// TODO (trask) javadoc class and methods +public abstract class Context { + + public static Context empty() { + return DefaultContext.EMPTY; + } + + // TODO (trask) remove this method and use CurrentContext.get() instead + // so that this is a pure Context object, and has nothing to do with thread binding. + // this changes was not done yet in order to reduce code churn + // and make the PR easier to review as there are a lot of places + // that call Context.current(). + public static Context current() { + return DefaultContextStorage.current(); + } + + /** + * Creates a new {@link Context} with the given {@link Span} set. + * + * @param span the {@link Span} to be set. + * @return a new context with the given {@link Span} set. + * @since 0.5.0 + */ + public abstract Context withSpan(Span span); + + /** + * Creates a new {@link Context} with the given {@link CorrelationContext} set. + * + * @param correlationContext the {@link CorrelationContext} to be set. + * @return a new context with the given {@link CorrelationContext} set. + * @since 0.5.0 + */ + public abstract Context withCorrelationContext(CorrelationContext correlationContext); + + /** + * Returns the {@link Span} from this context, falling back to an empty {@link Span}. + * + * @return the {@link Span} from this context. + * @since 0.5.0 + */ + public abstract Span getSpan(); + + // TODO (trask) is this needed? + public abstract Span getSpanWithoutDefault(); + + /** + * Returns the {@link CorrelationContext} from this context, falling back to an empty {@link + * CorrelationContext}. + * + * @return the {@link CorrelationContext} from this context. + * @since 0.5.0 + */ + // TODO (trask) do we need getCorrelationContextWithoutDefault()? + public abstract CorrelationContext getCorrelationContext(); +} diff --git a/api/src/main/java/io/opentelemetry/context/CurrentContext.java b/api/src/main/java/io/opentelemetry/context/CurrentContext.java new file mode 100644 index 00000000000..f8c777224b9 --- /dev/null +++ b/api/src/main/java/io/opentelemetry/context/CurrentContext.java @@ -0,0 +1,84 @@ +/* + * Copyright 2020, OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opentelemetry.context; + +import com.google.errorprone.annotations.MustBeClosed; +import io.opentelemetry.correlationcontext.CorrelationContext; +import io.opentelemetry.trace.Span; + +/** Static methods for interacting with the current (thread-bound) context. */ +// TODO (trask) javadoc class and methods +public class CurrentContext { + + @MustBeClosed + public static Scope withSpan(Span span) { + return withContext(get().withSpan(span)); + } + + @MustBeClosed + public static Scope withCorrelationContext(CorrelationContext correlationContext) { + return withContext(get().withCorrelationContext(correlationContext)); + } + + /** + * Binds the context to the current thread and returns a scope that must be used to unbind the + * context from the current thread and restore the previously bound context (if any). + */ + @MustBeClosed + public static Scope withContext(Context context) { + return new DefaultScope(context); + } + + /** Convenience method for {@code CurrentContext.get().getSpan()}. */ + public static Span getSpan() { + return get().getSpan(); + } + + // TODO (trask) is this needed? + /** Convenience method for {@code CurrentContext.get().getSpanWithoutDefault()}. */ + public static Span getSpanWithoutDefault() { + return get().getSpanWithoutDefault(); + } + + /** Convenience method for {@code CurrentContext.get().getCorrelationContext()}. */ + public static CorrelationContext getCorrelationContext() { + return get().getCorrelationContext(); + } + + /** Returns the context bound to the current thread. */ + public static Context get() { + return DefaultContextStorage.current(); + } + + private CurrentContext() {} + + static class DefaultScope implements Scope { + + private final Context priorContext; + private final Context context; + + DefaultScope(Context context) { + this.priorContext = DefaultContextStorage.doAttach(context); + this.context = context; + } + + @Override + public void close() { + DefaultContextStorage.detach(context, priorContext); + } + } +} diff --git a/api/src/main/java/io/opentelemetry/context/DefaultContext.java b/api/src/main/java/io/opentelemetry/context/DefaultContext.java new file mode 100644 index 00000000000..500962dfa56 --- /dev/null +++ b/api/src/main/java/io/opentelemetry/context/DefaultContext.java @@ -0,0 +1,64 @@ +/* + * Copyright 2020, OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opentelemetry.context; + +import io.opentelemetry.correlationcontext.CorrelationContext; +import io.opentelemetry.correlationcontext.EmptyCorrelationContext; +import io.opentelemetry.trace.DefaultSpan; +import io.opentelemetry.trace.Span; + +final class DefaultContext extends Context { + + static final Context EMPTY = new DefaultContext(null, null); + + private final Span span; + private final CorrelationContext correlationContext; + + DefaultContext(Span span, CorrelationContext correlationContext) { + this.span = span; + this.correlationContext = correlationContext; + } + + @Override + public Context withSpan(Span span) { + return new DefaultContext(span, correlationContext); + } + + @Override + public Context withCorrelationContext(CorrelationContext correlationContext) { + return new DefaultContext(span, correlationContext); + } + + @Override + public Span getSpan() { + return span == null ? DefaultSpan.getInvalid() : span; + } + + // TODO (trask) is this needed? + @Override + public Span getSpanWithoutDefault() { + return span; + } + + // TODO (trask) do we need getCorrelationContextWithoutDefault()? + // because if we don't need either of the ..WithoutDefault() methods, + // then we can just initialize span to invalid and correlationContext to empty + @Override + public CorrelationContext getCorrelationContext() { + return correlationContext == null ? EmptyCorrelationContext.getInstance() : correlationContext; + } +} diff --git a/api/src/main/java/io/opentelemetry/context/DefaultContextStorage.java b/api/src/main/java/io/opentelemetry/context/DefaultContextStorage.java new file mode 100644 index 00000000000..751b6d6a677 --- /dev/null +++ b/api/src/main/java/io/opentelemetry/context/DefaultContextStorage.java @@ -0,0 +1,70 @@ +/* + * Copyright 2020, OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opentelemetry.context; + +import java.util.logging.Level; +import java.util.logging.Logger; + +// TODO (trask) citation needed, copied from grpc-context +// if needed, we can make this storage pluggable similar to grpc-context +final class DefaultContextStorage { + private static final Logger log = Logger.getLogger(DefaultContextStorage.class.getName()); + + /** Currently bound context. */ + static final ThreadLocal localContext = new ThreadLocal<>(); + + static Context doAttach(Context toAttach) { + Context current = current(); + localContext.set(toAttach); + return current; + } + + static void detach(Context toDetach, Context toRestore) { + if (current() != toDetach) { + // Log a severe message instead of throwing an exception as the context to attach is assumed + // to be the correct one and the unbalanced state represents a coding mistake in a lower + // layer in the stack that cannot be recovered from here. + log.log( + Level.SEVERE, + "Context was not attached when detaching", + new Throwable().fillInStackTrace()); + } + if (toRestore != Context.empty()) { + localContext.set(toRestore); + } else { + // Avoid leaking our ClassLoader via ROOT if this Thread is reused across multiple + // ClassLoaders, as is common for Servlet Containers. The ThreadLocal is weakly referenced by + // the Thread, but its current value is strongly referenced and only lazily collected as new + // ThreadLocals are created. + // + // Use set(null) instead of remove() since remove() deletes the entry which is then re-created + // on the next get() (because of initialValue() handling). set(null) has same performance as + // set(toRestore). + localContext.set(null); + } + } + + static Context current() { + Context current = localContext.get(); + if (current == null) { + return DefaultContext.EMPTY; + } + return current; + } + + private DefaultContextStorage() {} +} diff --git a/context_prop/src/main/java/io/opentelemetry/context/Scope.java b/api/src/main/java/io/opentelemetry/context/Scope.java similarity index 100% rename from context_prop/src/main/java/io/opentelemetry/context/Scope.java rename to api/src/main/java/io/opentelemetry/context/Scope.java diff --git a/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java b/api/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java similarity index 90% rename from context_prop/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java rename to api/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java index 823909985b0..b19c8bdf300 100644 --- a/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java +++ b/api/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java @@ -23,9 +23,10 @@ * *

This container can be used to access a single, composite propagator for each supported format, * which will be responsible for injecting and extracting data for each registered concern (traces, - * correlations, etc). Propagation will happen through {@code io.grpc.Context}, from which values - * will be read upon injection, and which will store values from the extraction step. The resulting - * {@code Context} can then be used implicitly or explicitly by the OpenTelemetry API. + * correlations, etc). Propagation will happen through {@link io.opentelemetry.context.Context}, + * from which values will be read upon injection, and which will store values from the extraction + * step. The resulting {@link io.opentelemetry.context.Context} can then be used implicitly or + * explicitly by the OpenTelemetry API. * *

Example of usage on the client: * diff --git a/context_prop/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java b/api/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java similarity index 99% rename from context_prop/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java rename to api/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java index b09751e77e7..112d8a35da0 100644 --- a/context_prop/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java +++ b/api/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java @@ -16,7 +16,7 @@ package io.opentelemetry.context.propagation; -import io.grpc.Context; +import io.opentelemetry.context.Context; import java.util.ArrayList; import java.util.Collections; import java.util.List; diff --git a/context_prop/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java b/api/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java similarity index 99% rename from context_prop/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java rename to api/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java index e99a2d475bb..b5ea5170948 100644 --- a/context_prop/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java +++ b/api/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java @@ -16,7 +16,7 @@ package io.opentelemetry.context.propagation; -import io.grpc.Context; +import io.opentelemetry.context.Context; import java.util.List; import javax.annotation.Nullable; import javax.annotation.concurrent.ThreadSafe; diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationsContextUtils.java b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationsContextUtils.java deleted file mode 100644 index a676381bf2a..00000000000 --- a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationsContextUtils.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright 2019, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.correlationcontext; - -import io.grpc.Context; -import io.opentelemetry.context.ContextUtils; -import io.opentelemetry.context.Scope; -import javax.annotation.Nullable; -import javax.annotation.concurrent.Immutable; - -/** - * Utility methods for accessing the {@link CorrelationContext} contained in the {@link - * io.grpc.Context}. - * - * @since 0.1.0 - */ -@Immutable -public final class CorrelationsContextUtils { - private static final Context.Key CORR_CONTEXT_KEY = - Context.key("opentelemetry-corr-context-key"); - - /** - * Creates a new {@code Context} with the given value set. - * - * @param corrContext the value to be set. - * @param context the parent {@code Context}. - * @return a new context with the given value set. - * @since 0.1.0 - */ - public static Context withCorrelationContext(CorrelationContext corrContext, Context context) { - return context.withValue(CORR_CONTEXT_KEY, corrContext); - } - - /** - * Returns the {@link CorrelationContext} from the current {@code Context}, falling back to an - * empty {@link CorrelationContext}. - * - * @return the {@link CorrelationContext} from the current {@code Context}. - * @since 0.3.0 - */ - public static CorrelationContext getCurrentCorrelationContext() { - return getCorrelationContext(Context.current()); - } - - /** - * Returns the {@link CorrelationContext} from the specified {@code Context}, falling back to an - * empty {@link CorrelationContext}. - * - * @param context the specified {@code Context}. - * @return the {@link CorrelationContext} from the specified {@code Context}. - * @since 0.3.0 - */ - public static CorrelationContext getCorrelationContext(Context context) { - CorrelationContext corrContext = CORR_CONTEXT_KEY.get(context); - return corrContext == null ? EmptyCorrelationContext.getInstance() : corrContext; - } - - /** - * Returns the {@link CorrelationContext} from the specified {@code Context}. If none is found, - * this method returns {code null}. - * - * @param context the specified {@code Context}. - * @return the {@link CorrelationContext} from the specified {@code Context}. - * @since 0.1.0 - */ - @Nullable - public static CorrelationContext getCorrelationContextWithoutDefault(Context context) { - return CORR_CONTEXT_KEY.get(context); - } - - /** - * Returns a new {@link Scope} encapsulating the provided {@link CorrelationContext} added to the - * current {@code Context}. - * - * @param corrContext the {@link CorrelationContext} to be added to the current {@code Context}. - * @return the {@link Scope} for the updated {@code Context}. - * @since 0.1.0 - */ - public static Scope currentContextWith(CorrelationContext corrContext) { - Context context = withCorrelationContext(corrContext, Context.current()); - return ContextUtils.withScopedContext(context); - } - - private CorrelationsContextUtils() {} -} diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java index 8d47920faf1..0eb61ef7123 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java @@ -16,6 +16,8 @@ package io.opentelemetry.correlationcontext; +import com.google.errorprone.annotations.MustBeClosed; +import io.opentelemetry.context.CurrentContext; import io.opentelemetry.context.Scope; import io.opentelemetry.internal.Utils; import javax.annotation.concurrent.Immutable; @@ -42,9 +44,10 @@ public static CorrelationContextManager getInstance() { return INSTANCE; } + // TODO (trask) can we remove this now? @Override public CorrelationContext getCurrentContext() { - return CorrelationsContextUtils.getCurrentCorrelationContext(); + return CurrentContext.getCorrelationContext(); } @Override @@ -52,9 +55,11 @@ public CorrelationContext.Builder contextBuilder() { return new NoopCorrelationContextBuilder(); } + // TODO (trask) can we remove this now? @Override - public Scope withContext(CorrelationContext distContext) { - return CorrelationsContextUtils.currentContextWith(distContext); + @MustBeClosed + public Scope withContext(CorrelationContext correlationContext) { + return CurrentContext.withCorrelationContext(correlationContext); } @Immutable diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/package-info.java b/api/src/main/java/io/opentelemetry/correlationcontext/package-info.java index cafe13b13b3..aa5dba6e533 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/package-info.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/package-info.java @@ -17,17 +17,17 @@ /** * API for associating entries with scoped operations. * - *

This package manages a set of entries in the {@code io.grpc.Context}. The entries can be used - * to label anything that is associated with a specific operation. For example, the {@code - * opentelemetry.stats} package labels all stats with the current entries. + *

This package manages a set of entries in the {@link io.opentelemetry.context.Context}. The + * entries can be used to label anything that is associated with a specific operation. For example, + * the {@code opentelemetry.stats} package labels all stats with the current entries. * *

{@link io.opentelemetry.correlationcontext.Entry Entrys} are key-value pairs. The {@link * io.opentelemetry.correlationcontext.EntryKey keys} and {@link * io.opentelemetry.correlationcontext.EntryValue values} are wrapped {@code String}s. They are * stored as a map in a {@link io.opentelemetry.correlationcontext.CorrelationContext}. * - *

Note that entries are independent of the tracing data that is propagated in the {@code - * io.grpc.Context}, such as trace ID. + *

Note that entries are independent of the tracing data that is propagated in the {@link + * io.opentelemetry.context.Context}, such as trace ID. */ // TODO: Add code examples. package io.opentelemetry.correlationcontext; diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java index 8b2d94df6d6..1d047e801fc 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java @@ -16,7 +16,9 @@ package io.opentelemetry.trace; +import com.google.errorprone.annotations.MustBeClosed; import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.context.CurrentContext; import io.opentelemetry.context.Scope; import io.opentelemetry.internal.Utils; import java.util.Map; @@ -42,14 +44,17 @@ public static Tracer getInstance() { return INSTANCE; } + // TODO (trask) can we remove this now? @Override public Span getCurrentSpan() { - return TracingContextUtils.getCurrentSpan(); + return CurrentContext.getSpan(); } + // TODO (trask) can we remove this now? @Override + @MustBeClosed public Scope withSpan(Span span) { - return TracingContextUtils.currentContextWith(span); + return CurrentContext.withSpan(span); } @Override @@ -71,7 +76,7 @@ static NoopSpanBuilder create(String spanName) { @Override public Span startSpan() { if (spanContext == null && !isRootSpan) { - spanContext = TracingContextUtils.getCurrentSpan().getContext(); + spanContext = CurrentContext.getSpan().getContext(); } return spanContext != null && !SpanContext.getInvalid().equals(spanContext) diff --git a/api/src/main/java/io/opentelemetry/trace/Tracer.java b/api/src/main/java/io/opentelemetry/trace/Tracer.java index cb02053181c..bc4b09198da 100644 --- a/api/src/main/java/io/opentelemetry/trace/Tracer.java +++ b/api/src/main/java/io/opentelemetry/trace/Tracer.java @@ -26,10 +26,9 @@ *

Users may choose to use manual or automatic Context propagation. Because of that this class * offers APIs to facilitate both usages. * - *

The automatic context propagation is done using {@link io.grpc.Context} which is a gRPC - * independent implementation for in-process Context propagation mechanism which can carry - * scoped-values across API boundaries and between threads. Users of the library must propagate the - * {@link io.grpc.Context} between different threads. + *

The automatic context propagation is done using {@link io.opentelemetry.context.Context} which + * can carry scoped-values across API boundaries and between threads. Users of the library must + * propagate the {@link io.opentelemetry.context.Context} between different threads. * *

Example usage with automatic context propagation: * diff --git a/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java b/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java deleted file mode 100644 index 9660e0d6e3c..00000000000 --- a/api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright 2019, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.trace; - -import io.grpc.Context; -import io.opentelemetry.context.ContextUtils; -import io.opentelemetry.context.Scope; -import javax.annotation.Nullable; -import javax.annotation.concurrent.Immutable; - -/** - * Util methods/functionality to interact with the {@link io.grpc.Context}. - * - * @since 0.1.0 - */ -@Immutable -public final class TracingContextUtils { - private static final Context.Key CONTEXT_SPAN_KEY = - Context.key("opentelemetry-trace-span-key"); - - /** - * Creates a new {@code Context} with the given {@link Span} set. - * - * @param span the value to be set. - * @param context the parent {@code Context}. - * @return a new context with the given value set. - * @since 0.1.0 - */ - public static Context withSpan(Span span, Context context) { - return context.withValue(CONTEXT_SPAN_KEY, span); - } - - /** - * Returns the {@link Span} from the current {@code Context}, falling back to a default, no-op - * {@link Span}. - * - * @return the {@link Span} from the current {@code Context}. - * @since 0.3.0 - */ - public static Span getCurrentSpan() { - return getSpan(Context.current()); - } - - /** - * Returns the {@link Span} from the specified {@code Context}, falling back to a default, no-op - * {@link Span}. - * - * @param context the specified {@code Context}. - * @return the {@link Span} from the specified {@code Context}. - * @since 0.3.0 - */ - public static Span getSpan(Context context) { - Span span = CONTEXT_SPAN_KEY.get(context); - return span == null ? DefaultSpan.getInvalid() : span; - } - - /** - * Returns the {@link Span} from the specified {@code Context}. If none is found, this method - * returns {code null}. - * - * @param context the specified {@code Context}. - * @return the {@link Span} from the specified {@code Context}. - * @since 0.1.0 - */ - @Nullable - public static Span getSpanWithoutDefault(Context context) { - return CONTEXT_SPAN_KEY.get(context); - } - - /** - * Returns a new {@link Scope} encapsulating the provided {@link Span} added to the current {@code - * Context}. - * - * @param span the {@link Span} to be added to the current {@code Context}. - * @return the {@link Scope} for the updated {@code Context}. - * @since 0.1.0 - */ - public static Scope currentContextWith(Span span) { - return ContextUtils.withScopedContext(withSpan(span, Context.current())); - } - - private TracingContextUtils() {} -} diff --git a/api/src/main/java/io/opentelemetry/trace/package-info.java b/api/src/main/java/io/opentelemetry/trace/package-info.java index 6b6f2f70221..b47c1ffdef0 100644 --- a/api/src/main/java/io/opentelemetry/trace/package-info.java +++ b/api/src/main/java/io/opentelemetry/trace/package-info.java @@ -25,9 +25,9 @@ * *

{@link io.opentelemetry.trace.Span} represents a single operation within a trace. * - *

{@link io.opentelemetry.trace.Span Spans} are propagated in-process in the {@code - * io.grpc.Context} and between process using one of the wire propagation formats supported in the - * {@code opentelemetry.trace.propagation} package. + *

{@link io.opentelemetry.trace.Span Spans} are propagated in-process in the {@link + * io.opentelemetry.context.Context} and between process using one of the wire propagation formats + * supported in the {@code opentelemetry.trace.propagation} package. */ // TODO: Add code examples. package io.opentelemetry.trace; diff --git a/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java b/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java index ac0683626f4..996e03c4372 100644 --- a/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java +++ b/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java @@ -19,7 +19,7 @@ import static io.opentelemetry.internal.Utils.checkArgument; import static io.opentelemetry.internal.Utils.checkNotNull; -import io.grpc.Context; +import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; @@ -28,7 +28,6 @@ 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.Collections; import java.util.List; @@ -80,7 +79,7 @@ public void inject(Context context, C carrier, Setter setter) { checkNotNull(context, "context"); checkNotNull(setter, "setter"); - Span span = TracingContextUtils.getSpanWithoutDefault(context); + Span span = context.getSpanWithoutDefault(); if (span == null) { return; } @@ -125,7 +124,7 @@ private static void injectImpl(SpanContext spanContext, C carrier, Setter checkNotNull(getter, "getter"); SpanContext spanContext = extractImpl(carrier, getter); - return TracingContextUtils.withSpan(DefaultSpan.create(spanContext), context); + return context.withSpan(DefaultSpan.create(spanContext)); } private static SpanContext extractImpl(C carrier, Getter getter) { diff --git a/context_prop/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java b/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java similarity index 98% rename from context_prop/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java rename to api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java index 08d419c7e22..657c19614b9 100644 --- a/context_prop/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java +++ b/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java @@ -16,6 +16,7 @@ package io.opentelemetry.context.propagation; +/* import static com.google.common.truth.Truth.assertThat; import io.grpc.Context; @@ -28,11 +29,14 @@ import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; +*/ +// TODO is this use case (arbitrary context values) needed? /** Unit tests for {@link DefaultContextPropagators}. */ -@RunWith(JUnit4.class) +// @RunWith(JUnit4.class) public class DefaultPropagatorsTest { + /* @Rule public final ExpectedException thrown = ExpectedException.none(); @Test @@ -159,4 +163,5 @@ public String get(Map map, String key) { private MapGetter() {} } + */ } diff --git a/api/src/test/java/io/opentelemetry/correlationcontext/CorrelationsContextUtilsTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/CorrelationsContextUtilsTest.java deleted file mode 100644 index 13b8b0c8168..00000000000 --- a/api/src/test/java/io/opentelemetry/correlationcontext/CorrelationsContextUtilsTest.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright 2019, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.correlationcontext; - -import static com.google.common.truth.Truth.assertThat; - -import io.grpc.Context; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Unit tests for {@link CorrelationsContextUtils}. */ -@RunWith(JUnit4.class) -public final class CorrelationsContextUtilsTest { - - @Test - public void testGetCurrentCorrelationContext_Default() { - CorrelationContext corrContext = CorrelationsContextUtils.getCurrentCorrelationContext(); - assertThat(corrContext).isSameInstanceAs(EmptyCorrelationContext.getInstance()); - } - - @Test - public void testGetCurrentCorrelationContext_SetCorrContext() { - CorrelationContext corrContext = - DefaultCorrelationContextManager.getInstance().contextBuilder().build(); - Context orig = - CorrelationsContextUtils.withCorrelationContext(corrContext, Context.current()).attach(); - try { - assertThat(CorrelationsContextUtils.getCurrentCorrelationContext()) - .isSameInstanceAs(corrContext); - } finally { - Context.current().detach(orig); - } - } - - @Test - public void testGetCorrelationContext_DefaultContext() { - CorrelationContext corrContext = - CorrelationsContextUtils.getCorrelationContext(Context.current()); - assertThat(corrContext).isSameInstanceAs(EmptyCorrelationContext.getInstance()); - } - - @Test - public void testGetCorrelationContext_ExplicitContext() { - CorrelationContext corrContext = - DefaultCorrelationContextManager.getInstance().contextBuilder().build(); - Context context = - CorrelationsContextUtils.withCorrelationContext(corrContext, Context.current()); - assertThat(CorrelationsContextUtils.getCorrelationContext(context)) - .isSameInstanceAs(corrContext); - } - - @Test - public void testGetCorrelationContextWithoutDefault_DefaultContext() { - CorrelationContext corrContext = - CorrelationsContextUtils.getCorrelationContextWithoutDefault(Context.current()); - assertThat(corrContext).isNull(); - } - - @Test - public void testGetCorrelationContextWithoutDefault_ExplicitContext() { - CorrelationContext corrContext = - DefaultCorrelationContextManager.getInstance().contextBuilder().build(); - Context context = - CorrelationsContextUtils.withCorrelationContext(corrContext, Context.current()); - assertThat(CorrelationsContextUtils.getCorrelationContext(context)) - .isSameInstanceAs(corrContext); - } -} diff --git a/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java index 7f567c6c4af..c8e50776153 100644 --- a/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java +++ b/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java @@ -18,7 +18,7 @@ import static com.google.common.truth.Truth.assertThat; -import io.grpc.Context; +import io.opentelemetry.context.CurrentContext; import io.opentelemetry.context.Scope; import java.util.Arrays; import java.util.Collection; @@ -67,14 +67,13 @@ public void getCurrentContext_DefaultContext() { @Test public void getCurrentContext_ContextSetToNull() { - Context orig = - CorrelationsContextUtils.withCorrelationContext(null, Context.current()).attach(); + Scope scope = CurrentContext.withCorrelationContext(null); try { CorrelationContext distContext = defaultCorrelationContextManager.getCurrentContext(); assertThat(distContext).isNotNull(); assertThat(distContext.getEntries()).isEmpty(); } finally { - Context.current().detach(orig); + scope.close(); } } @@ -108,32 +107,6 @@ public void withContext_nullContext() { .isSameInstanceAs(EmptyCorrelationContext.getInstance()); } - @Test - public void withContextUsingWrap() { - Runnable runnable; - Scope wtm = defaultCorrelationContextManager.withContext(DIST_CONTEXT); - try { - assertThat(defaultCorrelationContextManager.getCurrentContext()) - .isSameInstanceAs(DIST_CONTEXT); - runnable = - Context.current() - .wrap( - new Runnable() { - @Override - public void run() { - assertThat(defaultCorrelationContextManager.getCurrentContext()) - .isSameInstanceAs(DIST_CONTEXT); - } - }); - } finally { - wtm.close(); - } - assertThat(defaultCorrelationContextManager.getCurrentContext()) - .isSameInstanceAs(EmptyCorrelationContext.getInstance()); - // When we run the runnable we will have the CorrelationContext in the current Context. - runnable.run(); - } - @Test public void noopContextBuilder_SetParent_DisallowsNullParent() { CorrelationContext.Builder noopBuilder = defaultCorrelationContextManager.contextBuilder(); diff --git a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java index a47fb8d3ed5..0bee9d043e6 100644 --- a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java +++ b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java @@ -18,8 +18,7 @@ import static com.google.common.truth.Truth.assertThat; -import io.grpc.Context; -import io.opentelemetry.context.ContextUtils; +import io.opentelemetry.context.CurrentContext; import io.opentelemetry.context.Scope; import org.junit.Rule; import org.junit.Test; @@ -122,9 +121,7 @@ public void testSpanContextPropagationCurrentSpan() { @Test public void testSpanContextPropagationCurrentSpanContext() { - Context context = - TracingContextUtils.withSpan(DefaultSpan.create(spanContext), Context.current()); - Scope scope = ContextUtils.withScopedContext(context); + Scope scope = CurrentContext.withSpan(DefaultSpan.create(spanContext)); try { Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); assertThat(span.getContext()).isSameInstanceAs(spanContext); diff --git a/api/src/test/java/io/opentelemetry/trace/TracingContextUtilsTest.java b/api/src/test/java/io/opentelemetry/trace/TracingContextUtilsTest.java deleted file mode 100644 index 422a6b002c8..00000000000 --- a/api/src/test/java/io/opentelemetry/trace/TracingContextUtilsTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2019, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.trace; - -import static com.google.common.truth.Truth.assertThat; - -import io.grpc.Context; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Unit tests for {@link TracingContextUtils}. */ -@RunWith(JUnit4.class) -public final class TracingContextUtilsTest { - - @Test - public void testGetCurrentSpan_Default() { - Span span = TracingContextUtils.getCurrentSpan(); - assertThat(span).isSameInstanceAs(DefaultSpan.getInvalid()); - } - - @Test - public void testGetCurrentSpan_SetSpan() { - Span span = DefaultSpan.create(SpanContext.getInvalid()); - Context orig = TracingContextUtils.withSpan(span, Context.current()).attach(); - try { - assertThat(TracingContextUtils.getCurrentSpan()).isSameInstanceAs(span); - } finally { - Context.current().detach(orig); - } - } - - @Test - public void testGetSpan_DefaultContext() { - Span span = TracingContextUtils.getSpan(Context.current()); - assertThat(span).isSameInstanceAs(DefaultSpan.getInvalid()); - } - - @Test - public void testGetSpan_ExplicitContext() { - Span span = DefaultSpan.create(SpanContext.getInvalid()); - Context context = TracingContextUtils.withSpan(span, Context.current()); - assertThat(TracingContextUtils.getSpan(context)).isSameInstanceAs(span); - } - - @Test - public void testGetSpanWithoutDefault_DefaultContext() { - Span span = TracingContextUtils.getSpanWithoutDefault(Context.current()); - assertThat(span).isNull(); - } - - @Test - public void testGetSpanWithoutDefault_ExplicitContext() { - Span span = DefaultSpan.create(SpanContext.getInvalid()); - Context context = TracingContextUtils.withSpan(span, Context.current()); - assertThat(TracingContextUtils.getSpanWithoutDefault(context)).isSameInstanceAs(span); - } -} diff --git a/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java b/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java index f425cfc7442..b77f6462ca9 100644 --- a/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java +++ b/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java @@ -20,7 +20,7 @@ import static io.opentelemetry.trace.propagation.HttpTraceContext.TRACE_PARENT; import static io.opentelemetry.trace.propagation.HttpTraceContext.TRACE_STATE; -import io.grpc.Context; +import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat.Getter; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; import io.opentelemetry.trace.DefaultSpan; @@ -29,7 +29,6 @@ import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; -import io.opentelemetry.trace.TracingContextUtils; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @@ -81,11 +80,11 @@ public String get(Map carrier, String key) { @Rule public ExpectedException thrown = ExpectedException.none(); private static SpanContext getSpanContext(Context context) { - return TracingContextUtils.getSpan(context).getContext(); + return context.getSpan().getContext(); } private static Context withSpanContext(SpanContext spanContext, Context context) { - return TracingContextUtils.withSpan(DefaultSpan.create(spanContext), context); + return context.withSpan(DefaultSpan.create(spanContext)); } @Test diff --git a/build.gradle b/build.gradle index 7d4e081f849..337e09fd117 100644 --- a/build.gradle +++ b/build.gradle @@ -116,7 +116,6 @@ subprojects { errorprone_core : "com.google.errorprone:error_prone_core:${errorProneVersion}", errorprone_javac : "com.google.errorprone:javac:${errorProneJavacVersion}", grpc_api : "io.grpc:grpc-api", - grpc_context : "io.grpc:grpc-context", grpc_protobuf : "io.grpc:grpc-protobuf", grpc_stub : "io.grpc:grpc-stub", guava : "com.google.guava:guava", diff --git a/context_prop/build.gradle b/context_prop/build.gradle index bc8932a4b5e..b4cf892d839 100644 --- a/context_prop/build.gradle +++ b/context_prop/build.gradle @@ -9,8 +9,6 @@ description = 'OpenTelemetry Context Propagation' ext.moduleName = "io.opentelemetry.context.propagation" dependencies { - api libraries.grpc_context - signature "org.codehaus.mojo.signature:java17:1.0@signature" signature "net.sf.androidscents.signature:android-api-level-14:4.0_r4@signature" } diff --git a/context_prop/src/main/java/io/opentelemetry/context/ContextUtils.java b/context_prop/src/main/java/io/opentelemetry/context/ContextUtils.java deleted file mode 100644 index aedce36d750..00000000000 --- a/context_prop/src/main/java/io/opentelemetry/context/ContextUtils.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2019, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.context; - -import io.grpc.Context; -import javax.annotation.concurrent.Immutable; - -/** - * Util methods/functionality to interact with the {@link io.grpc.Context}. - * - * @since 0.1.0 - */ -@Immutable -public final class ContextUtils { - /** - * Sets the specified {@code Context} as {@code Context.current()}, returning a {@link Scope} to - * end its active state and restore the previous active {@code Context}. - * - * @param context the {@code Context} to be set as {@code Context.current()}. - * @return the {@link Scope} for the updated {@code Context}. - * @since 0.1.0 - */ - public static Scope withScopedContext(Context context) { - if (context == null) { - throw new NullPointerException("context"); - } - - return new ContextInScope(context); - } - - private static final class ContextInScope implements Scope { - private final Context context; - private final Context previous; - - public ContextInScope(Context context) { - this.context = context; - this.previous = context.attach(); - } - - @Override - public void close() { - context.detach(previous); - } - } - - private ContextUtils() {} -} diff --git a/context_prop/src/test/java/io/opentelemetry/context/ContextUtilsTest.java b/context_prop/src/test/java/io/opentelemetry/context/ContextUtilsTest.java deleted file mode 100644 index c3b511a7459..00000000000 --- a/context_prop/src/test/java/io/opentelemetry/context/ContextUtilsTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2020, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.context; - -import static com.google.common.truth.Truth.assertThat; - -import io.grpc.Context; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Unit tests for {@link DefaultContextPropagators}. */ -@RunWith(JUnit4.class) -public class ContextUtilsTest { - - @Rule public final ExpectedException thrown = ExpectedException.none(); - - private static final Context.Key SIMPLE_KEY = Context.key("simple"); - - @Test - public void withScopedContextNull() { - thrown.expect(NullPointerException.class); - ContextUtils.withScopedContext(null); - } - - @Test - public void withScopedContext() { - Context prevCtx = Context.current(); - Context ctx = Context.current().withValue(SIMPLE_KEY, "value1"); - Scope scope = ContextUtils.withScopedContext(ctx); - try { - assertThat(scope).isNotNull(); - assertThat(Context.current()).isEqualTo(ctx); - - Context ctx2 = Context.current().withValue(SIMPLE_KEY, "value2"); - Scope scope2 = ContextUtils.withScopedContext(ctx2); - try { - assertThat(scope2).isNotNull(); - assertThat(Context.current()).isEqualTo(ctx2); - } finally { - scope2.close(); - } - assertThat(Context.current()).isEqualTo(ctx); - } finally { - scope.close(); - } - assertThat(Context.current()).isEqualTo(prevCtx); - } -} diff --git a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java index 051580cc6de..f0e816dd181 100644 --- a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java +++ b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java @@ -16,9 +16,8 @@ package io.opentelemetry.contrib.trace.propagation; -import io.grpc.Context; +import io.opentelemetry.context.Context; import io.opentelemetry.trace.Span; -import io.opentelemetry.trace.TracingContextUtils; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -65,7 +64,7 @@ public void setup() { @BenchmarkMode(Mode.AverageTime) @Fork(1) public Span measureExtract() { - return TracingContextUtils.getSpan(doExtract()); + return doExtract().getSpan(); } protected abstract Context doExtract(); diff --git a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java index 6d81e37e0bf..704b583c3e8 100644 --- a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java +++ b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java @@ -16,14 +16,14 @@ package io.opentelemetry.contrib.trace.propagation; -import io.grpc.Context; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.CurrentContext; 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; @@ -74,8 +74,7 @@ public abstract static class AbstractContextInjectBenchmark { @BenchmarkMode(Mode.AverageTime) @Fork(1) public Map measureInject() { - Context context = - TracingContextUtils.withSpan(DefaultSpan.create(contextToTest), Context.current()); + Context context = CurrentContext.get().withSpan(DefaultSpan.create(contextToTest)); doInject(context, carrier); return carrier; } diff --git a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3Propagator.java b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3Propagator.java index 837326c709b..7538ff92998 100644 --- a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3Propagator.java +++ b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3Propagator.java @@ -16,7 +16,7 @@ package io.opentelemetry.contrib.trace.propagation; -import io.grpc.Context; +import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.trace.SpanId; import io.opentelemetry.trace.TraceId; diff --git a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorExtractor.java b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorExtractor.java index 16a507ad8ee..6628a7e53cb 100644 --- a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorExtractor.java +++ b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorExtractor.java @@ -25,7 +25,7 @@ import static io.opentelemetry.contrib.trace.propagation.B3Propagator.TRACE_ID_HEADER; import static io.opentelemetry.contrib.trace.propagation.B3Propagator.TRUE_INT; -import io.grpc.Context; +import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.internal.StringUtils; import io.opentelemetry.trace.DefaultSpan; @@ -34,7 +34,6 @@ import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; -import io.opentelemetry.trace.TracingContextUtils; import java.util.Objects; import java.util.logging.Level; import java.util.logging.Logger; @@ -65,7 +64,7 @@ Context extract(Context context, C carrier, HttpTextFormat.Getter getter) spanContext = getSpanContextFromMultipleHeaders(carrier, getter); } - return TracingContextUtils.withSpan(DefaultSpan.create(spanContext), context); + return context.withSpan(DefaultSpan.create(spanContext)); } @SuppressWarnings("StringSplitter") diff --git a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java index c265f2a0a15..2c8e1e71824 100644 --- a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java +++ b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java @@ -16,13 +16,12 @@ package io.opentelemetry.contrib.trace.propagation; -import io.grpc.Context; +import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; import io.opentelemetry.trace.TraceId; -import io.opentelemetry.trace.TracingContextUtils; import java.util.Objects; import javax.annotation.concurrent.Immutable; @@ -47,7 +46,7 @@ void inject(Context context, C carrier, HttpTextFormat.Setter setter) { Objects.requireNonNull(context, "context"); Objects.requireNonNull(setter, "setter"); - Span span = TracingContextUtils.getSpanWithoutDefault(context); + Span span = context.getSpanWithoutDefault(); if (span == null) { return; } diff --git a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java index 0cb71b176f4..6915aa08455 100644 --- a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java +++ b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java @@ -18,7 +18,7 @@ import static io.opentelemetry.internal.Utils.checkNotNull; -import io.grpc.Context; +import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.internal.StringUtils; import io.opentelemetry.trace.DefaultSpan; @@ -28,7 +28,6 @@ import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; -import io.opentelemetry.trace.TracingContextUtils; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.Collections; @@ -88,7 +87,7 @@ public void inject(Context context, C carrier, Setter setter) { checkNotNull(context, "context"); checkNotNull(setter, "setter"); - Span span = TracingContextUtils.getSpanWithoutDefault(context); + Span span = context.getSpanWithoutDefault(); if (span == null) { return; } @@ -113,7 +112,7 @@ public Context extract(Context context, C carrier, Getter getter) { SpanContext spanContext = getSpanContextFromHeader(carrier, getter); - return TracingContextUtils.withSpan(DefaultSpan.create(spanContext), context); + return context.withSpan(DefaultSpan.create(spanContext)); } @SuppressWarnings("StringSplitter") diff --git a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java index 6e331be5503..68792a42a87 100644 --- a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java +++ b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java @@ -18,7 +18,7 @@ import static com.google.common.truth.Truth.assertThat; -import io.grpc.Context; +import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat.Getter; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; import io.opentelemetry.internal.StringUtils; @@ -28,7 +28,6 @@ import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; -import io.opentelemetry.trace.TracingContextUtils; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @@ -74,11 +73,11 @@ public String get(Map carrier, String key) { @Rule public ExpectedException thrown = ExpectedException.none(); private static SpanContext getSpanContext(Context context) { - return TracingContextUtils.getSpan(context).getContext(); + return context.getSpan().getContext(); } private static Context withSpanContext(SpanContext spanContext, Context context) { - return TracingContextUtils.withSpan(DefaultSpan.create(spanContext), context); + return context.withSpan(DefaultSpan.create(spanContext)); } @Test diff --git a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java index f38e9af97a7..d19fa0fa00d 100644 --- a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java +++ b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java @@ -21,9 +21,9 @@ import static io.opentelemetry.contrib.trace.propagation.JaegerPropagator.PROPAGATION_HEADER; import static io.opentelemetry.contrib.trace.propagation.JaegerPropagator.PROPAGATION_HEADER_DELIMITER; -import io.grpc.Context; import io.jaegertracing.internal.JaegerSpanContext; import io.jaegertracing.internal.propagation.TextMapCodec; +import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; import io.opentelemetry.trace.DefaultSpan; @@ -32,7 +32,6 @@ import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; -import io.opentelemetry.trace.TracingContextUtils; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.LinkedHashMap; @@ -84,11 +83,11 @@ public String get(Map carrier, String key) { @Rule public ExpectedException thrown = ExpectedException.none(); private static SpanContext getSpanContext(Context context) { - return TracingContextUtils.getSpan(context).getContext(); + return context.getSpan().getContext(); } private static Context withSpanContext(SpanContext spanContext, Context context) { - return TracingContextUtils.withSpan(DefaultSpan.create(spanContext), context); + return context.withSpan(DefaultSpan.create(spanContext)); } @Test diff --git a/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java b/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java index 374fd943f14..60064477f63 100644 --- a/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java +++ b/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java @@ -16,13 +16,16 @@ package io.opentelemetry.contrib.trace; -import io.grpc.Context; +import io.opentelemetry.context.CurrentContext; +import io.opentelemetry.context.Scope; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Status; -import io.opentelemetry.trace.TracingContextUtils; import java.util.concurrent.Callable; -/** Util methods/functionality to interact with the {@link Span} in the {@link io.grpc.Context}. */ +/** + * Util methods/functionality to interact with the {@link Span} in the {@link + * io.opentelemetry.context.Context}. + */ public final class CurrentSpanUtils { // No instance of this class. private CurrentSpanUtils() {} @@ -65,8 +68,7 @@ private RunnableInSpan(Span span, Runnable runnable, boolean endSpan) { @Override public void run() { - Context origContext = TracingContextUtils.withSpan(span, Context.current()).attach(); - try { + try (Scope ignored = CurrentContext.withSpan(span)) { runnable.run(); } catch (Throwable t) { setErrorStatus(span, t); @@ -77,7 +79,6 @@ public void run() { } throw new RuntimeException("unexpected", t); } finally { - Context.current().detach(origContext); if (endSpan) { span.end(); } @@ -98,8 +99,7 @@ private CallableInSpan(Span span, Callable callable, boolean endSpan) { @Override public V call() throws Exception { - Context origContext = TracingContextUtils.withSpan(span, Context.current()).attach(); - try { + try (Scope ignored = CurrentContext.withSpan(span)) { return callable.call(); } catch (Exception e) { setErrorStatus(span, e); @@ -111,7 +111,6 @@ public V call() throws Exception { } throw new RuntimeException("unexpected", t); } finally { - Context.current().detach(origContext); if (endSpan) { span.end(); } diff --git a/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java b/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java index f6cf250610f..613007cc15a 100644 --- a/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java +++ b/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java @@ -20,10 +20,10 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyZeroInteractions; +import io.opentelemetry.context.CurrentContext; import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Status; -import io.opentelemetry.trace.TracingContextUtils; import java.util.concurrent.Callable; import org.junit.Before; import org.junit.Test; @@ -249,6 +249,6 @@ public Object call() throws Exception { } private static Span getCurrentSpan() { - return TracingContextUtils.getCurrentSpan(); + return CurrentContext.getSpan(); } } diff --git a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClient.java b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClient.java index 019ad063c3c..0d3c47f4d55 100644 --- a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClient.java +++ b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClient.java @@ -21,7 +21,6 @@ import io.grpc.Channel; import io.grpc.ClientCall; import io.grpc.ClientInterceptor; -import io.grpc.Context; import io.grpc.ForwardingClientCall; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; @@ -29,6 +28,7 @@ import io.grpc.MethodDescriptor; import io.grpc.StatusRuntimeException; import io.opentelemetry.OpenTelemetry; +import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.exporters.logging.LoggingSpanExporter; diff --git a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClientStream.java b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClientStream.java index d2db13d06e1..e1ef5dc8905 100644 --- a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClientStream.java +++ b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClientStream.java @@ -21,7 +21,6 @@ import io.grpc.Channel; import io.grpc.ClientCall; import io.grpc.ClientInterceptor; -import io.grpc.Context; import io.grpc.ForwardingClientCall; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; @@ -30,6 +29,7 @@ import io.grpc.StatusRuntimeException; import io.grpc.stub.StreamObserver; import io.opentelemetry.OpenTelemetry; +import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.exporters.logging.LoggingSpanExporter; diff --git a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldServer.java b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldServer.java index 369fd9b1a17..538eb940f58 100644 --- a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldServer.java +++ b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldServer.java @@ -17,7 +17,6 @@ package io.opentelemetry.example; -import io.grpc.Context; import io.grpc.Contexts; import io.grpc.Grpc; import io.grpc.Metadata; @@ -27,6 +26,7 @@ import io.grpc.ServerCallHandler; import io.grpc.stub.StreamObserver; import io.opentelemetry.OpenTelemetry; +import io.opentelemetry.context.Context; import io.opentelemetry.context.ContextUtils; import io.opentelemetry.context.Scope; import io.opentelemetry.context.propagation.HttpTextFormat; diff --git a/examples/http/src/main/java/io/opentelemetry/example/http/HttpClient.java b/examples/http/src/main/java/io/opentelemetry/example/http/HttpClient.java index d3d7a2c985e..c888fdc67ee 100644 --- a/examples/http/src/main/java/io/opentelemetry/example/http/HttpClient.java +++ b/examples/http/src/main/java/io/opentelemetry/example/http/HttpClient.java @@ -16,8 +16,8 @@ package io.opentelemetry.example.http; -import io.grpc.Context; import io.opentelemetry.OpenTelemetry; +import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.exporters.logging.LoggingSpanExporter; diff --git a/examples/http/src/main/java/io/opentelemetry/example/http/HttpServer.java b/examples/http/src/main/java/io/opentelemetry/example/http/HttpServer.java index 31295d5503f..2cd83374d2c 100644 --- a/examples/http/src/main/java/io/opentelemetry/example/http/HttpServer.java +++ b/examples/http/src/main/java/io/opentelemetry/example/http/HttpServer.java @@ -18,9 +18,9 @@ import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; -import io.grpc.Context; import io.opentelemetry.OpenTelemetry; import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.context.Context; import io.opentelemetry.context.ContextUtils; import io.opentelemetry.context.Scope; import io.opentelemetry.context.propagation.HttpTextFormat; diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java index fdd1c3ad62c..6d4b0b59b8f 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java @@ -16,11 +16,10 @@ package io.opentelemetry.opentracingshim; -import io.grpc.Context; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.CurrentContext; import io.opentelemetry.context.propagation.HttpTextFormat; -import io.opentelemetry.correlationcontext.CorrelationsContextUtils; import io.opentelemetry.trace.DefaultSpan; -import io.opentelemetry.trace.TracingContextUtils; import io.opentracing.propagation.TextMapExtract; import io.opentracing.propagation.TextMapInject; import java.util.HashMap; @@ -34,11 +33,9 @@ final class Propagation extends BaseShimObject { public void injectTextFormat(SpanContextShim contextShim, TextMapInject carrier) { Context context = - TracingContextUtils.withSpan( - DefaultSpan.create(contextShim.getSpanContext()), Context.current()); - context = - CorrelationsContextUtils.withCorrelationContext( - contextShim.getCorrelationContext(), context); + CurrentContext.get() + .withSpan(DefaultSpan.create(contextShim.getSpanContext())) + .withCorrelationContext(contextShim.getCorrelationContext()); propagators().getHttpTextFormat().inject(context, carrier, TextMapSetter.INSTANCE); } @@ -55,13 +52,12 @@ public SpanContextShim extractTextFormat(TextMapExtract carrier) { .getHttpTextFormat() .extract(Context.current(), carrierMap, TextMapGetter.INSTANCE); - io.opentelemetry.trace.Span span = TracingContextUtils.getSpan(context); + io.opentelemetry.trace.Span span = context.getSpan(); if (!span.getContext().isValid()) { return null; } - return new SpanContextShim( - telemetryInfo, span.getContext(), CorrelationsContextUtils.getCorrelationContext(context)); + return new SpanContextShim(telemetryInfo, span.getContext(), context.getCorrelationContext()); } static final class TextMapSetter implements HttpTextFormat.Setter { diff --git a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java index 1e009d0a658..3616d94cea5 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java @@ -16,19 +16,21 @@ package io.opentelemetry.sdk.correlationcontext; +import com.google.errorprone.annotations.MustBeClosed; +import io.opentelemetry.context.CurrentContext; import io.opentelemetry.context.Scope; import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.correlationcontext.CorrelationContextManager; -import io.opentelemetry.correlationcontext.CorrelationsContextUtils; /** * {@link CorrelationContextManagerSdk} is SDK implementation of {@link CorrelationContextManager}. */ public class CorrelationContextManagerSdk implements CorrelationContextManager { + // TODO (trask) can we remove this now? @Override public CorrelationContext getCurrentContext() { - return CorrelationsContextUtils.getCurrentCorrelationContext(); + return CurrentContext.getCorrelationContext(); } @Override @@ -36,8 +38,10 @@ public CorrelationContext.Builder contextBuilder() { return new CorrelationContextSdk.Builder(); } + // TODO (trask) can we remove this now? @Override - public Scope withContext(CorrelationContext distContext) { - return CorrelationsContextUtils.currentContextWith(distContext); + @MustBeClosed + public Scope withContext(CorrelationContext correlationContext) { + return CurrentContext.withCorrelationContext(correlationContext); } } diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java index f1e484b0926..4b25122adec 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java @@ -16,8 +16,8 @@ package io.opentelemetry.sdk.trace; -import io.grpc.Context; import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.context.CurrentContext; import io.opentelemetry.internal.Utils; import io.opentelemetry.sdk.common.Clock; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; @@ -35,7 +35,6 @@ import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; -import io.opentelemetry.trace.TracingContextUtils; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -252,7 +251,7 @@ private static SpanContext parent( case NO_PARENT: return null; case CURRENT_CONTEXT: - return TracingContextUtils.getCurrentSpan().getContext(); + return CurrentContext.getSpan().getContext(); case EXPLICIT_PARENT: return explicitParent.getContext(); case EXPLICIT_REMOTE_PARENT: @@ -265,7 +264,7 @@ private static SpanContext parent( private static Span parentSpan(ParentType parentType, Span explicitParent) { switch (parentType) { case CURRENT_CONTEXT: - return TracingContextUtils.getSpanWithoutDefault(Context.current()); + return CurrentContext.getSpanWithoutDefault(); case EXPLICIT_PARENT: return explicitParent; default: diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java index cf92d0a59ef..e31baf303c3 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java @@ -16,12 +16,13 @@ package io.opentelemetry.sdk.trace; +import com.google.errorprone.annotations.MustBeClosed; +import io.opentelemetry.context.CurrentContext; import io.opentelemetry.context.Scope; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.trace.DefaultTracer; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Tracer; -import io.opentelemetry.trace.TracingContextUtils; /** {@link TracerSdk} is SDK implementation of {@link Tracer}. */ final class TracerSdk implements Tracer { @@ -33,14 +34,17 @@ final class TracerSdk implements Tracer { this.instrumentationLibraryInfo = instrumentationLibraryInfo; } + // TODO (trask) can we remove this now? @Override public Span getCurrentSpan() { - return TracingContextUtils.getCurrentSpan(); + return CurrentContext.getSpan(); } + // TODO (trask) can we remove this now? @Override + @MustBeClosed public Scope withSpan(Span span) { - return TracingContextUtils.currentContextWith(span); + return CurrentContext.withSpan(span); } @Override diff --git a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java index 7adf692d994..6f31ba70b85 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java @@ -18,10 +18,9 @@ import static com.google.common.truth.Truth.assertThat; -import io.grpc.Context; +import io.opentelemetry.context.CurrentContext; import io.opentelemetry.context.Scope; import io.opentelemetry.correlationcontext.CorrelationContext; -import io.opentelemetry.correlationcontext.CorrelationsContextUtils; import io.opentelemetry.correlationcontext.EmptyCorrelationContext; import org.junit.Before; import org.junit.Test; @@ -52,14 +51,10 @@ public void testGetCurrentContext_DefaultContext() { @Test public void testGetCurrentContext_ContextSetToNull() { - Context orig = - CorrelationsContextUtils.withCorrelationContext(null, Context.current()).attach(); - try { + try (Scope ignored = CurrentContext.withCorrelationContext(null)) { CorrelationContext distContext = contextManager.getCurrentContext(); assertThat(distContext).isNotNull(); assertThat(distContext.getEntries()).isEmpty(); - } finally { - Context.current().detach(orig); } } @@ -67,31 +62,10 @@ public void testGetCurrentContext_ContextSetToNull() { public void testWithCorrelationContext() { assertThat(contextManager.getCurrentContext()) .isSameInstanceAs(EmptyCorrelationContext.getInstance()); - try (Scope wtm = contextManager.withContext(distContext)) { + try (Scope ignored = contextManager.withContext(distContext)) { assertThat(contextManager.getCurrentContext()).isSameInstanceAs(distContext); } assertThat(contextManager.getCurrentContext()) .isSameInstanceAs(EmptyCorrelationContext.getInstance()); } - - @Test - public void testWithCorrelationContextUsingWrap() { - Runnable runnable; - try (Scope wtm = contextManager.withContext(distContext)) { - assertThat(contextManager.getCurrentContext()).isSameInstanceAs(distContext); - runnable = - Context.current() - .wrap( - new Runnable() { - @Override - public void run() { - assertThat(contextManager.getCurrentContext()).isSameInstanceAs(distContext); - } - }); - } - assertThat(contextManager.getCurrentContext()) - .isSameInstanceAs(EmptyCorrelationContext.getInstance()); - // When we run the runnable we will have the CorrelationContext in the current Context. - runnable.run(); - } } diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java index 06ce4b96fd4..ba815e18d01 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java @@ -18,8 +18,8 @@ import static com.google.common.truth.Truth.assertThat; -import io.grpc.Context; import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.context.CurrentContext; import io.opentelemetry.context.Scope; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.sdk.trace.StressTestRunner.OperationUpdater; @@ -28,7 +28,6 @@ import io.opentelemetry.sdk.trace.export.SpanExporter; import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; -import io.opentelemetry.trace.TracingContextUtils; import java.util.Collection; import java.util.concurrent.atomic.AtomicLong; import org.junit.Before; @@ -76,12 +75,8 @@ public void defaultSpanBuilder() { @Test public void getCurrentSpan() { assertThat(tracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); - Context origContext = TracingContextUtils.withSpan(span, Context.current()).attach(); - // Make sure context is detached even if test fails. - try { + try (Scope ignored = CurrentContext.withSpan(span)) { assertThat(tracer.getCurrentSpan()).isSameInstanceAs(span); - } finally { - Context.current().detach(origContext); } assertThat(tracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); } diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java index cf8a30e1e15..a21055d6deb 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java @@ -16,8 +16,8 @@ package io.opentelemetry.sdk.contrib.trace.testbed.clientserver; -import io.grpc.Context; import io.opentelemetry.OpenTelemetry; +import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; import io.opentelemetry.trace.Span; diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java index 3e453f054bb..3faab1c8cd3 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java @@ -16,15 +16,14 @@ package io.opentelemetry.sdk.contrib.trace.testbed.clientserver; -import io.grpc.Context; import io.opentelemetry.OpenTelemetry; +import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.context.propagation.HttpTextFormat.Getter; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.Tracer; -import io.opentelemetry.trace.TracingContextUtils; import java.util.concurrent.ArrayBlockingQueue; import javax.annotation.Nullable; @@ -52,7 +51,7 @@ public String get(Message carrier, String key) { return carrier.get(key); } }); - SpanContext spanContext = TracingContextUtils.getSpan(context).getContext(); + SpanContext spanContext = context.getSpan().getContext(); Span span = tracer.spanBuilder("receive").setSpanKind(Kind.SERVER).setParent(spanContext).startSpan(); span.setAttribute("component", "example-server"); From 2e09a45bf0eca2ac12914b897e567ace496d6bbe Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Thu, 7 May 2020 20:44:50 -0700 Subject: [PATCH 02/22] Remove ..WithoutDefault() methods --- .../io/opentelemetry/context/Context.java | 4 ---- .../opentelemetry/context/CurrentContext.java | 6 ----- .../opentelemetry/context/DefaultContext.java | 22 ++++++++----------- .../trace/propagation/HttpTraceContext.java | 4 ++-- .../propagation/B3PropagatorInjector.java | 4 ++-- .../trace/propagation/JaegerPropagator.java | 4 ++-- .../sdk/trace/SpanBuilderSdk.java | 2 +- 7 files changed, 16 insertions(+), 30 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/context/Context.java b/api/src/main/java/io/opentelemetry/context/Context.java index 348f8e682ad..e0ec78cc957 100644 --- a/api/src/main/java/io/opentelemetry/context/Context.java +++ b/api/src/main/java/io/opentelemetry/context/Context.java @@ -69,9 +69,6 @@ public static Context current() { */ public abstract Span getSpan(); - // TODO (trask) is this needed? - public abstract Span getSpanWithoutDefault(); - /** * Returns the {@link CorrelationContext} from this context, falling back to an empty {@link * CorrelationContext}. @@ -79,6 +76,5 @@ public static Context current() { * @return the {@link CorrelationContext} from this context. * @since 0.5.0 */ - // TODO (trask) do we need getCorrelationContextWithoutDefault()? public abstract CorrelationContext getCorrelationContext(); } diff --git a/api/src/main/java/io/opentelemetry/context/CurrentContext.java b/api/src/main/java/io/opentelemetry/context/CurrentContext.java index f8c777224b9..4a8fb62921d 100644 --- a/api/src/main/java/io/opentelemetry/context/CurrentContext.java +++ b/api/src/main/java/io/opentelemetry/context/CurrentContext.java @@ -48,12 +48,6 @@ public static Span getSpan() { return get().getSpan(); } - // TODO (trask) is this needed? - /** Convenience method for {@code CurrentContext.get().getSpanWithoutDefault()}. */ - public static Span getSpanWithoutDefault() { - return get().getSpanWithoutDefault(); - } - /** Convenience method for {@code CurrentContext.get().getCorrelationContext()}. */ public static CorrelationContext getCorrelationContext() { return get().getCorrelationContext(); diff --git a/api/src/main/java/io/opentelemetry/context/DefaultContext.java b/api/src/main/java/io/opentelemetry/context/DefaultContext.java index 500962dfa56..6a0acc1bf3e 100644 --- a/api/src/main/java/io/opentelemetry/context/DefaultContext.java +++ b/api/src/main/java/io/opentelemetry/context/DefaultContext.java @@ -23,7 +23,8 @@ final class DefaultContext extends Context { - static final Context EMPTY = new DefaultContext(null, null); + static final Context EMPTY = + new DefaultContext(DefaultSpan.getInvalid(), EmptyCorrelationContext.getInstance()); private final Span span; private final CorrelationContext correlationContext; @@ -35,30 +36,25 @@ final class DefaultContext extends Context { @Override public Context withSpan(Span span) { - return new DefaultContext(span, correlationContext); + // TODO (trask) can we checkNotNull(span)? + return new DefaultContext(span == null ? DefaultSpan.getInvalid() : span, correlationContext); } @Override public Context withCorrelationContext(CorrelationContext correlationContext) { - return new DefaultContext(span, correlationContext); + // TODO (trask) can we checkNotNull(correlationContext)? + return new DefaultContext( + span, + correlationContext == null ? EmptyCorrelationContext.getInstance() : correlationContext); } @Override public Span getSpan() { - return span == null ? DefaultSpan.getInvalid() : span; - } - - // TODO (trask) is this needed? - @Override - public Span getSpanWithoutDefault() { return span; } - // TODO (trask) do we need getCorrelationContextWithoutDefault()? - // because if we don't need either of the ..WithoutDefault() methods, - // then we can just initialize span to invalid and correlationContext to empty @Override public CorrelationContext getCorrelationContext() { - return correlationContext == null ? EmptyCorrelationContext.getInstance() : correlationContext; + return correlationContext; } } diff --git a/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java b/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java index 996e03c4372..999c4fad395 100644 --- a/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java +++ b/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java @@ -79,8 +79,8 @@ public void inject(Context context, C carrier, Setter setter) { checkNotNull(context, "context"); checkNotNull(setter, "setter"); - Span span = context.getSpanWithoutDefault(); - if (span == null) { + Span span = context.getSpan(); + if (!span.getContext().isValid()) { return; } diff --git a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java index 2c8e1e71824..807fe0f127f 100644 --- a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java +++ b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java @@ -46,8 +46,8 @@ void inject(Context context, C carrier, HttpTextFormat.Setter setter) { Objects.requireNonNull(context, "context"); Objects.requireNonNull(setter, "setter"); - Span span = context.getSpanWithoutDefault(); - if (span == null) { + Span span = context.getSpan(); + if (!span.getContext().isValid()) { return; } diff --git a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java index 6915aa08455..abca819d935 100644 --- a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java +++ b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java @@ -87,8 +87,8 @@ public void inject(Context context, C carrier, Setter setter) { checkNotNull(context, "context"); checkNotNull(setter, "setter"); - Span span = context.getSpanWithoutDefault(); - if (span == null) { + Span span = context.getSpan(); + if (!span.getContext().isValid()) { return; } diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java index 4b25122adec..4a0266f36b8 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java @@ -264,7 +264,7 @@ private static SpanContext parent( private static Span parentSpan(ParentType parentType, Span explicitParent) { switch (parentType) { case CURRENT_CONTEXT: - return CurrentContext.getSpanWithoutDefault(); + return CurrentContext.getSpan(); case EXPLICIT_PARENT: return explicitParent; default: From 361cb51ef749910582fef697507b1f5f75c0d8a2 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 8 May 2020 08:54:25 -0700 Subject: [PATCH 03/22] Move CurrentContext so currentcontext package --- .../java/io/opentelemetry/trace/DefaultTracerBenchmarks.java | 4 ++-- api/src/main/java/io/opentelemetry/context/Context.java | 5 +++-- .../correlationcontext/CorrelationContextManager.java | 2 +- .../correlationcontext/DefaultCorrelationContextManager.java | 4 ++-- .../{context => currentcontext}/CurrentContext.java | 3 ++- .../{context => currentcontext}/DefaultContextStorage.java | 5 +++-- .../io/opentelemetry/{context => currentcontext}/Scope.java | 2 +- api/src/main/java/io/opentelemetry/trace/DefaultTracer.java | 4 ++-- api/src/main/java/io/opentelemetry/trace/Tracer.java | 2 +- api/src/test/java/io/opentelemetry/OpenTelemetryTest.java | 2 +- .../DefaultCorrelationContextManagerTest.java | 4 ++-- .../test/java/io/opentelemetry/trace/DefaultTracerTest.java | 4 ++-- .../trace/propagation/PropagatorContextInjectBenchmark.java | 2 +- .../io/opentelemetry/contrib/trace/CurrentSpanUtils.java | 4 ++-- .../io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java | 2 +- .../java/io/opentelemetry/opentracingshim/Propagation.java | 2 +- .../java/io/opentelemetry/opentracingshim/ScopeShim.java | 4 ++-- .../testbed/OpenTelemetryInteroperabilityTest.java | 2 +- .../sdk/correlationcontext/CorrelationContextManagerSdk.java | 4 ++-- .../main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java | 2 +- sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java | 4 ++-- .../correlationcontext/CorrelationContextManagerSdkTest.java | 4 ++-- .../sdk/correlationcontext/ScopedCorrelationContextTest.java | 2 +- .../java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java | 2 +- .../test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java | 4 ++-- .../activespanreplacement/ActiveSpanReplacementTest.java | 2 +- .../sdk/contrib/trace/testbed/actorpropagation/Actor.java | 2 +- .../trace/testbed/actorpropagation/ActorPropagationTest.java | 2 +- .../sdk/contrib/trace/testbed/clientserver/Client.java | 2 +- .../sdk/contrib/trace/testbed/clientserver/Server.java | 2 +- .../testbed/concurrentcommonrequesthandler/HandlerTest.java | 2 +- .../trace/testbed/errorreporting/ErrorReportingTest.java | 2 +- .../trace/testbed/latespanfinish/LateSpanFinishTest.java | 2 +- .../sdk/contrib/trace/testbed/multiplecallbacks/Client.java | 2 +- .../testbed/multiplecallbacks/MultipleCallbacksTest.java | 2 +- .../trace/testbed/nestedcallbacks/NestedCallbacksTest.java | 2 +- .../contrib/trace/testbed/promisepropagation/Promise.java | 2 +- .../testbed/promisepropagation/PromisePropagationTest.java | 2 +- .../statelesscommonrequesthandler/RequestHandler.java | 2 +- .../testbed/suspendresumepropagation/SuspendResume.java | 2 +- 40 files changed, 56 insertions(+), 53 deletions(-) rename api/src/main/java/io/opentelemetry/{context => currentcontext}/CurrentContext.java (96%) rename api/src/main/java/io/opentelemetry/{context => currentcontext}/DefaultContextStorage.java (95%) rename api/src/main/java/io/opentelemetry/{context => currentcontext}/Scope.java (96%) diff --git a/api/src/jmh/java/io/opentelemetry/trace/DefaultTracerBenchmarks.java b/api/src/jmh/java/io/opentelemetry/trace/DefaultTracerBenchmarks.java index 0ee09cc8e24..35608773c29 100644 --- a/api/src/jmh/java/io/opentelemetry/trace/DefaultTracerBenchmarks.java +++ b/api/src/jmh/java/io/opentelemetry/trace/DefaultTracerBenchmarks.java @@ -45,7 +45,7 @@ public class DefaultTracerBenchmarks { @Warmup(iterations = 5, time = 1) public void measureFullSpanLifecycle() { span = tracer.spanBuilder("span").startSpan(); - io.opentelemetry.context.Scope ignored = tracer.withSpan(span); + io.opentelemetry.currentcontext.Scope ignored = tracer.withSpan(span); try { // no-op } finally { @@ -72,7 +72,7 @@ public void measureSpanBuilding() { @OutputTimeUnit(TimeUnit.NANOSECONDS) @Warmup(iterations = 5, time = 1) public void measureScopeLifecycle() { - io.opentelemetry.context.Scope ignored = tracer.withSpan(span); + io.opentelemetry.currentcontext.Scope ignored = tracer.withSpan(span); try { // no-op } finally { diff --git a/api/src/main/java/io/opentelemetry/context/Context.java b/api/src/main/java/io/opentelemetry/context/Context.java index e0ec78cc957..23da8db4f38 100644 --- a/api/src/main/java/io/opentelemetry/context/Context.java +++ b/api/src/main/java/io/opentelemetry/context/Context.java @@ -17,6 +17,7 @@ package io.opentelemetry.context; import io.opentelemetry.correlationcontext.CorrelationContext; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.trace.Span; // TODO (trask) once the current() method is removed (see below) @@ -34,13 +35,13 @@ public static Context empty() { return DefaultContext.EMPTY; } - // TODO (trask) remove this method and use CurrentContext.get() instead + // TODO (trask) remove this method and use CurrentContext.get() directly instead // so that this is a pure Context object, and has nothing to do with thread binding. // this changes was not done yet in order to reduce code churn // and make the PR easier to review as there are a lot of places // that call Context.current(). public static Context current() { - return DefaultContextStorage.current(); + return CurrentContext.get(); } /** diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextManager.java b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextManager.java index be1285d6f6c..1ad034fcf0f 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextManager.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextManager.java @@ -16,7 +16,7 @@ package io.opentelemetry.correlationcontext; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.Scope; import javax.annotation.concurrent.ThreadSafe; /** diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java index 0eb61ef7123..3422ab92ad4 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java @@ -17,8 +17,8 @@ package io.opentelemetry.correlationcontext; import com.google.errorprone.annotations.MustBeClosed; -import io.opentelemetry.context.CurrentContext; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.CurrentContext; +import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.internal.Utils; import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.ThreadSafe; diff --git a/api/src/main/java/io/opentelemetry/context/CurrentContext.java b/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java similarity index 96% rename from api/src/main/java/io/opentelemetry/context/CurrentContext.java rename to api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java index 4a8fb62921d..69c0ba70e07 100644 --- a/api/src/main/java/io/opentelemetry/context/CurrentContext.java +++ b/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java @@ -14,9 +14,10 @@ * limitations under the License. */ -package io.opentelemetry.context; +package io.opentelemetry.currentcontext; import com.google.errorprone.annotations.MustBeClosed; +import io.opentelemetry.context.Context; import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.trace.Span; diff --git a/api/src/main/java/io/opentelemetry/context/DefaultContextStorage.java b/api/src/main/java/io/opentelemetry/currentcontext/DefaultContextStorage.java similarity index 95% rename from api/src/main/java/io/opentelemetry/context/DefaultContextStorage.java rename to api/src/main/java/io/opentelemetry/currentcontext/DefaultContextStorage.java index 751b6d6a677..7096ae7e960 100644 --- a/api/src/main/java/io/opentelemetry/context/DefaultContextStorage.java +++ b/api/src/main/java/io/opentelemetry/currentcontext/DefaultContextStorage.java @@ -14,8 +14,9 @@ * limitations under the License. */ -package io.opentelemetry.context; +package io.opentelemetry.currentcontext; +import io.opentelemetry.context.Context; import java.util.logging.Level; import java.util.logging.Logger; @@ -61,7 +62,7 @@ static void detach(Context toDetach, Context toRestore) { static Context current() { Context current = localContext.get(); if (current == null) { - return DefaultContext.EMPTY; + return Context.empty(); } return current; } diff --git a/api/src/main/java/io/opentelemetry/context/Scope.java b/api/src/main/java/io/opentelemetry/currentcontext/Scope.java similarity index 96% rename from api/src/main/java/io/opentelemetry/context/Scope.java rename to api/src/main/java/io/opentelemetry/currentcontext/Scope.java index d32d07cfc6a..37cc48addbb 100644 --- a/api/src/main/java/io/opentelemetry/context/Scope.java +++ b/api/src/main/java/io/opentelemetry/currentcontext/Scope.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.opentelemetry.context; +package io.opentelemetry.currentcontext; import java.io.Closeable; import javax.annotation.concurrent.NotThreadSafe; diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java index 1d047e801fc..b7e33392bd3 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java @@ -18,8 +18,8 @@ import com.google.errorprone.annotations.MustBeClosed; import io.opentelemetry.common.AttributeValue; -import io.opentelemetry.context.CurrentContext; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.CurrentContext; +import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.internal.Utils; import java.util.Map; import javax.annotation.Nullable; diff --git a/api/src/main/java/io/opentelemetry/trace/Tracer.java b/api/src/main/java/io/opentelemetry/trace/Tracer.java index bc4b09198da..6c084bb1e5f 100644 --- a/api/src/main/java/io/opentelemetry/trace/Tracer.java +++ b/api/src/main/java/io/opentelemetry/trace/Tracer.java @@ -17,7 +17,7 @@ package io.opentelemetry.trace; import com.google.errorprone.annotations.MustBeClosed; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.Scope; import javax.annotation.concurrent.ThreadSafe; /** diff --git a/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java b/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java index 709b4a134f8..1d25fbc8fc2 100644 --- a/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java +++ b/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java @@ -19,13 +19,13 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertTrue; -import io.opentelemetry.context.Scope; import io.opentelemetry.context.propagation.ContextPropagators; import io.opentelemetry.context.propagation.DefaultContextPropagators; import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.correlationcontext.CorrelationContextManager; import io.opentelemetry.correlationcontext.DefaultCorrelationContextManager; import io.opentelemetry.correlationcontext.spi.CorrelationContextManagerProvider; +import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.metrics.BatchRecorder; import io.opentelemetry.metrics.DefaultMeterProvider; import io.opentelemetry.metrics.DoubleCounter; diff --git a/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java index c8e50776153..f2921c62103 100644 --- a/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java +++ b/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java @@ -18,8 +18,8 @@ import static com.google.common.truth.Truth.assertThat; -import io.opentelemetry.context.CurrentContext; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.CurrentContext; +import io.opentelemetry.currentcontext.Scope; import java.util.Arrays; import java.util.Collection; import javax.annotation.Nullable; diff --git a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java index 0bee9d043e6..f2e060aa67d 100644 --- a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java +++ b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java @@ -18,8 +18,8 @@ import static com.google.common.truth.Truth.assertThat; -import io.opentelemetry.context.CurrentContext; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.CurrentContext; +import io.opentelemetry.currentcontext.Scope; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; diff --git a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java index 704b583c3e8..1bc690c0129 100644 --- a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java +++ b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java @@ -17,7 +17,7 @@ package io.opentelemetry.contrib.trace.propagation; import io.opentelemetry.context.Context; -import io.opentelemetry.context.CurrentContext; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; diff --git a/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java b/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java index 60064477f63..02bcc4f47be 100644 --- a/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java +++ b/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java @@ -16,8 +16,8 @@ package io.opentelemetry.contrib.trace; -import io.opentelemetry.context.CurrentContext; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.CurrentContext; +import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Status; import java.util.concurrent.Callable; diff --git a/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java b/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java index 613007cc15a..4d661a2ebf0 100644 --- a/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java +++ b/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java @@ -20,7 +20,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyZeroInteractions; -import io.opentelemetry.context.CurrentContext; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Status; diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java index 6d4b0b59b8f..4b3e531a954 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java @@ -17,8 +17,8 @@ package io.opentelemetry.opentracingshim; import io.opentelemetry.context.Context; -import io.opentelemetry.context.CurrentContext; import io.opentelemetry.context.propagation.HttpTextFormat; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.trace.DefaultSpan; import io.opentracing.propagation.TextMapExtract; import io.opentracing.propagation.TextMapInject; diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeShim.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeShim.java index 7a443884d36..b8174797602 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeShim.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeShim.java @@ -19,9 +19,9 @@ import io.opentracing.Scope; final class ScopeShim implements Scope { - final io.opentelemetry.context.Scope scope; + final io.opentelemetry.currentcontext.Scope scope; - public ScopeShim(io.opentelemetry.context.Scope scope) { + public ScopeShim(io.opentelemetry.currentcontext.Scope scope) { this.scope = scope; } diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/OpenTelemetryInteroperabilityTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/OpenTelemetryInteroperabilityTest.java index e3a182c4fda..9f13bc21c3a 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/OpenTelemetryInteroperabilityTest.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/OpenTelemetryInteroperabilityTest.java @@ -64,7 +64,7 @@ public void sdkContinuesOpenTracingTrace() { @Test public void openTracingContinuesSdkTrace() { io.opentelemetry.trace.Span otelSpan = tracer.spanBuilder("otel_span").startSpan(); - try (io.opentelemetry.context.Scope scope = tracer.withSpan(otelSpan)) { + try (io.opentelemetry.currentcontext.Scope scope = tracer.withSpan(otelSpan)) { otTracer.buildSpan("ot_span").start().finish(); } finally { otelSpan.end(); diff --git a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java index 3616d94cea5..7d0a38c8930 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java @@ -17,10 +17,10 @@ package io.opentelemetry.sdk.correlationcontext; import com.google.errorprone.annotations.MustBeClosed; -import io.opentelemetry.context.CurrentContext; -import io.opentelemetry.context.Scope; import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.correlationcontext.CorrelationContextManager; +import io.opentelemetry.currentcontext.CurrentContext; +import io.opentelemetry.currentcontext.Scope; /** * {@link CorrelationContextManagerSdk} is SDK implementation of {@link CorrelationContextManager}. diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java index 4a0266f36b8..1939ceb68f5 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java @@ -17,7 +17,7 @@ package io.opentelemetry.sdk.trace; import io.opentelemetry.common.AttributeValue; -import io.opentelemetry.context.CurrentContext; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.internal.Utils; import io.opentelemetry.sdk.common.Clock; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java index e31baf303c3..69a2018c716 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java @@ -17,8 +17,8 @@ package io.opentelemetry.sdk.trace; import com.google.errorprone.annotations.MustBeClosed; -import io.opentelemetry.context.CurrentContext; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.CurrentContext; +import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.trace.DefaultTracer; import io.opentelemetry.trace.Span; diff --git a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java index 6f31ba70b85..15798b777d3 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java @@ -18,10 +18,10 @@ import static com.google.common.truth.Truth.assertThat; -import io.opentelemetry.context.CurrentContext; -import io.opentelemetry.context.Scope; import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.correlationcontext.EmptyCorrelationContext; +import io.opentelemetry.currentcontext.CurrentContext; +import io.opentelemetry.currentcontext.Scope; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java index d7179a79b6d..73084c17ff8 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java @@ -18,7 +18,6 @@ import static com.google.common.truth.Truth.assertThat; -import io.opentelemetry.context.Scope; import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.correlationcontext.CorrelationContextManager; import io.opentelemetry.correlationcontext.EmptyCorrelationContext; @@ -26,6 +25,7 @@ import io.opentelemetry.correlationcontext.EntryKey; import io.opentelemetry.correlationcontext.EntryMetadata; import io.opentelemetry.correlationcontext.EntryValue; +import io.opentelemetry.currentcontext.Scope; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java index 2a0be77a6bb..c58fc17254e 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java @@ -20,7 +20,7 @@ import static org.junit.Assert.assertFalse; import io.opentelemetry.common.AttributeValue; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.sdk.trace.config.TraceConfig; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.trace.DefaultSpan; diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java index ba815e18d01..e0ff1d3f8f6 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java @@ -19,8 +19,8 @@ import static com.google.common.truth.Truth.assertThat; import io.opentelemetry.common.AttributeValue; -import io.opentelemetry.context.CurrentContext; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.CurrentContext; +import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.sdk.trace.StressTestRunner.OperationUpdater; import io.opentelemetry.sdk.trace.data.SpanData; diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/activespanreplacement/ActiveSpanReplacementTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/activespanreplacement/ActiveSpanReplacementTest.java index 82f45d73e7b..937189b0f26 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/activespanreplacement/ActiveSpanReplacementTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/activespanreplacement/ActiveSpanReplacementTest.java @@ -22,7 +22,7 @@ import static org.awaitility.Awaitility.await; import static org.hamcrest.core.IsEqual.equalTo; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/Actor.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/Actor.java index e2c9db266ec..d3ecdafb65b 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/Actor.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/Actor.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.contrib.trace.testbed.actorpropagation; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.Tracer; diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/ActorPropagationTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/ActorPropagationTest.java index e29fd7021d1..51a7f762159 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/ActorPropagationTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/ActorPropagationTest.java @@ -18,7 +18,7 @@ import static com.google.common.truth.Truth.assertThat; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; import io.opentelemetry.sdk.trace.TracerSdkProvider; diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java index a21055d6deb..8b68499b9aa 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java @@ -18,8 +18,8 @@ import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; +import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.Tracer; diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java index 3faab1c8cd3..b3c8aa9b067 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java @@ -18,8 +18,8 @@ import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; import io.opentelemetry.context.propagation.HttpTextFormat.Getter; +import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.SpanContext; diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/concurrentcommonrequesthandler/HandlerTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/concurrentcommonrequesthandler/HandlerTest.java index bd47859b727..9346f701621 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/concurrentcommonrequesthandler/HandlerTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/concurrentcommonrequesthandler/HandlerTest.java @@ -18,7 +18,7 @@ import static com.google.common.truth.Truth.assertThat; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; import io.opentelemetry.sdk.trace.TracerSdkProvider; diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/errorreporting/ErrorReportingTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/errorreporting/ErrorReportingTest.java index fe7c8dcf88b..d347ae8d006 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/errorreporting/ErrorReportingTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/errorreporting/ErrorReportingTest.java @@ -21,7 +21,7 @@ import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertEquals; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; import io.opentelemetry.sdk.trace.TracerSdkProvider; diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/latespanfinish/LateSpanFinishTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/latespanfinish/LateSpanFinishTest.java index c7adf507524..8112fcbf21c 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/latespanfinish/LateSpanFinishTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/latespanfinish/LateSpanFinishTest.java @@ -18,7 +18,7 @@ import static com.google.common.truth.Truth.assertThat; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; import io.opentelemetry.sdk.trace.TracerSdkProvider; diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/Client.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/Client.java index df7f9fbdfec..21c62029ce1 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/Client.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/Client.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.contrib.trace.testbed.multiplecallbacks; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Tracer; import java.util.concurrent.Callable; diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/MultipleCallbacksTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/MultipleCallbacksTest.java index 2ffa710d8fb..9d52a1470fb 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/MultipleCallbacksTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/MultipleCallbacksTest.java @@ -20,7 +20,7 @@ import static org.awaitility.Awaitility.await; import static org.hamcrest.core.IsEqual.equalTo; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; import io.opentelemetry.sdk.trace.TracerSdkProvider; diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/nestedcallbacks/NestedCallbacksTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/nestedcallbacks/NestedCallbacksTest.java index fa533348603..f9e6a78bfa5 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/nestedcallbacks/NestedCallbacksTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/nestedcallbacks/NestedCallbacksTest.java @@ -21,7 +21,7 @@ import static org.hamcrest.core.IsEqual.equalTo; import io.opentelemetry.common.AttributeValue; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; import io.opentelemetry.sdk.trace.TracerSdkProvider; diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/Promise.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/Promise.java index 3ac41288e9b..7be4f519d39 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/Promise.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/Promise.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.contrib.trace.testbed.promisepropagation; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Tracer; import java.util.ArrayList; diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/PromisePropagationTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/PromisePropagationTest.java index c728dee2b18..6126e626a9c 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/PromisePropagationTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/PromisePropagationTest.java @@ -18,7 +18,7 @@ import static com.google.common.truth.Truth.assertThat; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; import io.opentelemetry.sdk.trace.TracerSdkProvider; diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/statelesscommonrequesthandler/RequestHandler.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/statelesscommonrequesthandler/RequestHandler.java index 4e24795930c..dd1e1aa7287 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/statelesscommonrequesthandler/RequestHandler.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/statelesscommonrequesthandler/RequestHandler.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.contrib.trace.testbed.statelesscommonrequesthandler; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.Tracer; diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/suspendresumepropagation/SuspendResume.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/suspendresumepropagation/SuspendResume.java index 28103d2806d..7f1301e8449 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/suspendresumepropagation/SuspendResume.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/suspendresumepropagation/SuspendResume.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.contrib.trace.testbed.suspendresumepropagation; -import io.opentelemetry.context.Scope; +import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Tracer; From 0614c034e9c993f84d41141f67b0b939e824f9f4 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 8 May 2020 12:01:53 -0700 Subject: [PATCH 04/22] Remove Tracer current span methods --- .../trace/DefaultTracerBenchmarks.java | 7 +- .../io/opentelemetry/trace/DefaultSpan.java | 3 +- .../io/opentelemetry/trace/DefaultTracer.java | 15 ---- .../java/io/opentelemetry/trace/Tracer.java | 70 +------------------ .../io/opentelemetry/OpenTelemetryTest.java | 12 ---- .../trace/DefaultTracerTest.java | 25 +++---- .../opentracingshim/ScopeManagerShim.java | 5 +- .../OpenTelemetryInteroperabilityTest.java | 7 +- .../io/opentelemetry/sdk/trace/TracerSdk.java | 16 ----- .../sdk/trace/SpanBuilderSdkTest.java | 7 +- .../sdk/trace/TracerSdkTest.java | 27 +++---- .../ActiveSpanReplacementTest.java | 11 +-- .../trace/testbed/actorpropagation/Actor.java | 7 +- .../ActorPropagationTest.java | 9 +-- .../trace/testbed/clientserver/Client.java | 3 +- .../trace/testbed/clientserver/Server.java | 5 +- .../clientserver/TestClientServerTest.java | 3 +- .../HandlerTest.java | 7 +- .../errorreporting/ErrorReportingTest.java | 21 +++--- .../latespanfinish/LateSpanFinishTest.java | 11 +-- .../listenerperrequest/ListenerTest.java | 3 +- .../testbed/multiplecallbacks/Client.java | 5 +- .../MultipleCallbacksTest.java | 5 +- .../nestedcallbacks/NestedCallbacksTest.java | 9 +-- .../testbed/promisepropagation/Promise.java | 7 +- .../PromisePropagationTest.java | 7 +- .../RequestHandler.java | 5 +- .../SuspendResume.java | 9 ++- 28 files changed, 116 insertions(+), 205 deletions(-) diff --git a/api/src/jmh/java/io/opentelemetry/trace/DefaultTracerBenchmarks.java b/api/src/jmh/java/io/opentelemetry/trace/DefaultTracerBenchmarks.java index 35608773c29..14960548b9a 100644 --- a/api/src/jmh/java/io/opentelemetry/trace/DefaultTracerBenchmarks.java +++ b/api/src/jmh/java/io/opentelemetry/trace/DefaultTracerBenchmarks.java @@ -16,6 +16,7 @@ package io.opentelemetry.trace; +import io.opentelemetry.currentcontext.CurrentContext; import java.util.concurrent.TimeUnit; import javax.annotation.Nullable; import org.openjdk.jmh.annotations.Benchmark; @@ -45,7 +46,7 @@ public class DefaultTracerBenchmarks { @Warmup(iterations = 5, time = 1) public void measureFullSpanLifecycle() { span = tracer.spanBuilder("span").startSpan(); - io.opentelemetry.currentcontext.Scope ignored = tracer.withSpan(span); + io.opentelemetry.currentcontext.Scope ignored = CurrentContext.withSpan(span); try { // no-op } finally { @@ -72,7 +73,7 @@ public void measureSpanBuilding() { @OutputTimeUnit(TimeUnit.NANOSECONDS) @Warmup(iterations = 5, time = 1) public void measureScopeLifecycle() { - io.opentelemetry.currentcontext.Scope ignored = tracer.withSpan(span); + io.opentelemetry.currentcontext.Scope ignored = CurrentContext.withSpan(span); try { // no-op } finally { @@ -87,7 +88,7 @@ public void measureScopeLifecycle() { @OutputTimeUnit(TimeUnit.NANOSECONDS) @Warmup(iterations = 5, time = 1) public void measureGetCurrentSpan() { - tracer.getCurrentSpan(); + CurrentContext.getSpan(); } @TearDown(Level.Iteration) diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java b/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java index cfdd4dc720a..92b366dd063 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java @@ -17,6 +17,7 @@ package io.opentelemetry.trace; import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.internal.Utils; import java.util.Map; import java.util.Random; @@ -26,7 +27,7 @@ * The {@code DefaultSpan} is the default {@link Span} that is used when no {@code Span} * implementation is available. All operations are no-op except context propagation. * - *

Used also to stop tracing, see {@link Tracer#withSpan}. + *

Used also to stop tracing, see {@link CurrentContext#withSpan}. * * @since 0.1.0 */ diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java index b7e33392bd3..e2de04d8806 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java @@ -16,10 +16,8 @@ package io.opentelemetry.trace; -import com.google.errorprone.annotations.MustBeClosed; import io.opentelemetry.common.AttributeValue; import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.internal.Utils; import java.util.Map; import javax.annotation.Nullable; @@ -44,19 +42,6 @@ public static Tracer getInstance() { return INSTANCE; } - // TODO (trask) can we remove this now? - @Override - public Span getCurrentSpan() { - return CurrentContext.getSpan(); - } - - // TODO (trask) can we remove this now? - @Override - @MustBeClosed - public Scope withSpan(Span span) { - return CurrentContext.withSpan(span); - } - @Override public Span.Builder spanBuilder(String spanName) { return NoopSpanBuilder.create(spanName); diff --git a/api/src/main/java/io/opentelemetry/trace/Tracer.java b/api/src/main/java/io/opentelemetry/trace/Tracer.java index 6c084bb1e5f..02fb7d491ce 100644 --- a/api/src/main/java/io/opentelemetry/trace/Tracer.java +++ b/api/src/main/java/io/opentelemetry/trace/Tracer.java @@ -16,8 +16,6 @@ package io.opentelemetry.trace; -import com.google.errorprone.annotations.MustBeClosed; -import io.opentelemetry.currentcontext.Scope; import javax.annotation.concurrent.ThreadSafe; /** @@ -69,75 +67,9 @@ * * @since 0.1.0 */ +// TODO (trask) update class javadoc @ThreadSafe public interface Tracer { - /** - * Gets the current Span from the current Context. - * - *

To install a {@link Span} to the current Context use {@link #withSpan(Span)}. - * - *

startSpan methods do NOT modify the current Context {@code Span}. - * - * @return a default {@code Span} that does nothing and has an invalid {@link SpanContext} if no - * {@code Span} is associated with the current Context, otherwise the current {@code Span} - * from the Context. - * @since 0.1.0 - */ - Span getCurrentSpan(); - - /** - * Enters the scope of code where the given {@link Span} is in the current Context, and returns an - * object that represents that scope. The scope is exited when the returned object is closed. - * - *

Supports try-with-resource idiom. - * - *

Can be called with {@link DefaultSpan} to enter a scope of code where tracing is stopped. - * - *

Example of usage: - * - *

{@code
-   * private static Tracer tracer = OpenTelemetry.getTracer();
-   * void doWork() {
-   *   // Create a Span as a child of the current Span.
-   *   Span span = tracer.spanBuilder("my span").startSpan();
-   *   try (Scope ws = tracer.withSpan(span)) {
-   *     tracer.getCurrentSpan().addEvent("my event");
-   *     doSomeOtherWork();  // Here "span" is the current Span.
-   *   }
-   *   span.end();
-   * }
-   * }
- * - *

Prior to Java SE 7, you can use a finally block to ensure that a resource is closed - * regardless of whether the try statement completes normally or abruptly. - * - *

Example of usage prior to Java SE7: - * - *

{@code
-   * private static Tracer tracer = OpenTelemetry.getTracer();
-   * void doWork() {
-   *   // Create a Span as a child of the current Span.
-   *   Span span = tracer.spanBuilder("my span").startSpan();
-   *   Scope ws = tracer.withSpan(span);
-   *   try {
-   *     tracer.getCurrentSpan().addEvent("my event");
-   *     doSomeOtherWork();  // Here "span" is the current Span.
-   *   } finally {
-   *     ws.close();
-   *   }
-   *   span.end();
-   * }
-   * }
- * - * @param span The {@link Span} to be set to the current Context. - * @return an object that defines a scope where the given {@link Span} will be set to the current - * Context. - * @throws NullPointerException if {@code span} is {@code null}. - * @since 0.1.0 - */ - @MustBeClosed - Scope withSpan(Span span); - /** * Returns a {@link Span.Builder} to create and start a new {@link Span}. * diff --git a/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java b/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java index 1d25fbc8fc2..246fefc28e1 100644 --- a/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java +++ b/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java @@ -259,18 +259,6 @@ public Tracer get(String instrumentationName, String instrumentationVersion) { return get(instrumentationName); } - @Nullable - @Override - public Span getCurrentSpan() { - return null; - } - - @Nullable - @Override - public Scope withSpan(Span span) { - return null; - } - @Nullable @Override public Span.Builder spanBuilder(String spanName) { diff --git a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java index f2e060aa67d..a98abaa5fa0 100644 --- a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java +++ b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java @@ -30,6 +30,7 @@ @RunWith(JUnit4.class) // Need to suppress warnings for MustBeClosed because Android 14 does not support // try-with-resources. +// TODO (trask) delete tests here that were designed to test Tracer methods that are now removed @SuppressWarnings("MustBeClosedChecker") public class DefaultTracerTest { private static final Tracer defaultTracer = DefaultTracer.getInstance(); @@ -47,19 +48,19 @@ public class DefaultTracerTest { @Test public void defaultGetCurrentSpan() { - assertThat(defaultTracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); } @Test public void getCurrentSpan_WithSpan() { - assertThat(defaultTracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); - Scope ws = defaultTracer.withSpan(DefaultSpan.createRandom()); + assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); + Scope ws = CurrentContext.withSpan(DefaultSpan.createRandom()); try { - assertThat(defaultTracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); } finally { ws.close(); } - assertThat(defaultTracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); } @Test @@ -76,21 +77,21 @@ public void defaultSpanBuilderWithName() { @Test public void testInProcessContext() { Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); - Scope scope = defaultTracer.withSpan(span); + Scope scope = CurrentContext.withSpan(span); try { - assertThat(defaultTracer.getCurrentSpan()).isEqualTo(span); + assertThat(CurrentContext.getSpan()).isEqualTo(span); Span secondSpan = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); - Scope secondScope = defaultTracer.withSpan(secondSpan); + Scope secondScope = CurrentContext.withSpan(secondSpan); try { - assertThat(defaultTracer.getCurrentSpan()).isEqualTo(secondSpan); + assertThat(CurrentContext.getSpan()).isEqualTo(secondSpan); } finally { secondScope.close(); - assertThat(defaultTracer.getCurrentSpan()).isEqualTo(span); + assertThat(CurrentContext.getSpan()).isEqualTo(span); } } finally { scope.close(); } - assertThat(defaultTracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); } @Test @@ -110,7 +111,7 @@ public void testSpanContextPropagation() { @Test public void testSpanContextPropagationCurrentSpan() { DefaultSpan parent = new DefaultSpan(spanContext); - Scope scope = defaultTracer.withSpan(parent); + Scope scope = CurrentContext.withSpan(parent); try { Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); assertThat(span.getContext()).isSameInstanceAs(spanContext); diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeManagerShim.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeManagerShim.java index baf296fea18..f74fe08ab1f 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeManagerShim.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeManagerShim.java @@ -16,6 +16,7 @@ package io.opentelemetry.opentracingshim; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentracing.Scope; import io.opentracing.ScopeManager; import io.opentracing.Span; @@ -32,7 +33,7 @@ public Span activeSpan() { // As OpenTracing simply returns null when no active instance is available, // we need to do an explicit check against DefaultSpan, // which is used in OpenTelemetry for this very case. - io.opentelemetry.trace.Span span = tracer().getCurrentSpan(); + io.opentelemetry.trace.Span span = CurrentContext.getSpan(); if (io.opentelemetry.trace.DefaultSpan.getInvalid().equals(span)) { return null; } @@ -45,7 +46,7 @@ public Span activeSpan() { @SuppressWarnings("MustBeClosedChecker") public Scope activate(Span span) { io.opentelemetry.trace.Span actualSpan = getActualSpan(span); - return new ScopeShim(tracer().withSpan(actualSpan)); + return new ScopeShim(CurrentContext.withSpan(actualSpan)); } static io.opentelemetry.trace.Span getActualSpan(Span span) { diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/OpenTelemetryInteroperabilityTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/OpenTelemetryInteroperabilityTest.java index 9f13bc21c3a..4902e378247 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/OpenTelemetryInteroperabilityTest.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/OpenTelemetryInteroperabilityTest.java @@ -20,6 +20,7 @@ import static org.junit.Assert.assertNull; import io.opentelemetry.correlationcontext.DefaultCorrelationContextManager; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.opentracingshim.TraceShim; import io.opentelemetry.sdk.trace.TracerSdkProvider; @@ -53,7 +54,7 @@ public void sdkContinuesOpenTracingTrace() { } finally { otSpan.finish(); } - assertEquals(tracer.getCurrentSpan().getClass(), DefaultSpan.class); + assertEquals(CurrentContext.getSpan().getClass(), DefaultSpan.class); assertNull(otTracer.activeSpan()); List finishedSpans = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); @@ -64,13 +65,13 @@ public void sdkContinuesOpenTracingTrace() { @Test public void openTracingContinuesSdkTrace() { io.opentelemetry.trace.Span otelSpan = tracer.spanBuilder("otel_span").startSpan(); - try (io.opentelemetry.currentcontext.Scope scope = tracer.withSpan(otelSpan)) { + try (io.opentelemetry.currentcontext.Scope scope = CurrentContext.withSpan(otelSpan)) { otTracer.buildSpan("ot_span").start().finish(); } finally { otelSpan.end(); } - assertEquals(tracer.getCurrentSpan().getClass(), DefaultSpan.class); + assertEquals(CurrentContext.getSpan().getClass(), DefaultSpan.class); assertNull(otTracer.activeSpan()); List finishedSpans = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java index 69a2018c716..4a3ed008f57 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java @@ -16,9 +16,6 @@ package io.opentelemetry.sdk.trace; -import com.google.errorprone.annotations.MustBeClosed; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.trace.DefaultTracer; import io.opentelemetry.trace.Span; @@ -34,19 +31,6 @@ final class TracerSdk implements Tracer { this.instrumentationLibraryInfo = instrumentationLibraryInfo; } - // TODO (trask) can we remove this now? - @Override - public Span getCurrentSpan() { - return CurrentContext.getSpan(); - } - - // TODO (trask) can we remove this now? - @Override - @MustBeClosed - public Scope withSpan(Span span) { - return CurrentContext.withSpan(span); - } - @Override public Span.Builder spanBuilder(String spanName) { if (sharedState.isStopped()) { diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java index c58fc17254e..7c503c6ce85 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java @@ -20,6 +20,7 @@ import static org.junit.Assert.assertFalse; import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.sdk.trace.config.TraceConfig; import io.opentelemetry.sdk.trace.data.SpanData; @@ -374,7 +375,7 @@ public void sampledViaParentLinks() { @Test public void noParent() { Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); - try (Scope scope = tracerSdk.withSpan(parent)) { + try (Scope scope = CurrentContext.withSpan(parent)) { Span span = tracerSdk.spanBuilder(SPAN_NAME).setNoParent().startSpan(); try { assertThat(span.getContext().getTraceId()).isNotEqualTo(parent.getContext().getTraceId()); @@ -456,7 +457,7 @@ public void overrideNoParent_remoteParent() { @Test public void parentCurrentSpan() { Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); - try (Scope scope = tracerSdk.withSpan(parent)) { + try (Scope scope = CurrentContext.withSpan(parent)) { RecordEventsReadableSpan span = (RecordEventsReadableSpan) tracerSdk.spanBuilder(SPAN_NAME).startSpan(); try { @@ -508,7 +509,7 @@ public void parent_clockIsSame() { @Test public void parentCurrentSpan_clockIsSame() { Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); - try (Scope scope = tracerSdk.withSpan(parent)) { + try (Scope scope = CurrentContext.withSpan(parent)) { RecordEventsReadableSpan span = (RecordEventsReadableSpan) tracerSdk.spanBuilder(SPAN_NAME).startSpan(); diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java index e0ff1d3f8f6..c9edc887862 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java @@ -41,6 +41,7 @@ @RunWith(JUnit4.class) // Need to suppress warnings for MustBeClosed because Android 14 does not support // try-with-resources. +// TODO (trask) delete tests here that were designed to test Tracer methods that are now removed @SuppressWarnings("MustBeClosedChecker") public class TracerSdkTest { @@ -64,7 +65,7 @@ public void setUp() { @Test public void defaultGetCurrentSpan() { - assertThat(tracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); } @Test @@ -74,29 +75,29 @@ public void defaultSpanBuilder() { @Test public void getCurrentSpan() { - assertThat(tracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); try (Scope ignored = CurrentContext.withSpan(span)) { - assertThat(tracer.getCurrentSpan()).isSameInstanceAs(span); + assertThat(CurrentContext.getSpan()).isSameInstanceAs(span); } - assertThat(tracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); } @Test public void withSpan_NullSpan() { - assertThat(tracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); - try (Scope ignored = tracer.withSpan(null)) { - assertThat(tracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); + try (Scope ignored = CurrentContext.withSpan(null)) { + assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); } - assertThat(tracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); } @Test public void getCurrentSpan_WithSpan() { - assertThat(tracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); - try (Scope ignored = tracer.withSpan(span)) { - assertThat(tracer.getCurrentSpan()).isSameInstanceAs(span); + assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); + try (Scope ignored = CurrentContext.withSpan(span)) { + assertThat(CurrentContext.getSpan()).isSameInstanceAs(span); } - assertThat(tracer.getCurrentSpan()).isInstanceOf(DefaultSpan.class); + assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); } @Test @@ -197,7 +198,7 @@ public SimpleSpanOperation(TracerSdk tracer) { @Override public void update() { Span span = tracer.spanBuilder("testSpan").startSpan(); - try (Scope ignored = tracer.withSpan(span)) { + try (Scope ignored = CurrentContext.withSpan(span)) { span.setAttribute("testAttribute", AttributeValue.stringAttributeValue("testValue")); } finally { span.end(); diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/activespanreplacement/ActiveSpanReplacementTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/activespanreplacement/ActiveSpanReplacementTest.java index 937189b0f26..6e271d9f7c9 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/activespanreplacement/ActiveSpanReplacementTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/activespanreplacement/ActiveSpanReplacementTest.java @@ -22,6 +22,7 @@ import static org.awaitility.Awaitility.await; import static org.hamcrest.core.IsEqual.equalTo; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.sdk.trace.TracerSdkProvider; @@ -49,7 +50,7 @@ public class ActiveSpanReplacementTest { public void test() { // Start an isolated task and query for its result in another task/thread Span span = tracer.spanBuilder("initial").startSpan(); - try (Scope scope = tracer.withSpan(span)) { + try (Scope scope = CurrentContext.withSpan(span)) { // Explicitly pass a Span to be finished once a late calculation is done. submitAnotherTask(span); } @@ -72,7 +73,7 @@ public void test() { assertThat(spans.get(0).getTraceId()).isNotEqualTo(spans.get(1).getTraceId()); assertThat(spans.get(0).getParentSpanId()).isEqualTo(SpanId.getInvalid()); - assertThat(tracer.getCurrentSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); + assertThat(CurrentContext.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); } private void submitAnotherTask(final Span initialSpan) { @@ -83,11 +84,11 @@ private void submitAnotherTask(final Span initialSpan) { public void run() { // Create a new Span for this task Span taskSpan = tracer.spanBuilder("task").startSpan(); - try (Scope scope = tracer.withSpan(taskSpan)) { + try (Scope scope = CurrentContext.withSpan(taskSpan)) { // Simulate work strictly related to the initial Span // and finish it. - try (Scope initialScope = tracer.withSpan(initialSpan)) { + try (Scope initialScope = CurrentContext.withSpan(initialSpan)) { sleep(50); } finally { initialSpan.end(); @@ -95,7 +96,7 @@ public void run() { // Restore the span for this task and create a subspan Span subTaskSpan = tracer.spanBuilder("subtask").startSpan(); - try (Scope subTaskScope = tracer.withSpan(subTaskSpan)) { + try (Scope subTaskScope = CurrentContext.withSpan(subTaskSpan)) { sleep(50); } finally { subTaskSpan.end(); diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/Actor.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/Actor.java index d3ecdafb65b..4a21c8ffc7f 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/Actor.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/Actor.java @@ -16,6 +16,7 @@ package io.opentelemetry.sdk.contrib.trace.testbed.actorpropagation; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; @@ -45,7 +46,7 @@ public void close() { } Future tell(final String message) { - final Span parent = tracer.getCurrentSpan(); + final Span parent = CurrentContext.getSpan(); phaser.register(); return executor.submit( new Runnable() { @@ -57,7 +58,7 @@ public void run() { .setParent(parent) .setSpanKind(Kind.CONSUMER) .startSpan(); - try (Scope ignored = tracer.withSpan(child)) { + try (Scope ignored = CurrentContext.withSpan(child)) { phaser.arriveAndAwaitAdvance(); // child tracer started child.addEvent("received " + message); phaser.arriveAndAwaitAdvance(); // assert size @@ -72,7 +73,7 @@ public void run() { } Future ask(final String message) { - final Span parent = tracer.getCurrentSpan(); + final Span parent = CurrentContext.getSpan(); phaser.register(); return executor.submit( new Callable() { diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/ActorPropagationTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/ActorPropagationTest.java index 51a7f762159..9601ea9d672 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/ActorPropagationTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/ActorPropagationTest.java @@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; @@ -60,7 +61,7 @@ public void testActorTell() { phaser.register(); Span parent = tracer.spanBuilder("actorTell").setSpanKind(Kind.PRODUCER).startSpan(); parent.setAttribute("component", "example-actor"); - try (Scope ignored = tracer.withSpan(parent)) { + try (Scope ignored = CurrentContext.withSpan(parent)) { actor.tell("my message 1"); actor.tell("my message 2"); } finally { @@ -84,7 +85,7 @@ public void testActorTell() { assertThat(TestUtils.getByKind(finished, Span.Kind.CONSUMER)).hasSize(2); assertThat(TestUtils.getOneByKind(finished, Span.Kind.PRODUCER)).isNotNull(); - assertThat(tracer.getCurrentSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); + assertThat(CurrentContext.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); } } @@ -97,7 +98,7 @@ public void testActorAsk() throws ExecutionException, InterruptedException { Span span = tracer.spanBuilder("actorAsk").setSpanKind(Kind.PRODUCER).startSpan(); span.setAttribute("component", "example-actor"); - try (Scope ignored = tracer.withSpan(span)) { + try (Scope ignored = CurrentContext.withSpan(span)) { future1 = actor.ask("my message 1"); future2 = actor.ask("my message 2"); } finally { @@ -125,7 +126,7 @@ public void testActorAsk() throws ExecutionException, InterruptedException { assertThat(TestUtils.getByKind(finished, Span.Kind.CONSUMER)).hasSize(2); assertThat(TestUtils.getOneByKind(finished, Span.Kind.PRODUCER)).isNotNull(); - assertThat(tracer.getCurrentSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); + assertThat(CurrentContext.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); } } } diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java index 8b68499b9aa..0a590e71f8a 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java @@ -19,6 +19,7 @@ import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; @@ -41,7 +42,7 @@ public void send() throws InterruptedException { Span span = tracer.spanBuilder("send").setSpanKind(Kind.CLIENT).startSpan(); span.setAttribute("component", "example-client"); - try (Scope ignored = tracer.withSpan(span)) { + try (Scope ignored = CurrentContext.withSpan(span)) { OpenTelemetry.getPropagators() .getHttpTextFormat() .inject( diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java index b3c8aa9b067..c851f80e310 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java @@ -19,6 +19,7 @@ import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat.Getter; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; @@ -56,9 +57,9 @@ public String get(Message carrier, String key) { tracer.spanBuilder("receive").setSpanKind(Kind.SERVER).setParent(spanContext).startSpan(); span.setAttribute("component", "example-server"); - try (Scope ignored = tracer.withSpan(span)) { + try (Scope ignored = CurrentContext.withSpan(span)) { // Simulate work. - tracer.getCurrentSpan().addEvent("DoWork"); + CurrentContext.getSpan().addEvent("DoWork"); } finally { span.end(); } diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/TestClientServerTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/TestClientServerTest.java index 301318eaee4..e681f1ac391 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/TestClientServerTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/TestClientServerTest.java @@ -21,6 +21,7 @@ import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertEquals; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; import io.opentelemetry.sdk.trace.TracerSdkProvider; @@ -73,6 +74,6 @@ public void test() throws Exception { assertThat(finished.get(0).getKind()).isEqualTo(Kind.CLIENT); assertThat(finished.get(1).getKind()).isEqualTo(Kind.SERVER); - assertThat(tracer.getCurrentSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); + assertThat(CurrentContext.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); } } diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/concurrentcommonrequesthandler/HandlerTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/concurrentcommonrequesthandler/HandlerTest.java index 9346f701621..b8ebe1a4db4 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/concurrentcommonrequesthandler/HandlerTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/concurrentcommonrequesthandler/HandlerTest.java @@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; @@ -70,14 +71,14 @@ public void two_requests() throws Exception { assertThat(finished.get(0).getParentSpanId()).isEqualTo(SpanId.getInvalid()); assertThat(finished.get(1).getParentSpanId()).isEqualTo(SpanId.getInvalid()); - assertThat(tracer.getCurrentSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); + assertThat(CurrentContext.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); } /** Active parent is not picked up by child. */ @Test public void parent_not_picked_up() throws Exception { Span parentSpan = tracer.spanBuilder("parent").startSpan(); - try (Scope ignored = tracer.withSpan(parentSpan)) { + try (Scope ignored = CurrentContext.withSpan(parentSpan)) { String response = client.send("no_parent").get(15, TimeUnit.SECONDS); assertThat(response).isEqualTo("no_parent:response"); } finally { @@ -107,7 +108,7 @@ public void parent_not_picked_up() throws Exception { public void bad_solution_to_set_parent() throws Exception { Client client; Span parentSpan = tracer.spanBuilder("parent").startSpan(); - try (Scope ignored = tracer.withSpan(parentSpan)) { + try (Scope ignored = CurrentContext.withSpan(parentSpan)) { client = new Client(new RequestHandler(tracer, parentSpan.getContext())); String response = client.send("correct_parent").get(15, TimeUnit.SECONDS); assertThat(response).isEqualTo("correct_parent:response"); diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/errorreporting/ErrorReportingTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/errorreporting/ErrorReportingTest.java index d347ae8d006..73d8ca7edc1 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/errorreporting/ErrorReportingTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/errorreporting/ErrorReportingTest.java @@ -21,6 +21,7 @@ import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertEquals; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; @@ -50,7 +51,7 @@ public final class ErrorReportingTest { @Test public void testSimpleError() { Span span = tracer.spanBuilder("one").startSpan(); - try (Scope ignored = tracer.withSpan(span)) { + try (Scope ignored = CurrentContext.withSpan(span)) { throw new RuntimeException("Invalid state"); } catch (Exception e) { span.setStatus(Status.UNKNOWN); @@ -58,7 +59,7 @@ public void testSimpleError() { span.end(); } - assertThat(tracer.getCurrentSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); + assertThat(CurrentContext.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); List spans = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); assertThat(spans).hasSize(1); @@ -74,7 +75,7 @@ public void testCallbackError() { new Runnable() { @Override public void run() { - try (Scope ignored = tracer.withSpan(span)) { + try (Scope ignored = CurrentContext.withSpan(span)) { throw new RuntimeException("Invalid state"); } catch (Exception exc) { span.setStatus(Status.UNKNOWN); @@ -103,7 +104,7 @@ public void testErrorRecovery() { Object res = null; Span span = tracer.spanBuilder("one").startSpan(); - try (Scope ignored = tracer.withSpan(span)) { + try (Scope ignored = CurrentContext.withSpan(span)) { while (res == null && retries++ < maxRetries) { try { throw new RuntimeException("No url could be fetched"); @@ -118,7 +119,7 @@ public void testErrorRecovery() { } span.end(); - assertThat(tracer.getCurrentSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); + assertThat(CurrentContext.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); List spans = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); assertThat(spans).hasSize(1); @@ -135,7 +136,7 @@ public void testErrorRecovery() { @Test public void testInstrumentationLayer() { Span span = tracer.spanBuilder("one").startSpan(); - try (Scope ignored = tracer.withSpan(span)) { + try (Scope ignored = CurrentContext.withSpan(span)) { // ScopedRunnable captures the active Span at this time. executor.submit( new ScopedRunnable( @@ -145,9 +146,9 @@ public void run() { try { throw new RuntimeException("Invalid state"); } catch (Exception exc) { - tracer.getCurrentSpan().setStatus(Status.UNKNOWN); + CurrentContext.getSpan().setStatus(Status.UNKNOWN); } finally { - tracer.getCurrentSpan().end(); + CurrentContext.getSpan().end(); } } }, @@ -171,13 +172,13 @@ private static class ScopedRunnable implements Runnable { private ScopedRunnable(Runnable runnable, Tracer tracer) { this.runnable = runnable; this.tracer = tracer; - this.span = tracer.getCurrentSpan(); + this.span = CurrentContext.getSpan(); } @Override public void run() { // No error reporting is done, as we are a simple wrapper. - try (Scope ignored = tracer.withSpan(span)) { + try (Scope ignored = CurrentContext.withSpan(span)) { runnable.run(); } } diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/latespanfinish/LateSpanFinishTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/latespanfinish/LateSpanFinishTest.java index 8112fcbf21c..dc3122a83d3 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/latespanfinish/LateSpanFinishTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/latespanfinish/LateSpanFinishTest.java @@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; @@ -62,7 +63,7 @@ public void test() throws Exception { TestUtils.assertSameTrace(spans); - assertThat(tracer.getCurrentSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); + assertThat(CurrentContext.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); } /* @@ -77,9 +78,9 @@ private void submitTasks(final Span parentSpan) { public void run() { /* Alternative to calling activate() is to pass it manually to asChildOf() for each * created Span. */ - try (Scope scope = tracer.withSpan(parentSpan)) { + try (Scope scope = CurrentContext.withSpan(parentSpan)) { Span childSpan = tracer.spanBuilder("task1").startSpan(); - try (Scope childScope = tracer.withSpan(childSpan)) { + try (Scope childScope = CurrentContext.withSpan(childSpan)) { TestUtils.sleep(55); } finally { childSpan.end(); @@ -92,9 +93,9 @@ public void run() { new Runnable() { @Override public void run() { - try (Scope scope = tracer.withSpan(parentSpan)) { + try (Scope scope = CurrentContext.withSpan(parentSpan)) { Span childSpan = tracer.spanBuilder("task2").startSpan(); - try (Scope childScope = tracer.withSpan(childSpan)) { + try (Scope childScope = CurrentContext.withSpan(childSpan)) { TestUtils.sleep(85); } finally { childSpan.end(); diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/listenerperrequest/ListenerTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/listenerperrequest/ListenerTest.java index a3e7a20004e..c8eace80149 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/listenerperrequest/ListenerTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/listenerperrequest/ListenerTest.java @@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; @@ -44,6 +45,6 @@ public void test() throws Exception { assertThat(finished).hasSize(1); assertThat(finished.get(0).getKind()).isEqualTo(Kind.CLIENT); - assertThat(tracer.getCurrentSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); + assertThat(CurrentContext.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); } } diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/Client.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/Client.java index 21c62029ce1..d10291191a2 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/Client.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/Client.java @@ -16,6 +16,7 @@ package io.opentelemetry.sdk.contrib.trace.testbed.multiplecallbacks; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Tracer; @@ -36,14 +37,14 @@ public Client(Tracer tracer, CountDownLatch parentDoneLatch) { } public Future send(final Object message) { - final Span parent = tracer.getCurrentSpan(); + final Span parent = CurrentContext.getSpan(); return executor.submit( new Callable() { @Override public Object call() throws Exception { Span span = tracer.spanBuilder("subtask").setParent(parent).startSpan(); - try (Scope subtaskScope = tracer.withSpan(span)) { + try (Scope subtaskScope = CurrentContext.withSpan(span)) { // Simulate work - make sure we finish *after* the parent Span. parentDoneLatch.await(); } finally { diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/MultipleCallbacksTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/MultipleCallbacksTest.java index 9d52a1470fb..b5731baa472 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/MultipleCallbacksTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/MultipleCallbacksTest.java @@ -20,6 +20,7 @@ import static org.awaitility.Awaitility.await; import static org.hamcrest.core.IsEqual.equalTo; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; @@ -52,7 +53,7 @@ public void test() { Client client = new Client(tracer, parentDoneLatch); Span span = tracer.spanBuilder("parent").startSpan(); - try (Scope scope = tracer.withSpan(span)) { + try (Scope scope = CurrentContext.withSpan(span)) { client.send("task1"); client.send("task2"); client.send("task3"); @@ -75,6 +76,6 @@ public void test() { assertThat(spans.get(i).getParentSpanId()).isEqualTo(parentSpan.getSpanId()); } - assertThat(tracer.getCurrentSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); + assertThat(CurrentContext.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); } } diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/nestedcallbacks/NestedCallbacksTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/nestedcallbacks/NestedCallbacksTest.java index f9e6a78bfa5..bf4de83a688 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/nestedcallbacks/NestedCallbacksTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/nestedcallbacks/NestedCallbacksTest.java @@ -21,6 +21,7 @@ import static org.hamcrest.core.IsEqual.equalTo; import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; @@ -65,7 +66,7 @@ public void test() { assertThat(attrs.get("key" + i).getStringValue()).isEqualTo(Integer.toString(i)); } - assertThat(tracer.getCurrentSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); + assertThat(CurrentContext.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); } private void submitCallbacks(final Span span) { @@ -74,21 +75,21 @@ private void submitCallbacks(final Span span) { new Runnable() { @Override public void run() { - try (Scope ignored = tracer.withSpan(span)) { + try (Scope ignored = CurrentContext.withSpan(span)) { span.setAttribute("key1", "1"); executor.submit( new Runnable() { @Override public void run() { - try (Scope ignored = tracer.withSpan(span)) { + try (Scope ignored = CurrentContext.withSpan(span)) { span.setAttribute("key2", "2"); executor.submit( new Runnable() { @Override public void run() { - try (Scope ignored = tracer.withSpan(span)) { + try (Scope ignored = CurrentContext.withSpan(span)) { span.setAttribute("key3", "3"); } finally { span.end(); diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/Promise.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/Promise.java index 7be4f519d39..e9d6239a55a 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/Promise.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/Promise.java @@ -16,6 +16,7 @@ package io.opentelemetry.sdk.contrib.trace.testbed.promisepropagation; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Tracer; @@ -35,7 +36,7 @@ final class Promise { // Passed along here for testing. Normally should be referenced via GlobalTracer.get(). this.tracer = tracer; - parentSpan = tracer.getCurrentSpan(); + parentSpan = CurrentContext.getSpan(); } void onSuccess(SuccessCallback successCallback) { @@ -55,7 +56,7 @@ void success(final T result) { public void run() { Span childSpan = tracer.spanBuilder("success").setParent(parentSpan).startSpan(); childSpan.setAttribute("component", "success"); - try (Scope ignored = tracer.withSpan(childSpan)) { + try (Scope ignored = CurrentContext.withSpan(childSpan)) { callback.accept(result); } finally { childSpan.end(); @@ -75,7 +76,7 @@ void error(final Throwable error) { public void run() { Span childSpan = tracer.spanBuilder("error").setParent(parentSpan).startSpan(); childSpan.setAttribute("component", "error"); - try (Scope ignored = tracer.withSpan(childSpan)) { + try (Scope ignored = CurrentContext.withSpan(childSpan)) { callback.accept(error); } finally { childSpan.end(); diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/PromisePropagationTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/PromisePropagationTest.java index 6126e626a9c..74b305e7e48 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/PromisePropagationTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/PromisePropagationTest.java @@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; @@ -62,14 +63,14 @@ public void testPromiseCallback() { Span parentSpan = tracer.spanBuilder("promises").startSpan(); parentSpan.setAttribute("component", "example-promises"); - try (Scope ignored = tracer.withSpan(parentSpan)) { + try (Scope ignored = CurrentContext.withSpan(parentSpan)) { Promise successPromise = new Promise<>(context, tracer); successPromise.onSuccess( new Promise.SuccessCallback() { @Override public void accept(String s) { - tracer.getCurrentSpan().addEvent("Promised 1 " + s); + CurrentContext.getSpan().addEvent("Promised 1 " + s); successResult1.set(s); phaser.arriveAndAwaitAdvance(); // result set } @@ -78,7 +79,7 @@ public void accept(String s) { new Promise.SuccessCallback() { @Override public void accept(String s) { - tracer.getCurrentSpan().addEvent("Promised 2 " + s); + CurrentContext.getSpan().addEvent("Promised 2 " + s); successResult2.set(s); phaser.arriveAndAwaitAdvance(); // result set } diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/statelesscommonrequesthandler/RequestHandler.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/statelesscommonrequesthandler/RequestHandler.java index dd1e1aa7287..ae8fcac5bf1 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/statelesscommonrequesthandler/RequestHandler.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/statelesscommonrequesthandler/RequestHandler.java @@ -16,6 +16,7 @@ package io.opentelemetry.sdk.contrib.trace.testbed.statelesscommonrequesthandler; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; @@ -41,13 +42,13 @@ public RequestHandler(Tracer tracer) { /** beforeRequest handler....... */ public void beforeRequest(Object request) { Span span = tracer.spanBuilder(OPERATION_NAME).setSpanKind(Kind.SERVER).startSpan(); - tlsScope.set(tracer.withSpan(span)); + tlsScope.set(CurrentContext.withSpan(span)); } /** afterResponse handler....... */ public void afterResponse(Object response) { // Finish the Span - tracer.getCurrentSpan().end(); + CurrentContext.getSpan().end(); // Deactivate the Span tlsScope.get().close(); diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/suspendresumepropagation/SuspendResume.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/suspendresumepropagation/SuspendResume.java index 7f1301e8449..fb607cbbba6 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/suspendresumepropagation/SuspendResume.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/suspendresumepropagation/SuspendResume.java @@ -16,27 +16,26 @@ package io.opentelemetry.sdk.contrib.trace.testbed.suspendresumepropagation; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Tracer; final class SuspendResume { - private final Tracer tracer; private final Span span; public SuspendResume(int id, Tracer tracer) { - // Passed along here for testing. Normally should be referenced via GlobalTracer.get(). - this.tracer = tracer; + // Tracer passed along here for testing. Normally should be referenced via GlobalTracer.get(). Span span = tracer.spanBuilder("job " + id).startSpan(); span.setAttribute("component", "suspend-resume"); - try (Scope scope = tracer.withSpan(span)) { + try (Scope scope = CurrentContext.withSpan(span)) { this.span = span; } } public void doPart(String name) { - try (Scope scope = tracer.withSpan(span)) { + try (Scope scope = CurrentContext.withSpan(span)) { span.addEvent("part: " + name); } } From 3c60b5bfb404112bf043d13e76d7ef07d55f71bb Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 8 May 2020 12:16:12 -0700 Subject: [PATCH 05/22] Remove ContextCorrelationManager current methods --- .../CorrelationContext.java | 8 +-- .../CorrelationContextManager.java | 25 +-------- .../DefaultCorrelationContextManager.java | 16 ------ .../io/opentelemetry/OpenTelemetryTest.java | 13 ----- .../DefaultCorrelationContextManagerTest.java | 23 ++++---- .../CorrelationContextManagerSdk.java | 16 ------ .../CorrelationContextSdk.java | 4 +- .../CorrelationContextManagerSdkTest.java | 15 +++--- .../ScopedCorrelationContextTest.java | 53 ++++++++++--------- 9 files changed, 56 insertions(+), 117 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java index 93815e2d115..8d91782a182 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java @@ -16,6 +16,7 @@ package io.opentelemetry.correlationcontext; +import io.opentelemetry.currentcontext.CurrentContext; import java.util.Collection; import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; @@ -58,8 +59,8 @@ public interface CorrelationContext { interface Builder { /** * Sets the parent {@link CorrelationContext} to use. If no parent is provided, the value of - * {@link CorrelationContextManager#getCurrentContext()} at {@link #build()} time will be used - * as parent, unless {@link #setNoParent()} was called. + * {@link CurrentContext#getCorrelationContext()} at {@link #build()} time will be used as + * parent, unless {@link #setNoParent()} was called. * *

This must be used to create a {@link CorrelationContext} when manual Context * propagation is used. @@ -77,8 +78,7 @@ interface Builder { /** * Sets the option to become a root {@link CorrelationContext} with no parent. If not * called, the value provided using {@link #setParent(CorrelationContext)} or otherwise {@link - * CorrelationContextManager#getCurrentContext()} at {@link #build()} time will be used as - * parent. + * CurrentContext#getCorrelationContext()} at {@link #build()} time will be used as parent. * * @return this. * @since 0.1.0 diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextManager.java b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextManager.java index 1ad034fcf0f..0f6c4e5db8a 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextManager.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextManager.java @@ -16,7 +16,6 @@ package io.opentelemetry.correlationcontext; -import io.opentelemetry.currentcontext.Scope; import javax.annotation.concurrent.ThreadSafe; /** @@ -27,22 +26,14 @@ * implementation-dependent {@link CorrelationContext}s. * *

Implementations may have different constraints and are free to convert entry contexts to their - * own subtypes. This means callers cannot assume the {@link #getCurrentContext() current context} - * is the same instance as the one {@link #withContext(CorrelationContext) placed into scope}. + * own subtypes. * * @since 0.1.0 */ +// TODO (trask) update class javadoc @ThreadSafe public interface CorrelationContextManager { - /** - * Returns the current {@code CorrelationContext}. - * - * @return the current {@code CorrelationContext}. - * @since 0.1.0 - */ - CorrelationContext getCurrentContext(); - /** * Returns a new {@code Builder}. * @@ -50,16 +41,4 @@ public interface CorrelationContextManager { * @since 0.1.0 */ CorrelationContext.Builder contextBuilder(); - - /** - * Enters the scope of code where the given {@code CorrelationContext} is in the current context - * (replacing the previous {@code CorrelationContext}) and returns an object that represents that - * scope. The scope is exited when the returned object is closed. - * - * @param distContext the {@code CorrelationContext} to be set as the current context. - * @return an object that defines a scope where the given {@code CorrelationContext} is set as the - * current context. - * @since 0.1.0 - */ - Scope withContext(CorrelationContext distContext); } diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java index 3422ab92ad4..8a540e3a597 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManager.java @@ -16,9 +16,6 @@ package io.opentelemetry.correlationcontext; -import com.google.errorprone.annotations.MustBeClosed; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.internal.Utils; import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.ThreadSafe; @@ -44,24 +41,11 @@ public static CorrelationContextManager getInstance() { return INSTANCE; } - // TODO (trask) can we remove this now? - @Override - public CorrelationContext getCurrentContext() { - return CurrentContext.getCorrelationContext(); - } - @Override public CorrelationContext.Builder contextBuilder() { return new NoopCorrelationContextBuilder(); } - // TODO (trask) can we remove this now? - @Override - @MustBeClosed - public Scope withContext(CorrelationContext correlationContext) { - return CurrentContext.withCorrelationContext(correlationContext); - } - @Immutable private static final class NoopCorrelationContextBuilder implements CorrelationContext.Builder { @Override diff --git a/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java b/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java index 246fefc28e1..973b7440418 100644 --- a/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java +++ b/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java @@ -25,7 +25,6 @@ import io.opentelemetry.correlationcontext.CorrelationContextManager; import io.opentelemetry.correlationcontext.DefaultCorrelationContextManager; import io.opentelemetry.correlationcontext.spi.CorrelationContextManagerProvider; -import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.metrics.BatchRecorder; import io.opentelemetry.metrics.DefaultMeterProvider; import io.opentelemetry.metrics.DoubleCounter; @@ -361,22 +360,10 @@ public CorrelationContextManager create() { return new FirstCorrelationContextManager(); } - @Nullable - @Override - public CorrelationContext getCurrentContext() { - return null; - } - @Nullable @Override public CorrelationContext.Builder contextBuilder() { return null; } - - @Nullable - @Override - public Scope withContext(CorrelationContext distContext) { - return null; - } } } diff --git a/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java index f2921c62103..fa9bf21154a 100644 --- a/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java +++ b/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java @@ -30,6 +30,8 @@ import org.junit.runners.JUnit4; /** Unit tests for {@link DefaultCorrelationContextManager}. */ +// TODO (trask) delete tests here that were designed to test CorrelationContextManager methods that +// are now removed @RunWith(JUnit4.class) public final class DefaultCorrelationContextManagerTest { private static final CorrelationContextManager defaultCorrelationContextManager = @@ -61,7 +63,7 @@ public void builderMethod() { @Test public void getCurrentContext_DefaultContext() { - assertThat(defaultCorrelationContextManager.getCurrentContext()) + assertThat(CurrentContext.getCorrelationContext()) .isSameInstanceAs(EmptyCorrelationContext.getInstance()); } @@ -69,7 +71,7 @@ public void getCurrentContext_DefaultContext() { public void getCurrentContext_ContextSetToNull() { Scope scope = CurrentContext.withCorrelationContext(null); try { - CorrelationContext distContext = defaultCorrelationContextManager.getCurrentContext(); + CorrelationContext distContext = CurrentContext.getCorrelationContext(); assertThat(distContext).isNotNull(); assertThat(distContext.getEntries()).isEmpty(); } finally { @@ -79,31 +81,30 @@ public void getCurrentContext_ContextSetToNull() { @Test public void withContext() { - assertThat(defaultCorrelationContextManager.getCurrentContext()) + assertThat(CurrentContext.getCorrelationContext()) .isSameInstanceAs(EmptyCorrelationContext.getInstance()); - Scope wtm = defaultCorrelationContextManager.withContext(DIST_CONTEXT); + Scope wtm = CurrentContext.withCorrelationContext(DIST_CONTEXT); try { - assertThat(defaultCorrelationContextManager.getCurrentContext()) - .isSameInstanceAs(DIST_CONTEXT); + assertThat(CurrentContext.getCorrelationContext()).isSameInstanceAs(DIST_CONTEXT); } finally { wtm.close(); } - assertThat(defaultCorrelationContextManager.getCurrentContext()) + assertThat(CurrentContext.getCorrelationContext()) .isSameInstanceAs(EmptyCorrelationContext.getInstance()); } @Test public void withContext_nullContext() { - assertThat(defaultCorrelationContextManager.getCurrentContext()) + assertThat(CurrentContext.getCorrelationContext()) .isSameInstanceAs(EmptyCorrelationContext.getInstance()); - Scope wtm = defaultCorrelationContextManager.withContext(null); + Scope wtm = CurrentContext.withCorrelationContext(null); try { - assertThat(defaultCorrelationContextManager.getCurrentContext()) + assertThat(CurrentContext.getCorrelationContext()) .isSameInstanceAs(EmptyCorrelationContext.getInstance()); } finally { wtm.close(); } - assertThat(defaultCorrelationContextManager.getCurrentContext()) + assertThat(CurrentContext.getCorrelationContext()) .isSameInstanceAs(EmptyCorrelationContext.getInstance()); } diff --git a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java index 7d0a38c8930..9cbf4c2192c 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java @@ -16,32 +16,16 @@ package io.opentelemetry.sdk.correlationcontext; -import com.google.errorprone.annotations.MustBeClosed; import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.correlationcontext.CorrelationContextManager; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; /** * {@link CorrelationContextManagerSdk} is SDK implementation of {@link CorrelationContextManager}. */ public class CorrelationContextManagerSdk implements CorrelationContextManager { - // TODO (trask) can we remove this now? - @Override - public CorrelationContext getCurrentContext() { - return CurrentContext.getCorrelationContext(); - } - @Override public CorrelationContext.Builder contextBuilder() { return new CorrelationContextSdk.Builder(); } - - // TODO (trask) can we remove this now? - @Override - @MustBeClosed - public Scope withContext(CorrelationContext correlationContext) { - return CurrentContext.withCorrelationContext(correlationContext); - } } diff --git a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java index e4b03cb21be..c3a5ca548c7 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java @@ -16,12 +16,12 @@ package io.opentelemetry.sdk.correlationcontext; -import io.opentelemetry.OpenTelemetry; import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.correlationcontext.Entry; import io.opentelemetry.correlationcontext.EntryKey; import io.opentelemetry.correlationcontext.EntryMetadata; import io.opentelemetry.correlationcontext.EntryValue; +import io.opentelemetry.currentcontext.CurrentContext; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -157,7 +157,7 @@ public CorrelationContext.Builder remove(EntryKey key) { @Override public CorrelationContextSdk build() { if (parent == null && !noImplicitParent) { - parent = OpenTelemetry.getCorrelationContextManager().getCurrentContext(); + parent = CurrentContext.getCorrelationContext(); } return new CorrelationContextSdk(entries, parent); } diff --git a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java index 15798b777d3..45e4ed121d1 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java @@ -33,10 +33,11 @@ @RunWith(JUnit4.class) // Need to suppress warnings for MustBeClosed because Android 14 does not support // try-with-resources. +// TODO (trask) delete tests here that were designed to test CorrelationContextManager methods that +// are now removed @SuppressWarnings("MustBeClosedChecker") public class CorrelationContextManagerSdkTest { @Mock private CorrelationContext distContext; - private final CorrelationContextManagerSdk contextManager = new CorrelationContextManagerSdk(); @Before public void setUp() { @@ -45,14 +46,14 @@ public void setUp() { @Test public void testGetCurrentContext_DefaultContext() { - assertThat(contextManager.getCurrentContext()) + assertThat(CurrentContext.getCorrelationContext()) .isSameInstanceAs(EmptyCorrelationContext.getInstance()); } @Test public void testGetCurrentContext_ContextSetToNull() { try (Scope ignored = CurrentContext.withCorrelationContext(null)) { - CorrelationContext distContext = contextManager.getCurrentContext(); + CorrelationContext distContext = CurrentContext.getCorrelationContext(); assertThat(distContext).isNotNull(); assertThat(distContext.getEntries()).isEmpty(); } @@ -60,12 +61,12 @@ public void testGetCurrentContext_ContextSetToNull() { @Test public void testWithCorrelationContext() { - assertThat(contextManager.getCurrentContext()) + assertThat(CurrentContext.getCorrelationContext()) .isSameInstanceAs(EmptyCorrelationContext.getInstance()); - try (Scope ignored = contextManager.withContext(distContext)) { - assertThat(contextManager.getCurrentContext()).isSameInstanceAs(distContext); + try (Scope ignored = CurrentContext.withCorrelationContext(distContext)) { + assertThat(CurrentContext.getCorrelationContext()).isSameInstanceAs(distContext); } - assertThat(contextManager.getCurrentContext()) + assertThat(CurrentContext.getCorrelationContext()) .isSameInstanceAs(EmptyCorrelationContext.getInstance()); } } diff --git a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java index 73084c17ff8..39e4c925c11 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java @@ -25,6 +25,7 @@ import io.opentelemetry.correlationcontext.EntryKey; import io.opentelemetry.correlationcontext.EntryMetadata; import io.opentelemetry.correlationcontext.EntryValue; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.currentcontext.Scope; import org.junit.Test; import org.junit.runner.RunWith; @@ -34,6 +35,8 @@ * Unit tests for the methods in {@link CorrelationContextManagerSdk} and {@link * CorrelationContextSdk.Builder} that interact with the current {@link CorrelationContextSdk}. */ +// TODO (trask) delete tests here that were designed to test CorrelationContextManager methods that +// are now removed @RunWith(JUnit4.class) public class ScopedCorrelationContextTest { private static final EntryKey KEY_1 = EntryKey.create("key 1"); @@ -54,27 +57,27 @@ public class ScopedCorrelationContextTest { @Test public void emptyCorrelationContext() { - CorrelationContext defaultCorrelationContext = contextManager.getCurrentContext(); + CorrelationContext defaultCorrelationContext = CurrentContext.getCorrelationContext(); assertThat(defaultCorrelationContext.getEntries()).isEmpty(); assertThat(defaultCorrelationContext).isInstanceOf(EmptyCorrelationContext.class); } @Test public void withContext() { - assertThat(contextManager.getCurrentContext().getEntries()).isEmpty(); + assertThat(CurrentContext.getCorrelationContext().getEntries()).isEmpty(); CorrelationContext scopedEntries = contextManager.contextBuilder().put(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION).build(); - try (Scope scope = contextManager.withContext(scopedEntries)) { - assertThat(contextManager.getCurrentContext()).isSameInstanceAs(scopedEntries); + try (Scope scope = CurrentContext.withCorrelationContext(scopedEntries)) { + assertThat(CurrentContext.getCorrelationContext()).isSameInstanceAs(scopedEntries); } - assertThat(contextManager.getCurrentContext().getEntries()).isEmpty(); + assertThat(CurrentContext.getCorrelationContext().getEntries()).isEmpty(); } @Test public void createBuilderFromCurrentEntries() { CorrelationContext scopedDistContext = contextManager.contextBuilder().put(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION).build(); - try (Scope scope = contextManager.withContext(scopedDistContext)) { + try (Scope scope = CurrentContext.withCorrelationContext(scopedDistContext)) { CorrelationContext newEntries = contextManager .contextBuilder() @@ -84,41 +87,41 @@ public void createBuilderFromCurrentEntries() { .containsExactly( Entry.create(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION), Entry.create(KEY_2, VALUE_2, METADATA_UNLIMITED_PROPAGATION)); - assertThat(contextManager.getCurrentContext()).isSameInstanceAs(scopedDistContext); + assertThat(CurrentContext.getCorrelationContext()).isSameInstanceAs(scopedDistContext); } } @Test public void setCurrentEntriesWithBuilder() { - assertThat(contextManager.getCurrentContext().getEntries()).isEmpty(); + assertThat(CurrentContext.getCorrelationContext().getEntries()).isEmpty(); CorrelationContext scopedDistContext = contextManager.contextBuilder().put(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION).build(); - try (Scope scope = contextManager.withContext(scopedDistContext)) { - assertThat(contextManager.getCurrentContext().getEntries()) + try (Scope scope = CurrentContext.withCorrelationContext(scopedDistContext)) { + assertThat(CurrentContext.getCorrelationContext().getEntries()) .containsExactly(Entry.create(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION)); - assertThat(contextManager.getCurrentContext()).isSameInstanceAs(scopedDistContext); + assertThat(CurrentContext.getCorrelationContext()).isSameInstanceAs(scopedDistContext); } - assertThat(contextManager.getCurrentContext().getEntries()).isEmpty(); + assertThat(CurrentContext.getCorrelationContext().getEntries()).isEmpty(); } @Test public void addToCurrentEntriesWithBuilder() { CorrelationContext scopedDistContext = contextManager.contextBuilder().put(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION).build(); - try (Scope scope1 = contextManager.withContext(scopedDistContext)) { + try (Scope scope1 = CurrentContext.withCorrelationContext(scopedDistContext)) { CorrelationContext innerDistContext = contextManager .contextBuilder() .put(KEY_2, VALUE_2, METADATA_UNLIMITED_PROPAGATION) .build(); - try (Scope scope2 = contextManager.withContext(innerDistContext)) { - assertThat(contextManager.getCurrentContext().getEntries()) + try (Scope scope2 = CurrentContext.withCorrelationContext(innerDistContext)) { + assertThat(CurrentContext.getCorrelationContext().getEntries()) .containsExactly( Entry.create(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION), Entry.create(KEY_2, VALUE_2, METADATA_UNLIMITED_PROPAGATION)); - assertThat(contextManager.getCurrentContext()).isSameInstanceAs(innerDistContext); + assertThat(CurrentContext.getCorrelationContext()).isSameInstanceAs(innerDistContext); } - assertThat(contextManager.getCurrentContext()).isSameInstanceAs(scopedDistContext); + assertThat(CurrentContext.getCorrelationContext()).isSameInstanceAs(scopedDistContext); } } @@ -130,31 +133,31 @@ public void multiScopeCorrelationContextWithMetadata() { .put(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION) .put(KEY_2, VALUE_2, METADATA_UNLIMITED_PROPAGATION) .build(); - try (Scope scope1 = contextManager.withContext(scopedDistContext)) { + try (Scope scope1 = CurrentContext.withCorrelationContext(scopedDistContext)) { CorrelationContext innerDistContext = contextManager .contextBuilder() .put(KEY_3, VALUE_3, METADATA_NO_PROPAGATION) .put(KEY_2, VALUE_4, METADATA_NO_PROPAGATION) .build(); - try (Scope scope2 = contextManager.withContext(innerDistContext)) { - assertThat(contextManager.getCurrentContext().getEntries()) + try (Scope scope2 = CurrentContext.withCorrelationContext(innerDistContext)) { + assertThat(CurrentContext.getCorrelationContext().getEntries()) .containsExactly( Entry.create(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION), Entry.create(KEY_2, VALUE_4, METADATA_NO_PROPAGATION), Entry.create(KEY_3, VALUE_3, METADATA_NO_PROPAGATION)); - assertThat(contextManager.getCurrentContext()).isSameInstanceAs(innerDistContext); + assertThat(CurrentContext.getCorrelationContext()).isSameInstanceAs(innerDistContext); } - assertThat(contextManager.getCurrentContext()).isSameInstanceAs(scopedDistContext); + assertThat(CurrentContext.getCorrelationContext()).isSameInstanceAs(scopedDistContext); } } @Test public void setNoParent_doesNotInheritContext() { - assertThat(contextManager.getCurrentContext().getEntries()).isEmpty(); + assertThat(CurrentContext.getCorrelationContext().getEntries()).isEmpty(); CorrelationContext scopedDistContext = contextManager.contextBuilder().put(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION).build(); - try (Scope scope = contextManager.withContext(scopedDistContext)) { + try (Scope scope = CurrentContext.withCorrelationContext(scopedDistContext)) { CorrelationContext innerDistContext = contextManager .contextBuilder() @@ -164,6 +167,6 @@ public void setNoParent_doesNotInheritContext() { assertThat(innerDistContext.getEntries()) .containsExactly(Entry.create(KEY_2, VALUE_2, METADATA_UNLIMITED_PROPAGATION)); } - assertThat(contextManager.getCurrentContext().getEntries()).isEmpty(); + assertThat(CurrentContext.getCorrelationContext().getEntries()).isEmpty(); } } From 35205ce0004c1a580006f1628e523cb32bab16b9 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 8 May 2020 13:49:45 -0700 Subject: [PATCH 06/22] Remove Context.current() --- QUICKSTART.md | 4 +- .../io/opentelemetry/context/Context.java | 10 --- .../propagation/ContextPropagators.java | 4 +- .../propagation/DefaultPropagatorsTest.java | 6 +- .../propagation/HttpTraceContextTest.java | 59 ++++++++------ .../PropagatorContextExtractBenchmark.java | 9 ++- .../trace/propagation/B3PropagatorTest.java | 79 +++++++++++-------- .../propagation/JaegerPropagatorTest.java | 45 +++++++---- .../example/HelloWorldClient.java | 2 +- .../example/HelloWorldClientStream.java | 2 +- .../example/HelloWorldServer.java | 4 +- .../example/http/HttpClient.java | 2 +- .../example/http/HttpServer.java | 2 +- .../opentracingshim/Propagation.java | 2 +- .../trace/testbed/clientserver/Client.java | 3 +- .../trace/testbed/clientserver/Server.java | 2 +- 16 files changed, 129 insertions(+), 106 deletions(-) diff --git a/QUICKSTART.md b/QUICKSTART.md index d122dfaf8f6..5748d2e9561 100644 --- a/QUICKSTART.md +++ b/QUICKSTART.md @@ -193,7 +193,7 @@ try (Scope scope = tracer.withSpan(outGoing)) { outGoing.setAttribute("http.url", url.toString()); HttpURLConnection transportLayer = (HttpURLConnection) url.openConnection(); // Inject the request with the *current* Context, which contains our current Span. - OpenTelemetry.getPropagators().getHttpTextFormat().inject(Context.current(), transportLayer, setter); + OpenTelemetry.getPropagators().getHttpTextFormat().inject(CurrentContext.get(), transportLayer, setter); // Make outgoing call } finally { outGoing.end(); @@ -220,7 +220,7 @@ HttpTextFormat.Getter getter = public void handle(HttpExchange httpExchange) { // Extract the SpanContext and other elements from the request. Context extractedContext = OpenTelemetry.getPropagators().getHttpTextFormat() - .extract(Context.current(), httpExchange, getter); + .extract(CurrentContext.get(), httpExchange, getter); Span serverSpan = null; try (Scope scope = ContextUtils.withScopedContext(extractedContext)) { // Automatically use the extracted SpanContext as parent. diff --git a/api/src/main/java/io/opentelemetry/context/Context.java b/api/src/main/java/io/opentelemetry/context/Context.java index 23da8db4f38..ec4b03e4dab 100644 --- a/api/src/main/java/io/opentelemetry/context/Context.java +++ b/api/src/main/java/io/opentelemetry/context/Context.java @@ -17,7 +17,6 @@ package io.opentelemetry.context; import io.opentelemetry.correlationcontext.CorrelationContext; -import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.trace.Span; // TODO (trask) once the current() method is removed (see below) @@ -35,15 +34,6 @@ public static Context empty() { return DefaultContext.EMPTY; } - // TODO (trask) remove this method and use CurrentContext.get() directly instead - // so that this is a pure Context object, and has nothing to do with thread binding. - // this changes was not done yet in order to reduce code churn - // and make the PR easier to review as there are a lot of places - // that call Context.current(). - public static Context current() { - return CurrentContext.get(); - } - /** * Creates a new {@link Context} with the given {@link Span} set. * diff --git a/api/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java b/api/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java index b19c8bdf300..1d42d69cb6c 100644 --- a/api/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java +++ b/api/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java @@ -40,7 +40,7 @@ * // Inject the span's SpanContext and other available concerns (such as correlations) * // contained in the specified Context. * Map map = new HashMap<>(); - * httpTextFormat.inject(Context.current(), map, new Setter() { + * httpTextFormat.inject(CurrentContext.get(), map, new Setter() { * public void put(Map map, String key, String value) { * map.put(key, value); * } @@ -60,7 +60,7 @@ * * // Extract and store the propagated span's SpanContext and other available concerns * // in the specified Context. - * Context context = textFormat.extract(Context.current(), request, new Getter() { + * Context context = textFormat.extract(CurrentContext.get(), request, new Getter() { * public String get(Object request, String key) { * // Return the value associated to the key, if available. * } diff --git a/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java b/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java index 657c19614b9..e3cfb325e4e 100644 --- a/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java +++ b/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java @@ -55,7 +55,7 @@ public void testInject() { .addHttpTextFormat(propagator2) .build(); - Context context = Context.current(); + Context context = CurrentContext.get(); context = context.withValue(propagator1.getKey(), "value1"); context = context.withValue(propagator2.getKey(), "value2"); @@ -82,7 +82,7 @@ public void testExtract() { map.put(propagator2.getKeyName(), "value2"); Context context = - propagators.getHttpTextFormat().extract(Context.current(), map, MapGetter.INSTANCE); + propagators.getHttpTextFormat().extract(CurrentContext.get(), map, MapGetter.INSTANCE); assertThat(propagator1.getKey().get(context)).isEqualTo("value1"); assertThat(propagator2.getKey().get(context)).isEqualTo("value2"); assertThat(propagator3.getKey().get(context)).isNull(); // Handle missing value. @@ -92,7 +92,7 @@ public void testExtract() { public void noopPropagator() { ContextPropagators propagators = DefaultContextPropagators.builder().build(); - Context context = Context.current(); + Context context = CurrentContext.get(); Map map = new HashMap<>(); propagators.getHttpTextFormat().inject(context, map, MapSetter.INSTANCE); assertThat(map).isEmpty(); diff --git a/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java b/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java index b77f6462ca9..fc0cff6f145 100644 --- a/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java +++ b/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java @@ -23,6 +23,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat.Getter; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; @@ -90,7 +91,7 @@ private static Context withSpanContext(SpanContext spanContext, Context context) @Test public void inject_Nothing() { Map carrier = new LinkedHashMap<>(); - httpTraceContext.inject(Context.current(), carrier, setter); + httpTraceContext.inject(CurrentContext.get(), carrier, setter); assertThat(carrier).hasSize(0); } @@ -100,7 +101,7 @@ public void inject_NullCarrierUsage() { Context context = withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), - Context.current()); + CurrentContext.get()); httpTraceContext.inject( context, null, @@ -119,7 +120,7 @@ public void inject_SampledContext() { Context context = withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), - Context.current()); + CurrentContext.get()); httpTraceContext.inject(context, carrier, setter); assertThat(carrier).containsExactly(TRACE_PARENT, TRACEPARENT_HEADER_SAMPLED); } @@ -130,7 +131,7 @@ public void inject_NotSampledContext() { Context context = withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), - Context.current()); + CurrentContext.get()); httpTraceContext.inject(context, carrier, setter); assertThat(carrier).containsExactly(TRACE_PARENT, TRACEPARENT_HEADER_NOT_SAMPLED); } @@ -141,7 +142,7 @@ public void inject_SampledContext_WithTraceState() { Context context = withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_NOT_DEFAULT), - Context.current()); + CurrentContext.get()); httpTraceContext.inject(context, carrier, setter); assertThat(carrier) .containsExactly( @@ -154,7 +155,7 @@ public void inject_NotSampledContext_WithTraceState() { Context context = withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_NOT_DEFAULT), - Context.current()); + CurrentContext.get()); httpTraceContext.inject(context, carrier, setter); assertThat(carrier) .containsExactly( @@ -168,7 +169,7 @@ public void inject_NotSampledContext_WithTraceState() { public void extract_SampledContext() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_SAMPLED); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) + assertThat(getSpanContext(httpTraceContext.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -178,7 +179,7 @@ public void extract_SampledContext() { public void extract_NotSampledContext() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_NOT_SAMPLED); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) + assertThat(getSpanContext(httpTraceContext.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -189,7 +190,7 @@ public void extract_SampledContext_WithTraceState() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_SAMPLED); carrier.put(TRACE_STATE, TRACESTATE_NOT_DEFAULT_ENCODING); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) + assertThat(getSpanContext(httpTraceContext.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_NOT_DEFAULT)); @@ -200,7 +201,7 @@ public void extract_NotSampledContext_WithTraceState() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_NOT_SAMPLED); carrier.put(TRACE_STATE, TRACESTATE_NOT_DEFAULT_ENCODING); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) + assertThat(getSpanContext(httpTraceContext.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_NOT_DEFAULT)); @@ -210,7 +211,7 @@ public void extract_NotSampledContext_WithTraceState() { public void extract_NotSampledContext_NextVersion() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, "01-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-00-02"); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) + assertThat(getSpanContext(httpTraceContext.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -221,7 +222,7 @@ public void extract_NotSampledContext_EmptyTraceState() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_NOT_SAMPLED); carrier.put(TRACE_STATE, ""); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) + assertThat(getSpanContext(httpTraceContext.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -232,7 +233,7 @@ public void extract_NotSampledContext_TraceStateWithSpaces() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_NOT_SAMPLED); carrier.put(TRACE_STATE, TRACESTATE_NOT_DEFAULT_ENCODING_WITH_SPACES); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) + assertThat(getSpanContext(httpTraceContext.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_NOT_DEFAULT)); @@ -242,7 +243,8 @@ public void extract_NotSampledContext_TraceStateWithSpaces() { public void extract_EmptyHeader() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(TRACE_PARENT, ""); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) + assertThat( + getSpanContext(httpTraceContext.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -251,7 +253,8 @@ public void extract_InvalidTraceId() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put( TRACE_PARENT, "00-" + "abcdefghijklmnopabcdefghijklmnop" + "-" + SPAN_ID_BASE16 + "-01"); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) + assertThat( + getSpanContext(httpTraceContext.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -259,7 +262,8 @@ public void extract_InvalidTraceId() { public void extract_InvalidTraceId_Size() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "00-" + SPAN_ID_BASE16 + "-01"); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) + assertThat( + getSpanContext(httpTraceContext.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -267,7 +271,8 @@ public void extract_InvalidTraceId_Size() { public void extract_InvalidSpanId() { Map invalidHeaders = new HashMap(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + "abcdefghijklmnop" + "-01"); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) + assertThat( + getSpanContext(httpTraceContext.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -275,7 +280,8 @@ public void extract_InvalidSpanId() { public void extract_InvalidSpanId_Size() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "00-01"); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) + assertThat( + getSpanContext(httpTraceContext.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -283,7 +289,8 @@ public void extract_InvalidSpanId_Size() { public void extract_InvalidTraceFlags() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-gh"); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) + assertThat( + getSpanContext(httpTraceContext.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -291,7 +298,8 @@ public void extract_InvalidTraceFlags() { public void extract_InvalidTraceFlags_Size() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-0100"); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) + assertThat( + getSpanContext(httpTraceContext.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -300,7 +308,8 @@ public void extract_InvalidTracestate_EntriesDelimiter() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-01"); invalidHeaders.put(TRACE_STATE, "foo=bar;test=test"); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) + assertThat( + getSpanContext(httpTraceContext.extract(CurrentContext.get(), invalidHeaders, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -311,7 +320,8 @@ public void extract_InvalidTracestate_KeyValueDelimiter() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-01"); invalidHeaders.put(TRACE_STATE, "foo=bar,test-test"); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) + assertThat( + getSpanContext(httpTraceContext.extract(CurrentContext.get(), invalidHeaders, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -322,7 +332,8 @@ public void extract_InvalidTracestate_OneString() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-01"); invalidHeaders.put(TRACE_STATE, "test-test"); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) + assertThat( + getSpanContext(httpTraceContext.extract(CurrentContext.get(), invalidHeaders, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -342,7 +353,7 @@ public void headerNames() { @Test public void extract_emptyCarrier() { Map emptyHeaders = new HashMap<>(); - assertThat(getSpanContext(httpTraceContext.extract(Context.current(), emptyHeaders, getter))) + assertThat(getSpanContext(httpTraceContext.extract(CurrentContext.get(), emptyHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } } diff --git a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java index f0e816dd181..5aaf84e6d9a 100644 --- a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java +++ b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java @@ -17,6 +17,7 @@ package io.opentelemetry.contrib.trace.propagation; import io.opentelemetry.context.Context; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.trace.Span; import java.util.Arrays; import java.util.Collections; @@ -114,7 +115,7 @@ public String get(Map carrier, String key) { @Override protected Context doExtract() { - return jaegerPropagator.extract(Context.current(), getCarrier(), getter); + return jaegerPropagator.extract(CurrentContext.get(), getCarrier(), getter); } @Override @@ -157,7 +158,7 @@ public String get(Map carrier, String key) { @Override protected Context doExtract() { - return jaegerPropagator.extract(Context.current(), getCarrier(), getter); + return jaegerPropagator.extract(CurrentContext.get(), getCarrier(), getter); } @Override @@ -200,7 +201,7 @@ public String get(Map carrier, String key) { @Override protected Context doExtract() { - return b3Propagator.extract(Context.current(), getCarrier(), getter); + return b3Propagator.extract(CurrentContext.get(), getCarrier(), getter); } @Override @@ -246,7 +247,7 @@ public String get(Map carrier, String key) { @Override protected Context doExtract() { - return b3Propagator.extract(Context.current(), getCarrier(), getter); + return b3Propagator.extract(CurrentContext.get(), getCarrier(), getter); } @Override diff --git a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java index 68792a42a87..c65559cb83a 100644 --- a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java +++ b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java @@ -21,6 +21,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat.Getter; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.internal.StringUtils; import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.SpanContext; @@ -86,7 +87,7 @@ public void inject_SampledContext() { b3Propagator.inject( withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), - Context.current()), + CurrentContext.get()), carrier, setter); assertThat(carrier).containsEntry(B3Propagator.TRACE_ID_HEADER, TRACE_ID_BASE16); @@ -100,7 +101,7 @@ public void inject_SampledContext_nullCarrierUsage() { b3Propagator.inject( withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), - Context.current()), + CurrentContext.get()), null, new Setter>() { @Override @@ -119,7 +120,7 @@ public void inject_NotSampledContext() { b3Propagator.inject( withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), - Context.current()), + CurrentContext.get()), carrier, setter); assertThat(carrier).containsEntry(B3Propagator.TRACE_ID_HEADER, TRACE_ID_BASE16); @@ -134,7 +135,7 @@ public void extract_SampledContext_Int() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); carrier.put(B3Propagator.SAMPLED_HEADER, B3Propagator.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) + assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -147,7 +148,7 @@ public void extract_SampledContext_Bool() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); carrier.put(B3Propagator.SAMPLED_HEADER, "true"); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) + assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -160,7 +161,7 @@ public void extract_NotSampledContext() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); carrier.put(B3Propagator.SAMPLED_HEADER, B3Propagator.FALSE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) + assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -173,7 +174,7 @@ public void extract_SampledContext_Int_Short_TraceId() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); carrier.put(B3Propagator.SAMPLED_HEADER, B3Propagator.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) + assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -186,7 +187,7 @@ public void extract_SampledContext_Bool_Short_TraceId() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); carrier.put(B3Propagator.SAMPLED_HEADER, "true"); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) + assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -199,7 +200,7 @@ public void extract_NotSampledContext_Short_TraceId() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); carrier.put(B3Propagator.SAMPLED_HEADER, B3Propagator.FALSE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) + assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -211,7 +212,7 @@ public void extract_InvalidTraceId() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, "abcdefghijklmnopabcdefghijklmnop"); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, B3Propagator.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) + assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -221,7 +222,7 @@ public void extract_InvalidTraceId_Size() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID_BASE16 + "00"); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, B3Propagator.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) + assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -231,7 +232,7 @@ public void extract_InvalidSpanId() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID_BASE16); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, "abcdefghijklmnop"); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, B3Propagator.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) + assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -241,7 +242,7 @@ public void extract_InvalidSpanId_Size() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID_BASE16); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, "abcdefghijklmnop" + "00"); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, B3Propagator.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) + assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -251,7 +252,7 @@ public void inject_SampledContext_SingleHeader() { b3PropagatorSingleHeader.inject( withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), - Context.current()), + CurrentContext.get()), carrier, setter); @@ -266,7 +267,7 @@ public void inject_NotSampledContext_SingleHeader() { b3PropagatorSingleHeader.inject( withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), - Context.current()), + CurrentContext.get()), carrier, setter); @@ -282,7 +283,8 @@ public void extract_SampledContext_Int_SingleHeader() { B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + B3Propagator.TRUE_INT); - assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) + assertThat( + getSpanContext(b3PropagatorSingleHeader.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -295,7 +297,8 @@ public void extract_SampledContext_DebugFlag_SingleHeader() { B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + B3Propagator.TRUE_INT + "-" + "0"); - assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) + assertThat( + getSpanContext(b3PropagatorSingleHeader.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -307,7 +310,8 @@ public void extract_SampledContext_Bool_SingleHeader() { carrier.put( B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + "true"); - assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) + assertThat( + getSpanContext(b3PropagatorSingleHeader.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -320,7 +324,8 @@ public void extract_SampledContext_Bool_DebugFlag_SingleHeader() { B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + "true" + "-" + "0"); - assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) + assertThat( + getSpanContext(b3PropagatorSingleHeader.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -333,7 +338,8 @@ public void extract_NotSampledContext_SingleHeader() { B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + B3Propagator.FALSE_INT); - assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) + assertThat( + getSpanContext(b3PropagatorSingleHeader.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -346,7 +352,8 @@ public void extract_SampledContext_Int_SingleHeader_Short_TraceId() { B3Propagator.COMBINED_HEADER, SHORT_TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + B3Propagator.TRUE_INT); - assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) + assertThat( + getSpanContext(b3PropagatorSingleHeader.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -359,7 +366,8 @@ public void extract_SampledContext_DebugFlag_SingleHeader_Short_TraceId() { B3Propagator.COMBINED_HEADER, SHORT_TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + B3Propagator.TRUE_INT + "-" + "0"); - assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) + assertThat( + getSpanContext(b3PropagatorSingleHeader.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -371,7 +379,8 @@ public void extract_SampledContext_Bool_SingleHeader_Short_TraceId() { carrier.put( B3Propagator.COMBINED_HEADER, SHORT_TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + "true"); - assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) + assertThat( + getSpanContext(b3PropagatorSingleHeader.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -384,7 +393,8 @@ public void extract_SampledContext_Bool_DebugFlag_SingleHeader_Short_TraceId() { B3Propagator.COMBINED_HEADER, SHORT_TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + "true" + "-" + "0"); - assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) + assertThat( + getSpanContext(b3PropagatorSingleHeader.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -397,7 +407,8 @@ public void extract_NotSampledContext_SingleHeader_Short_TraceId() { B3Propagator.COMBINED_HEADER, SHORT_TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + B3Propagator.FALSE_INT); - assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) + assertThat( + getSpanContext(b3PropagatorSingleHeader.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -410,7 +421,7 @@ public void extract_Null_SingleHeader() { assertThat( getSpanContext( - b3PropagatorSingleHeader.extract(Context.current(), invalidHeaders, getter))) + b3PropagatorSingleHeader.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -421,7 +432,7 @@ public void extract_Empty_SingleHeader() { assertThat( getSpanContext( - b3PropagatorSingleHeader.extract(Context.current(), invalidHeaders, getter))) + b3PropagatorSingleHeader.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -434,7 +445,7 @@ public void extract_InvalidTraceId_SingleHeader() { assertThat( getSpanContext( - b3PropagatorSingleHeader.extract(Context.current(), invalidHeaders, getter))) + b3PropagatorSingleHeader.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -452,7 +463,7 @@ public void extract_InvalidTraceId_Size_SingleHeader() { assertThat( getSpanContext( - b3PropagatorSingleHeader.extract(Context.current(), invalidHeaders, getter))) + b3PropagatorSingleHeader.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -465,7 +476,7 @@ public void extract_InvalidSpanId_SingleHeader() { assertThat( getSpanContext( - b3PropagatorSingleHeader.extract(Context.current(), invalidHeaders, getter))) + b3PropagatorSingleHeader.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -476,7 +487,7 @@ public void extract_InvalidSpanId_Size_SingleHeader() { B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16 + "-" + "abcdefghijklmnop" + "00" + "-" + B3Propagator.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) + assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -485,7 +496,7 @@ public void extract_TooFewParts_SingleHeader() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) + assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -496,7 +507,7 @@ public void extract_TooManyParts_SingleHeader() { B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + B3Propagator.TRUE_INT + "-extra"); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) + assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -517,7 +528,7 @@ public void headerNames() { @Test public void extract_emptyCarrier() { Map emptyHeaders = new HashMap<>(); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), emptyHeaders, getter))) + assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), emptyHeaders, getter))) .isEqualTo(SpanContext.getInvalid()); } } diff --git a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java index d19fa0fa00d..835382a654c 100644 --- a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java +++ b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java @@ -26,6 +26,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; +import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; @@ -96,7 +97,7 @@ public void inject_SampledContext() { jaegerPropagator.inject( withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), - Context.current()), + CurrentContext.get()), carrier, setter); @@ -114,7 +115,7 @@ public void inject_SampledContext_nullCarrierUsage() { jaegerPropagator.inject( withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), - Context.current()), + CurrentContext.get()), null, new Setter>() { @Override @@ -136,7 +137,7 @@ public void inject_NotSampledContext() { jaegerPropagator.inject( withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), - Context.current()), + CurrentContext.get()), carrier, setter); assertThat(carrier) @@ -151,7 +152,8 @@ public void extract_EmptyHeaderValue() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(PROPAGATION_HEADER, ""); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) + assertThat( + getSpanContext(jaegerPropagator.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -160,7 +162,8 @@ public void extract_NotEnoughParts() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(PROPAGATION_HEADER, "aa:bb:cc"); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) + assertThat( + getSpanContext(jaegerPropagator.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -169,7 +172,8 @@ public void extract_TooManyParts() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(PROPAGATION_HEADER, "aa:bb:cc:dd:ee"); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) + assertThat( + getSpanContext(jaegerPropagator.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -181,7 +185,8 @@ public void extract_InvalidTraceId() { generateTraceIdHeaderValue( "abcdefghijklmnopabcdefghijklmnop", SPAN_ID_BASE16, DEPRECATED_PARENT_SPAN, "0")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) + assertThat( + getSpanContext(jaegerPropagator.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -193,7 +198,8 @@ public void extract_InvalidTraceId_Size() { generateTraceIdHeaderValue( TRACE_ID_BASE16 + "00", SPAN_ID_BASE16, DEPRECATED_PARENT_SPAN, "0")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) + assertThat( + getSpanContext(jaegerPropagator.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -205,7 +211,8 @@ public void extract_InvalidSpanId() { generateTraceIdHeaderValue( TRACE_ID_BASE16, "abcdefghijklmnop", DEPRECATED_PARENT_SPAN, "0")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) + assertThat( + getSpanContext(jaegerPropagator.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -217,7 +224,8 @@ public void extract_InvalidSpanId_Size() { generateTraceIdHeaderValue( TRACE_ID_BASE16, SPAN_ID_BASE16 + "00", DEPRECATED_PARENT_SPAN, "0")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) + assertThat( + getSpanContext(jaegerPropagator.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -228,7 +236,8 @@ public void extract_InvalidFlags() { PROPAGATION_HEADER, generateTraceIdHeaderValue(TRACE_ID_BASE16, SPAN_ID_BASE16, DEPRECATED_PARENT_SPAN, "")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) + assertThat( + getSpanContext(jaegerPropagator.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -240,7 +249,8 @@ public void extract_InvalidFlags_Size() { generateTraceIdHeaderValue( TRACE_ID_BASE16, SPAN_ID_BASE16, DEPRECATED_PARENT_SPAN, "10220")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) + assertThat( + getSpanContext(jaegerPropagator.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -252,7 +262,8 @@ public void extract_InvalidFlags_NonNumeric() { generateTraceIdHeaderValue( TRACE_ID_BASE16, SPAN_ID_BASE16, DEPRECATED_PARENT_SPAN, "abcdefr")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) + assertThat( + getSpanContext(jaegerPropagator.extract(CurrentContext.get(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -264,7 +275,7 @@ public void extract_SampledContext() { TRACE_ID_HI, TRACE_ID_LOW, SPAN_ID_LONG, DEPRECATED_PARENT_SPAN_LONG, (byte) 5); carrier.put(PROPAGATION_HEADER, TextMapCodec.contextAsString(context)); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), carrier, getter))) + assertThat(getSpanContext(jaegerPropagator.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -278,7 +289,7 @@ public void extract_NotSampledContext() { TRACE_ID_HI, TRACE_ID_LOW, SPAN_ID_LONG, DEPRECATED_PARENT_SPAN_LONG, (byte) 0); carrier.put(PROPAGATION_HEADER, TextMapCodec.contextAsString(context)); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), carrier, getter))) + assertThat(getSpanContext(jaegerPropagator.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -296,7 +307,7 @@ public void extract_SampledContext_Short_TraceId() { (byte) 1); carrier.put(PROPAGATION_HEADER, TextMapCodec.contextAsString(context)); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), carrier, getter))) + assertThat(getSpanContext(jaegerPropagator.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -311,7 +322,7 @@ public void extract_UrlEncodedContext() throws UnsupportedEncodingException { carrier.put( PROPAGATION_HEADER, URLEncoder.encode(TextMapCodec.contextAsString(context), "UTF-8")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), carrier, getter))) + assertThat(getSpanContext(jaegerPropagator.extract(CurrentContext.get(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); diff --git a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClient.java b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClient.java index 0d3c47f4d55..4df0a941c51 100644 --- a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClient.java +++ b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClient.java @@ -133,7 +133,7 @@ public ClientCall interceptCall( @Override public void start(Listener responseListener, Metadata headers) { // Inject the request with the current context - textFormat.inject(Context.current(), headers, setter); + textFormat.inject(CurrentContext.get(), headers, setter); // Perform the gRPC request super.start(responseListener, headers); } diff --git a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClientStream.java b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClientStream.java index e1ef5dc8905..94304b0988d 100644 --- a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClientStream.java +++ b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClientStream.java @@ -175,7 +175,7 @@ public ClientCall interceptCall( @Override public void start(Listener responseListener, Metadata headers) { // Inject the request with the current context - textFormat.inject(Context.current(), headers, setter); + textFormat.inject(CurrentContext.get(), headers, setter); // Perform the gRPC request super.start(responseListener, headers); } diff --git a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldServer.java b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldServer.java index 538eb940f58..b0293c68c5f 100644 --- a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldServer.java +++ b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldServer.java @@ -161,7 +161,7 @@ private class OpenTelemetryServerInterceptor implements io.grpc.ServerIntercepto public ServerCall.Listener interceptCall( ServerCall call, Metadata headers, ServerCallHandler next) { // Extract the Span Context from the metadata of the gRPC request - Context extractedContext = textFormat.extract(Context.current(), headers, getter); + Context extractedContext = textFormat.extract(CurrentContext.get(), headers, getter); InetSocketAddress clientInfo = (InetSocketAddress) call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR); // Build a span based on the received context @@ -178,7 +178,7 @@ public ServerCall.Listener interceptCall( // Process the gRPC call normally try { span.setStatus(Status.OK); - return Contexts.interceptCall(Context.current(), call, headers, next); + return Contexts.interceptCall(CurrentContext.get(), call, headers, next); } finally { span.end(); } diff --git a/examples/http/src/main/java/io/opentelemetry/example/http/HttpClient.java b/examples/http/src/main/java/io/opentelemetry/example/http/HttpClient.java index c888fdc67ee..4fc169cf397 100644 --- a/examples/http/src/main/java/io/opentelemetry/example/http/HttpClient.java +++ b/examples/http/src/main/java/io/opentelemetry/example/http/HttpClient.java @@ -86,7 +86,7 @@ private HttpClient() throws Exception { span.setAttribute("http.url", url.toString()); // Inject the request with the current Context/Span. - OpenTelemetry.getPropagators().getHttpTextFormat().inject(Context.current(), con, setter); + OpenTelemetry.getPropagators().getHttpTextFormat().inject(CurrentContext.get(), con, setter); try { // Process the request diff --git a/examples/http/src/main/java/io/opentelemetry/example/http/HttpServer.java b/examples/http/src/main/java/io/opentelemetry/example/http/HttpServer.java index 2cd83374d2c..edffa2730a7 100644 --- a/examples/http/src/main/java/io/opentelemetry/example/http/HttpServer.java +++ b/examples/http/src/main/java/io/opentelemetry/example/http/HttpServer.java @@ -48,7 +48,7 @@ public void handle(HttpExchange he) throws IOException { // Extract the context from the HTTP request Context ctx = - OpenTelemetry.getPropagators().getHttpTextFormat().extract(Context.current(), he, getter); + OpenTelemetry.getPropagators().getHttpTextFormat().extract(CurrentContext.get(), he, getter); try (Scope scope = ContextUtils.withScopedContext(ctx)) { // Build a span automatically using the received context span = spanBuilder.startSpan(); diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java index 4b3e531a954..060ca4a0cf7 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java @@ -50,7 +50,7 @@ public SpanContextShim extractTextFormat(TextMapExtract carrier) { Context context = propagators() .getHttpTextFormat() - .extract(Context.current(), carrierMap, TextMapGetter.INSTANCE); + .extract(CurrentContext.get(), carrierMap, TextMapGetter.INSTANCE); io.opentelemetry.trace.Span span = context.getSpan(); if (!span.getContext().isValid()) { diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java index 0a590e71f8a..23a76490cb6 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java @@ -17,7 +17,6 @@ package io.opentelemetry.sdk.contrib.trace.testbed.clientserver; import io.opentelemetry.OpenTelemetry; -import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.currentcontext.Scope; @@ -46,7 +45,7 @@ public void send() throws InterruptedException { OpenTelemetry.getPropagators() .getHttpTextFormat() .inject( - Context.current(), + CurrentContext.get(), message, new Setter() { @Override diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java index c851f80e310..a96acc97c26 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java @@ -43,7 +43,7 @@ private void process(Message message) { OpenTelemetry.getPropagators() .getHttpTextFormat() .extract( - Context.current(), + CurrentContext.get(), message, new Getter() { @Nullable From 33aae0be11ffb32def319da9936882e79a122151 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 8 May 2020 13:57:07 -0700 Subject: [PATCH 07/22] Change Context to an interface --- .../HttpTraceContextExtractBenchmark.java | 2 +- .../HttpTraceContextInjectBenchmark.java | 2 +- .../io/opentelemetry/context/Context.java | 22 +++++-------------- .../opentelemetry/context/DefaultContext.java | 2 +- .../propagation/ContextPropagators.java | 3 ++- .../currentcontext/DefaultContextStorage.java | 4 ++-- 6 files changed, 13 insertions(+), 22 deletions(-) diff --git a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextExtractBenchmark.java b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextExtractBenchmark.java index 72b4650077b..6af25aae68d 100644 --- a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextExtractBenchmark.java +++ b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextExtractBenchmark.java @@ -71,7 +71,7 @@ public String get(Map carrier, String key) { public Context measureExtract() { Context result = null; for (int i = 0; i < COUNT; i++) { - result = httpTraceContext.extract(Context.empty(), carriers.get(i), getter); + result = httpTraceContext.extract(Context.EMPTY, carriers.get(i), getter); } return result; } diff --git a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java index 1561f110619..268de0fb0c1 100644 --- a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java +++ b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java @@ -92,7 +92,7 @@ private static SpanContext createTestSpanContext(String traceId, String spanId) private static List createContexts(List spanContexts) { List contexts = new ArrayList<>(); for (SpanContext context : spanContexts) { - contexts.add(Context.empty().withSpan(DefaultSpan.create(context))); + contexts.add(Context.EMPTY.withSpan(DefaultSpan.create(context))); } return contexts; } diff --git a/api/src/main/java/io/opentelemetry/context/Context.java b/api/src/main/java/io/opentelemetry/context/Context.java index ec4b03e4dab..d3df17bc761 100644 --- a/api/src/main/java/io/opentelemetry/context/Context.java +++ b/api/src/main/java/io/opentelemetry/context/Context.java @@ -19,20 +19,10 @@ import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.trace.Span; -// TODO (trask) once the current() method is removed (see below) -// this could be turned into an interface -// which improves interop possibilities with other libraries' contexts -// but also limits the ability to evolve this class with additional methods, -// e.g. withLoggingContext??? (insert sad Java 7 face here) -// -// TODO (trask) does this also need to support arbitrary key-values? -// // TODO (trask) javadoc class and methods -public abstract class Context { +public interface Context { - public static Context empty() { - return DefaultContext.EMPTY; - } + Context EMPTY = DefaultContext.EMPTY; /** * Creates a new {@link Context} with the given {@link Span} set. @@ -41,7 +31,7 @@ public static Context empty() { * @return a new context with the given {@link Span} set. * @since 0.5.0 */ - public abstract Context withSpan(Span span); + Context withSpan(Span span); /** * Creates a new {@link Context} with the given {@link CorrelationContext} set. @@ -50,7 +40,7 @@ public static Context empty() { * @return a new context with the given {@link CorrelationContext} set. * @since 0.5.0 */ - public abstract Context withCorrelationContext(CorrelationContext correlationContext); + Context withCorrelationContext(CorrelationContext correlationContext); /** * Returns the {@link Span} from this context, falling back to an empty {@link Span}. @@ -58,7 +48,7 @@ public static Context empty() { * @return the {@link Span} from this context. * @since 0.5.0 */ - public abstract Span getSpan(); + Span getSpan(); /** * Returns the {@link CorrelationContext} from this context, falling back to an empty {@link @@ -67,5 +57,5 @@ public static Context empty() { * @return the {@link CorrelationContext} from this context. * @since 0.5.0 */ - public abstract CorrelationContext getCorrelationContext(); + CorrelationContext getCorrelationContext(); } diff --git a/api/src/main/java/io/opentelemetry/context/DefaultContext.java b/api/src/main/java/io/opentelemetry/context/DefaultContext.java index 6a0acc1bf3e..9d90f0ee516 100644 --- a/api/src/main/java/io/opentelemetry/context/DefaultContext.java +++ b/api/src/main/java/io/opentelemetry/context/DefaultContext.java @@ -21,7 +21,7 @@ import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; -final class DefaultContext extends Context { +final class DefaultContext implements Context { static final Context EMPTY = new DefaultContext(DefaultSpan.getInvalid(), EmptyCorrelationContext.getInstance()); diff --git a/api/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java b/api/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java index 1d42d69cb6c..3c4416714ad 100644 --- a/api/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java +++ b/api/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java @@ -60,7 +60,8 @@ * * // Extract and store the propagated span's SpanContext and other available concerns * // in the specified Context. - * Context context = textFormat.extract(CurrentContext.get(), request, new Getter() { + * Context context = textFormat.extract(CurrentContext.get(), request, + * new Getter() { * public String get(Object request, String key) { * // Return the value associated to the key, if available. * } diff --git a/api/src/main/java/io/opentelemetry/currentcontext/DefaultContextStorage.java b/api/src/main/java/io/opentelemetry/currentcontext/DefaultContextStorage.java index 7096ae7e960..031aa1e5b66 100644 --- a/api/src/main/java/io/opentelemetry/currentcontext/DefaultContextStorage.java +++ b/api/src/main/java/io/opentelemetry/currentcontext/DefaultContextStorage.java @@ -44,7 +44,7 @@ static void detach(Context toDetach, Context toRestore) { "Context was not attached when detaching", new Throwable().fillInStackTrace()); } - if (toRestore != Context.empty()) { + if (toRestore != Context.EMPTY) { localContext.set(toRestore); } else { // Avoid leaking our ClassLoader via ROOT if this Thread is reused across multiple @@ -62,7 +62,7 @@ static void detach(Context toDetach, Context toRestore) { static Context current() { Context current = localContext.get(); if (current == null) { - return Context.empty(); + return Context.EMPTY; } return current; } From 78e14e85254d4b3f189ae76d1b653992cc6dc3a6 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 8 May 2020 14:48:00 -0700 Subject: [PATCH 08/22] Change context to pure key-value store --- .../HttpTraceContextInjectBenchmark.java | 3 +- .../io/opentelemetry/context/Context.java | 54 ++++++------------ .../opentelemetry/context/DefaultContext.java | 57 ++++++++++--------- .../CorrelationContextKey.java | 36 ++++++++++++ .../currentcontext/CurrentContext.java | 10 ++-- .../java/io/opentelemetry/trace/SpanKey.java | 36 ++++++++++++ .../trace/propagation/HttpTraceContext.java | 5 +- .../propagation/HttpTraceContextTest.java | 5 +- .../PropagatorContextExtractBenchmark.java | 3 +- .../PropagatorContextInjectBenchmark.java | 3 +- .../propagation/B3PropagatorExtractor.java | 3 +- .../propagation/B3PropagatorInjector.java | 3 +- .../trace/propagation/JaegerPropagator.java | 5 +- .../trace/propagation/B3PropagatorTest.java | 5 +- .../propagation/JaegerPropagatorTest.java | 5 +- .../opentracingshim/Propagation.java | 12 ++-- .../trace/testbed/clientserver/Server.java | 3 +- 17 files changed, 161 insertions(+), 87 deletions(-) create mode 100644 api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextKey.java create mode 100644 api/src/main/java/io/opentelemetry/trace/SpanKey.java diff --git a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java index 268de0fb0c1..018f9d45888 100644 --- a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java +++ b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java @@ -21,6 +21,7 @@ import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; +import io.opentelemetry.trace.SpanKey; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; @@ -92,7 +93,7 @@ private static SpanContext createTestSpanContext(String traceId, String spanId) private static List createContexts(List spanContexts) { List contexts = new ArrayList<>(); for (SpanContext context : spanContexts) { - contexts.add(Context.EMPTY.withSpan(DefaultSpan.create(context))); + contexts.add(SpanKey.put(Context.EMPTY, DefaultSpan.create(context))); } return contexts; } diff --git a/api/src/main/java/io/opentelemetry/context/Context.java b/api/src/main/java/io/opentelemetry/context/Context.java index d3df17bc761..724a32efa9f 100644 --- a/api/src/main/java/io/opentelemetry/context/Context.java +++ b/api/src/main/java/io/opentelemetry/context/Context.java @@ -16,46 +16,28 @@ package io.opentelemetry.context; -import io.opentelemetry.correlationcontext.CorrelationContext; -import io.opentelemetry.trace.Span; +import javax.annotation.Nullable; // TODO (trask) javadoc class and methods public interface Context { Context EMPTY = DefaultContext.EMPTY; - /** - * Creates a new {@link Context} with the given {@link Span} set. - * - * @param span the {@link Span} to be set. - * @return a new context with the given {@link Span} set. - * @since 0.5.0 - */ - Context withSpan(Span span); - - /** - * Creates a new {@link Context} with the given {@link CorrelationContext} set. - * - * @param correlationContext the {@link CorrelationContext} to be set. - * @return a new context with the given {@link CorrelationContext} set. - * @since 0.5.0 - */ - Context withCorrelationContext(CorrelationContext correlationContext); - - /** - * Returns the {@link Span} from this context, falling back to an empty {@link Span}. - * - * @return the {@link Span} from this context. - * @since 0.5.0 - */ - Span getSpan(); - - /** - * Returns the {@link CorrelationContext} from this context, falling back to an empty {@link - * CorrelationContext}. - * - * @return the {@link CorrelationContext} from this context. - * @since 0.5.0 - */ - CorrelationContext getCorrelationContext(); + @Nullable + T get(Key key); + + Context put(Key key, T value); + + class Key { + private final String name; + + public Key(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } + } } diff --git a/api/src/main/java/io/opentelemetry/context/DefaultContext.java b/api/src/main/java/io/opentelemetry/context/DefaultContext.java index 9d90f0ee516..365f469a68b 100644 --- a/api/src/main/java/io/opentelemetry/context/DefaultContext.java +++ b/api/src/main/java/io/opentelemetry/context/DefaultContext.java @@ -16,45 +16,50 @@ package io.opentelemetry.context; -import io.opentelemetry.correlationcontext.CorrelationContext; -import io.opentelemetry.correlationcontext.EmptyCorrelationContext; -import io.opentelemetry.trace.DefaultSpan; -import io.opentelemetry.trace.Span; +import javax.annotation.Nullable; +// TODO (trask) replace naive implementation final class DefaultContext implements Context { - static final Context EMPTY = - new DefaultContext(DefaultSpan.getInvalid(), EmptyCorrelationContext.getInstance()); + static final Context EMPTY = new EmptyContext(); - private final Span span; - private final CorrelationContext correlationContext; + private final Context parent; + private final Key key; + private final Object value; - DefaultContext(Span span, CorrelationContext correlationContext) { - this.span = span; - this.correlationContext = correlationContext; + DefaultContext(Key key, Object value, Context parent) { + this.key = key; + this.value = value; + this.parent = parent; } @Override - public Context withSpan(Span span) { - // TODO (trask) can we checkNotNull(span)? - return new DefaultContext(span == null ? DefaultSpan.getInvalid() : span, correlationContext); + @Nullable + @SuppressWarnings("unchecked") + public T get(Key key) { + if (key.equals(this.key)) { + return (T) value; + } else { + return parent.get(key); + } } @Override - public Context withCorrelationContext(CorrelationContext correlationContext) { - // TODO (trask) can we checkNotNull(correlationContext)? - return new DefaultContext( - span, - correlationContext == null ? EmptyCorrelationContext.getInstance() : correlationContext); + public Context put(Key key, T value) { + return new DefaultContext(key, value, this); } - @Override - public Span getSpan() { - return span; - } + static class EmptyContext implements Context { - @Override - public CorrelationContext getCorrelationContext() { - return correlationContext; + @Override + @Nullable + public T get(Key key) { + return null; + } + + @Override + public Context put(Key key, T value) { + return new DefaultContext(key, value, this); + } } } diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextKey.java b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextKey.java new file mode 100644 index 00000000000..84132bba723 --- /dev/null +++ b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextKey.java @@ -0,0 +1,36 @@ +/* + * Copyright 2020, OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opentelemetry.correlationcontext; + +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Context.Key; + +public class CorrelationContextKey { + + private static final Key KEY = new Key<>("CorrelationContext"); + + public static CorrelationContext get(Context context) { + CorrelationContext correlationContext = context.get(KEY); + return correlationContext == null ? EmptyCorrelationContext.getInstance() : correlationContext; + } + + public static Context put(Context context, CorrelationContext span) { + return context.put(KEY, span); + } + + private CorrelationContextKey() {} +} diff --git a/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java b/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java index 69c0ba70e07..9017cfdc5b8 100644 --- a/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java +++ b/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java @@ -19,7 +19,9 @@ import com.google.errorprone.annotations.MustBeClosed; import io.opentelemetry.context.Context; import io.opentelemetry.correlationcontext.CorrelationContext; +import io.opentelemetry.correlationcontext.CorrelationContextKey; import io.opentelemetry.trace.Span; +import io.opentelemetry.trace.SpanKey; /** Static methods for interacting with the current (thread-bound) context. */ // TODO (trask) javadoc class and methods @@ -27,12 +29,12 @@ public class CurrentContext { @MustBeClosed public static Scope withSpan(Span span) { - return withContext(get().withSpan(span)); + return withContext(SpanKey.put(get(), span)); } @MustBeClosed public static Scope withCorrelationContext(CorrelationContext correlationContext) { - return withContext(get().withCorrelationContext(correlationContext)); + return withContext(CorrelationContextKey.put(get(), correlationContext)); } /** @@ -46,12 +48,12 @@ public static Scope withContext(Context context) { /** Convenience method for {@code CurrentContext.get().getSpan()}. */ public static Span getSpan() { - return get().getSpan(); + return SpanKey.get(get()); } /** Convenience method for {@code CurrentContext.get().getCorrelationContext()}. */ public static CorrelationContext getCorrelationContext() { - return get().getCorrelationContext(); + return CorrelationContextKey.get(get()); } /** Returns the context bound to the current thread. */ diff --git a/api/src/main/java/io/opentelemetry/trace/SpanKey.java b/api/src/main/java/io/opentelemetry/trace/SpanKey.java new file mode 100644 index 00000000000..c5ab2a4f0b1 --- /dev/null +++ b/api/src/main/java/io/opentelemetry/trace/SpanKey.java @@ -0,0 +1,36 @@ +/* + * Copyright 2020, OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opentelemetry.trace; + +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Context.Key; + +public class SpanKey { + + private static final Key KEY = new Key<>("Span"); + + public static Span get(Context context) { + Span span = context.get(KEY); + return span == null ? DefaultSpan.getInvalid() : span; + } + + public static Context put(Context context, Span span) { + return context.put(KEY, span); + } + + private SpanKey() {} +} diff --git a/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java b/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java index 999c4fad395..0c06716890d 100644 --- a/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java +++ b/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java @@ -25,6 +25,7 @@ import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; +import io.opentelemetry.trace.SpanKey; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; @@ -79,7 +80,7 @@ public void inject(Context context, C carrier, Setter setter) { checkNotNull(context, "context"); checkNotNull(setter, "setter"); - Span span = context.getSpan(); + Span span = SpanKey.get(context); if (!span.getContext().isValid()) { return; } @@ -124,7 +125,7 @@ private static void injectImpl(SpanContext spanContext, C carrier, Setter checkNotNull(getter, "getter"); SpanContext spanContext = extractImpl(carrier, getter); - return context.withSpan(DefaultSpan.create(spanContext)); + return SpanKey.put(context, DefaultSpan.create(spanContext)); } private static SpanContext extractImpl(C carrier, Getter getter) { diff --git a/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java b/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java index fc0cff6f145..f75070cb21c 100644 --- a/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java +++ b/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java @@ -27,6 +27,7 @@ import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; +import io.opentelemetry.trace.SpanKey; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; @@ -81,11 +82,11 @@ public String get(Map carrier, String key) { @Rule public ExpectedException thrown = ExpectedException.none(); private static SpanContext getSpanContext(Context context) { - return context.getSpan().getContext(); + return SpanKey.get(context).getContext(); } private static Context withSpanContext(SpanContext spanContext, Context context) { - return context.withSpan(DefaultSpan.create(spanContext)); + return SpanKey.put(context, DefaultSpan.create(spanContext)); } @Test diff --git a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java index 5aaf84e6d9a..c98e3936ef7 100644 --- a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java +++ b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java @@ -19,6 +19,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.trace.Span; +import io.opentelemetry.trace.SpanKey; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -65,7 +66,7 @@ public void setup() { @BenchmarkMode(Mode.AverageTime) @Fork(1) public Span measureExtract() { - return doExtract().getSpan(); + return SpanKey.get(doExtract()); } protected abstract Context doExtract(); diff --git a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java index 1bc690c0129..4ac44802941 100644 --- a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java +++ b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java @@ -21,6 +21,7 @@ import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; +import io.opentelemetry.trace.SpanKey; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; @@ -74,7 +75,7 @@ public abstract static class AbstractContextInjectBenchmark { @BenchmarkMode(Mode.AverageTime) @Fork(1) public Map measureInject() { - Context context = CurrentContext.get().withSpan(DefaultSpan.create(contextToTest)); + Context context = SpanKey.put(CurrentContext.get(), DefaultSpan.create(contextToTest)); doInject(context, carrier); return carrier; } diff --git a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorExtractor.java b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorExtractor.java index 6628a7e53cb..139fd0170fa 100644 --- a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorExtractor.java +++ b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorExtractor.java @@ -31,6 +31,7 @@ import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; +import io.opentelemetry.trace.SpanKey; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; @@ -64,7 +65,7 @@ Context extract(Context context, C carrier, HttpTextFormat.Getter getter) spanContext = getSpanContextFromMultipleHeaders(carrier, getter); } - return context.withSpan(DefaultSpan.create(spanContext)); + return SpanKey.put(context, DefaultSpan.create(spanContext)); } @SuppressWarnings("StringSplitter") diff --git a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java index 807fe0f127f..8ac600f1a37 100644 --- a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java +++ b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java @@ -21,6 +21,7 @@ import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; +import io.opentelemetry.trace.SpanKey; import io.opentelemetry.trace.TraceId; import java.util.Objects; import javax.annotation.concurrent.Immutable; @@ -46,7 +47,7 @@ void inject(Context context, C carrier, HttpTextFormat.Setter setter) { Objects.requireNonNull(context, "context"); Objects.requireNonNull(setter, "setter"); - Span span = context.getSpan(); + Span span = SpanKey.get(context); if (!span.getContext().isValid()) { return; } diff --git a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java index abca819d935..5948b1529d3 100644 --- a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java +++ b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java @@ -25,6 +25,7 @@ import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; +import io.opentelemetry.trace.SpanKey; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; @@ -87,7 +88,7 @@ public void inject(Context context, C carrier, Setter setter) { checkNotNull(context, "context"); checkNotNull(setter, "setter"); - Span span = context.getSpan(); + Span span = SpanKey.get(context); if (!span.getContext().isValid()) { return; } @@ -112,7 +113,7 @@ public Context extract(Context context, C carrier, Getter getter) { SpanContext spanContext = getSpanContextFromHeader(carrier, getter); - return context.withSpan(DefaultSpan.create(spanContext)); + return SpanKey.put(context, DefaultSpan.create(spanContext)); } @SuppressWarnings("StringSplitter") diff --git a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java index c65559cb83a..3175801348d 100644 --- a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java +++ b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java @@ -26,6 +26,7 @@ import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; +import io.opentelemetry.trace.SpanKey; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; @@ -74,11 +75,11 @@ public String get(Map carrier, String key) { @Rule public ExpectedException thrown = ExpectedException.none(); private static SpanContext getSpanContext(Context context) { - return context.getSpan().getContext(); + return SpanKey.get(context).getContext(); } private static Context withSpanContext(SpanContext spanContext, Context context) { - return context.withSpan(DefaultSpan.create(spanContext)); + return SpanKey.put(context, DefaultSpan.create(spanContext)); } @Test diff --git a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java index 835382a654c..23b51eb59da 100644 --- a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java +++ b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java @@ -30,6 +30,7 @@ import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; +import io.opentelemetry.trace.SpanKey; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; @@ -84,11 +85,11 @@ public String get(Map carrier, String key) { @Rule public ExpectedException thrown = ExpectedException.none(); private static SpanContext getSpanContext(Context context) { - return context.getSpan().getContext(); + return SpanKey.get(context).getContext(); } private static Context withSpanContext(SpanContext spanContext, Context context) { - return context.withSpan(DefaultSpan.create(spanContext)); + return SpanKey.put(context, DefaultSpan.create(spanContext)); } @Test diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java index 060ca4a0cf7..4cce5b66613 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java @@ -18,8 +18,10 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat; +import io.opentelemetry.correlationcontext.CorrelationContextKey; import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.trace.DefaultSpan; +import io.opentelemetry.trace.SpanKey; import io.opentracing.propagation.TextMapExtract; import io.opentracing.propagation.TextMapInject; import java.util.HashMap; @@ -33,9 +35,8 @@ final class Propagation extends BaseShimObject { public void injectTextFormat(SpanContextShim contextShim, TextMapInject carrier) { Context context = - CurrentContext.get() - .withSpan(DefaultSpan.create(contextShim.getSpanContext())) - .withCorrelationContext(contextShim.getCorrelationContext()); + SpanKey.put(CurrentContext.get(), DefaultSpan.create(contextShim.getSpanContext())); + context = CorrelationContextKey.put(context, contextShim.getCorrelationContext()); propagators().getHttpTextFormat().inject(context, carrier, TextMapSetter.INSTANCE); } @@ -52,12 +53,13 @@ public SpanContextShim extractTextFormat(TextMapExtract carrier) { .getHttpTextFormat() .extract(CurrentContext.get(), carrierMap, TextMapGetter.INSTANCE); - io.opentelemetry.trace.Span span = context.getSpan(); + io.opentelemetry.trace.Span span = SpanKey.get(context); if (!span.getContext().isValid()) { return null; } - return new SpanContextShim(telemetryInfo, span.getContext(), context.getCorrelationContext()); + return new SpanContextShim( + telemetryInfo, span.getContext(), CorrelationContextKey.get(context)); } static final class TextMapSetter implements HttpTextFormat.Setter { diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java index a96acc97c26..e482af96484 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java @@ -24,6 +24,7 @@ import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.SpanContext; +import io.opentelemetry.trace.SpanKey; import io.opentelemetry.trace.Tracer; import java.util.concurrent.ArrayBlockingQueue; import javax.annotation.Nullable; @@ -52,7 +53,7 @@ public String get(Message carrier, String key) { return carrier.get(key); } }); - SpanContext spanContext = context.getSpan().getContext(); + SpanContext spanContext = SpanKey.get(context).getContext(); Span span = tracer.spanBuilder("receive").setSpanKind(Kind.SERVER).setParent(spanContext).startSpan(); span.setAttribute("component", "example-server"); From 45bd531ba945e440db742998eac8dcf5c0aa4f34 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 8 May 2020 18:15:34 -0700 Subject: [PATCH 09/22] Move SpanKey to Span.KEY --- .../HttpTraceContextInjectBenchmark.java | 4 +-- .../io/opentelemetry/context/Context.java | 14 ++++++-- .../opentelemetry/context/DefaultContext.java | 4 +-- .../currentcontext/CurrentContext.java | 5 ++- .../java/io/opentelemetry/trace/Span.java | 4 +++ .../java/io/opentelemetry/trace/SpanKey.java | 36 ------------------- .../trace/propagation/HttpTraceContext.java | 5 ++- .../propagation/HttpTraceContextTest.java | 6 ++-- .../PropagatorContextExtractBenchmark.java | 3 +- .../PropagatorContextInjectBenchmark.java | 4 +-- .../propagation/B3PropagatorExtractor.java | 4 +-- .../propagation/B3PropagatorInjector.java | 3 +- .../trace/propagation/JaegerPropagator.java | 5 ++- .../trace/propagation/B3PropagatorTest.java | 6 ++-- .../propagation/JaegerPropagatorTest.java | 6 ++-- .../opentracingshim/Propagation.java | 6 ++-- .../trace/testbed/clientserver/Server.java | 3 +- 17 files changed, 44 insertions(+), 74 deletions(-) delete mode 100644 api/src/main/java/io/opentelemetry/trace/SpanKey.java diff --git a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java index 018f9d45888..ae72627bede 100644 --- a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java +++ b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java @@ -19,9 +19,9 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; import io.opentelemetry.trace.DefaultSpan; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; -import io.opentelemetry.trace.SpanKey; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; @@ -93,7 +93,7 @@ private static SpanContext createTestSpanContext(String traceId, String spanId) private static List createContexts(List spanContexts) { List contexts = new ArrayList<>(); for (SpanContext context : spanContexts) { - contexts.add(SpanKey.put(Context.EMPTY, DefaultSpan.create(context))); + contexts.add(Context.EMPTY.put(Span.KEY, DefaultSpan.create(context))); } return contexts; } diff --git a/api/src/main/java/io/opentelemetry/context/Context.java b/api/src/main/java/io/opentelemetry/context/Context.java index 724a32efa9f..1f1763c8ca0 100644 --- a/api/src/main/java/io/opentelemetry/context/Context.java +++ b/api/src/main/java/io/opentelemetry/context/Context.java @@ -16,23 +16,31 @@ package io.opentelemetry.context; -import javax.annotation.Nullable; - // TODO (trask) javadoc class and methods public interface Context { Context EMPTY = DefaultContext.EMPTY; - @Nullable + // returns default value from key T get(Key key); Context put(Key key, T value); class Key { private final String name; + private final T defaultValue; public Key(String name) { + this(name, null); + } + + public Key(String name, T defaultValue) { this.name = name; + this.defaultValue = defaultValue; + } + + public T getDefaultValue() { + return defaultValue; } @Override diff --git a/api/src/main/java/io/opentelemetry/context/DefaultContext.java b/api/src/main/java/io/opentelemetry/context/DefaultContext.java index 365f469a68b..fdd4ab7e48e 100644 --- a/api/src/main/java/io/opentelemetry/context/DefaultContext.java +++ b/api/src/main/java/io/opentelemetry/context/DefaultContext.java @@ -38,7 +38,7 @@ final class DefaultContext implements Context { @SuppressWarnings("unchecked") public T get(Key key) { if (key.equals(this.key)) { - return (T) value; + return value == null ? key.getDefaultValue() : (T) value; } else { return parent.get(key); } @@ -54,7 +54,7 @@ static class EmptyContext implements Context { @Override @Nullable public T get(Key key) { - return null; + return key.getDefaultValue(); } @Override diff --git a/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java b/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java index 9017cfdc5b8..9bacac54a22 100644 --- a/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java +++ b/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java @@ -21,7 +21,6 @@ import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.correlationcontext.CorrelationContextKey; import io.opentelemetry.trace.Span; -import io.opentelemetry.trace.SpanKey; /** Static methods for interacting with the current (thread-bound) context. */ // TODO (trask) javadoc class and methods @@ -29,7 +28,7 @@ public class CurrentContext { @MustBeClosed public static Scope withSpan(Span span) { - return withContext(SpanKey.put(get(), span)); + return withContext(get().put(Span.KEY, span)); } @MustBeClosed @@ -48,7 +47,7 @@ public static Scope withContext(Context context) { /** Convenience method for {@code CurrentContext.get().getSpan()}. */ public static Span getSpan() { - return SpanKey.get(get()); + return get().get(Span.KEY); } /** Convenience method for {@code CurrentContext.get().getCorrelationContext()}. */ diff --git a/api/src/main/java/io/opentelemetry/trace/Span.java b/api/src/main/java/io/opentelemetry/trace/Span.java index b18949eb197..479237eb950 100644 --- a/api/src/main/java/io/opentelemetry/trace/Span.java +++ b/api/src/main/java/io/opentelemetry/trace/Span.java @@ -17,6 +17,7 @@ package io.opentelemetry.trace; import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.context.Context; import java.util.Map; import javax.annotation.Nullable; import javax.annotation.concurrent.ThreadSafe; @@ -33,6 +34,9 @@ @ThreadSafe public interface Span { + // TODO (trask) javadoc + Context.Key KEY = new Context.Key("Span", DefaultSpan.getInvalid()); + /** * Type of span. Can be used to specify additional relationships between spans in addition to a * parent/child relationship. diff --git a/api/src/main/java/io/opentelemetry/trace/SpanKey.java b/api/src/main/java/io/opentelemetry/trace/SpanKey.java deleted file mode 100644 index c5ab2a4f0b1..00000000000 --- a/api/src/main/java/io/opentelemetry/trace/SpanKey.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2020, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.trace; - -import io.opentelemetry.context.Context; -import io.opentelemetry.context.Context.Key; - -public class SpanKey { - - private static final Key KEY = new Key<>("Span"); - - public static Span get(Context context) { - Span span = context.get(KEY); - return span == null ? DefaultSpan.getInvalid() : span; - } - - public static Context put(Context context, Span span) { - return context.put(KEY, span); - } - - private SpanKey() {} -} diff --git a/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java b/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java index 0c06716890d..2dfd2c14802 100644 --- a/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java +++ b/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java @@ -25,7 +25,6 @@ import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; -import io.opentelemetry.trace.SpanKey; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; @@ -80,7 +79,7 @@ public void inject(Context context, C carrier, Setter setter) { checkNotNull(context, "context"); checkNotNull(setter, "setter"); - Span span = SpanKey.get(context); + Span span = context.get(Span.KEY); if (!span.getContext().isValid()) { return; } @@ -125,7 +124,7 @@ private static void injectImpl(SpanContext spanContext, C carrier, Setter checkNotNull(getter, "getter"); SpanContext spanContext = extractImpl(carrier, getter); - return SpanKey.put(context, DefaultSpan.create(spanContext)); + return context.put(Span.KEY, DefaultSpan.create(spanContext)); } private static SpanContext extractImpl(C carrier, Getter getter) { diff --git a/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java b/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java index f75070cb21c..84e572004bb 100644 --- a/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java +++ b/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java @@ -25,9 +25,9 @@ import io.opentelemetry.context.propagation.HttpTextFormat.Setter; import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.trace.DefaultSpan; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; -import io.opentelemetry.trace.SpanKey; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; @@ -82,11 +82,11 @@ public String get(Map carrier, String key) { @Rule public ExpectedException thrown = ExpectedException.none(); private static SpanContext getSpanContext(Context context) { - return SpanKey.get(context).getContext(); + return context.get(Span.KEY).getContext(); } private static Context withSpanContext(SpanContext spanContext, Context context) { - return SpanKey.put(context, DefaultSpan.create(spanContext)); + return context.put(Span.KEY, DefaultSpan.create(spanContext)); } @Test diff --git a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java index c98e3936ef7..2f68986a775 100644 --- a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java +++ b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java @@ -19,7 +19,6 @@ import io.opentelemetry.context.Context; import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.trace.Span; -import io.opentelemetry.trace.SpanKey; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -66,7 +65,7 @@ public void setup() { @BenchmarkMode(Mode.AverageTime) @Fork(1) public Span measureExtract() { - return SpanKey.get(doExtract()); + return doExtract().get(Span.KEY); } protected abstract Context doExtract(); diff --git a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java index 4ac44802941..b9260f9114b 100644 --- a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java +++ b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java @@ -19,9 +19,9 @@ import io.opentelemetry.context.Context; import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.trace.DefaultSpan; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; -import io.opentelemetry.trace.SpanKey; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; @@ -75,7 +75,7 @@ public abstract static class AbstractContextInjectBenchmark { @BenchmarkMode(Mode.AverageTime) @Fork(1) public Map measureInject() { - Context context = SpanKey.put(CurrentContext.get(), DefaultSpan.create(contextToTest)); + Context context = CurrentContext.get().put(Span.KEY, DefaultSpan.create(contextToTest)); doInject(context, carrier); return carrier; } diff --git a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorExtractor.java b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorExtractor.java index 139fd0170fa..3b1b89d2c3c 100644 --- a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorExtractor.java +++ b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorExtractor.java @@ -29,9 +29,9 @@ import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.internal.StringUtils; import io.opentelemetry.trace.DefaultSpan; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; -import io.opentelemetry.trace.SpanKey; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; @@ -65,7 +65,7 @@ Context extract(Context context, C carrier, HttpTextFormat.Getter getter) spanContext = getSpanContextFromMultipleHeaders(carrier, getter); } - return SpanKey.put(context, DefaultSpan.create(spanContext)); + return context.put(Span.KEY, DefaultSpan.create(spanContext)); } @SuppressWarnings("StringSplitter") diff --git a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java index 8ac600f1a37..faca820e7ba 100644 --- a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java +++ b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java @@ -21,7 +21,6 @@ import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; -import io.opentelemetry.trace.SpanKey; import io.opentelemetry.trace.TraceId; import java.util.Objects; import javax.annotation.concurrent.Immutable; @@ -47,7 +46,7 @@ void inject(Context context, C carrier, HttpTextFormat.Setter setter) { Objects.requireNonNull(context, "context"); Objects.requireNonNull(setter, "setter"); - Span span = SpanKey.get(context); + Span span = context.get(Span.KEY); if (!span.getContext().isValid()) { return; } diff --git a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java index 5948b1529d3..688f2ce84b7 100644 --- a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java +++ b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java @@ -25,7 +25,6 @@ import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; -import io.opentelemetry.trace.SpanKey; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; @@ -88,7 +87,7 @@ public void inject(Context context, C carrier, Setter setter) { checkNotNull(context, "context"); checkNotNull(setter, "setter"); - Span span = SpanKey.get(context); + Span span = context.get(Span.KEY); if (!span.getContext().isValid()) { return; } @@ -113,7 +112,7 @@ public Context extract(Context context, C carrier, Getter getter) { SpanContext spanContext = getSpanContextFromHeader(carrier, getter); - return SpanKey.put(context, DefaultSpan.create(spanContext)); + return context.put(Span.KEY, DefaultSpan.create(spanContext)); } @SuppressWarnings("StringSplitter") diff --git a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java index 3175801348d..5955adb83b1 100644 --- a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java +++ b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java @@ -24,9 +24,9 @@ import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.internal.StringUtils; import io.opentelemetry.trace.DefaultSpan; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; -import io.opentelemetry.trace.SpanKey; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; @@ -75,11 +75,11 @@ public String get(Map carrier, String key) { @Rule public ExpectedException thrown = ExpectedException.none(); private static SpanContext getSpanContext(Context context) { - return SpanKey.get(context).getContext(); + return context.get(Span.KEY).getContext(); } private static Context withSpanContext(SpanContext spanContext, Context context) { - return SpanKey.put(context, DefaultSpan.create(spanContext)); + return context.put(Span.KEY, DefaultSpan.create(spanContext)); } @Test diff --git a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java index 23b51eb59da..8b612268b38 100644 --- a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java +++ b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java @@ -28,9 +28,9 @@ import io.opentelemetry.context.propagation.HttpTextFormat.Setter; import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.trace.DefaultSpan; +import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.SpanId; -import io.opentelemetry.trace.SpanKey; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; @@ -85,11 +85,11 @@ public String get(Map carrier, String key) { @Rule public ExpectedException thrown = ExpectedException.none(); private static SpanContext getSpanContext(Context context) { - return SpanKey.get(context).getContext(); + return context.get(Span.KEY).getContext(); } private static Context withSpanContext(SpanContext spanContext, Context context) { - return SpanKey.put(context, DefaultSpan.create(spanContext)); + return context.put(Span.KEY, DefaultSpan.create(spanContext)); } @Test diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java index 4cce5b66613..74506359cbf 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java @@ -21,7 +21,7 @@ import io.opentelemetry.correlationcontext.CorrelationContextKey; import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.trace.DefaultSpan; -import io.opentelemetry.trace.SpanKey; +import io.opentelemetry.trace.Span; import io.opentracing.propagation.TextMapExtract; import io.opentracing.propagation.TextMapInject; import java.util.HashMap; @@ -35,7 +35,7 @@ final class Propagation extends BaseShimObject { public void injectTextFormat(SpanContextShim contextShim, TextMapInject carrier) { Context context = - SpanKey.put(CurrentContext.get(), DefaultSpan.create(contextShim.getSpanContext())); + CurrentContext.get().put(Span.KEY, DefaultSpan.create(contextShim.getSpanContext())); context = CorrelationContextKey.put(context, contextShim.getCorrelationContext()); propagators().getHttpTextFormat().inject(context, carrier, TextMapSetter.INSTANCE); @@ -53,7 +53,7 @@ public SpanContextShim extractTextFormat(TextMapExtract carrier) { .getHttpTextFormat() .extract(CurrentContext.get(), carrierMap, TextMapGetter.INSTANCE); - io.opentelemetry.trace.Span span = SpanKey.get(context); + io.opentelemetry.trace.Span span = context.get(Span.KEY); if (!span.getContext().isValid()) { return null; } diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java index e482af96484..a3d47106e5a 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java @@ -24,7 +24,6 @@ import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.SpanContext; -import io.opentelemetry.trace.SpanKey; import io.opentelemetry.trace.Tracer; import java.util.concurrent.ArrayBlockingQueue; import javax.annotation.Nullable; @@ -53,7 +52,7 @@ public String get(Message carrier, String key) { return carrier.get(key); } }); - SpanContext spanContext = SpanKey.get(context).getContext(); + SpanContext spanContext = context.get(Span.KEY).getContext(); Span span = tracer.spanBuilder("receive").setSpanKind(Kind.SERVER).setParent(spanContext).startSpan(); span.setAttribute("component", "example-server"); From cccb351c6ebd776c2217ed4e96d08cfc8385fd38 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 8 May 2020 18:49:35 -0700 Subject: [PATCH 10/22] Move key to ContextCorrelation.KEY --- .../CorrelationContext.java | 6 ++++ .../CorrelationContextKey.java | 36 ------------------- .../currentcontext/CurrentContext.java | 5 ++- .../DefaultCorrelationContextManagerTest.java | 15 ++++---- .../opentracingshim/Propagation.java | 6 ++-- 5 files changed, 19 insertions(+), 49 deletions(-) delete mode 100644 api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextKey.java diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java index 8d91782a182..e97e273f9a2 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java @@ -16,6 +16,7 @@ package io.opentelemetry.correlationcontext; +import io.opentelemetry.context.Context; import io.opentelemetry.currentcontext.CurrentContext; import java.util.Collection; import javax.annotation.Nullable; @@ -32,6 +33,11 @@ */ @Immutable public interface CorrelationContext { + + // TODO (trask) javadoc + Context.Key KEY = + new Context.Key<>("CorrelationContext", EmptyCorrelationContext.getInstance()); + /** * Returns an immutable collection of the entries in this {@code CorrelationContext}. Order of * entries is not guaranteed. diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextKey.java b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextKey.java deleted file mode 100644 index 84132bba723..00000000000 --- a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContextKey.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2020, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.correlationcontext; - -import io.opentelemetry.context.Context; -import io.opentelemetry.context.Context.Key; - -public class CorrelationContextKey { - - private static final Key KEY = new Key<>("CorrelationContext"); - - public static CorrelationContext get(Context context) { - CorrelationContext correlationContext = context.get(KEY); - return correlationContext == null ? EmptyCorrelationContext.getInstance() : correlationContext; - } - - public static Context put(Context context, CorrelationContext span) { - return context.put(KEY, span); - } - - private CorrelationContextKey() {} -} diff --git a/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java b/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java index 9bacac54a22..9980a9219b9 100644 --- a/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java +++ b/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java @@ -19,7 +19,6 @@ import com.google.errorprone.annotations.MustBeClosed; import io.opentelemetry.context.Context; import io.opentelemetry.correlationcontext.CorrelationContext; -import io.opentelemetry.correlationcontext.CorrelationContextKey; import io.opentelemetry.trace.Span; /** Static methods for interacting with the current (thread-bound) context. */ @@ -33,7 +32,7 @@ public static Scope withSpan(Span span) { @MustBeClosed public static Scope withCorrelationContext(CorrelationContext correlationContext) { - return withContext(CorrelationContextKey.put(get(), correlationContext)); + return withContext(get().put(CorrelationContext.KEY, correlationContext)); } /** @@ -52,7 +51,7 @@ public static Span getSpan() { /** Convenience method for {@code CurrentContext.get().getCorrelationContext()}. */ public static CorrelationContext getCorrelationContext() { - return CorrelationContextKey.get(get()); + return get().get(CorrelationContext.KEY); } /** Returns the context bound to the current thread. */ diff --git a/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java index fa9bf21154a..7a4d37b5dc3 100644 --- a/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java +++ b/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java @@ -36,21 +36,22 @@ public final class DefaultCorrelationContextManagerTest { private static final CorrelationContextManager defaultCorrelationContextManager = DefaultCorrelationContextManager.getInstance(); - private static final EntryKey KEY = EntryKey.create("key"); - private static final EntryValue VALUE = EntryValue.create("value"); + private static final EntryKey TEST_KEY = EntryKey.create("key"); + private static final EntryValue TEST_VALUE = EntryValue.create("value"); private static final CorrelationContext DIST_CONTEXT = new CorrelationContext() { @Override public Collection getEntries() { - return Arrays.asList(Entry.create(KEY, VALUE, Entry.METADATA_UNLIMITED_PROPAGATION)); + return Arrays.asList( + Entry.create(TEST_KEY, TEST_VALUE, Entry.METADATA_UNLIMITED_PROPAGATION)); } @Nullable @Override public EntryValue getEntryValue(EntryKey entryKey) { - return VALUE; + return TEST_VALUE; } }; @@ -119,21 +120,21 @@ public void noopContextBuilder_SetParent_DisallowsNullParent() { public void noopContextBuilder_Put_DisallowsNullKey() { CorrelationContext.Builder noopBuilder = defaultCorrelationContextManager.contextBuilder(); thrown.expect(NullPointerException.class); - noopBuilder.put(null, VALUE, Entry.METADATA_UNLIMITED_PROPAGATION); + noopBuilder.put(null, TEST_VALUE, Entry.METADATA_UNLIMITED_PROPAGATION); } @Test public void noopContextBuilder_Put_DisallowsNullValue() { CorrelationContext.Builder noopBuilder = defaultCorrelationContextManager.contextBuilder(); thrown.expect(NullPointerException.class); - noopBuilder.put(KEY, null, Entry.METADATA_UNLIMITED_PROPAGATION); + noopBuilder.put(TEST_KEY, null, Entry.METADATA_UNLIMITED_PROPAGATION); } @Test public void noopContextBuilder_Put_DisallowsNullEntryMetadata() { CorrelationContext.Builder noopBuilder = defaultCorrelationContextManager.contextBuilder(); thrown.expect(NullPointerException.class); - noopBuilder.put(KEY, VALUE, null); + noopBuilder.put(TEST_KEY, TEST_VALUE, null); } @Test diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java index 74506359cbf..b62b380546f 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java @@ -18,7 +18,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat; -import io.opentelemetry.correlationcontext.CorrelationContextKey; +import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; @@ -36,7 +36,7 @@ final class Propagation extends BaseShimObject { public void injectTextFormat(SpanContextShim contextShim, TextMapInject carrier) { Context context = CurrentContext.get().put(Span.KEY, DefaultSpan.create(contextShim.getSpanContext())); - context = CorrelationContextKey.put(context, contextShim.getCorrelationContext()); + context = context.put(CorrelationContext.KEY, contextShim.getCorrelationContext()); propagators().getHttpTextFormat().inject(context, carrier, TextMapSetter.INSTANCE); } @@ -59,7 +59,7 @@ public SpanContextShim extractTextFormat(TextMapExtract carrier) { } return new SpanContextShim( - telemetryInfo, span.getContext(), CorrelationContextKey.get(context)); + telemetryInfo, span.getContext(), context.get(CorrelationContext.KEY)); } static final class TextMapSetter implements HttpTextFormat.Setter { From 20a6bd1f525b418080db7301f9ac5dccca877f42 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 8 May 2020 18:58:24 -0700 Subject: [PATCH 11/22] Bring back commented out test --- .../propagation/DefaultPropagatorsTest.java | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java b/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java index e3cfb325e4e..9a1141b155e 100644 --- a/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java +++ b/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java @@ -16,10 +16,10 @@ package io.opentelemetry.context.propagation; -/* import static com.google.common.truth.Truth.assertThat; -import io.grpc.Context; +import io.opentelemetry.context.Context; +import io.opentelemetry.currentcontext.CurrentContext; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -27,16 +27,11 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; -*/ -// TODO is this use case (arbitrary context values) needed? /** Unit tests for {@link DefaultContextPropagators}. */ // @RunWith(JUnit4.class) public class DefaultPropagatorsTest { - /* @Rule public final ExpectedException thrown = ExpectedException.none(); @Test @@ -56,8 +51,8 @@ public void testInject() { .build(); Context context = CurrentContext.get(); - context = context.withValue(propagator1.getKey(), "value1"); - context = context.withValue(propagator2.getKey(), "value2"); + context = context.put(propagator1.getKey(), "value1"); + context = context.put(propagator2.getKey(), "value2"); Map map = new HashMap<>(); propagators.getHttpTextFormat().inject(context, map, MapSetter.INSTANCE); @@ -83,9 +78,9 @@ public void testExtract() { Context context = propagators.getHttpTextFormat().extract(CurrentContext.get(), map, MapGetter.INSTANCE); - assertThat(propagator1.getKey().get(context)).isEqualTo("value1"); - assertThat(propagator2.getKey().get(context)).isEqualTo("value2"); - assertThat(propagator3.getKey().get(context)).isNull(); // Handle missing value. + assertThat(context.get(propagator1.getKey())).isEqualTo("value1"); + assertThat(context.get(propagator2.getKey())).isEqualTo("value2"); + assertThat(context.get(propagator3.getKey())).isNull(); // Handle missing value. } @Test @@ -107,7 +102,7 @@ class CustomHttpTextFormat implements HttpTextFormat { public CustomHttpTextFormat(String name) { this.name = name; - this.key = Context.key(name); + this.key = new Context.Key<>(name); } public Context.Key getKey() { @@ -125,7 +120,7 @@ public List fields() { @Override public void inject(Context context, C carrier, Setter setter) { - Object payload = key.get(context); + Object payload = context.get(key); if (payload != null) { setter.set(carrier, name, payload.toString()); } @@ -135,7 +130,7 @@ public void inject(Context context, C carrier, Setter setter) { public Context extract(Context context, C carrier, Getter getter) { String payload = getter.get(carrier, name); if (payload != null) { - context = context.withValue(key, payload); + context = context.put(key, payload); } return context; @@ -163,5 +158,4 @@ public String get(Map map, String key) { private MapGetter() {} } - */ } From c5b194dfd52474002e287bb28dfcd2cde1a207a6 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 8 May 2020 19:01:11 -0700 Subject: [PATCH 12/22] Move classes back to context_prop --- .../main/java/io/opentelemetry/context/Context.java | 0 .../java/io/opentelemetry/context/DefaultContext.java | 0 .../context/propagation/ContextPropagators.java | 0 .../context/propagation/DefaultContextPropagators.java | 0 .../context/propagation/HttpTextFormat.java | 0 .../context/propagation/DefaultPropagatorsTest.java | 10 +++++----- 6 files changed, 5 insertions(+), 5 deletions(-) rename {api => context_prop}/src/main/java/io/opentelemetry/context/Context.java (100%) rename {api => context_prop}/src/main/java/io/opentelemetry/context/DefaultContext.java (100%) rename {api => context_prop}/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java (100%) rename {api => context_prop}/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java (100%) rename {api => context_prop}/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java (100%) rename {api => context_prop}/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java (93%) diff --git a/api/src/main/java/io/opentelemetry/context/Context.java b/context_prop/src/main/java/io/opentelemetry/context/Context.java similarity index 100% rename from api/src/main/java/io/opentelemetry/context/Context.java rename to context_prop/src/main/java/io/opentelemetry/context/Context.java diff --git a/api/src/main/java/io/opentelemetry/context/DefaultContext.java b/context_prop/src/main/java/io/opentelemetry/context/DefaultContext.java similarity index 100% rename from api/src/main/java/io/opentelemetry/context/DefaultContext.java rename to context_prop/src/main/java/io/opentelemetry/context/DefaultContext.java diff --git a/api/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java b/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java similarity index 100% rename from api/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java rename to context_prop/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java diff --git a/api/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java b/context_prop/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java similarity index 100% rename from api/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java rename to context_prop/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java diff --git a/api/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java b/context_prop/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java similarity index 100% rename from api/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java rename to context_prop/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java diff --git a/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java b/context_prop/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java similarity index 93% rename from api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java rename to context_prop/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java index 9a1141b155e..9348ff6e424 100644 --- a/api/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java +++ b/context_prop/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java @@ -18,8 +18,8 @@ import static com.google.common.truth.Truth.assertThat; +import com.google.common.truth.Truth; import io.opentelemetry.context.Context; -import io.opentelemetry.currentcontext.CurrentContext; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -50,7 +50,7 @@ public void testInject() { .addHttpTextFormat(propagator2) .build(); - Context context = CurrentContext.get(); + Context context = Context.EMPTY; context = context.put(propagator1.getKey(), "value1"); context = context.put(propagator2.getKey(), "value2"); @@ -77,7 +77,7 @@ public void testExtract() { map.put(propagator2.getKeyName(), "value2"); Context context = - propagators.getHttpTextFormat().extract(CurrentContext.get(), map, MapGetter.INSTANCE); + propagators.getHttpTextFormat().extract(Context.EMPTY, map, MapGetter.INSTANCE); assertThat(context.get(propagator1.getKey())).isEqualTo("value1"); assertThat(context.get(propagator2.getKey())).isEqualTo("value2"); assertThat(context.get(propagator3.getKey())).isNull(); // Handle missing value. @@ -87,12 +87,12 @@ public void testExtract() { public void noopPropagator() { ContextPropagators propagators = DefaultContextPropagators.builder().build(); - Context context = CurrentContext.get(); + Context context = Context.EMPTY; Map map = new HashMap<>(); propagators.getHttpTextFormat().inject(context, map, MapSetter.INSTANCE); assertThat(map).isEmpty(); - assertThat(propagators.getHttpTextFormat().extract(context, map, MapGetter.INSTANCE)) + Truth.assertThat(propagators.getHttpTextFormat().extract(context, map, MapGetter.INSTANCE)) .isSameInstanceAs(context); } From 227fb66105cf04bd211e63d6e6d7dd43a18ed356 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 8 May 2020 19:30:52 -0700 Subject: [PATCH 13/22] Remove incorrect javadoc --- .../java/io/opentelemetry/currentcontext/CurrentContext.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java b/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java index 9980a9219b9..86eaafe8c7a 100644 --- a/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java +++ b/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java @@ -44,12 +44,10 @@ public static Scope withContext(Context context) { return new DefaultScope(context); } - /** Convenience method for {@code CurrentContext.get().getSpan()}. */ public static Span getSpan() { return get().get(Span.KEY); } - /** Convenience method for {@code CurrentContext.get().getCorrelationContext()}. */ public static CorrelationContext getCorrelationContext() { return get().get(CorrelationContext.KEY); } From 409b551b33dc9f49d4b123bc3530cf5a5c28eb58 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 8 May 2020 19:33:56 -0700 Subject: [PATCH 14/22] Update examples --- QUICKSTART.md | 8 ++++---- .../main/java/io/opentelemetry/currentcontext/Scope.java | 2 +- api/src/main/java/io/opentelemetry/trace/Span.java | 4 ++-- api/src/main/java/io/opentelemetry/trace/Tracer.java | 2 +- .../context/propagation/ContextPropagators.java | 4 ++-- .../java/io/opentelemetry/example/HelloWorldClient.java | 5 +++-- .../io/opentelemetry/example/HelloWorldClientStream.java | 5 +++-- .../java/io/opentelemetry/example/HelloWorldServer.java | 6 +++--- .../java/io/opentelemetry/example/http/HttpClient.java | 5 +++-- .../java/io/opentelemetry/example/http/HttpServer.java | 6 +++--- 10 files changed, 25 insertions(+), 22 deletions(-) diff --git a/QUICKSTART.md b/QUICKSTART.md index 5748d2e9561..2aae2e3afb7 100644 --- a/QUICKSTART.md +++ b/QUICKSTART.md @@ -48,7 +48,7 @@ To create a basic span, you only need to specify the name of the span. The start and end time of the span is automatically set by the OpenTelemetry SDK. ```java Span span = tracer.spanBuilder("my span").startSpan(); -try (Scope scope = tracer.withSpan(span)) { +try (Scope scope = CurrentContext.withSpan(span)) { // your use case ... } catch (Throwable t) { @@ -85,7 +85,7 @@ The OpenTelemetry API offers also an automated way to propagate the `parentSpan` ```java void a() { Span parentSpan = tracer.spanBuilder("a").startSpan(); - try(Scope scope = tracer.withSpan(parentSpan)){ + try(Scope scope = CurrentContext.withSpan(parentSpan)){ b(); } finally { parentSpan.end(); @@ -186,7 +186,7 @@ HttpTextFormat.Setter setter = URL url = new URL("http://127.0.0.1:8080/resource"); Span outGoing = tracer.spanBuilder("/resource").setSpanKind(Span.Kind.CLIENT).startSpan(); -try (Scope scope = tracer.withSpan(outGoing)) { +try (Scope scope = CurrentContext.withSpan(outGoing)) { // Semantic Convention. // (Observe that to set these, Span does not *need* to be the current instance.) outGoing.setAttribute("http.method", "GET"); @@ -222,7 +222,7 @@ public void handle(HttpExchange httpExchange) { Context extractedContext = OpenTelemetry.getPropagators().getHttpTextFormat() .extract(CurrentContext.get(), httpExchange, getter); Span serverSpan = null; - try (Scope scope = ContextUtils.withScopedContext(extractedContext)) { + try (Scope scope = CurrentContext.withContext(extractedContext)) { // Automatically use the extracted SpanContext as parent. serverSpan = tracer.spanBuilder("/resource").setSpanKind(Span.Kind.SERVER) .startSpan(); diff --git a/api/src/main/java/io/opentelemetry/currentcontext/Scope.java b/api/src/main/java/io/opentelemetry/currentcontext/Scope.java index 37cc48addbb..c7fb4304c41 100644 --- a/api/src/main/java/io/opentelemetry/currentcontext/Scope.java +++ b/api/src/main/java/io/opentelemetry/currentcontext/Scope.java @@ -26,7 +26,7 @@ *

Example of usage: * *

- *   try (Scope ctx = tracer.withSpan(span)) {
+ *   try (Scope ctx = CurrentContext.withSpan(span)) {
  *     ...
  *   }
  * 
diff --git a/api/src/main/java/io/opentelemetry/trace/Span.java b/api/src/main/java/io/opentelemetry/trace/Span.java index 479237eb950..205aed5e079 100644 --- a/api/src/main/java/io/opentelemetry/trace/Span.java +++ b/api/src/main/java/io/opentelemetry/trace/Span.java @@ -287,7 +287,7 @@ enum Kind { * void doWork { * // Create a Span as a child of the current Span. * Span span = tracer.spanBuilder("MyChildSpan").startSpan(); - * try (Scope ss = tracer.withSpan(span)) { + * try (Scope ss = CurrentContext.withSpan(span)) { * tracer.getCurrentSpan().addEvent("my event"); * doSomeWork(); // Here the new span is in the current Context, so it can be used * // implicitly anywhere down the stack. @@ -315,7 +315,7 @@ enum Kind { * } * * public void onExecuteHandler(ServerCallHandler serverCallHandler) { - * try (Scope ws = tracer.withSpan(mySpan)) { + * try (Scope ws = CurrentContext.withSpan(mySpan)) { * tracer.getCurrentSpan().addEvent("Start rpc execution."); * serverCallHandler.run(); // Here the new span is in the current Context, so it can be * // used implicitly anywhere down the stack. diff --git a/api/src/main/java/io/opentelemetry/trace/Tracer.java b/api/src/main/java/io/opentelemetry/trace/Tracer.java index 02fb7d491ce..551888fb907 100644 --- a/api/src/main/java/io/opentelemetry/trace/Tracer.java +++ b/api/src/main/java/io/opentelemetry/trace/Tracer.java @@ -35,7 +35,7 @@ * private static final Tracer tracer = OpenTelemetry.getTracer(); * void doWork() { * Span span = tracer.spanBuilder("MyClass.DoWork").startSpan(); - * try(Scope ss = tracer.withSpan(span)) { + * try(Scope ss = CurrentContext.withSpan(span)) { * tracer.getCurrentSpan().addEvent("Starting the work."); * doWorkInternal(); * tracer.getCurrentSpan().addEvent("Finished working."); diff --git a/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java b/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java index 3c4416714ad..82c322a4cd4 100644 --- a/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java +++ b/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java @@ -33,7 +33,7 @@ *
{@code
  * private static final Tracer tracer = OpenTelemetry.getTracer();
  * void onSendRequest() {
- *   try (Scope scope = tracer.withSpan(span)) {
+ *   try (Scope scope = CurrentContext.withSpan(span)) {
  *     ContextPropagators propagators = OpenTelemetry.getPropagators();
  *     HttpTextFormat textFormat = propagators.getHttpTextFormat();
  *
@@ -69,7 +69,7 @@
  *   Span span = tracer.spanBuilder("MyRequest")
  *       .setParent(context)
  *       .setSpanKind(Span.Kind.SERVER).startSpan();
- *   try (Scope ss = tracer.withSpan(span)) {
+ *   try (Scope ss = CurrentContext.withSpan(span)) {
  *     // Handle request and send response back.
  *   } finally {
  *     span.end();
diff --git a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClient.java b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClient.java
index 4df0a941c51..6402399194b 100644
--- a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClient.java
+++ b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClient.java
@@ -29,8 +29,9 @@
 import io.grpc.StatusRuntimeException;
 import io.opentelemetry.OpenTelemetry;
 import io.opentelemetry.context.Context;
-import io.opentelemetry.context.Scope;
 import io.opentelemetry.context.propagation.HttpTextFormat;
+import io.opentelemetry.currentcontext.CurrentContext;
+import io.opentelemetry.currentcontext.Scope;
 import io.opentelemetry.exporters.logging.LoggingSpanExporter;
 import io.opentelemetry.sdk.OpenTelemetrySdk;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
@@ -106,7 +107,7 @@ public void greet(String name) {
     span.setAttribute("net.peer.port", this.serverPort);
 
     // Set the context with the current span
-    try (Scope scope = tracer.withSpan(span)) {
+    try (Scope scope = CurrentContext.withSpan(span)) {
       HelloRequest request = HelloRequest.newBuilder().setName(name).build();
       try {
         HelloReply response = blockingStub.sayHello(request);
diff --git a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClientStream.java b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClientStream.java
index 94304b0988d..1eb05073fca 100644
--- a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClientStream.java
+++ b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClientStream.java
@@ -30,8 +30,9 @@
 import io.grpc.stub.StreamObserver;
 import io.opentelemetry.OpenTelemetry;
 import io.opentelemetry.context.Context;
-import io.opentelemetry.context.Scope;
 import io.opentelemetry.context.propagation.HttpTextFormat;
+import io.opentelemetry.currentcontext.CurrentContext;
+import io.opentelemetry.currentcontext.Scope;
 import io.opentelemetry.exporters.logging.LoggingSpanExporter;
 import io.opentelemetry.sdk.OpenTelemetrySdk;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
@@ -111,7 +112,7 @@ public void greet(List names) {
     StreamObserver requestObserver = null;
 
     // Set the context with the current span
-    try (Scope scope = tracer.withSpan(span)) {
+    try (Scope scope = CurrentContext.withSpan(span)) {
       HelloReplyStreamObserver replyObserver = new HelloReplyStreamObserver();
       requestObserver = asyncStub.sayHelloStream(replyObserver);
       for (String name : names) {
diff --git a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldServer.java b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldServer.java
index b0293c68c5f..8113d037277 100644
--- a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldServer.java
+++ b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldServer.java
@@ -27,9 +27,9 @@
 import io.grpc.stub.StreamObserver;
 import io.opentelemetry.OpenTelemetry;
 import io.opentelemetry.context.Context;
-import io.opentelemetry.context.ContextUtils;
-import io.opentelemetry.context.Scope;
 import io.opentelemetry.context.propagation.HttpTextFormat;
+import io.opentelemetry.currentcontext.CurrentContext;
+import io.opentelemetry.currentcontext.Scope;
 import io.opentelemetry.exporters.logging.LoggingSpanExporter;
 import io.opentelemetry.sdk.OpenTelemetrySdk;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
@@ -165,7 +165,7 @@ public  ServerCall.Listener interceptCall(
       InetSocketAddress clientInfo =
           (InetSocketAddress) call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR);
       // Build a span based on the received context
-      try (Scope scope = ContextUtils.withScopedContext(extractedContext)) {
+      try (Scope scope = CurrentContext.withContext(extractedContext)) {
         Span span =
             tracer
                 .spanBuilder("helloworld.Greeter/SayHello")
diff --git a/examples/http/src/main/java/io/opentelemetry/example/http/HttpClient.java b/examples/http/src/main/java/io/opentelemetry/example/http/HttpClient.java
index 4fc169cf397..eafc1e060b5 100644
--- a/examples/http/src/main/java/io/opentelemetry/example/http/HttpClient.java
+++ b/examples/http/src/main/java/io/opentelemetry/example/http/HttpClient.java
@@ -18,8 +18,9 @@
 
 import io.opentelemetry.OpenTelemetry;
 import io.opentelemetry.context.Context;
-import io.opentelemetry.context.Scope;
 import io.opentelemetry.context.propagation.HttpTextFormat;
+import io.opentelemetry.currentcontext.CurrentContext;
+import io.opentelemetry.currentcontext.Scope;
 import io.opentelemetry.exporters.logging.LoggingSpanExporter;
 import io.opentelemetry.sdk.OpenTelemetrySdk;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
@@ -72,7 +73,7 @@ private HttpClient() throws Exception {
     // Name convention for the Span is not yet defined.
     // See: https://github.com/open-telemetry/opentelemetry-specification/issues/270
     Span span = tracer.spanBuilder("/").setSpanKind(Span.Kind.CLIENT).startSpan();
-    try (Scope scope = tracer.withSpan(span)) {
+    try (Scope scope = CurrentContext.withSpan(span)) {
       // TODO provide semantic convention attributes to Span.Builder
       span.setAttribute("component", "http");
       span.setAttribute("http.method", "GET");
diff --git a/examples/http/src/main/java/io/opentelemetry/example/http/HttpServer.java b/examples/http/src/main/java/io/opentelemetry/example/http/HttpServer.java
index edffa2730a7..891057aaf3f 100644
--- a/examples/http/src/main/java/io/opentelemetry/example/http/HttpServer.java
+++ b/examples/http/src/main/java/io/opentelemetry/example/http/HttpServer.java
@@ -21,9 +21,9 @@
 import io.opentelemetry.OpenTelemetry;
 import io.opentelemetry.common.AttributeValue;
 import io.opentelemetry.context.Context;
-import io.opentelemetry.context.ContextUtils;
-import io.opentelemetry.context.Scope;
 import io.opentelemetry.context.propagation.HttpTextFormat;
+import io.opentelemetry.currentcontext.CurrentContext;
+import io.opentelemetry.currentcontext.Scope;
 import io.opentelemetry.exporters.logging.LoggingSpanExporter;
 import io.opentelemetry.sdk.OpenTelemetrySdk;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
@@ -49,7 +49,7 @@ public void handle(HttpExchange he) throws IOException {
       // Extract the context from the HTTP request
       Context ctx =
           OpenTelemetry.getPropagators().getHttpTextFormat().extract(CurrentContext.get(), he, getter);
-      try (Scope scope = ContextUtils.withScopedContext(ctx)) {
+      try (Scope scope = CurrentContext.withContext(ctx)) {
         // Build a span automatically using the received context
         span = spanBuilder.startSpan();
       }

From a29d041296aa27f462c0543bcca3e169340cc5c9 Mon Sep 17 00:00:00 2001
From: Trask Stalnaker 
Date: Fri, 8 May 2020 19:19:33 -0700
Subject: [PATCH 15/22] Back to gRPC Context

---
 QUICKSTART.md                                 |  4 +-
 .../HttpTraceContextExtractBenchmark.java     |  4 +-
 .../HttpTraceContextInjectBenchmark.java      |  4 +-
 .../CorrelationContext.java                   |  4 +-
 .../correlationcontext/package-info.java      | 10 +--
 .../currentcontext/CurrentContext.java        | 16 ++--
 .../currentcontext/DefaultContextStorage.java | 71 ----------------
 .../java/io/opentelemetry/trace/Span.java     |  4 +-
 .../java/io/opentelemetry/trace/Tracer.java   |  7 +-
 .../io/opentelemetry/trace/package-info.java  |  6 +-
 .../trace/propagation/HttpTraceContext.java   |  6 +-
 .../propagation/HttpTraceContextTest.java     | 65 ++++++--------
 build.gradle                                  |  1 +
 context_prop/build.gradle                     |  2 +
 .../io/opentelemetry/context/Context.java     | 51 -----------
 .../opentelemetry/context/DefaultContext.java | 65 --------------
 .../propagation/ContextPropagators.java       | 11 ++-
 .../DefaultContextPropagators.java            |  2 +-
 .../context/propagation/HttpTextFormat.java   |  2 +-
 .../propagation/DefaultPropagatorsTest.java   | 24 +++---
 .../PropagatorContextExtractBenchmark.java    | 13 ++-
 .../PropagatorContextInjectBenchmark.java     |  5 +-
 .../trace/propagation/B3Propagator.java       |  2 +-
 .../propagation/B3PropagatorExtractor.java    |  4 +-
 .../propagation/B3PropagatorInjector.java     |  4 +-
 .../trace/propagation/JaegerPropagator.java   |  6 +-
 .../trace/propagation/B3PropagatorTest.java   | 85 ++++++++-----------
 .../propagation/JaegerPropagatorTest.java     | 51 +++++------
 .../contrib/trace/CurrentSpanUtils.java       |  5 +-
 .../example/HelloWorldClient.java             |  2 +-
 .../example/HelloWorldClientStream.java       |  2 +-
 .../example/HelloWorldServer.java             |  4 +-
 .../example/http/HttpClient.java              |  4 +-
 .../example/http/HttpServer.java              |  2 +-
 .../opentracingshim/Propagation.java          | 13 ++-
 .../trace/testbed/clientserver/Client.java    |  3 +-
 .../trace/testbed/clientserver/Server.java    |  6 +-
 37 files changed, 174 insertions(+), 396 deletions(-)
 delete mode 100644 api/src/main/java/io/opentelemetry/currentcontext/DefaultContextStorage.java
 delete mode 100644 context_prop/src/main/java/io/opentelemetry/context/Context.java
 delete mode 100644 context_prop/src/main/java/io/opentelemetry/context/DefaultContext.java

diff --git a/QUICKSTART.md b/QUICKSTART.md
index 2aae2e3afb7..1c86042b9de 100644
--- a/QUICKSTART.md
+++ b/QUICKSTART.md
@@ -193,7 +193,7 @@ try (Scope scope = CurrentContext.withSpan(outGoing)) {
   outGoing.setAttribute("http.url", url.toString());
   HttpURLConnection transportLayer = (HttpURLConnection) url.openConnection();
   // Inject the request with the *current*  Context, which contains our current Span.
-  OpenTelemetry.getPropagators().getHttpTextFormat().inject(CurrentContext.get(), transportLayer, setter);
+  OpenTelemetry.getPropagators().getHttpTextFormat().inject(Context.current(), transportLayer, setter);
   // Make outgoing call
 } finally {
   outGoing.end();
@@ -220,7 +220,7 @@ HttpTextFormat.Getter getter =
 public void handle(HttpExchange httpExchange) {
   // Extract the SpanContext and other elements from the request.
   Context extractedContext = OpenTelemetry.getPropagators().getHttpTextFormat()
-        .extract(CurrentContext.get(), httpExchange, getter);
+        .extract(Context.current(), httpExchange, getter);
   Span serverSpan = null;
   try (Scope scope = CurrentContext.withContext(extractedContext)) {
     // Automatically use the extracted SpanContext as parent.
diff --git a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextExtractBenchmark.java b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextExtractBenchmark.java
index 6af25aae68d..86febd0a0bc 100644
--- a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextExtractBenchmark.java
+++ b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextExtractBenchmark.java
@@ -16,7 +16,7 @@
 
 package io.opentelemetry.trace.propagation;
 
-import io.opentelemetry.context.Context;
+import io.grpc.Context;
 import io.opentelemetry.context.propagation.HttpTextFormat.Getter;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -71,7 +71,7 @@ public String get(Map carrier, String key) {
   public Context measureExtract() {
     Context result = null;
     for (int i = 0; i < COUNT; i++) {
-      result = httpTraceContext.extract(Context.EMPTY, carriers.get(i), getter);
+      result = httpTraceContext.extract(Context.ROOT, carriers.get(i), getter);
     }
     return result;
   }
diff --git a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java
index ae72627bede..a8647fce998 100644
--- a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java
+++ b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java
@@ -16,7 +16,7 @@
 
 package io.opentelemetry.trace.propagation;
 
-import io.opentelemetry.context.Context;
+import io.grpc.Context;
 import io.opentelemetry.context.propagation.HttpTextFormat.Setter;
 import io.opentelemetry.trace.DefaultSpan;
 import io.opentelemetry.trace.Span;
@@ -93,7 +93,7 @@ private static SpanContext createTestSpanContext(String traceId, String spanId)
   private static List createContexts(List spanContexts) {
     List contexts = new ArrayList<>();
     for (SpanContext context : spanContexts) {
-      contexts.add(Context.EMPTY.put(Span.KEY, DefaultSpan.create(context)));
+      contexts.add(Context.ROOT.withValue(Span.KEY, DefaultSpan.create(context)));
     }
     return contexts;
   }
diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java
index e97e273f9a2..e685b8f05c2 100644
--- a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java
+++ b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java
@@ -16,7 +16,7 @@
 
 package io.opentelemetry.correlationcontext;
 
-import io.opentelemetry.context.Context;
+import io.grpc.Context;
 import io.opentelemetry.currentcontext.CurrentContext;
 import java.util.Collection;
 import javax.annotation.Nullable;
@@ -36,7 +36,7 @@ public interface CorrelationContext {
 
   // TODO (trask) javadoc
   Context.Key KEY =
-      new Context.Key<>("CorrelationContext", EmptyCorrelationContext.getInstance());
+      Context.keyWithDefault("CorrelationContext", EmptyCorrelationContext.getInstance());
 
   /**
    * Returns an immutable collection of the entries in this {@code CorrelationContext}. Order of
diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/package-info.java b/api/src/main/java/io/opentelemetry/correlationcontext/package-info.java
index aa5dba6e533..cafe13b13b3 100644
--- a/api/src/main/java/io/opentelemetry/correlationcontext/package-info.java
+++ b/api/src/main/java/io/opentelemetry/correlationcontext/package-info.java
@@ -17,17 +17,17 @@
 /**
  * API for associating entries with scoped operations.
  *
- * 

This package manages a set of entries in the {@link io.opentelemetry.context.Context}. The - * entries can be used to label anything that is associated with a specific operation. For example, - * the {@code opentelemetry.stats} package labels all stats with the current entries. + *

This package manages a set of entries in the {@code io.grpc.Context}. The entries can be used + * to label anything that is associated with a specific operation. For example, the {@code + * opentelemetry.stats} package labels all stats with the current entries. * *

{@link io.opentelemetry.correlationcontext.Entry Entrys} are key-value pairs. The {@link * io.opentelemetry.correlationcontext.EntryKey keys} and {@link * io.opentelemetry.correlationcontext.EntryValue values} are wrapped {@code String}s. They are * stored as a map in a {@link io.opentelemetry.correlationcontext.CorrelationContext}. * - *

Note that entries are independent of the tracing data that is propagated in the {@link - * io.opentelemetry.context.Context}, such as trace ID. + *

Note that entries are independent of the tracing data that is propagated in the {@code + * io.grpc.Context}, such as trace ID. */ // TODO: Add code examples. package io.opentelemetry.correlationcontext; diff --git a/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java b/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java index 86eaafe8c7a..48b8e41355e 100644 --- a/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java +++ b/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java @@ -17,7 +17,7 @@ package io.opentelemetry.currentcontext; import com.google.errorprone.annotations.MustBeClosed; -import io.opentelemetry.context.Context; +import io.grpc.Context; import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.trace.Span; @@ -27,12 +27,12 @@ public class CurrentContext { @MustBeClosed public static Scope withSpan(Span span) { - return withContext(get().put(Span.KEY, span)); + return withContext(get().withValue(Span.KEY, span)); } @MustBeClosed public static Scope withCorrelationContext(CorrelationContext correlationContext) { - return withContext(get().put(CorrelationContext.KEY, correlationContext)); + return withContext(get().withValue(CorrelationContext.KEY, correlationContext)); } /** @@ -45,16 +45,16 @@ public static Scope withContext(Context context) { } public static Span getSpan() { - return get().get(Span.KEY); + return Span.KEY.get(); } public static CorrelationContext getCorrelationContext() { - return get().get(CorrelationContext.KEY); + return CorrelationContext.KEY.get(); } /** Returns the context bound to the current thread. */ public static Context get() { - return DefaultContextStorage.current(); + return Context.current(); } private CurrentContext() {} @@ -65,13 +65,13 @@ static class DefaultScope implements Scope { private final Context context; DefaultScope(Context context) { - this.priorContext = DefaultContextStorage.doAttach(context); + this.priorContext = context.attach(); this.context = context; } @Override public void close() { - DefaultContextStorage.detach(context, priorContext); + context.detach(priorContext); } } } diff --git a/api/src/main/java/io/opentelemetry/currentcontext/DefaultContextStorage.java b/api/src/main/java/io/opentelemetry/currentcontext/DefaultContextStorage.java deleted file mode 100644 index 031aa1e5b66..00000000000 --- a/api/src/main/java/io/opentelemetry/currentcontext/DefaultContextStorage.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 2020, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.currentcontext; - -import io.opentelemetry.context.Context; -import java.util.logging.Level; -import java.util.logging.Logger; - -// TODO (trask) citation needed, copied from grpc-context -// if needed, we can make this storage pluggable similar to grpc-context -final class DefaultContextStorage { - private static final Logger log = Logger.getLogger(DefaultContextStorage.class.getName()); - - /** Currently bound context. */ - static final ThreadLocal localContext = new ThreadLocal<>(); - - static Context doAttach(Context toAttach) { - Context current = current(); - localContext.set(toAttach); - return current; - } - - static void detach(Context toDetach, Context toRestore) { - if (current() != toDetach) { - // Log a severe message instead of throwing an exception as the context to attach is assumed - // to be the correct one and the unbalanced state represents a coding mistake in a lower - // layer in the stack that cannot be recovered from here. - log.log( - Level.SEVERE, - "Context was not attached when detaching", - new Throwable().fillInStackTrace()); - } - if (toRestore != Context.EMPTY) { - localContext.set(toRestore); - } else { - // Avoid leaking our ClassLoader via ROOT if this Thread is reused across multiple - // ClassLoaders, as is common for Servlet Containers. The ThreadLocal is weakly referenced by - // the Thread, but its current value is strongly referenced and only lazily collected as new - // ThreadLocals are created. - // - // Use set(null) instead of remove() since remove() deletes the entry which is then re-created - // on the next get() (because of initialValue() handling). set(null) has same performance as - // set(toRestore). - localContext.set(null); - } - } - - static Context current() { - Context current = localContext.get(); - if (current == null) { - return Context.EMPTY; - } - return current; - } - - private DefaultContextStorage() {} -} diff --git a/api/src/main/java/io/opentelemetry/trace/Span.java b/api/src/main/java/io/opentelemetry/trace/Span.java index 205aed5e079..44277775164 100644 --- a/api/src/main/java/io/opentelemetry/trace/Span.java +++ b/api/src/main/java/io/opentelemetry/trace/Span.java @@ -16,8 +16,8 @@ package io.opentelemetry.trace; +import io.grpc.Context; import io.opentelemetry.common.AttributeValue; -import io.opentelemetry.context.Context; import java.util.Map; import javax.annotation.Nullable; import javax.annotation.concurrent.ThreadSafe; @@ -35,7 +35,7 @@ public interface Span { // TODO (trask) javadoc - Context.Key KEY = new Context.Key("Span", DefaultSpan.getInvalid()); + Context.Key KEY = Context.keyWithDefault("Span", DefaultSpan.getInvalid()); /** * Type of span. Can be used to specify additional relationships between spans in addition to a diff --git a/api/src/main/java/io/opentelemetry/trace/Tracer.java b/api/src/main/java/io/opentelemetry/trace/Tracer.java index 551888fb907..2df15d0b5f8 100644 --- a/api/src/main/java/io/opentelemetry/trace/Tracer.java +++ b/api/src/main/java/io/opentelemetry/trace/Tracer.java @@ -24,9 +24,10 @@ *

Users may choose to use manual or automatic Context propagation. Because of that this class * offers APIs to facilitate both usages. * - *

The automatic context propagation is done using {@link io.opentelemetry.context.Context} which - * can carry scoped-values across API boundaries and between threads. Users of the library must - * propagate the {@link io.opentelemetry.context.Context} between different threads. + *

The automatic context propagation is done using {@link io.grpc.Context} which is a gRPC + * independent implementation for in-process Context propagation mechanism which can carry + * scoped-values across API boundaries and between threads. Users of the library must propagate the + * {@link io.grpc.Context} between different threads. * *

Example usage with automatic context propagation: * diff --git a/api/src/main/java/io/opentelemetry/trace/package-info.java b/api/src/main/java/io/opentelemetry/trace/package-info.java index b47c1ffdef0..6b6f2f70221 100644 --- a/api/src/main/java/io/opentelemetry/trace/package-info.java +++ b/api/src/main/java/io/opentelemetry/trace/package-info.java @@ -25,9 +25,9 @@ * *

{@link io.opentelemetry.trace.Span} represents a single operation within a trace. * - *

{@link io.opentelemetry.trace.Span Spans} are propagated in-process in the {@link - * io.opentelemetry.context.Context} and between process using one of the wire propagation formats - * supported in the {@code opentelemetry.trace.propagation} package. + *

{@link io.opentelemetry.trace.Span Spans} are propagated in-process in the {@code + * io.grpc.Context} and between process using one of the wire propagation formats supported in the + * {@code opentelemetry.trace.propagation} package. */ // TODO: Add code examples. package io.opentelemetry.trace; diff --git a/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java b/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java index 2dfd2c14802..dbfbfd972ac 100644 --- a/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java +++ b/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java @@ -19,7 +19,7 @@ import static io.opentelemetry.internal.Utils.checkArgument; import static io.opentelemetry.internal.Utils.checkNotNull; -import io.opentelemetry.context.Context; +import io.grpc.Context; import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; @@ -79,7 +79,7 @@ public void inject(Context context, C carrier, Setter setter) { checkNotNull(context, "context"); checkNotNull(setter, "setter"); - Span span = context.get(Span.KEY); + Span span = Span.KEY.get(context); if (!span.getContext().isValid()) { return; } @@ -124,7 +124,7 @@ private static void injectImpl(SpanContext spanContext, C carrier, Setter checkNotNull(getter, "getter"); SpanContext spanContext = extractImpl(carrier, getter); - return context.put(Span.KEY, DefaultSpan.create(spanContext)); + return context.withValue(Span.KEY, DefaultSpan.create(spanContext)); } private static SpanContext extractImpl(C carrier, Getter getter) { diff --git a/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java b/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java index 84e572004bb..089c3100968 100644 --- a/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java +++ b/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java @@ -20,10 +20,9 @@ import static io.opentelemetry.trace.propagation.HttpTraceContext.TRACE_PARENT; import static io.opentelemetry.trace.propagation.HttpTraceContext.TRACE_STATE; -import io.opentelemetry.context.Context; +import io.grpc.Context; import io.opentelemetry.context.propagation.HttpTextFormat.Getter; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; -import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanContext; @@ -82,17 +81,17 @@ public String get(Map carrier, String key) { @Rule public ExpectedException thrown = ExpectedException.none(); private static SpanContext getSpanContext(Context context) { - return context.get(Span.KEY).getContext(); + return Span.KEY.get(context).getContext(); } private static Context withSpanContext(SpanContext spanContext, Context context) { - return context.put(Span.KEY, DefaultSpan.create(spanContext)); + return context.withValue(Span.KEY, DefaultSpan.create(spanContext)); } @Test public void inject_Nothing() { Map carrier = new LinkedHashMap<>(); - httpTraceContext.inject(CurrentContext.get(), carrier, setter); + httpTraceContext.inject(Context.current(), carrier, setter); assertThat(carrier).hasSize(0); } @@ -102,7 +101,7 @@ public void inject_NullCarrierUsage() { Context context = withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), - CurrentContext.get()); + Context.current()); httpTraceContext.inject( context, null, @@ -121,7 +120,7 @@ public void inject_SampledContext() { Context context = withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), - CurrentContext.get()); + Context.current()); httpTraceContext.inject(context, carrier, setter); assertThat(carrier).containsExactly(TRACE_PARENT, TRACEPARENT_HEADER_SAMPLED); } @@ -132,7 +131,7 @@ public void inject_NotSampledContext() { Context context = withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), - CurrentContext.get()); + Context.current()); httpTraceContext.inject(context, carrier, setter); assertThat(carrier).containsExactly(TRACE_PARENT, TRACEPARENT_HEADER_NOT_SAMPLED); } @@ -143,7 +142,7 @@ public void inject_SampledContext_WithTraceState() { Context context = withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_NOT_DEFAULT), - CurrentContext.get()); + Context.current()); httpTraceContext.inject(context, carrier, setter); assertThat(carrier) .containsExactly( @@ -156,7 +155,7 @@ public void inject_NotSampledContext_WithTraceState() { Context context = withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_NOT_DEFAULT), - CurrentContext.get()); + Context.current()); httpTraceContext.inject(context, carrier, setter); assertThat(carrier) .containsExactly( @@ -170,7 +169,7 @@ public void inject_NotSampledContext_WithTraceState() { public void extract_SampledContext() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_SAMPLED); - assertThat(getSpanContext(httpTraceContext.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -180,7 +179,7 @@ public void extract_SampledContext() { public void extract_NotSampledContext() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_NOT_SAMPLED); - assertThat(getSpanContext(httpTraceContext.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -191,7 +190,7 @@ public void extract_SampledContext_WithTraceState() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_SAMPLED); carrier.put(TRACE_STATE, TRACESTATE_NOT_DEFAULT_ENCODING); - assertThat(getSpanContext(httpTraceContext.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_NOT_DEFAULT)); @@ -202,7 +201,7 @@ public void extract_NotSampledContext_WithTraceState() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_NOT_SAMPLED); carrier.put(TRACE_STATE, TRACESTATE_NOT_DEFAULT_ENCODING); - assertThat(getSpanContext(httpTraceContext.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_NOT_DEFAULT)); @@ -212,7 +211,7 @@ public void extract_NotSampledContext_WithTraceState() { public void extract_NotSampledContext_NextVersion() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, "01-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-00-02"); - assertThat(getSpanContext(httpTraceContext.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -223,7 +222,7 @@ public void extract_NotSampledContext_EmptyTraceState() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_NOT_SAMPLED); carrier.put(TRACE_STATE, ""); - assertThat(getSpanContext(httpTraceContext.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -234,7 +233,7 @@ public void extract_NotSampledContext_TraceStateWithSpaces() { Map carrier = new LinkedHashMap<>(); carrier.put(TRACE_PARENT, TRACEPARENT_HEADER_NOT_SAMPLED); carrier.put(TRACE_STATE, TRACESTATE_NOT_DEFAULT_ENCODING_WITH_SPACES); - assertThat(getSpanContext(httpTraceContext.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_NOT_DEFAULT)); @@ -244,8 +243,7 @@ public void extract_NotSampledContext_TraceStateWithSpaces() { public void extract_EmptyHeader() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(TRACE_PARENT, ""); - assertThat( - getSpanContext(httpTraceContext.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -254,8 +252,7 @@ public void extract_InvalidTraceId() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put( TRACE_PARENT, "00-" + "abcdefghijklmnopabcdefghijklmnop" + "-" + SPAN_ID_BASE16 + "-01"); - assertThat( - getSpanContext(httpTraceContext.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -263,8 +260,7 @@ public void extract_InvalidTraceId() { public void extract_InvalidTraceId_Size() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "00-" + SPAN_ID_BASE16 + "-01"); - assertThat( - getSpanContext(httpTraceContext.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -272,8 +268,7 @@ public void extract_InvalidTraceId_Size() { public void extract_InvalidSpanId() { Map invalidHeaders = new HashMap(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + "abcdefghijklmnop" + "-01"); - assertThat( - getSpanContext(httpTraceContext.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -281,8 +276,7 @@ public void extract_InvalidSpanId() { public void extract_InvalidSpanId_Size() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "00-01"); - assertThat( - getSpanContext(httpTraceContext.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -290,8 +284,7 @@ public void extract_InvalidSpanId_Size() { public void extract_InvalidTraceFlags() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-gh"); - assertThat( - getSpanContext(httpTraceContext.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -299,8 +292,7 @@ public void extract_InvalidTraceFlags() { public void extract_InvalidTraceFlags_Size() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-0100"); - assertThat( - getSpanContext(httpTraceContext.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -309,8 +301,7 @@ public void extract_InvalidTracestate_EntriesDelimiter() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-01"); invalidHeaders.put(TRACE_STATE, "foo=bar;test=test"); - assertThat( - getSpanContext(httpTraceContext.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -321,8 +312,7 @@ public void extract_InvalidTracestate_KeyValueDelimiter() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-01"); invalidHeaders.put(TRACE_STATE, "foo=bar,test-test"); - assertThat( - getSpanContext(httpTraceContext.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -333,8 +323,7 @@ public void extract_InvalidTracestate_OneString() { Map invalidHeaders = new HashMap<>(); invalidHeaders.put(TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-01"); invalidHeaders.put(TRACE_STATE, "test-test"); - assertThat( - getSpanContext(httpTraceContext.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), invalidHeaders, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -354,7 +343,7 @@ public void headerNames() { @Test public void extract_emptyCarrier() { Map emptyHeaders = new HashMap<>(); - assertThat(getSpanContext(httpTraceContext.extract(CurrentContext.get(), emptyHeaders, getter))) + assertThat(getSpanContext(httpTraceContext.extract(Context.current(), emptyHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } } diff --git a/build.gradle b/build.gradle index 337e09fd117..7d4e081f849 100644 --- a/build.gradle +++ b/build.gradle @@ -116,6 +116,7 @@ subprojects { errorprone_core : "com.google.errorprone:error_prone_core:${errorProneVersion}", errorprone_javac : "com.google.errorprone:javac:${errorProneJavacVersion}", grpc_api : "io.grpc:grpc-api", + grpc_context : "io.grpc:grpc-context", grpc_protobuf : "io.grpc:grpc-protobuf", grpc_stub : "io.grpc:grpc-stub", guava : "com.google.guava:guava", diff --git a/context_prop/build.gradle b/context_prop/build.gradle index b4cf892d839..bc8932a4b5e 100644 --- a/context_prop/build.gradle +++ b/context_prop/build.gradle @@ -9,6 +9,8 @@ description = 'OpenTelemetry Context Propagation' ext.moduleName = "io.opentelemetry.context.propagation" dependencies { + api libraries.grpc_context + signature "org.codehaus.mojo.signature:java17:1.0@signature" signature "net.sf.androidscents.signature:android-api-level-14:4.0_r4@signature" } diff --git a/context_prop/src/main/java/io/opentelemetry/context/Context.java b/context_prop/src/main/java/io/opentelemetry/context/Context.java deleted file mode 100644 index 1f1763c8ca0..00000000000 --- a/context_prop/src/main/java/io/opentelemetry/context/Context.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2020, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.context; - -// TODO (trask) javadoc class and methods -public interface Context { - - Context EMPTY = DefaultContext.EMPTY; - - // returns default value from key - T get(Key key); - - Context put(Key key, T value); - - class Key { - private final String name; - private final T defaultValue; - - public Key(String name) { - this(name, null); - } - - public Key(String name, T defaultValue) { - this.name = name; - this.defaultValue = defaultValue; - } - - public T getDefaultValue() { - return defaultValue; - } - - @Override - public String toString() { - return name; - } - } -} diff --git a/context_prop/src/main/java/io/opentelemetry/context/DefaultContext.java b/context_prop/src/main/java/io/opentelemetry/context/DefaultContext.java deleted file mode 100644 index fdd4ab7e48e..00000000000 --- a/context_prop/src/main/java/io/opentelemetry/context/DefaultContext.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2020, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.context; - -import javax.annotation.Nullable; - -// TODO (trask) replace naive implementation -final class DefaultContext implements Context { - - static final Context EMPTY = new EmptyContext(); - - private final Context parent; - private final Key key; - private final Object value; - - DefaultContext(Key key, Object value, Context parent) { - this.key = key; - this.value = value; - this.parent = parent; - } - - @Override - @Nullable - @SuppressWarnings("unchecked") - public T get(Key key) { - if (key.equals(this.key)) { - return value == null ? key.getDefaultValue() : (T) value; - } else { - return parent.get(key); - } - } - - @Override - public Context put(Key key, T value) { - return new DefaultContext(key, value, this); - } - - static class EmptyContext implements Context { - - @Override - @Nullable - public T get(Key key) { - return key.getDefaultValue(); - } - - @Override - public Context put(Key key, T value) { - return new DefaultContext(key, value, this); - } - } -} diff --git a/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java b/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java index 82c322a4cd4..700626b1969 100644 --- a/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java +++ b/context_prop/src/main/java/io/opentelemetry/context/propagation/ContextPropagators.java @@ -23,10 +23,9 @@ * *

This container can be used to access a single, composite propagator for each supported format, * which will be responsible for injecting and extracting data for each registered concern (traces, - * correlations, etc). Propagation will happen through {@link io.opentelemetry.context.Context}, - * from which values will be read upon injection, and which will store values from the extraction - * step. The resulting {@link io.opentelemetry.context.Context} can then be used implicitly or - * explicitly by the OpenTelemetry API. + * correlations, etc). Propagation will happen through {@code io.grpc.Context}, from which values + * will be read upon injection, and which will store values from the extraction step. The resulting + * {@code Context} can then be used implicitly or explicitly by the OpenTelemetry API. * *

Example of usage on the client: * @@ -40,7 +39,7 @@ * // Inject the span's SpanContext and other available concerns (such as correlations) * // contained in the specified Context. * Map map = new HashMap<>(); - * httpTextFormat.inject(CurrentContext.get(), map, new Setter() { + * httpTextFormat.inject(Context.current(), map, new Setter() { * public void put(Map map, String key, String value) { * map.put(key, value); * } @@ -60,7 +59,7 @@ * * // Extract and store the propagated span's SpanContext and other available concerns * // in the specified Context. - * Context context = textFormat.extract(CurrentContext.get(), request, + * Context context = textFormat.extract(Context.current(), request, * new Getter() { * public String get(Object request, String key) { * // Return the value associated to the key, if available. diff --git a/context_prop/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java b/context_prop/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java index 112d8a35da0..b09751e77e7 100644 --- a/context_prop/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java +++ b/context_prop/src/main/java/io/opentelemetry/context/propagation/DefaultContextPropagators.java @@ -16,7 +16,7 @@ package io.opentelemetry.context.propagation; -import io.opentelemetry.context.Context; +import io.grpc.Context; import java.util.ArrayList; import java.util.Collections; import java.util.List; diff --git a/context_prop/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java b/context_prop/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java index b5ea5170948..e99a2d475bb 100644 --- a/context_prop/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java +++ b/context_prop/src/main/java/io/opentelemetry/context/propagation/HttpTextFormat.java @@ -16,7 +16,7 @@ package io.opentelemetry.context.propagation; -import io.opentelemetry.context.Context; +import io.grpc.Context; import java.util.List; import javax.annotation.Nullable; import javax.annotation.concurrent.ThreadSafe; diff --git a/context_prop/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java b/context_prop/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java index 9348ff6e424..fad99ea543c 100644 --- a/context_prop/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java +++ b/context_prop/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java @@ -19,7 +19,7 @@ import static com.google.common.truth.Truth.assertThat; import com.google.common.truth.Truth; -import io.opentelemetry.context.Context; +import io.grpc.Context; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -50,9 +50,9 @@ public void testInject() { .addHttpTextFormat(propagator2) .build(); - Context context = Context.EMPTY; - context = context.put(propagator1.getKey(), "value1"); - context = context.put(propagator2.getKey(), "value2"); + Context context = Context.ROOT; + context = context.withValue(propagator1.getKey(), "value1"); + context = context.withValue(propagator2.getKey(), "value2"); Map map = new HashMap<>(); propagators.getHttpTextFormat().inject(context, map, MapSetter.INSTANCE); @@ -77,17 +77,17 @@ public void testExtract() { map.put(propagator2.getKeyName(), "value2"); Context context = - propagators.getHttpTextFormat().extract(Context.EMPTY, map, MapGetter.INSTANCE); - assertThat(context.get(propagator1.getKey())).isEqualTo("value1"); - assertThat(context.get(propagator2.getKey())).isEqualTo("value2"); - assertThat(context.get(propagator3.getKey())).isNull(); // Handle missing value. + propagators.getHttpTextFormat().extract(Context.ROOT, map, MapGetter.INSTANCE); + assertThat(propagator1.getKey().get(context)).isEqualTo("value1"); + assertThat(propagator2.getKey().get(context)).isEqualTo("value2"); + assertThat(propagator3.getKey().get(context)).isNull(); // Handle missing value. } @Test public void noopPropagator() { ContextPropagators propagators = DefaultContextPropagators.builder().build(); - Context context = Context.EMPTY; + Context context = Context.ROOT; Map map = new HashMap<>(); propagators.getHttpTextFormat().inject(context, map, MapSetter.INSTANCE); assertThat(map).isEmpty(); @@ -102,7 +102,7 @@ class CustomHttpTextFormat implements HttpTextFormat { public CustomHttpTextFormat(String name) { this.name = name; - this.key = new Context.Key<>(name); + this.key = Context.key(name); } public Context.Key getKey() { @@ -120,7 +120,7 @@ public List fields() { @Override public void inject(Context context, C carrier, Setter setter) { - Object payload = context.get(key); + Object payload = key.get(context); if (payload != null) { setter.set(carrier, name, payload.toString()); } @@ -130,7 +130,7 @@ public void inject(Context context, C carrier, Setter setter) { public Context extract(Context context, C carrier, Getter getter) { String payload = getter.get(carrier, name); if (payload != null) { - context = context.put(key, payload); + context = context.withValue(key, payload); } return context; diff --git a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java index 2f68986a775..38a7ae67c82 100644 --- a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java +++ b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java @@ -16,8 +16,7 @@ package io.opentelemetry.contrib.trace.propagation; -import io.opentelemetry.context.Context; -import io.opentelemetry.currentcontext.CurrentContext; +import io.grpc.Context; import io.opentelemetry.trace.Span; import java.util.Arrays; import java.util.Collections; @@ -65,7 +64,7 @@ public void setup() { @BenchmarkMode(Mode.AverageTime) @Fork(1) public Span measureExtract() { - return doExtract().get(Span.KEY); + return Span.KEY.get(doExtract()); } protected abstract Context doExtract(); @@ -115,7 +114,7 @@ public String get(Map carrier, String key) { @Override protected Context doExtract() { - return jaegerPropagator.extract(CurrentContext.get(), getCarrier(), getter); + return jaegerPropagator.extract(Context.current(), getCarrier(), getter); } @Override @@ -158,7 +157,7 @@ public String get(Map carrier, String key) { @Override protected Context doExtract() { - return jaegerPropagator.extract(CurrentContext.get(), getCarrier(), getter); + return jaegerPropagator.extract(Context.current(), getCarrier(), getter); } @Override @@ -201,7 +200,7 @@ public String get(Map carrier, String key) { @Override protected Context doExtract() { - return b3Propagator.extract(CurrentContext.get(), getCarrier(), getter); + return b3Propagator.extract(Context.current(), getCarrier(), getter); } @Override @@ -247,7 +246,7 @@ public String get(Map carrier, String key) { @Override protected Context doExtract() { - return b3Propagator.extract(CurrentContext.get(), getCarrier(), getter); + return b3Propagator.extract(Context.current(), getCarrier(), getter); } @Override diff --git a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java index b9260f9114b..22d50569659 100644 --- a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java +++ b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java @@ -16,8 +16,7 @@ package io.opentelemetry.contrib.trace.propagation; -import io.opentelemetry.context.Context; -import io.opentelemetry.currentcontext.CurrentContext; +import io.grpc.Context; import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanContext; @@ -75,7 +74,7 @@ public abstract static class AbstractContextInjectBenchmark { @BenchmarkMode(Mode.AverageTime) @Fork(1) public Map measureInject() { - Context context = CurrentContext.get().put(Span.KEY, DefaultSpan.create(contextToTest)); + Context context = Context.current().withValue(Span.KEY, DefaultSpan.create(contextToTest)); doInject(context, carrier); return carrier; } diff --git a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3Propagator.java b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3Propagator.java index 7538ff92998..837326c709b 100644 --- a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3Propagator.java +++ b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3Propagator.java @@ -16,7 +16,7 @@ package io.opentelemetry.contrib.trace.propagation; -import io.opentelemetry.context.Context; +import io.grpc.Context; import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.trace.SpanId; import io.opentelemetry.trace.TraceId; diff --git a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorExtractor.java b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorExtractor.java index 3b1b89d2c3c..3babfa61cab 100644 --- a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorExtractor.java +++ b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorExtractor.java @@ -25,7 +25,7 @@ import static io.opentelemetry.contrib.trace.propagation.B3Propagator.TRACE_ID_HEADER; import static io.opentelemetry.contrib.trace.propagation.B3Propagator.TRUE_INT; -import io.opentelemetry.context.Context; +import io.grpc.Context; import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.internal.StringUtils; import io.opentelemetry.trace.DefaultSpan; @@ -65,7 +65,7 @@ Context extract(Context context, C carrier, HttpTextFormat.Getter getter) spanContext = getSpanContextFromMultipleHeaders(carrier, getter); } - return context.put(Span.KEY, DefaultSpan.create(spanContext)); + return context.withValue(Span.KEY, DefaultSpan.create(spanContext)); } @SuppressWarnings("StringSplitter") diff --git a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java index faca820e7ba..46bea76218c 100644 --- a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java +++ b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java @@ -16,7 +16,7 @@ package io.opentelemetry.contrib.trace.propagation; -import io.opentelemetry.context.Context; +import io.grpc.Context; import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanContext; @@ -46,7 +46,7 @@ void inject(Context context, C carrier, HttpTextFormat.Setter setter) { Objects.requireNonNull(context, "context"); Objects.requireNonNull(setter, "setter"); - Span span = context.get(Span.KEY); + Span span = Span.KEY.get(context); if (!span.getContext().isValid()) { return; } diff --git a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java index 688f2ce84b7..9ddf310fee1 100644 --- a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java +++ b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java @@ -18,7 +18,7 @@ import static io.opentelemetry.internal.Utils.checkNotNull; -import io.opentelemetry.context.Context; +import io.grpc.Context; import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.internal.StringUtils; import io.opentelemetry.trace.DefaultSpan; @@ -87,7 +87,7 @@ public void inject(Context context, C carrier, Setter setter) { checkNotNull(context, "context"); checkNotNull(setter, "setter"); - Span span = context.get(Span.KEY); + Span span = Span.KEY.get(context); if (!span.getContext().isValid()) { return; } @@ -112,7 +112,7 @@ public Context extract(Context context, C carrier, Getter getter) { SpanContext spanContext = getSpanContextFromHeader(carrier, getter); - return context.put(Span.KEY, DefaultSpan.create(spanContext)); + return context.withValue(Span.KEY, DefaultSpan.create(spanContext)); } @SuppressWarnings("StringSplitter") diff --git a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java index 5955adb83b1..22db68e46fa 100644 --- a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java +++ b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java @@ -18,10 +18,9 @@ import static com.google.common.truth.Truth.assertThat; -import io.opentelemetry.context.Context; +import io.grpc.Context; import io.opentelemetry.context.propagation.HttpTextFormat.Getter; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; -import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.internal.StringUtils; import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; @@ -75,11 +74,11 @@ public String get(Map carrier, String key) { @Rule public ExpectedException thrown = ExpectedException.none(); private static SpanContext getSpanContext(Context context) { - return context.get(Span.KEY).getContext(); + return Span.KEY.get(context).getContext(); } private static Context withSpanContext(SpanContext spanContext, Context context) { - return context.put(Span.KEY, DefaultSpan.create(spanContext)); + return context.withValue(Span.KEY, DefaultSpan.create(spanContext)); } @Test @@ -88,7 +87,7 @@ public void inject_SampledContext() { b3Propagator.inject( withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), - CurrentContext.get()), + Context.current()), carrier, setter); assertThat(carrier).containsEntry(B3Propagator.TRACE_ID_HEADER, TRACE_ID_BASE16); @@ -102,7 +101,7 @@ public void inject_SampledContext_nullCarrierUsage() { b3Propagator.inject( withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), - CurrentContext.get()), + Context.current()), null, new Setter>() { @Override @@ -121,7 +120,7 @@ public void inject_NotSampledContext() { b3Propagator.inject( withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), - CurrentContext.get()), + Context.current()), carrier, setter); assertThat(carrier).containsEntry(B3Propagator.TRACE_ID_HEADER, TRACE_ID_BASE16); @@ -136,7 +135,7 @@ public void extract_SampledContext_Int() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); carrier.put(B3Propagator.SAMPLED_HEADER, B3Propagator.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -149,7 +148,7 @@ public void extract_SampledContext_Bool() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); carrier.put(B3Propagator.SAMPLED_HEADER, "true"); - assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -162,7 +161,7 @@ public void extract_NotSampledContext() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); carrier.put(B3Propagator.SAMPLED_HEADER, B3Propagator.FALSE_INT); - assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -175,7 +174,7 @@ public void extract_SampledContext_Int_Short_TraceId() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); carrier.put(B3Propagator.SAMPLED_HEADER, B3Propagator.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -188,7 +187,7 @@ public void extract_SampledContext_Bool_Short_TraceId() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); carrier.put(B3Propagator.SAMPLED_HEADER, "true"); - assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -201,7 +200,7 @@ public void extract_NotSampledContext_Short_TraceId() { carrier.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); carrier.put(B3Propagator.SAMPLED_HEADER, B3Propagator.FALSE_INT); - assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -213,7 +212,7 @@ public void extract_InvalidTraceId() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, "abcdefghijklmnopabcdefghijklmnop"); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, B3Propagator.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -223,7 +222,7 @@ public void extract_InvalidTraceId_Size() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID_BASE16 + "00"); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_BASE16); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, B3Propagator.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -233,7 +232,7 @@ public void extract_InvalidSpanId() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID_BASE16); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, "abcdefghijklmnop"); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, B3Propagator.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -243,7 +242,7 @@ public void extract_InvalidSpanId_Size() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID_BASE16); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, "abcdefghijklmnop" + "00"); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, B3Propagator.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -253,7 +252,7 @@ public void inject_SampledContext_SingleHeader() { b3PropagatorSingleHeader.inject( withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), - CurrentContext.get()), + Context.current()), carrier, setter); @@ -268,7 +267,7 @@ public void inject_NotSampledContext_SingleHeader() { b3PropagatorSingleHeader.inject( withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), - CurrentContext.get()), + Context.current()), carrier, setter); @@ -284,8 +283,7 @@ public void extract_SampledContext_Int_SingleHeader() { B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + B3Propagator.TRUE_INT); - assertThat( - getSpanContext(b3PropagatorSingleHeader.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -298,8 +296,7 @@ public void extract_SampledContext_DebugFlag_SingleHeader() { B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + B3Propagator.TRUE_INT + "-" + "0"); - assertThat( - getSpanContext(b3PropagatorSingleHeader.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -311,8 +308,7 @@ public void extract_SampledContext_Bool_SingleHeader() { carrier.put( B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + "true"); - assertThat( - getSpanContext(b3PropagatorSingleHeader.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -325,8 +321,7 @@ public void extract_SampledContext_Bool_DebugFlag_SingleHeader() { B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + "true" + "-" + "0"); - assertThat( - getSpanContext(b3PropagatorSingleHeader.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -339,8 +334,7 @@ public void extract_NotSampledContext_SingleHeader() { B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + B3Propagator.FALSE_INT); - assertThat( - getSpanContext(b3PropagatorSingleHeader.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -353,8 +347,7 @@ public void extract_SampledContext_Int_SingleHeader_Short_TraceId() { B3Propagator.COMBINED_HEADER, SHORT_TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + B3Propagator.TRUE_INT); - assertThat( - getSpanContext(b3PropagatorSingleHeader.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -367,8 +360,7 @@ public void extract_SampledContext_DebugFlag_SingleHeader_Short_TraceId() { B3Propagator.COMBINED_HEADER, SHORT_TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + B3Propagator.TRUE_INT + "-" + "0"); - assertThat( - getSpanContext(b3PropagatorSingleHeader.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -380,8 +372,7 @@ public void extract_SampledContext_Bool_SingleHeader_Short_TraceId() { carrier.put( B3Propagator.COMBINED_HEADER, SHORT_TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + "true"); - assertThat( - getSpanContext(b3PropagatorSingleHeader.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -394,8 +385,7 @@ public void extract_SampledContext_Bool_DebugFlag_SingleHeader_Short_TraceId() { B3Propagator.COMBINED_HEADER, SHORT_TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + "true" + "-" + "0"); - assertThat( - getSpanContext(b3PropagatorSingleHeader.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -408,8 +398,7 @@ public void extract_NotSampledContext_SingleHeader_Short_TraceId() { B3Propagator.COMBINED_HEADER, SHORT_TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + B3Propagator.FALSE_INT); - assertThat( - getSpanContext(b3PropagatorSingleHeader.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(b3PropagatorSingleHeader.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -422,7 +411,7 @@ public void extract_Null_SingleHeader() { assertThat( getSpanContext( - b3PropagatorSingleHeader.extract(CurrentContext.get(), invalidHeaders, getter))) + b3PropagatorSingleHeader.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -433,7 +422,7 @@ public void extract_Empty_SingleHeader() { assertThat( getSpanContext( - b3PropagatorSingleHeader.extract(CurrentContext.get(), invalidHeaders, getter))) + b3PropagatorSingleHeader.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -446,7 +435,7 @@ public void extract_InvalidTraceId_SingleHeader() { assertThat( getSpanContext( - b3PropagatorSingleHeader.extract(CurrentContext.get(), invalidHeaders, getter))) + b3PropagatorSingleHeader.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -464,7 +453,7 @@ public void extract_InvalidTraceId_Size_SingleHeader() { assertThat( getSpanContext( - b3PropagatorSingleHeader.extract(CurrentContext.get(), invalidHeaders, getter))) + b3PropagatorSingleHeader.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -477,7 +466,7 @@ public void extract_InvalidSpanId_SingleHeader() { assertThat( getSpanContext( - b3PropagatorSingleHeader.extract(CurrentContext.get(), invalidHeaders, getter))) + b3PropagatorSingleHeader.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -488,7 +477,7 @@ public void extract_InvalidSpanId_Size_SingleHeader() { B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16 + "-" + "abcdefghijklmnop" + "00" + "-" + B3Propagator.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -497,7 +486,7 @@ public void extract_TooFewParts_SingleHeader() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16); - assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -508,7 +497,7 @@ public void extract_TooManyParts_SingleHeader() { B3Propagator.COMBINED_HEADER, TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-" + B3Propagator.TRUE_INT + "-extra"); - assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -529,7 +518,7 @@ public void headerNames() { @Test public void extract_emptyCarrier() { Map emptyHeaders = new HashMap<>(); - assertThat(getSpanContext(b3Propagator.extract(CurrentContext.get(), emptyHeaders, getter))) + assertThat(getSpanContext(b3Propagator.extract(Context.current(), emptyHeaders, getter))) .isEqualTo(SpanContext.getInvalid()); } } diff --git a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java index 8b612268b38..4392ea55e18 100644 --- a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java +++ b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java @@ -21,12 +21,11 @@ import static io.opentelemetry.contrib.trace.propagation.JaegerPropagator.PROPAGATION_HEADER; import static io.opentelemetry.contrib.trace.propagation.JaegerPropagator.PROPAGATION_HEADER_DELIMITER; +import io.grpc.Context; import io.jaegertracing.internal.JaegerSpanContext; import io.jaegertracing.internal.propagation.TextMapCodec; -import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; -import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanContext; @@ -85,11 +84,11 @@ public String get(Map carrier, String key) { @Rule public ExpectedException thrown = ExpectedException.none(); private static SpanContext getSpanContext(Context context) { - return context.get(Span.KEY).getContext(); + return Span.KEY.get(context).getContext(); } private static Context withSpanContext(SpanContext spanContext, Context context) { - return context.put(Span.KEY, DefaultSpan.create(spanContext)); + return context.withValue(Span.KEY, DefaultSpan.create(spanContext)); } @Test @@ -98,7 +97,7 @@ public void inject_SampledContext() { jaegerPropagator.inject( withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), - CurrentContext.get()), + Context.current()), carrier, setter); @@ -116,7 +115,7 @@ public void inject_SampledContext_nullCarrierUsage() { jaegerPropagator.inject( withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT), - CurrentContext.get()), + Context.current()), null, new Setter>() { @Override @@ -138,7 +137,7 @@ public void inject_NotSampledContext() { jaegerPropagator.inject( withSpanContext( SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT), - CurrentContext.get()), + Context.current()), carrier, setter); assertThat(carrier) @@ -153,8 +152,7 @@ public void extract_EmptyHeaderValue() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(PROPAGATION_HEADER, ""); - assertThat( - getSpanContext(jaegerPropagator.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -163,8 +161,7 @@ public void extract_NotEnoughParts() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(PROPAGATION_HEADER, "aa:bb:cc"); - assertThat( - getSpanContext(jaegerPropagator.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -173,8 +170,7 @@ public void extract_TooManyParts() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(PROPAGATION_HEADER, "aa:bb:cc:dd:ee"); - assertThat( - getSpanContext(jaegerPropagator.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -186,8 +182,7 @@ public void extract_InvalidTraceId() { generateTraceIdHeaderValue( "abcdefghijklmnopabcdefghijklmnop", SPAN_ID_BASE16, DEPRECATED_PARENT_SPAN, "0")); - assertThat( - getSpanContext(jaegerPropagator.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -199,8 +194,7 @@ public void extract_InvalidTraceId_Size() { generateTraceIdHeaderValue( TRACE_ID_BASE16 + "00", SPAN_ID_BASE16, DEPRECATED_PARENT_SPAN, "0")); - assertThat( - getSpanContext(jaegerPropagator.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -212,8 +206,7 @@ public void extract_InvalidSpanId() { generateTraceIdHeaderValue( TRACE_ID_BASE16, "abcdefghijklmnop", DEPRECATED_PARENT_SPAN, "0")); - assertThat( - getSpanContext(jaegerPropagator.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -225,8 +218,7 @@ public void extract_InvalidSpanId_Size() { generateTraceIdHeaderValue( TRACE_ID_BASE16, SPAN_ID_BASE16 + "00", DEPRECATED_PARENT_SPAN, "0")); - assertThat( - getSpanContext(jaegerPropagator.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -237,8 +229,7 @@ public void extract_InvalidFlags() { PROPAGATION_HEADER, generateTraceIdHeaderValue(TRACE_ID_BASE16, SPAN_ID_BASE16, DEPRECATED_PARENT_SPAN, "")); - assertThat( - getSpanContext(jaegerPropagator.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -250,8 +241,7 @@ public void extract_InvalidFlags_Size() { generateTraceIdHeaderValue( TRACE_ID_BASE16, SPAN_ID_BASE16, DEPRECATED_PARENT_SPAN, "10220")); - assertThat( - getSpanContext(jaegerPropagator.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -263,8 +253,7 @@ public void extract_InvalidFlags_NonNumeric() { generateTraceIdHeaderValue( TRACE_ID_BASE16, SPAN_ID_BASE16, DEPRECATED_PARENT_SPAN, "abcdefr")); - assertThat( - getSpanContext(jaegerPropagator.extract(CurrentContext.get(), invalidHeaders, getter))) + assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) .isSameInstanceAs(SpanContext.getInvalid()); } @@ -276,7 +265,7 @@ public void extract_SampledContext() { TRACE_ID_HI, TRACE_ID_LOW, SPAN_ID_LONG, DEPRECATED_PARENT_SPAN_LONG, (byte) 5); carrier.put(PROPAGATION_HEADER, TextMapCodec.contextAsString(context)); - assertThat(getSpanContext(jaegerPropagator.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -290,7 +279,7 @@ public void extract_NotSampledContext() { TRACE_ID_HI, TRACE_ID_LOW, SPAN_ID_LONG, DEPRECATED_PARENT_SPAN_LONG, (byte) 0); carrier.put(PROPAGATION_HEADER, TextMapCodec.contextAsString(context)); - assertThat(getSpanContext(jaegerPropagator.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACE_STATE_DEFAULT)); @@ -308,7 +297,7 @@ public void extract_SampledContext_Short_TraceId() { (byte) 1); carrier.put(PROPAGATION_HEADER, TextMapCodec.contextAsString(context)); - assertThat(getSpanContext(jaegerPropagator.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( SHORT_TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); @@ -323,7 +312,7 @@ public void extract_UrlEncodedContext() throws UnsupportedEncodingException { carrier.put( PROPAGATION_HEADER, URLEncoder.encode(TextMapCodec.contextAsString(context), "UTF-8")); - assertThat(getSpanContext(jaegerPropagator.extract(CurrentContext.get(), carrier, getter))) + assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), carrier, getter))) .isEqualTo( SpanContext.createFromRemoteParent( TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACE_STATE_DEFAULT)); diff --git a/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java b/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java index 02bcc4f47be..39036d69362 100644 --- a/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java +++ b/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java @@ -22,10 +22,7 @@ import io.opentelemetry.trace.Status; import java.util.concurrent.Callable; -/** - * Util methods/functionality to interact with the {@link Span} in the {@link - * io.opentelemetry.context.Context}. - */ +/** Util methods/functionality to interact with the {@link Span} in the {@link io.grpc.Context}. */ public final class CurrentSpanUtils { // No instance of this class. private CurrentSpanUtils() {} diff --git a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClient.java b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClient.java index 6402399194b..243a73514de 100644 --- a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClient.java +++ b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClient.java @@ -134,7 +134,7 @@ public ClientCall interceptCall( @Override public void start(Listener responseListener, Metadata headers) { // Inject the request with the current context - textFormat.inject(CurrentContext.get(), headers, setter); + textFormat.inject(Context.current(), headers, setter); // Perform the gRPC request super.start(responseListener, headers); } diff --git a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClientStream.java b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClientStream.java index 1eb05073fca..40aca2edfbc 100644 --- a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClientStream.java +++ b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClientStream.java @@ -176,7 +176,7 @@ public ClientCall interceptCall( @Override public void start(Listener responseListener, Metadata headers) { // Inject the request with the current context - textFormat.inject(CurrentContext.get(), headers, setter); + textFormat.inject(Context.current(), headers, setter); // Perform the gRPC request super.start(responseListener, headers); } diff --git a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldServer.java b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldServer.java index 8113d037277..554300b9de5 100644 --- a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldServer.java +++ b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldServer.java @@ -161,7 +161,7 @@ private class OpenTelemetryServerInterceptor implements io.grpc.ServerIntercepto public ServerCall.Listener interceptCall( ServerCall call, Metadata headers, ServerCallHandler next) { // Extract the Span Context from the metadata of the gRPC request - Context extractedContext = textFormat.extract(CurrentContext.get(), headers, getter); + Context extractedContext = textFormat.extract(Context.current(), headers, getter); InetSocketAddress clientInfo = (InetSocketAddress) call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR); // Build a span based on the received context @@ -178,7 +178,7 @@ public ServerCall.Listener interceptCall( // Process the gRPC call normally try { span.setStatus(Status.OK); - return Contexts.interceptCall(CurrentContext.get(), call, headers, next); + return Contexts.interceptCall(Context.current(), call, headers, next); } finally { span.end(); } diff --git a/examples/http/src/main/java/io/opentelemetry/example/http/HttpClient.java b/examples/http/src/main/java/io/opentelemetry/example/http/HttpClient.java index eafc1e060b5..20539b91773 100644 --- a/examples/http/src/main/java/io/opentelemetry/example/http/HttpClient.java +++ b/examples/http/src/main/java/io/opentelemetry/example/http/HttpClient.java @@ -16,8 +16,8 @@ package io.opentelemetry.example.http; +import io.grpc.Context; import io.opentelemetry.OpenTelemetry; -import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.currentcontext.Scope; @@ -87,7 +87,7 @@ private HttpClient() throws Exception { span.setAttribute("http.url", url.toString()); // Inject the request with the current Context/Span. - OpenTelemetry.getPropagators().getHttpTextFormat().inject(CurrentContext.get(), con, setter); + OpenTelemetry.getPropagators().getHttpTextFormat().inject(Context.current(), con, setter); try { // Process the request diff --git a/examples/http/src/main/java/io/opentelemetry/example/http/HttpServer.java b/examples/http/src/main/java/io/opentelemetry/example/http/HttpServer.java index 891057aaf3f..f9c77d1d5ad 100644 --- a/examples/http/src/main/java/io/opentelemetry/example/http/HttpServer.java +++ b/examples/http/src/main/java/io/opentelemetry/example/http/HttpServer.java @@ -48,7 +48,7 @@ public void handle(HttpExchange he) throws IOException { // Extract the context from the HTTP request Context ctx = - OpenTelemetry.getPropagators().getHttpTextFormat().extract(CurrentContext.get(), he, getter); + OpenTelemetry.getPropagators().getHttpTextFormat().extract(Context.current(), he, getter); try (Scope scope = CurrentContext.withContext(ctx)) { // Build a span automatically using the received context span = spanBuilder.startSpan(); diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java index b62b380546f..7fb39426808 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java @@ -16,10 +16,9 @@ package io.opentelemetry.opentracingshim; -import io.opentelemetry.context.Context; +import io.grpc.Context; import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.correlationcontext.CorrelationContext; -import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; import io.opentracing.propagation.TextMapExtract; @@ -35,8 +34,8 @@ final class Propagation extends BaseShimObject { public void injectTextFormat(SpanContextShim contextShim, TextMapInject carrier) { Context context = - CurrentContext.get().put(Span.KEY, DefaultSpan.create(contextShim.getSpanContext())); - context = context.put(CorrelationContext.KEY, contextShim.getCorrelationContext()); + Context.current().withValue(Span.KEY, DefaultSpan.create(contextShim.getSpanContext())); + context = context.withValue(CorrelationContext.KEY, contextShim.getCorrelationContext()); propagators().getHttpTextFormat().inject(context, carrier, TextMapSetter.INSTANCE); } @@ -51,15 +50,15 @@ public SpanContextShim extractTextFormat(TextMapExtract carrier) { Context context = propagators() .getHttpTextFormat() - .extract(CurrentContext.get(), carrierMap, TextMapGetter.INSTANCE); + .extract(Context.current(), carrierMap, TextMapGetter.INSTANCE); - io.opentelemetry.trace.Span span = context.get(Span.KEY); + io.opentelemetry.trace.Span span = Span.KEY.get(context); if (!span.getContext().isValid()) { return null; } return new SpanContextShim( - telemetryInfo, span.getContext(), context.get(CorrelationContext.KEY)); + telemetryInfo, span.getContext(), CorrelationContext.KEY.get(context)); } static final class TextMapSetter implements HttpTextFormat.Setter { diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java index 23a76490cb6..ed3bbe69d44 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java @@ -16,6 +16,7 @@ package io.opentelemetry.sdk.contrib.trace.testbed.clientserver; +import io.grpc.Context; import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; import io.opentelemetry.currentcontext.CurrentContext; @@ -45,7 +46,7 @@ public void send() throws InterruptedException { OpenTelemetry.getPropagators() .getHttpTextFormat() .inject( - CurrentContext.get(), + Context.current(), message, new Setter() { @Override diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java index a3d47106e5a..36b9115e2ef 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java @@ -16,8 +16,8 @@ package io.opentelemetry.sdk.contrib.trace.testbed.clientserver; +import io.grpc.Context; import io.opentelemetry.OpenTelemetry; -import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat.Getter; import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.currentcontext.Scope; @@ -43,7 +43,7 @@ private void process(Message message) { OpenTelemetry.getPropagators() .getHttpTextFormat() .extract( - CurrentContext.get(), + Context.current(), message, new Getter() { @Nullable @@ -52,7 +52,7 @@ public String get(Message carrier, String key) { return carrier.get(key); } }); - SpanContext spanContext = context.get(Span.KEY).getContext(); + SpanContext spanContext = Span.KEY.get(context).getContext(); Span span = tracer.spanBuilder("receive").setSpanKind(Kind.SERVER).setParent(spanContext).startSpan(); span.setAttribute("component", "example-server"); From 1bfcf9accee55bafe6ba7d4236f8420b15c0c8f3 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 8 May 2020 19:54:29 -0700 Subject: [PATCH 16/22] CurrentContext.get() is no longer really needed --- .../java/io/opentelemetry/currentcontext/CurrentContext.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java b/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java index 48b8e41355e..7344bcd5679 100644 --- a/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java +++ b/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java @@ -53,7 +53,10 @@ public static CorrelationContext getCorrelationContext() { } /** Returns the context bound to the current thread. */ - public static Context get() { + // TODO (trask) this method is not needed currently, in favor of using Context.current() directly, + // but if we move to a Context object that doesn't have built-in thread-binding, then this is + // the only additional method we would need + private static Context get() { return Context.current(); } From 323de172a5249526aec6f0d923e53fbb84c6335a Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 8 May 2020 19:58:17 -0700 Subject: [PATCH 17/22] Forgot to fully uncomment test --- .../context/propagation/DefaultPropagatorsTest.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/context_prop/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java b/context_prop/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java index fad99ea543c..280a5f39383 100644 --- a/context_prop/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java +++ b/context_prop/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java @@ -27,9 +27,11 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; /** Unit tests for {@link DefaultContextPropagators}. */ -// @RunWith(JUnit4.class) +@RunWith(JUnit4.class) public class DefaultPropagatorsTest { @Rule public final ExpectedException thrown = ExpectedException.none(); @@ -92,7 +94,7 @@ public void noopPropagator() { propagators.getHttpTextFormat().inject(context, map, MapSetter.INSTANCE); assertThat(map).isEmpty(); - Truth.assertThat(propagators.getHttpTextFormat().extract(context, map, MapGetter.INSTANCE)) + assertThat(propagators.getHttpTextFormat().extract(context, map, MapGetter.INSTANCE)) .isSameInstanceAs(context); } From f78092086c665ad72aab8b195e374d9169acc3f6 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 8 May 2020 20:21:23 -0700 Subject: [PATCH 18/22] Small simplification --- .../java/io/opentelemetry/opentracingshim/Propagation.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java index 7fb39426808..1ddbfb70e83 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java @@ -34,8 +34,9 @@ final class Propagation extends BaseShimObject { public void injectTextFormat(SpanContextShim contextShim, TextMapInject carrier) { Context context = - Context.current().withValue(Span.KEY, DefaultSpan.create(contextShim.getSpanContext())); - context = context.withValue(CorrelationContext.KEY, contextShim.getCorrelationContext()); + Context.current() + .withValue(Span.KEY, DefaultSpan.create(contextShim.getSpanContext())) + .withValue(CorrelationContext.KEY, contextShim.getCorrelationContext()); propagators().getHttpTextFormat().inject(context, carrier, TextMapSetter.INSTANCE); } From 8fa85faaab66c48c4453f82bf90910b826a383fc Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 8 May 2020 20:24:01 -0700 Subject: [PATCH 19/22] Run google format --- .../context/propagation/DefaultPropagatorsTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/context_prop/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java b/context_prop/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java index 280a5f39383..e3a266157ff 100644 --- a/context_prop/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java +++ b/context_prop/src/test/java/io/opentelemetry/context/propagation/DefaultPropagatorsTest.java @@ -18,7 +18,6 @@ import static com.google.common.truth.Truth.assertThat; -import com.google.common.truth.Truth; import io.grpc.Context; import java.util.Collections; import java.util.HashMap; From 670b7bbe7720d57f99b215a1ccbab8d9763c2253 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Sun, 10 May 2020 20:50:21 -0700 Subject: [PATCH 20/22] Make context keys private --- .../HttpTraceContextInjectBenchmark.java | 2 +- .../CorrelationContext.java | 20 +++++++++++++++---- .../currentcontext/CurrentContext.java | 14 ++++++------- .../java/io/opentelemetry/trace/Span.java | 19 +++++++++++++++--- .../trace/propagation/HttpTraceContext.java | 4 ++-- .../propagation/HttpTraceContextTest.java | 4 ++-- .../PropagatorContextExtractBenchmark.java | 2 +- .../PropagatorContextInjectBenchmark.java | 2 +- .../propagation/B3PropagatorExtractor.java | 2 +- .../propagation/B3PropagatorInjector.java | 2 +- .../trace/propagation/JaegerPropagator.java | 4 ++-- .../trace/propagation/B3PropagatorTest.java | 4 ++-- .../propagation/JaegerPropagatorTest.java | 4 ++-- .../opentracingshim/Propagation.java | 9 ++++----- .../trace/testbed/clientserver/Server.java | 2 +- 15 files changed, 59 insertions(+), 35 deletions(-) diff --git a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java index a8647fce998..4f620e0ec5e 100644 --- a/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java +++ b/api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java @@ -93,7 +93,7 @@ private static SpanContext createTestSpanContext(String traceId, String spanId) private static List createContexts(List spanContexts) { List contexts = new ArrayList<>(); for (SpanContext context : spanContexts) { - contexts.add(Context.ROOT.withValue(Span.KEY, DefaultSpan.create(context))); + contexts.add(Span.Key.put(DefaultSpan.create(context), Context.ROOT)); } return contexts; } diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java index e685b8f05c2..46d8a28bad6 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java @@ -34,10 +34,6 @@ @Immutable public interface CorrelationContext { - // TODO (trask) javadoc - Context.Key KEY = - Context.keyWithDefault("CorrelationContext", EmptyCorrelationContext.getInstance()); - /** * Returns an immutable collection of the entries in this {@code CorrelationContext}. Order of * entries is not guaranteed. @@ -57,6 +53,22 @@ public interface CorrelationContext { @Nullable EntryValue getEntryValue(EntryKey entryKey); + // TODO (trask) javadoc class and methods + class Key { + private static final Context.Key KEY = + Context.keyWithDefault("CorrelationContext", EmptyCorrelationContext.getInstance()); + + public static CorrelationContext get(Context context) { + return KEY.get(context); + } + + public static Context put(CorrelationContext correlationContext, Context context) { + return context.withValue(KEY, correlationContext); + } + + private Key() {} + } + /** * Builder for the {@link CorrelationContext} class. * diff --git a/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java b/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java index 7344bcd5679..7cea85144aa 100644 --- a/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java +++ b/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java @@ -27,12 +27,12 @@ public class CurrentContext { @MustBeClosed public static Scope withSpan(Span span) { - return withContext(get().withValue(Span.KEY, span)); + return withContext(Span.Key.put(span, Context.current())); } @MustBeClosed public static Scope withCorrelationContext(CorrelationContext correlationContext) { - return withContext(get().withValue(CorrelationContext.KEY, correlationContext)); + return withContext(CorrelationContext.Key.put(correlationContext, Context.current())); } /** @@ -45,20 +45,20 @@ public static Scope withContext(Context context) { } public static Span getSpan() { - return Span.KEY.get(); + return Span.Key.get(Context.current()); } public static CorrelationContext getCorrelationContext() { - return CorrelationContext.KEY.get(); + return CorrelationContext.Key.get(Context.current()); } /** Returns the context bound to the current thread. */ // TODO (trask) this method is not needed currently, in favor of using Context.current() directly, // but if we move to a Context object that doesn't have built-in thread-binding, then this is // the only additional method we would need - private static Context get() { - return Context.current(); - } + // private static Context get() { + // return Context.current(); + // } private CurrentContext() {} diff --git a/api/src/main/java/io/opentelemetry/trace/Span.java b/api/src/main/java/io/opentelemetry/trace/Span.java index 44277775164..4d0ee1ef04d 100644 --- a/api/src/main/java/io/opentelemetry/trace/Span.java +++ b/api/src/main/java/io/opentelemetry/trace/Span.java @@ -34,9 +34,6 @@ @ThreadSafe public interface Span { - // TODO (trask) javadoc - Context.Key KEY = Context.keyWithDefault("Span", DefaultSpan.getInvalid()); - /** * Type of span. Can be used to specify additional relationships between spans in addition to a * parent/child relationship. @@ -274,6 +271,22 @@ enum Kind { */ boolean isRecording(); + // TODO (trask) javadoc class and methods + class Key { + private static final Context.Key KEY = + Context.keyWithDefault("Span", DefaultSpan.getInvalid()); + + public static Span get(Context context) { + return KEY.get(context); + } + + public static Context put(Span span, Context context) { + return context.withValue(KEY, span); + } + + private Key() {} + } + /** * {@link Builder} is used to construct {@link Span} instances which define arbitrary scopes of * code that are sampled for distributed tracing as a single atomic unit. diff --git a/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java b/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java index dbfbfd972ac..332529990a4 100644 --- a/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java +++ b/api/src/main/java/io/opentelemetry/trace/propagation/HttpTraceContext.java @@ -79,7 +79,7 @@ public void inject(Context context, C carrier, Setter setter) { checkNotNull(context, "context"); checkNotNull(setter, "setter"); - Span span = Span.KEY.get(context); + Span span = Span.Key.get(context); if (!span.getContext().isValid()) { return; } @@ -124,7 +124,7 @@ private static void injectImpl(SpanContext spanContext, C carrier, Setter checkNotNull(getter, "getter"); SpanContext spanContext = extractImpl(carrier, getter); - return context.withValue(Span.KEY, DefaultSpan.create(spanContext)); + return Span.Key.put(DefaultSpan.create(spanContext), context); } private static SpanContext extractImpl(C carrier, Getter getter) { diff --git a/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java b/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java index 089c3100968..9bc03d00c58 100644 --- a/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java +++ b/api/src/test/java/io/opentelemetry/trace/propagation/HttpTraceContextTest.java @@ -81,11 +81,11 @@ public String get(Map carrier, String key) { @Rule public ExpectedException thrown = ExpectedException.none(); private static SpanContext getSpanContext(Context context) { - return Span.KEY.get(context).getContext(); + return Span.Key.get(context).getContext(); } private static Context withSpanContext(SpanContext spanContext, Context context) { - return context.withValue(Span.KEY, DefaultSpan.create(spanContext)); + return Span.Key.put(DefaultSpan.create(spanContext), context); } @Test diff --git a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java index 38a7ae67c82..956e378970b 100644 --- a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java +++ b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextExtractBenchmark.java @@ -64,7 +64,7 @@ public void setup() { @BenchmarkMode(Mode.AverageTime) @Fork(1) public Span measureExtract() { - return Span.KEY.get(doExtract()); + return Span.Key.get(doExtract()); } protected abstract Context doExtract(); diff --git a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java index 22d50569659..bb24628809e 100644 --- a/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java +++ b/contrib/trace_propagators/src/jmh/java/io/opentelemetry/contrib/trace/propagation/PropagatorContextInjectBenchmark.java @@ -74,7 +74,7 @@ public abstract static class AbstractContextInjectBenchmark { @BenchmarkMode(Mode.AverageTime) @Fork(1) public Map measureInject() { - Context context = Context.current().withValue(Span.KEY, DefaultSpan.create(contextToTest)); + Context context = Span.Key.put(DefaultSpan.create(contextToTest), Context.current()); doInject(context, carrier); return carrier; } diff --git a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorExtractor.java b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorExtractor.java index 3babfa61cab..e0d24eae954 100644 --- a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorExtractor.java +++ b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorExtractor.java @@ -65,7 +65,7 @@ Context extract(Context context, C carrier, HttpTextFormat.Getter getter) spanContext = getSpanContextFromMultipleHeaders(carrier, getter); } - return context.withValue(Span.KEY, DefaultSpan.create(spanContext)); + return Span.Key.put(DefaultSpan.create(spanContext), context); } @SuppressWarnings("StringSplitter") diff --git a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java index 46bea76218c..9dff00bd3bd 100644 --- a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java +++ b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorInjector.java @@ -46,7 +46,7 @@ void inject(Context context, C carrier, HttpTextFormat.Setter setter) { Objects.requireNonNull(context, "context"); Objects.requireNonNull(setter, "setter"); - Span span = Span.KEY.get(context); + Span span = Span.Key.get(context); if (!span.getContext().isValid()) { return; } diff --git a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java index 9ddf310fee1..35d0322e121 100644 --- a/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java +++ b/contrib/trace_propagators/src/main/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagator.java @@ -87,7 +87,7 @@ public void inject(Context context, C carrier, Setter setter) { checkNotNull(context, "context"); checkNotNull(setter, "setter"); - Span span = Span.KEY.get(context); + Span span = Span.Key.get(context); if (!span.getContext().isValid()) { return; } @@ -112,7 +112,7 @@ public Context extract(Context context, C carrier, Getter getter) { SpanContext spanContext = getSpanContextFromHeader(carrier, getter); - return context.withValue(Span.KEY, DefaultSpan.create(spanContext)); + return Span.Key.put(DefaultSpan.create(spanContext), context); } @SuppressWarnings("StringSplitter") diff --git a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java index 22db68e46fa..bc80f906b18 100644 --- a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java +++ b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/B3PropagatorTest.java @@ -74,11 +74,11 @@ public String get(Map carrier, String key) { @Rule public ExpectedException thrown = ExpectedException.none(); private static SpanContext getSpanContext(Context context) { - return Span.KEY.get(context).getContext(); + return Span.Key.get(context).getContext(); } private static Context withSpanContext(SpanContext spanContext, Context context) { - return context.withValue(Span.KEY, DefaultSpan.create(spanContext)); + return Span.Key.put(DefaultSpan.create(spanContext), context); } @Test diff --git a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java index 4392ea55e18..1c83517e14b 100644 --- a/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java +++ b/contrib/trace_propagators/src/test/java/io/opentelemetry/contrib/trace/propagation/JaegerPropagatorTest.java @@ -84,11 +84,11 @@ public String get(Map carrier, String key) { @Rule public ExpectedException thrown = ExpectedException.none(); private static SpanContext getSpanContext(Context context) { - return Span.KEY.get(context).getContext(); + return Span.Key.get(context).getContext(); } private static Context withSpanContext(SpanContext spanContext, Context context) { - return context.withValue(Span.KEY, DefaultSpan.create(spanContext)); + return Span.Key.put(DefaultSpan.create(spanContext), context); } @Test diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java index 1ddbfb70e83..3879b1a42c3 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/Propagation.java @@ -34,9 +34,8 @@ final class Propagation extends BaseShimObject { public void injectTextFormat(SpanContextShim contextShim, TextMapInject carrier) { Context context = - Context.current() - .withValue(Span.KEY, DefaultSpan.create(contextShim.getSpanContext())) - .withValue(CorrelationContext.KEY, contextShim.getCorrelationContext()); + Span.Key.put(DefaultSpan.create(contextShim.getSpanContext()), Context.current()); + context = CorrelationContext.Key.put(contextShim.getCorrelationContext(), context); propagators().getHttpTextFormat().inject(context, carrier, TextMapSetter.INSTANCE); } @@ -53,13 +52,13 @@ public SpanContextShim extractTextFormat(TextMapExtract carrier) { .getHttpTextFormat() .extract(Context.current(), carrierMap, TextMapGetter.INSTANCE); - io.opentelemetry.trace.Span span = Span.KEY.get(context); + io.opentelemetry.trace.Span span = Span.Key.get(context); if (!span.getContext().isValid()) { return null; } return new SpanContextShim( - telemetryInfo, span.getContext(), CorrelationContext.KEY.get(context)); + telemetryInfo, span.getContext(), CorrelationContext.Key.get(context)); } static final class TextMapSetter implements HttpTextFormat.Setter { diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java index 36b9115e2ef..28e2c54c5ba 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java @@ -52,7 +52,7 @@ public String get(Message carrier, String key) { return carrier.get(key); } }); - SpanContext spanContext = Span.KEY.get(context).getContext(); + SpanContext spanContext = Span.Key.get(context).getContext(); Span span = tracer.spanBuilder("receive").setSpanKind(Kind.SERVER).setParent(spanContext).startSpan(); span.setAttribute("component", "example-server"); From 9032bc75fffa09a48c1036824ecf19edeba2b461 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Thu, 14 May 2020 16:42:33 -0700 Subject: [PATCH 21/22] Replace CurrentContext statics with ScopeManager --- .../trace/DefaultTracerBenchmarks.java | 10 ++-- .../CorrelationContext.java | 8 +-- .../DefaultScopeManager.java} | 34 ++++++----- .../{currentcontext => scope}/Scope.java | 2 +- .../io/opentelemetry/scope/ScopeManager.java | 49 ++++++++++++++++ .../io/opentelemetry/trace/DefaultSpan.java | 4 +- .../io/opentelemetry/trace/DefaultTracer.java | 18 ++++-- .../DefaultCorrelationContextManagerTest.java | 28 +++++----- .../trace/DefaultTracerTest.java | 32 ++++++----- .../contrib/trace/CurrentSpanUtils.java | 34 +++++++---- .../contrib/trace/CurrentSpanUtilsTest.java | 21 ++++--- .../example/HelloWorldClient.java | 4 +- .../example/HelloWorldClientStream.java | 4 +- .../example/HelloWorldServer.java | 4 +- .../example/http/HttpClient.java | 4 +- .../example/http/HttpServer.java | 4 +- .../opentracingshim/ScopeManagerShim.java | 13 +++-- .../opentracingshim/ScopeShim.java | 4 +- .../OpenTelemetryInteroperabilityTest.java | 10 ++-- .../CorrelationContextManagerSdk.java | 7 ++- .../CorrelationContextSdk.java | 8 ++- .../sdk/trace/SpanBuilderSdk.java | 16 +++--- .../io/opentelemetry/sdk/trace/TracerSdk.java | 7 ++- .../CorrelationContextManagerSdkTest.java | 21 ++++--- .../CorrelationContextSdkTest.java | 9 ++- .../CorrelationContextTestUtil.java | 4 +- .../ScopedCorrelationContextTest.java | 56 ++++++++++--------- .../sdk/trace/SpanBuilderSdkTest.java | 12 ++-- .../sdk/trace/TracerSdkTest.java | 40 ++++++------- .../ActiveSpanReplacementTest.java | 16 +++--- .../trace/testbed/actorpropagation/Actor.java | 15 +++-- .../ActorPropagationTest.java | 14 +++-- .../trace/testbed/clientserver/Client.java | 10 +++- .../trace/testbed/clientserver/Server.java | 11 ++-- .../clientserver/TestClientServerTest.java | 6 +- .../HandlerTest.java | 12 ++-- .../errorreporting/ErrorReportingTest.java | 33 ++++++----- .../latespanfinish/LateSpanFinishTest.java | 16 +++--- .../listenerperrequest/ListenerTest.java | 6 +- .../testbed/multiplecallbacks/Client.java | 11 ++-- .../MultipleCallbacksTest.java | 10 ++-- .../nestedcallbacks/NestedCallbacksTest.java | 14 +++-- .../testbed/promisepropagation/Promise.java | 13 +++-- .../PromisePropagationTest.java | 12 ++-- .../RequestHandler.java | 11 ++-- .../SuspendResume.java | 11 ++-- 46 files changed, 424 insertions(+), 264 deletions(-) rename api/src/main/java/io/opentelemetry/{currentcontext/CurrentContext.java => scope/DefaultScopeManager.java} (76%) rename api/src/main/java/io/opentelemetry/{currentcontext => scope}/Scope.java (96%) create mode 100644 api/src/main/java/io/opentelemetry/scope/ScopeManager.java diff --git a/api/src/jmh/java/io/opentelemetry/trace/DefaultTracerBenchmarks.java b/api/src/jmh/java/io/opentelemetry/trace/DefaultTracerBenchmarks.java index 14960548b9a..930c954e44b 100644 --- a/api/src/jmh/java/io/opentelemetry/trace/DefaultTracerBenchmarks.java +++ b/api/src/jmh/java/io/opentelemetry/trace/DefaultTracerBenchmarks.java @@ -16,7 +16,8 @@ package io.opentelemetry.trace; -import io.opentelemetry.currentcontext.CurrentContext; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.ScopeManager; import java.util.concurrent.TimeUnit; import javax.annotation.Nullable; import org.openjdk.jmh.annotations.Benchmark; @@ -35,6 +36,7 @@ public class DefaultTracerBenchmarks { private final Tracer tracer = DefaultTracer.getInstance(); + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); @Nullable private Span span = null; /** Benchmark the full span lifecycle. */ @@ -46,7 +48,7 @@ public class DefaultTracerBenchmarks { @Warmup(iterations = 5, time = 1) public void measureFullSpanLifecycle() { span = tracer.spanBuilder("span").startSpan(); - io.opentelemetry.currentcontext.Scope ignored = CurrentContext.withSpan(span); + io.opentelemetry.scope.Scope ignored = scopeManager.withSpan(span); try { // no-op } finally { @@ -73,7 +75,7 @@ public void measureSpanBuilding() { @OutputTimeUnit(TimeUnit.NANOSECONDS) @Warmup(iterations = 5, time = 1) public void measureScopeLifecycle() { - io.opentelemetry.currentcontext.Scope ignored = CurrentContext.withSpan(span); + io.opentelemetry.scope.Scope ignored = scopeManager.withSpan(span); try { // no-op } finally { @@ -88,7 +90,7 @@ public void measureScopeLifecycle() { @OutputTimeUnit(TimeUnit.NANOSECONDS) @Warmup(iterations = 5, time = 1) public void measureGetCurrentSpan() { - CurrentContext.getSpan(); + scopeManager.getSpan(); } @TearDown(Level.Iteration) diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java index 46d8a28bad6..691467ef1e2 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/CorrelationContext.java @@ -17,7 +17,7 @@ package io.opentelemetry.correlationcontext; import io.grpc.Context; -import io.opentelemetry.currentcontext.CurrentContext; +import io.opentelemetry.scope.ScopeManager; import java.util.Collection; import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; @@ -77,8 +77,8 @@ private Key() {} interface Builder { /** * Sets the parent {@link CorrelationContext} to use. If no parent is provided, the value of - * {@link CurrentContext#getCorrelationContext()} at {@link #build()} time will be used as - * parent, unless {@link #setNoParent()} was called. + * {@link ScopeManager#getCorrelationContext()} at {@link #build()} time will be used as parent, + * unless {@link #setNoParent()} was called. * *

This must be used to create a {@link CorrelationContext} when manual Context * propagation is used. @@ -96,7 +96,7 @@ interface Builder { /** * Sets the option to become a root {@link CorrelationContext} with no parent. If not * called, the value provided using {@link #setParent(CorrelationContext)} or otherwise {@link - * CurrentContext#getCorrelationContext()} at {@link #build()} time will be used as parent. + * ScopeManager#getCorrelationContext()} at {@link #build()} time will be used as parent. * * @return this. * @since 0.1.0 diff --git a/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java b/api/src/main/java/io/opentelemetry/scope/DefaultScopeManager.java similarity index 76% rename from api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java rename to api/src/main/java/io/opentelemetry/scope/DefaultScopeManager.java index 7cea85144aa..04c28b21e8b 100644 --- a/api/src/main/java/io/opentelemetry/currentcontext/CurrentContext.java +++ b/api/src/main/java/io/opentelemetry/scope/DefaultScopeManager.java @@ -14,24 +14,30 @@ * limitations under the License. */ -package io.opentelemetry.currentcontext; +package io.opentelemetry.scope; -import com.google.errorprone.annotations.MustBeClosed; import io.grpc.Context; import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.trace.Span; /** Static methods for interacting with the current (thread-bound) context. */ // TODO (trask) javadoc class and methods -public class CurrentContext { +public class DefaultScopeManager implements ScopeManager { + private static final DefaultScopeManager INSTANCE = new DefaultScopeManager(); - @MustBeClosed - public static Scope withSpan(Span span) { + public static ScopeManager getInstance() { + return INSTANCE; + } + + private DefaultScopeManager() {} + + @Override + public Scope withSpan(Span span) { return withContext(Span.Key.put(span, Context.current())); } - @MustBeClosed - public static Scope withCorrelationContext(CorrelationContext correlationContext) { + @Override + public Scope withCorrelationContext(CorrelationContext correlationContext) { return withContext(CorrelationContext.Key.put(correlationContext, Context.current())); } @@ -39,16 +45,18 @@ public static Scope withCorrelationContext(CorrelationContext correlationContext * Binds the context to the current thread and returns a scope that must be used to unbind the * context from the current thread and restore the previously bound context (if any). */ - @MustBeClosed - public static Scope withContext(Context context) { + @Override + public Scope withContext(Context context) { return new DefaultScope(context); } - public static Span getSpan() { + @Override + public Span getSpan() { return Span.Key.get(Context.current()); } - public static CorrelationContext getCorrelationContext() { + @Override + public CorrelationContext getCorrelationContext() { return CorrelationContext.Key.get(Context.current()); } @@ -56,12 +64,10 @@ public static CorrelationContext getCorrelationContext() { // TODO (trask) this method is not needed currently, in favor of using Context.current() directly, // but if we move to a Context object that doesn't have built-in thread-binding, then this is // the only additional method we would need - // private static Context get() { + // private static Context getContext() { // return Context.current(); // } - private CurrentContext() {} - static class DefaultScope implements Scope { private final Context priorContext; diff --git a/api/src/main/java/io/opentelemetry/currentcontext/Scope.java b/api/src/main/java/io/opentelemetry/scope/Scope.java similarity index 96% rename from api/src/main/java/io/opentelemetry/currentcontext/Scope.java rename to api/src/main/java/io/opentelemetry/scope/Scope.java index c7fb4304c41..572f067561d 100644 --- a/api/src/main/java/io/opentelemetry/currentcontext/Scope.java +++ b/api/src/main/java/io/opentelemetry/scope/Scope.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.opentelemetry.currentcontext; +package io.opentelemetry.scope; import java.io.Closeable; import javax.annotation.concurrent.NotThreadSafe; diff --git a/api/src/main/java/io/opentelemetry/scope/ScopeManager.java b/api/src/main/java/io/opentelemetry/scope/ScopeManager.java new file mode 100644 index 00000000000..69b7fe10047 --- /dev/null +++ b/api/src/main/java/io/opentelemetry/scope/ScopeManager.java @@ -0,0 +1,49 @@ +/* + * Copyright 2020, OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opentelemetry.scope; + +import com.google.errorprone.annotations.MustBeClosed; +import io.grpc.Context; +import io.opentelemetry.correlationcontext.CorrelationContext; +import io.opentelemetry.trace.Span; + +// TODO (trask) javadoc class and methods +public interface ScopeManager { + + @MustBeClosed + Scope withSpan(Span span); + + @MustBeClosed + Scope withCorrelationContext(CorrelationContext correlationContext); + + /** + * Binds the context to the current thread and returns a scope that must be used to unbind the + * context from the current thread and restore the previously bound context (if any). + */ + @MustBeClosed + Scope withContext(Context context); + + Span getSpan(); + + CorrelationContext getCorrelationContext(); + + /** Returns the context bound to the current thread. */ + // TODO (trask) this method is not needed currently, in favor of using Context.current() directly, + // but if we move to a Context object that doesn't have built-in thread-binding, then this is + // the only additional method we would need + // Context getContext(); +} diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java b/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java index 92b366dd063..c203e2fd309 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java @@ -17,8 +17,8 @@ package io.opentelemetry.trace; import io.opentelemetry.common.AttributeValue; -import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.internal.Utils; +import io.opentelemetry.scope.ScopeManager; import java.util.Map; import java.util.Random; import javax.annotation.concurrent.Immutable; @@ -27,7 +27,7 @@ * The {@code DefaultSpan} is the default {@link Span} that is used when no {@code Span} * implementation is available. All operations are no-op except context propagation. * - *

Used also to stop tracing, see {@link CurrentContext#withSpan}. + *

Used also to stop tracing, see {@link ScopeManager#withSpan}. * * @since 0.1.0 */ diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java index e2de04d8806..f8d3b363217 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java @@ -17,8 +17,9 @@ package io.opentelemetry.trace; import io.opentelemetry.common.AttributeValue; -import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.internal.Utils; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.ScopeManager; import java.util.Map; import javax.annotation.Nullable; import javax.annotation.concurrent.ThreadSafe; @@ -42,26 +43,30 @@ public static Tracer getInstance() { return INSTANCE; } + // TODO (trask) should be injected + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); + @Override public Span.Builder spanBuilder(String spanName) { - return NoopSpanBuilder.create(spanName); + return NoopSpanBuilder.create(spanName, scopeManager); } private DefaultTracer() {} // Noop implementation of Span.Builder. private static final class NoopSpanBuilder implements Span.Builder { - static NoopSpanBuilder create(String spanName) { - return new NoopSpanBuilder(spanName); + static NoopSpanBuilder create(String spanName, ScopeManager scopeManager) { + return new NoopSpanBuilder(spanName, scopeManager); } private boolean isRootSpan; @Nullable private SpanContext spanContext; + private final ScopeManager scopeManager; @Override public Span startSpan() { if (spanContext == null && !isRootSpan) { - spanContext = CurrentContext.getSpan().getContext(); + spanContext = scopeManager.getSpan().getContext(); } return spanContext != null && !SpanContext.getInvalid().equals(spanContext) @@ -147,8 +152,9 @@ public NoopSpanBuilder setStartTimestamp(long startTimestamp) { return this; } - private NoopSpanBuilder(String name) { + private NoopSpanBuilder(String name, ScopeManager scopeManager) { Utils.checkNotNull(name, "name"); + this.scopeManager = scopeManager; } } } diff --git a/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java b/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java index 7a4d37b5dc3..1f183f8635c 100644 --- a/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java +++ b/api/src/test/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerTest.java @@ -18,8 +18,9 @@ import static com.google.common.truth.Truth.assertThat; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.Scope; +import io.opentelemetry.scope.ScopeManager; import java.util.Arrays; import java.util.Collection; import javax.annotation.Nullable; @@ -34,6 +35,7 @@ // are now removed @RunWith(JUnit4.class) public final class DefaultCorrelationContextManagerTest { + private static final ScopeManager defaultScopeManager = DefaultScopeManager.getInstance(); private static final CorrelationContextManager defaultCorrelationContextManager = DefaultCorrelationContextManager.getInstance(); private static final EntryKey TEST_KEY = EntryKey.create("key"); @@ -64,15 +66,15 @@ public void builderMethod() { @Test public void getCurrentContext_DefaultContext() { - assertThat(CurrentContext.getCorrelationContext()) + assertThat(defaultScopeManager.getCorrelationContext()) .isSameInstanceAs(EmptyCorrelationContext.getInstance()); } @Test public void getCurrentContext_ContextSetToNull() { - Scope scope = CurrentContext.withCorrelationContext(null); + Scope scope = defaultScopeManager.withCorrelationContext(null); try { - CorrelationContext distContext = CurrentContext.getCorrelationContext(); + CorrelationContext distContext = defaultScopeManager.getCorrelationContext(); assertThat(distContext).isNotNull(); assertThat(distContext.getEntries()).isEmpty(); } finally { @@ -82,30 +84,30 @@ public void getCurrentContext_ContextSetToNull() { @Test public void withContext() { - assertThat(CurrentContext.getCorrelationContext()) + assertThat(defaultScopeManager.getCorrelationContext()) .isSameInstanceAs(EmptyCorrelationContext.getInstance()); - Scope wtm = CurrentContext.withCorrelationContext(DIST_CONTEXT); + Scope wtm = defaultScopeManager.withCorrelationContext(DIST_CONTEXT); try { - assertThat(CurrentContext.getCorrelationContext()).isSameInstanceAs(DIST_CONTEXT); + assertThat(defaultScopeManager.getCorrelationContext()).isSameInstanceAs(DIST_CONTEXT); } finally { wtm.close(); } - assertThat(CurrentContext.getCorrelationContext()) + assertThat(defaultScopeManager.getCorrelationContext()) .isSameInstanceAs(EmptyCorrelationContext.getInstance()); } @Test public void withContext_nullContext() { - assertThat(CurrentContext.getCorrelationContext()) + assertThat(defaultScopeManager.getCorrelationContext()) .isSameInstanceAs(EmptyCorrelationContext.getInstance()); - Scope wtm = CurrentContext.withCorrelationContext(null); + Scope wtm = defaultScopeManager.withCorrelationContext(null); try { - assertThat(CurrentContext.getCorrelationContext()) + assertThat(defaultScopeManager.getCorrelationContext()) .isSameInstanceAs(EmptyCorrelationContext.getInstance()); } finally { wtm.close(); } - assertThat(CurrentContext.getCorrelationContext()) + assertThat(defaultScopeManager.getCorrelationContext()) .isSameInstanceAs(EmptyCorrelationContext.getInstance()); } diff --git a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java index a98abaa5fa0..bbafefca484 100644 --- a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java +++ b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java @@ -18,8 +18,9 @@ import static com.google.common.truth.Truth.assertThat; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.Scope; +import io.opentelemetry.scope.ScopeManager; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -33,6 +34,7 @@ // TODO (trask) delete tests here that were designed to test Tracer methods that are now removed @SuppressWarnings("MustBeClosedChecker") public class DefaultTracerTest { + private static final ScopeManager defaultScopeManager = DefaultScopeManager.getInstance(); private static final Tracer defaultTracer = DefaultTracer.getInstance(); private static final String SPAN_NAME = "MySpanName"; private static final byte[] firstBytes = @@ -48,19 +50,19 @@ public class DefaultTracerTest { @Test public void defaultGetCurrentSpan() { - assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); + assertThat(defaultScopeManager.getSpan()).isInstanceOf(DefaultSpan.class); } @Test public void getCurrentSpan_WithSpan() { - assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); - Scope ws = CurrentContext.withSpan(DefaultSpan.createRandom()); + assertThat(defaultScopeManager.getSpan()).isInstanceOf(DefaultSpan.class); + Scope ws = defaultScopeManager.withSpan(DefaultSpan.createRandom()); try { - assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); + assertThat(defaultScopeManager.getSpan()).isInstanceOf(DefaultSpan.class); } finally { ws.close(); } - assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); + assertThat(defaultScopeManager.getSpan()).isInstanceOf(DefaultSpan.class); } @Test @@ -77,21 +79,21 @@ public void defaultSpanBuilderWithName() { @Test public void testInProcessContext() { Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); - Scope scope = CurrentContext.withSpan(span); + Scope scope = defaultScopeManager.withSpan(span); try { - assertThat(CurrentContext.getSpan()).isEqualTo(span); + assertThat(defaultScopeManager.getSpan()).isEqualTo(span); Span secondSpan = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); - Scope secondScope = CurrentContext.withSpan(secondSpan); + Scope secondScope = defaultScopeManager.withSpan(secondSpan); try { - assertThat(CurrentContext.getSpan()).isEqualTo(secondSpan); + assertThat(defaultScopeManager.getSpan()).isEqualTo(secondSpan); } finally { secondScope.close(); - assertThat(CurrentContext.getSpan()).isEqualTo(span); + assertThat(defaultScopeManager.getSpan()).isEqualTo(span); } } finally { scope.close(); } - assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); + assertThat(defaultScopeManager.getSpan()).isInstanceOf(DefaultSpan.class); } @Test @@ -111,7 +113,7 @@ public void testSpanContextPropagation() { @Test public void testSpanContextPropagationCurrentSpan() { DefaultSpan parent = new DefaultSpan(spanContext); - Scope scope = CurrentContext.withSpan(parent); + Scope scope = defaultScopeManager.withSpan(parent); try { Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); assertThat(span.getContext()).isSameInstanceAs(spanContext); @@ -122,7 +124,7 @@ public void testSpanContextPropagationCurrentSpan() { @Test public void testSpanContextPropagationCurrentSpanContext() { - Scope scope = CurrentContext.withSpan(DefaultSpan.create(spanContext)); + Scope scope = defaultScopeManager.withSpan(DefaultSpan.create(spanContext)); try { Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); assertThat(span.getContext()).isSameInstanceAs(spanContext); diff --git a/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java b/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java index 39036d69362..cace23cbc92 100644 --- a/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java +++ b/contrib/trace_utils/src/main/java/io/opentelemetry/contrib/trace/CurrentSpanUtils.java @@ -16,16 +16,20 @@ package io.opentelemetry.contrib.trace; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; +import io.opentelemetry.scope.Scope; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Status; import java.util.concurrent.Callable; /** Util methods/functionality to interact with the {@link Span} in the {@link io.grpc.Context}. */ public final class CurrentSpanUtils { - // No instance of this class. - private CurrentSpanUtils() {} + + private final ScopeManager scopeManager; + + public CurrentSpanUtils(ScopeManager scopeManager) { + this.scopeManager = scopeManager; + } /** * Wraps a {@link Runnable} so that it executes with the {@code span} as the current {@code Span}. @@ -35,8 +39,8 @@ private CurrentSpanUtils() {} * @param runnable the {@code Runnable} to run in the {@code Span}. * @return the wrapped {@code Runnable}. */ - public static Runnable withSpan(Span span, boolean endSpan, Runnable runnable) { - return new RunnableInSpan(span, runnable, endSpan); + public Runnable withSpan(Span span, boolean endSpan, Runnable runnable) { + return new RunnableInSpan(span, runnable, endSpan, scopeManager); } /** @@ -48,24 +52,27 @@ public static Runnable withSpan(Span span, boolean endSpan, Runnable runnable) { * @param the {@code Callable} result type. * @return the wrapped {@code Callable}. */ - public static Callable withSpan(Span span, boolean endSpan, Callable callable) { - return new CallableInSpan(span, callable, endSpan); + public Callable withSpan(Span span, boolean endSpan, Callable callable) { + return new CallableInSpan(span, callable, endSpan, scopeManager); } private static final class RunnableInSpan implements Runnable { private final Span span; private final Runnable runnable; private final boolean endSpan; + private final ScopeManager scopeManager; - private RunnableInSpan(Span span, Runnable runnable, boolean endSpan) { + private RunnableInSpan( + Span span, Runnable runnable, boolean endSpan, ScopeManager scopeManager) { this.span = span; this.runnable = runnable; this.endSpan = endSpan; + this.scopeManager = scopeManager; } @Override public void run() { - try (Scope ignored = CurrentContext.withSpan(span)) { + try (Scope ignored = scopeManager.withSpan(span)) { runnable.run(); } catch (Throwable t) { setErrorStatus(span, t); @@ -87,16 +94,19 @@ private static final class CallableInSpan implements Callable { private final Span span; private final Callable callable; private final boolean endSpan; + private final ScopeManager scopeManager; - private CallableInSpan(Span span, Callable callable, boolean endSpan) { + private CallableInSpan( + Span span, Callable callable, boolean endSpan, ScopeManager scopeManager) { this.span = span; this.callable = callable; this.endSpan = endSpan; + this.scopeManager = scopeManager; } @Override public V call() throws Exception { - try (Scope ignored = CurrentContext.withSpan(span)) { + try (Scope ignored = scopeManager.withSpan(span)) { return callable.call(); } catch (Exception e) { setErrorStatus(span, e); diff --git a/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java b/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java index 4d661a2ebf0..bcb2dfa06f7 100644 --- a/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java +++ b/contrib/trace_utils/src/test/java/io/opentelemetry/contrib/trace/CurrentSpanUtilsTest.java @@ -20,7 +20,8 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyZeroInteractions; -import io.opentelemetry.currentcontext.CurrentContext; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.trace.DefaultSpan; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Status; @@ -35,6 +36,10 @@ /** Unit tests for {@link CurrentSpanUtils}. */ @RunWith(JUnit4.class) public class CurrentSpanUtilsTest { + private static final ScopeManager defaultScopeManager = DefaultScopeManager.getInstance(); + private static final CurrentSpanUtils currentSpanUtils = + new CurrentSpanUtils(defaultScopeManager); + @Mock private Span span; @Before @@ -46,7 +51,7 @@ public void setUp() { private void executeRunnableAndExpectError(Runnable runnable, Throwable error) { boolean called = false; try { - CurrentSpanUtils.withSpan(span, true, runnable).run(); + currentSpanUtils.withSpan(span, true, runnable).run(); } catch (Throwable e) { assertThat(e).isEqualTo(error); called = true; @@ -58,7 +63,7 @@ private void executeRunnableAndExpectError(Runnable runnable, Throwable error) { private void executeCallableAndExpectError(Callable callable, Throwable error) { boolean called = false; try { - CurrentSpanUtils.withSpan(span, true, callable).call(); + currentSpanUtils.withSpan(span, true, callable).call(); } catch (Throwable e) { assertThat(e).isEqualTo(error); called = true; @@ -77,7 +82,7 @@ public void run() { assertThat(getCurrentSpan()).isSameInstanceAs(span); } }; - CurrentSpanUtils.withSpan(span, false, runnable).run(); + currentSpanUtils.withSpan(span, false, runnable).run(); verifyZeroInteractions(span); assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); } @@ -93,7 +98,7 @@ public void run() { assertThat(getCurrentSpan()).isSameInstanceAs(span); } }; - CurrentSpanUtils.withSpan(span, true, runnable).run(); + currentSpanUtils.withSpan(span, true, runnable).run(); verify(span).end(); assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); } @@ -149,7 +154,7 @@ public Object call() throws Exception { return ret; } }; - assertThat(CurrentSpanUtils.withSpan(span, false, callable).call()).isEqualTo(ret); + assertThat(currentSpanUtils.withSpan(span, false, callable).call()).isEqualTo(ret); verifyZeroInteractions(span); assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); } @@ -167,7 +172,7 @@ public Object call() throws Exception { return ret; } }; - assertThat(CurrentSpanUtils.withSpan(span, true, callable).call()).isEqualTo(ret); + assertThat(currentSpanUtils.withSpan(span, true, callable).call()).isEqualTo(ret); verify(span).end(); assertThat(getCurrentSpan()).isInstanceOf(DefaultSpan.class); } @@ -249,6 +254,6 @@ public Object call() throws Exception { } private static Span getCurrentSpan() { - return CurrentContext.getSpan(); + return defaultScopeManager.getSpan(); } } diff --git a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClient.java b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClient.java index 243a73514de..1e9d8723460 100644 --- a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClient.java +++ b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClient.java @@ -30,8 +30,8 @@ import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; +import io.opentelemetry.context.CurrentContext; +import io.opentelemetry.context.Scope; import io.opentelemetry.exporters.logging.LoggingSpanExporter; import io.opentelemetry.sdk.OpenTelemetrySdk; import io.opentelemetry.sdk.trace.TracerSdkProvider; diff --git a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClientStream.java b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClientStream.java index 40aca2edfbc..fcd570614b1 100644 --- a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClientStream.java +++ b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldClientStream.java @@ -31,8 +31,8 @@ import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; +import io.opentelemetry.context.CurrentContext; +import io.opentelemetry.context.Scope; import io.opentelemetry.exporters.logging.LoggingSpanExporter; import io.opentelemetry.sdk.OpenTelemetrySdk; import io.opentelemetry.sdk.trace.TracerSdkProvider; diff --git a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldServer.java b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldServer.java index 554300b9de5..39be28d1e2c 100644 --- a/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldServer.java +++ b/examples/grpc/src/main/java/io/opentelemetry/example/HelloWorldServer.java @@ -28,8 +28,8 @@ import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; +import io.opentelemetry.context.CurrentContext; +import io.opentelemetry.context.Scope; import io.opentelemetry.exporters.logging.LoggingSpanExporter; import io.opentelemetry.sdk.OpenTelemetrySdk; import io.opentelemetry.sdk.trace.TracerSdkProvider; diff --git a/examples/http/src/main/java/io/opentelemetry/example/http/HttpClient.java b/examples/http/src/main/java/io/opentelemetry/example/http/HttpClient.java index 20539b91773..bfc17ca9531 100644 --- a/examples/http/src/main/java/io/opentelemetry/example/http/HttpClient.java +++ b/examples/http/src/main/java/io/opentelemetry/example/http/HttpClient.java @@ -19,8 +19,8 @@ import io.grpc.Context; import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.propagation.HttpTextFormat; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; +import io.opentelemetry.context.CurrentContext; +import io.opentelemetry.context.Scope; import io.opentelemetry.exporters.logging.LoggingSpanExporter; import io.opentelemetry.sdk.OpenTelemetrySdk; import io.opentelemetry.sdk.trace.TracerSdkProvider; diff --git a/examples/http/src/main/java/io/opentelemetry/example/http/HttpServer.java b/examples/http/src/main/java/io/opentelemetry/example/http/HttpServer.java index f9c77d1d5ad..eef1f63a26f 100644 --- a/examples/http/src/main/java/io/opentelemetry/example/http/HttpServer.java +++ b/examples/http/src/main/java/io/opentelemetry/example/http/HttpServer.java @@ -22,8 +22,8 @@ import io.opentelemetry.common.AttributeValue; import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.HttpTextFormat; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; +import io.opentelemetry.context.CurrentContext; +import io.opentelemetry.context.Scope; import io.opentelemetry.exporters.logging.LoggingSpanExporter; import io.opentelemetry.sdk.OpenTelemetrySdk; import io.opentelemetry.sdk.trace.TracerSdkProvider; diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeManagerShim.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeManagerShim.java index f74fe08ab1f..79a855d02af 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeManagerShim.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeManagerShim.java @@ -16,12 +16,15 @@ package io.opentelemetry.opentracingshim; -import io.opentelemetry.currentcontext.CurrentContext; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.ScopeManager; import io.opentracing.Scope; -import io.opentracing.ScopeManager; import io.opentracing.Span; -final class ScopeManagerShim extends BaseShimObject implements ScopeManager { +final class ScopeManagerShim extends BaseShimObject implements io.opentracing.ScopeManager { + + // TODO (trask) should be injected + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); public ScopeManagerShim(TelemetryInfo telemetryInfo) { super(telemetryInfo); @@ -33,7 +36,7 @@ public Span activeSpan() { // As OpenTracing simply returns null when no active instance is available, // we need to do an explicit check against DefaultSpan, // which is used in OpenTelemetry for this very case. - io.opentelemetry.trace.Span span = CurrentContext.getSpan(); + io.opentelemetry.trace.Span span = scopeManager.getSpan(); if (io.opentelemetry.trace.DefaultSpan.getInvalid().equals(span)) { return null; } @@ -46,7 +49,7 @@ public Span activeSpan() { @SuppressWarnings("MustBeClosedChecker") public Scope activate(Span span) { io.opentelemetry.trace.Span actualSpan = getActualSpan(span); - return new ScopeShim(CurrentContext.withSpan(actualSpan)); + return new ScopeShim(scopeManager.withSpan(actualSpan)); } static io.opentelemetry.trace.Span getActualSpan(Span span) { diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeShim.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeShim.java index b8174797602..f407144c949 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeShim.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeShim.java @@ -19,9 +19,9 @@ import io.opentracing.Scope; final class ScopeShim implements Scope { - final io.opentelemetry.currentcontext.Scope scope; + final io.opentelemetry.scope.Scope scope; - public ScopeShim(io.opentelemetry.currentcontext.Scope scope) { + public ScopeShim(io.opentelemetry.scope.Scope scope) { this.scope = scope; } diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/OpenTelemetryInteroperabilityTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/OpenTelemetryInteroperabilityTest.java index 4902e378247..e96e647f6cd 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/OpenTelemetryInteroperabilityTest.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/OpenTelemetryInteroperabilityTest.java @@ -20,9 +20,10 @@ import static org.junit.Assert.assertNull; import io.opentelemetry.correlationcontext.DefaultCorrelationContextManager; -import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.opentracingshim.TraceShim; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.trace.DefaultSpan; @@ -34,6 +35,7 @@ import org.junit.Test; public class OpenTelemetryInteroperabilityTest { + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); private final TracerSdkProvider sdk = TracerSdkProvider.builder().build(); private final io.opentelemetry.trace.Tracer tracer = sdk.get("opentracingshim"); private final InMemoryTracing inMemoryTracing = @@ -54,7 +56,7 @@ public void sdkContinuesOpenTracingTrace() { } finally { otSpan.finish(); } - assertEquals(CurrentContext.getSpan().getClass(), DefaultSpan.class); + assertEquals(scopeManager.getSpan().getClass(), DefaultSpan.class); assertNull(otTracer.activeSpan()); List finishedSpans = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); @@ -65,13 +67,13 @@ public void sdkContinuesOpenTracingTrace() { @Test public void openTracingContinuesSdkTrace() { io.opentelemetry.trace.Span otelSpan = tracer.spanBuilder("otel_span").startSpan(); - try (io.opentelemetry.currentcontext.Scope scope = CurrentContext.withSpan(otelSpan)) { + try (io.opentelemetry.scope.Scope scope = scopeManager.withSpan(otelSpan)) { otTracer.buildSpan("ot_span").start().finish(); } finally { otelSpan.end(); } - assertEquals(CurrentContext.getSpan().getClass(), DefaultSpan.class); + assertEquals(scopeManager.getSpan().getClass(), DefaultSpan.class); assertNull(otTracer.activeSpan()); List finishedSpans = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); diff --git a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java index 9cbf4c2192c..bade22efdc8 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java @@ -18,14 +18,19 @@ import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.correlationcontext.CorrelationContextManager; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.ScopeManager; /** * {@link CorrelationContextManagerSdk} is SDK implementation of {@link CorrelationContextManager}. */ public class CorrelationContextManagerSdk implements CorrelationContextManager { + // TODO (trask) should be injected + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); + @Override public CorrelationContext.Builder contextBuilder() { - return new CorrelationContextSdk.Builder(); + return new CorrelationContextSdk.Builder(scopeManager); } } diff --git a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java index c3a5ca548c7..64100e81a4f 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdk.java @@ -21,7 +21,7 @@ import io.opentelemetry.correlationcontext.EntryKey; import io.opentelemetry.correlationcontext.EntryMetadata; import io.opentelemetry.correlationcontext.EntryValue; -import io.opentelemetry.currentcontext.CurrentContext; +import io.opentelemetry.scope.ScopeManager; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -114,10 +114,12 @@ static class Builder implements CorrelationContext.Builder { @Nullable private CorrelationContext parent; private boolean noImplicitParent; private final Map entries; + private final ScopeManager scopeManager; /** Create a new empty CorrelationContext builder. */ - Builder() { + Builder(ScopeManager scopeManager) { this.entries = new HashMap<>(); + this.scopeManager = scopeManager; } @Override @@ -157,7 +159,7 @@ public CorrelationContext.Builder remove(EntryKey key) { @Override public CorrelationContextSdk build() { if (parent == null && !noImplicitParent) { - parent = CurrentContext.getCorrelationContext(); + parent = scopeManager.getCorrelationContext(); } return new CorrelationContextSdk(entries, parent); } diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java index 1939ceb68f5..fcefd3097bb 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java @@ -17,8 +17,8 @@ package io.opentelemetry.sdk.trace; import io.opentelemetry.common.AttributeValue; -import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.internal.Utils; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.sdk.common.Clock; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.sdk.internal.MonotonicClock; @@ -49,6 +49,7 @@ final class SpanBuilderSdk implements Span.Builder { private static final TraceFlags TRACE_OPTIONS_NOT_SAMPLED = TraceFlags.builder().setIsSampled(false).build(); + private final ScopeManager scopeManager; private final String spanName; private final InstrumentationLibraryInfo instrumentationLibraryInfo; private final SpanProcessor spanProcessor; @@ -73,7 +74,8 @@ final class SpanBuilderSdk implements Span.Builder { TraceConfig traceConfig, Resource resource, IdsGenerator idsGenerator, - Clock clock) { + Clock clock, + ScopeManager scopeManager) { this.spanName = spanName; this.instrumentationLibraryInfo = instrumentationLibraryInfo; this.spanProcessor = spanProcessor; @@ -83,6 +85,7 @@ final class SpanBuilderSdk implements Span.Builder { this.links = Collections.emptyList(); this.idsGenerator = idsGenerator; this.clock = clock; + this.scopeManager = scopeManager; } @Override @@ -245,13 +248,12 @@ private static Clock getClock(Span parent, Clock clock) { } @Nullable - private static SpanContext parent( - ParentType parentType, Span explicitParent, SpanContext remoteParent) { + private SpanContext parent(ParentType parentType, Span explicitParent, SpanContext remoteParent) { switch (parentType) { case NO_PARENT: return null; case CURRENT_CONTEXT: - return CurrentContext.getSpan().getContext(); + return scopeManager.getSpan().getContext(); case EXPLICIT_PARENT: return explicitParent.getContext(); case EXPLICIT_REMOTE_PARENT: @@ -261,10 +263,10 @@ private static SpanContext parent( } @Nullable - private static Span parentSpan(ParentType parentType, Span explicitParent) { + private Span parentSpan(ParentType parentType, Span explicitParent) { switch (parentType) { case CURRENT_CONTEXT: - return CurrentContext.getSpan(); + return scopeManager.getSpan(); case EXPLICIT_PARENT: return explicitParent; default: diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java index 4a3ed008f57..d29a3d7733b 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java @@ -16,6 +16,8 @@ package io.opentelemetry.sdk.trace; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.trace.DefaultTracer; import io.opentelemetry.trace.Span; @@ -25,6 +27,8 @@ final class TracerSdk implements Tracer { private final TracerSharedState sharedState; private final InstrumentationLibraryInfo instrumentationLibraryInfo; + // TODO (trask) should be injected + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); TracerSdk(TracerSharedState sharedState, InstrumentationLibraryInfo instrumentationLibraryInfo) { this.sharedState = sharedState; @@ -43,7 +47,8 @@ public Span.Builder spanBuilder(String spanName) { sharedState.getActiveTraceConfig(), sharedState.getResource(), sharedState.getIdsGenerator(), - sharedState.getClock()); + sharedState.getClock(), + scopeManager); } /** diff --git a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java index 45e4ed121d1..4c949b20fac 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdkTest.java @@ -20,8 +20,9 @@ import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.correlationcontext.EmptyCorrelationContext; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.Scope; +import io.opentelemetry.scope.ScopeManager; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -37,6 +38,8 @@ // are now removed @SuppressWarnings("MustBeClosedChecker") public class CorrelationContextManagerSdkTest { + private static final ScopeManager scopeManager = DefaultScopeManager.getInstance(); + @Mock private CorrelationContext distContext; @Before @@ -46,14 +49,14 @@ public void setUp() { @Test public void testGetCurrentContext_DefaultContext() { - assertThat(CurrentContext.getCorrelationContext()) + assertThat(scopeManager.getCorrelationContext()) .isSameInstanceAs(EmptyCorrelationContext.getInstance()); } @Test public void testGetCurrentContext_ContextSetToNull() { - try (Scope ignored = CurrentContext.withCorrelationContext(null)) { - CorrelationContext distContext = CurrentContext.getCorrelationContext(); + try (Scope ignored = scopeManager.withCorrelationContext(null)) { + CorrelationContext distContext = scopeManager.getCorrelationContext(); assertThat(distContext).isNotNull(); assertThat(distContext.getEntries()).isEmpty(); } @@ -61,12 +64,12 @@ public void testGetCurrentContext_ContextSetToNull() { @Test public void testWithCorrelationContext() { - assertThat(CurrentContext.getCorrelationContext()) + assertThat(scopeManager.getCorrelationContext()) .isSameInstanceAs(EmptyCorrelationContext.getInstance()); - try (Scope ignored = CurrentContext.withCorrelationContext(distContext)) { - assertThat(CurrentContext.getCorrelationContext()).isSameInstanceAs(distContext); + try (Scope ignored = scopeManager.withCorrelationContext(distContext)) { + assertThat(scopeManager.getCorrelationContext()).isSameInstanceAs(distContext); } - assertThat(CurrentContext.getCorrelationContext()) + assertThat(scopeManager.getCorrelationContext()) .isSameInstanceAs(EmptyCorrelationContext.getInstance()); } } diff --git a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java index 3e51d607622..92bd45bdc4b 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java @@ -26,6 +26,8 @@ import io.opentelemetry.correlationcontext.EntryKey; import io.opentelemetry.correlationcontext.EntryMetadata; import io.opentelemetry.correlationcontext.EntryValue; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.ScopeManager; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -40,6 +42,7 @@ */ @RunWith(JUnit4.class) public class CorrelationContextSdkTest { + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); private final CorrelationContextManager contextManager = new CorrelationContextManagerSdk(); private static final EntryMetadata TMD = @@ -58,7 +61,7 @@ public class CorrelationContextSdkTest { @Test public void getEntries_empty() { - CorrelationContextSdk distContext = new CorrelationContextSdk.Builder().build(); + CorrelationContextSdk distContext = new CorrelationContextSdk.Builder(scopeManager).build(); assertThat(distContext.getEntries()).isEmpty(); } @@ -142,7 +145,7 @@ public void setParent_setNoParent() { @Test public void remove_existingKey() { - CorrelationContextSdk.Builder builder = new CorrelationContextSdk.Builder(); + CorrelationContextSdk.Builder builder = new CorrelationContextSdk.Builder(scopeManager); builder.put(T1.getKey(), T1.getValue(), T1.getEntryMetadata()); builder.put(T2.getKey(), T2.getValue(), T2.getEntryMetadata()); @@ -151,7 +154,7 @@ public void remove_existingKey() { @Test public void remove_differentKey() { - CorrelationContextSdk.Builder builder = new CorrelationContextSdk.Builder(); + CorrelationContextSdk.Builder builder = new CorrelationContextSdk.Builder(scopeManager); builder.put(T1.getKey(), T1.getValue(), T1.getEntryMetadata()); builder.put(T2.getKey(), T2.getValue(), T2.getEntryMetadata()); diff --git a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextTestUtil.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextTestUtil.java index 0db4f7860f5..8e6d0b0fc5d 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextTestUtil.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextTestUtil.java @@ -17,6 +17,7 @@ package io.opentelemetry.sdk.correlationcontext; import io.opentelemetry.correlationcontext.Entry; +import io.opentelemetry.scope.DefaultScopeManager; import java.util.Arrays; import java.util.List; @@ -27,7 +28,8 @@ static CorrelationContextSdk listToCorrelationContext(Entry... entries) { } static CorrelationContextSdk listToCorrelationContext(List entries) { - CorrelationContextSdk.Builder builder = new CorrelationContextSdk.Builder(); + CorrelationContextSdk.Builder builder = + new CorrelationContextSdk.Builder(DefaultScopeManager.getInstance()); for (Entry entry : entries) { builder.put(entry.getKey(), entry.getValue(), entry.getEntryMetadata()); } diff --git a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java index 39e4c925c11..bae04674da4 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java @@ -25,8 +25,9 @@ import io.opentelemetry.correlationcontext.EntryKey; import io.opentelemetry.correlationcontext.EntryMetadata; import io.opentelemetry.correlationcontext.EntryValue; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.Scope; +import io.opentelemetry.scope.ScopeManager; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -53,31 +54,32 @@ public class ScopedCorrelationContextTest { private static final EntryMetadata METADATA_NO_PROPAGATION = EntryMetadata.create(EntryMetadata.EntryTtl.NO_PROPAGATION); + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); private final CorrelationContextManager contextManager = new CorrelationContextManagerSdk(); @Test public void emptyCorrelationContext() { - CorrelationContext defaultCorrelationContext = CurrentContext.getCorrelationContext(); + CorrelationContext defaultCorrelationContext = scopeManager.getCorrelationContext(); assertThat(defaultCorrelationContext.getEntries()).isEmpty(); assertThat(defaultCorrelationContext).isInstanceOf(EmptyCorrelationContext.class); } @Test public void withContext() { - assertThat(CurrentContext.getCorrelationContext().getEntries()).isEmpty(); + assertThat(scopeManager.getCorrelationContext().getEntries()).isEmpty(); CorrelationContext scopedEntries = contextManager.contextBuilder().put(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION).build(); - try (Scope scope = CurrentContext.withCorrelationContext(scopedEntries)) { - assertThat(CurrentContext.getCorrelationContext()).isSameInstanceAs(scopedEntries); + try (Scope scope = scopeManager.withCorrelationContext(scopedEntries)) { + assertThat(scopeManager.getCorrelationContext()).isSameInstanceAs(scopedEntries); } - assertThat(CurrentContext.getCorrelationContext().getEntries()).isEmpty(); + assertThat(scopeManager.getCorrelationContext().getEntries()).isEmpty(); } @Test public void createBuilderFromCurrentEntries() { CorrelationContext scopedDistContext = contextManager.contextBuilder().put(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION).build(); - try (Scope scope = CurrentContext.withCorrelationContext(scopedDistContext)) { + try (Scope scope = scopeManager.withCorrelationContext(scopedDistContext)) { CorrelationContext newEntries = contextManager .contextBuilder() @@ -87,41 +89,41 @@ public void createBuilderFromCurrentEntries() { .containsExactly( Entry.create(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION), Entry.create(KEY_2, VALUE_2, METADATA_UNLIMITED_PROPAGATION)); - assertThat(CurrentContext.getCorrelationContext()).isSameInstanceAs(scopedDistContext); + assertThat(scopeManager.getCorrelationContext()).isSameInstanceAs(scopedDistContext); } } @Test public void setCurrentEntriesWithBuilder() { - assertThat(CurrentContext.getCorrelationContext().getEntries()).isEmpty(); + assertThat(scopeManager.getCorrelationContext().getEntries()).isEmpty(); CorrelationContext scopedDistContext = contextManager.contextBuilder().put(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION).build(); - try (Scope scope = CurrentContext.withCorrelationContext(scopedDistContext)) { - assertThat(CurrentContext.getCorrelationContext().getEntries()) + try (Scope scope = scopeManager.withCorrelationContext(scopedDistContext)) { + assertThat(scopeManager.getCorrelationContext().getEntries()) .containsExactly(Entry.create(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION)); - assertThat(CurrentContext.getCorrelationContext()).isSameInstanceAs(scopedDistContext); + assertThat(scopeManager.getCorrelationContext()).isSameInstanceAs(scopedDistContext); } - assertThat(CurrentContext.getCorrelationContext().getEntries()).isEmpty(); + assertThat(scopeManager.getCorrelationContext().getEntries()).isEmpty(); } @Test public void addToCurrentEntriesWithBuilder() { CorrelationContext scopedDistContext = contextManager.contextBuilder().put(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION).build(); - try (Scope scope1 = CurrentContext.withCorrelationContext(scopedDistContext)) { + try (Scope scope1 = scopeManager.withCorrelationContext(scopedDistContext)) { CorrelationContext innerDistContext = contextManager .contextBuilder() .put(KEY_2, VALUE_2, METADATA_UNLIMITED_PROPAGATION) .build(); - try (Scope scope2 = CurrentContext.withCorrelationContext(innerDistContext)) { - assertThat(CurrentContext.getCorrelationContext().getEntries()) + try (Scope scope2 = scopeManager.withCorrelationContext(innerDistContext)) { + assertThat(scopeManager.getCorrelationContext().getEntries()) .containsExactly( Entry.create(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION), Entry.create(KEY_2, VALUE_2, METADATA_UNLIMITED_PROPAGATION)); - assertThat(CurrentContext.getCorrelationContext()).isSameInstanceAs(innerDistContext); + assertThat(scopeManager.getCorrelationContext()).isSameInstanceAs(innerDistContext); } - assertThat(CurrentContext.getCorrelationContext()).isSameInstanceAs(scopedDistContext); + assertThat(scopeManager.getCorrelationContext()).isSameInstanceAs(scopedDistContext); } } @@ -133,31 +135,31 @@ public void multiScopeCorrelationContextWithMetadata() { .put(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION) .put(KEY_2, VALUE_2, METADATA_UNLIMITED_PROPAGATION) .build(); - try (Scope scope1 = CurrentContext.withCorrelationContext(scopedDistContext)) { + try (Scope scope1 = scopeManager.withCorrelationContext(scopedDistContext)) { CorrelationContext innerDistContext = contextManager .contextBuilder() .put(KEY_3, VALUE_3, METADATA_NO_PROPAGATION) .put(KEY_2, VALUE_4, METADATA_NO_PROPAGATION) .build(); - try (Scope scope2 = CurrentContext.withCorrelationContext(innerDistContext)) { - assertThat(CurrentContext.getCorrelationContext().getEntries()) + try (Scope scope2 = scopeManager.withCorrelationContext(innerDistContext)) { + assertThat(scopeManager.getCorrelationContext().getEntries()) .containsExactly( Entry.create(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION), Entry.create(KEY_2, VALUE_4, METADATA_NO_PROPAGATION), Entry.create(KEY_3, VALUE_3, METADATA_NO_PROPAGATION)); - assertThat(CurrentContext.getCorrelationContext()).isSameInstanceAs(innerDistContext); + assertThat(scopeManager.getCorrelationContext()).isSameInstanceAs(innerDistContext); } - assertThat(CurrentContext.getCorrelationContext()).isSameInstanceAs(scopedDistContext); + assertThat(scopeManager.getCorrelationContext()).isSameInstanceAs(scopedDistContext); } } @Test public void setNoParent_doesNotInheritContext() { - assertThat(CurrentContext.getCorrelationContext().getEntries()).isEmpty(); + assertThat(scopeManager.getCorrelationContext().getEntries()).isEmpty(); CorrelationContext scopedDistContext = contextManager.contextBuilder().put(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION).build(); - try (Scope scope = CurrentContext.withCorrelationContext(scopedDistContext)) { + try (Scope scope = scopeManager.withCorrelationContext(scopedDistContext)) { CorrelationContext innerDistContext = contextManager .contextBuilder() @@ -167,6 +169,6 @@ public void setNoParent_doesNotInheritContext() { assertThat(innerDistContext.getEntries()) .containsExactly(Entry.create(KEY_2, VALUE_2, METADATA_UNLIMITED_PROPAGATION)); } - assertThat(CurrentContext.getCorrelationContext().getEntries()).isEmpty(); + assertThat(scopeManager.getCorrelationContext().getEntries()).isEmpty(); } } diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java index 7c503c6ce85..64eb94c57ad 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java @@ -20,8 +20,9 @@ import static org.junit.Assert.assertFalse; import io.opentelemetry.common.AttributeValue; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.Scope; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.sdk.trace.config.TraceConfig; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.trace.DefaultSpan; @@ -55,6 +56,7 @@ public class SpanBuilderSdkTest { TraceFlags.builder().setIsSampled(true).build(), TraceState.getDefault()); + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); private final TracerSdkProvider tracerSdkFactory = TracerSdkProvider.builder().build(); private final TracerSdk tracerSdk = tracerSdkFactory.get("SpanBuilderSdkTest"); @@ -375,7 +377,7 @@ public void sampledViaParentLinks() { @Test public void noParent() { Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); - try (Scope scope = CurrentContext.withSpan(parent)) { + try (Scope scope = scopeManager.withSpan(parent)) { Span span = tracerSdk.spanBuilder(SPAN_NAME).setNoParent().startSpan(); try { assertThat(span.getContext().getTraceId()).isNotEqualTo(parent.getContext().getTraceId()); @@ -457,7 +459,7 @@ public void overrideNoParent_remoteParent() { @Test public void parentCurrentSpan() { Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); - try (Scope scope = CurrentContext.withSpan(parent)) { + try (Scope scope = scopeManager.withSpan(parent)) { RecordEventsReadableSpan span = (RecordEventsReadableSpan) tracerSdk.spanBuilder(SPAN_NAME).startSpan(); try { @@ -509,7 +511,7 @@ public void parent_clockIsSame() { @Test public void parentCurrentSpan_clockIsSame() { Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan(); - try (Scope scope = CurrentContext.withSpan(parent)) { + try (Scope scope = scopeManager.withSpan(parent)) { RecordEventsReadableSpan span = (RecordEventsReadableSpan) tracerSdk.spanBuilder(SPAN_NAME).startSpan(); diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java index c9edc887862..6ea15c6981d 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java @@ -19,8 +19,9 @@ import static com.google.common.truth.Truth.assertThat; import io.opentelemetry.common.AttributeValue; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.Scope; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.sdk.trace.StressTestRunner.OperationUpdater; import io.opentelemetry.sdk.trace.data.SpanData; @@ -53,6 +54,8 @@ public class TracerSdkTest { InstrumentationLibraryInfo.create( INSTRUMENTATION_LIBRARY_NAME, INSTRUMENTATION_LIBRARY_VERSION); @Mock private Span span; + + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); private final TracerSdk tracer = TracerSdkProvider.builder() .build() @@ -65,7 +68,7 @@ public void setUp() { @Test public void defaultGetCurrentSpan() { - assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); + assertThat(scopeManager.getSpan()).isInstanceOf(DefaultSpan.class); } @Test @@ -75,29 +78,29 @@ public void defaultSpanBuilder() { @Test public void getCurrentSpan() { - assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); - try (Scope ignored = CurrentContext.withSpan(span)) { - assertThat(CurrentContext.getSpan()).isSameInstanceAs(span); + assertThat(scopeManager.getSpan()).isInstanceOf(DefaultSpan.class); + try (Scope ignored = scopeManager.withSpan(span)) { + assertThat(scopeManager.getSpan()).isSameInstanceAs(span); } - assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); + assertThat(scopeManager.getSpan()).isInstanceOf(DefaultSpan.class); } @Test public void withSpan_NullSpan() { - assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); - try (Scope ignored = CurrentContext.withSpan(null)) { - assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); + assertThat(scopeManager.getSpan()).isInstanceOf(DefaultSpan.class); + try (Scope ignored = scopeManager.withSpan(null)) { + assertThat(scopeManager.getSpan()).isInstanceOf(DefaultSpan.class); } - assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); + assertThat(scopeManager.getSpan()).isInstanceOf(DefaultSpan.class); } @Test public void getCurrentSpan_WithSpan() { - assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); - try (Scope ignored = CurrentContext.withSpan(span)) { - assertThat(CurrentContext.getSpan()).isSameInstanceAs(span); + assertThat(scopeManager.getSpan()).isInstanceOf(DefaultSpan.class); + try (Scope ignored = scopeManager.withSpan(span)) { + assertThat(scopeManager.getSpan()).isSameInstanceAs(span); } - assertThat(CurrentContext.getSpan()).isInstanceOf(DefaultSpan.class); + assertThat(scopeManager.getSpan()).isInstanceOf(DefaultSpan.class); } @Test @@ -198,11 +201,8 @@ public SimpleSpanOperation(TracerSdk tracer) { @Override public void update() { Span span = tracer.spanBuilder("testSpan").startSpan(); - try (Scope ignored = CurrentContext.withSpan(span)) { - span.setAttribute("testAttribute", AttributeValue.stringAttributeValue("testValue")); - } finally { - span.end(); - } + span.setAttribute("testAttribute", AttributeValue.stringAttributeValue("testValue")); + span.end(); } } diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/activespanreplacement/ActiveSpanReplacementTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/activespanreplacement/ActiveSpanReplacementTest.java index 6e271d9f7c9..d522e7f3c1e 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/activespanreplacement/ActiveSpanReplacementTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/activespanreplacement/ActiveSpanReplacementTest.java @@ -22,9 +22,10 @@ import static org.awaitility.Awaitility.await; import static org.hamcrest.core.IsEqual.equalTo; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.Scope; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.trace.DefaultSpan; @@ -40,6 +41,7 @@ @SuppressWarnings("FutureReturnValueIgnored") public class ActiveSpanReplacementTest { + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); private final TracerSdkProvider sdk = TracerSdkProvider.builder().build(); private final InMemoryTracing inMemoryTracing = InMemoryTracing.builder().setTracerProvider(sdk).build(); @@ -50,7 +52,7 @@ public class ActiveSpanReplacementTest { public void test() { // Start an isolated task and query for its result in another task/thread Span span = tracer.spanBuilder("initial").startSpan(); - try (Scope scope = CurrentContext.withSpan(span)) { + try (Scope scope = scopeManager.withSpan(span)) { // Explicitly pass a Span to be finished once a late calculation is done. submitAnotherTask(span); } @@ -73,7 +75,7 @@ public void test() { assertThat(spans.get(0).getTraceId()).isNotEqualTo(spans.get(1).getTraceId()); assertThat(spans.get(0).getParentSpanId()).isEqualTo(SpanId.getInvalid()); - assertThat(CurrentContext.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); + assertThat(scopeManager.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); } private void submitAnotherTask(final Span initialSpan) { @@ -84,11 +86,11 @@ private void submitAnotherTask(final Span initialSpan) { public void run() { // Create a new Span for this task Span taskSpan = tracer.spanBuilder("task").startSpan(); - try (Scope scope = CurrentContext.withSpan(taskSpan)) { + try (Scope scope = scopeManager.withSpan(taskSpan)) { // Simulate work strictly related to the initial Span // and finish it. - try (Scope initialScope = CurrentContext.withSpan(initialSpan)) { + try (Scope initialScope = scopeManager.withSpan(initialSpan)) { sleep(50); } finally { initialSpan.end(); @@ -96,7 +98,7 @@ public void run() { // Restore the span for this task and create a subspan Span subTaskSpan = tracer.spanBuilder("subtask").startSpan(); - try (Scope subTaskScope = CurrentContext.withSpan(subTaskSpan)) { + try (Scope subTaskScope = scopeManager.withSpan(subTaskSpan)) { sleep(50); } finally { subTaskSpan.end(); diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/Actor.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/Actor.java index 4a21c8ffc7f..e8b06e34271 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/Actor.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/Actor.java @@ -16,8 +16,9 @@ package io.opentelemetry.sdk.contrib.trace.testbed.actorpropagation; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.Scope; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.Tracer; @@ -28,6 +29,10 @@ import java.util.concurrent.Phaser; final class Actor implements AutoCloseable { + + // TODO (trask) should be injected + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); + private final ExecutorService executor; private final Tracer tracer; private final Phaser phaser; @@ -46,7 +51,7 @@ public void close() { } Future tell(final String message) { - final Span parent = CurrentContext.getSpan(); + final Span parent = scopeManager.getSpan(); phaser.register(); return executor.submit( new Runnable() { @@ -58,7 +63,7 @@ public void run() { .setParent(parent) .setSpanKind(Kind.CONSUMER) .startSpan(); - try (Scope ignored = CurrentContext.withSpan(child)) { + try (Scope ignored = scopeManager.withSpan(child)) { phaser.arriveAndAwaitAdvance(); // child tracer started child.addEvent("received " + message); phaser.arriveAndAwaitAdvance(); // assert size @@ -73,7 +78,7 @@ public void run() { } Future ask(final String message) { - final Span parent = CurrentContext.getSpan(); + final Span parent = scopeManager.getSpan(); phaser.register(); return executor.submit( new Callable() { diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/ActorPropagationTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/ActorPropagationTest.java index 9601ea9d672..ac66f1cce26 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/ActorPropagationTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/ActorPropagationTest.java @@ -18,9 +18,10 @@ import static com.google.common.truth.Truth.assertThat; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.Scope; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; @@ -44,6 +45,7 @@ */ @SuppressWarnings("FutureReturnValueIgnored") public class ActorPropagationTest { + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); private final TracerSdkProvider sdk = TracerSdkProvider.builder().build(); private final InMemoryTracing inMemoryTracing = InMemoryTracing.builder().setTracerProvider(sdk).build(); @@ -61,7 +63,7 @@ public void testActorTell() { phaser.register(); Span parent = tracer.spanBuilder("actorTell").setSpanKind(Kind.PRODUCER).startSpan(); parent.setAttribute("component", "example-actor"); - try (Scope ignored = CurrentContext.withSpan(parent)) { + try (Scope ignored = scopeManager.withSpan(parent)) { actor.tell("my message 1"); actor.tell("my message 2"); } finally { @@ -85,7 +87,7 @@ public void testActorTell() { assertThat(TestUtils.getByKind(finished, Span.Kind.CONSUMER)).hasSize(2); assertThat(TestUtils.getOneByKind(finished, Span.Kind.PRODUCER)).isNotNull(); - assertThat(CurrentContext.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); + assertThat(scopeManager.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); } } @@ -98,7 +100,7 @@ public void testActorAsk() throws ExecutionException, InterruptedException { Span span = tracer.spanBuilder("actorAsk").setSpanKind(Kind.PRODUCER).startSpan(); span.setAttribute("component", "example-actor"); - try (Scope ignored = CurrentContext.withSpan(span)) { + try (Scope ignored = scopeManager.withSpan(span)) { future1 = actor.ask("my message 1"); future2 = actor.ask("my message 2"); } finally { @@ -126,7 +128,7 @@ public void testActorAsk() throws ExecutionException, InterruptedException { assertThat(TestUtils.getByKind(finished, Span.Kind.CONSUMER)).hasSize(2); assertThat(TestUtils.getOneByKind(finished, Span.Kind.PRODUCER)).isNotNull(); - assertThat(CurrentContext.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); + assertThat(scopeManager.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); } } } diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java index ed3bbe69d44..29c9340c2ab 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java @@ -19,8 +19,9 @@ import io.grpc.Context; import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.Scope; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.Tracer; @@ -28,6 +29,9 @@ final class Client { + // TODO (trask) should be injected + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); + private final ArrayBlockingQueue queue; private final Tracer tracer; @@ -42,7 +46,7 @@ public void send() throws InterruptedException { Span span = tracer.spanBuilder("send").setSpanKind(Kind.CLIENT).startSpan(); span.setAttribute("component", "example-client"); - try (Scope ignored = CurrentContext.withSpan(span)) { + try (Scope ignored = scopeManager.withSpan(span)) { OpenTelemetry.getPropagators() .getHttpTextFormat() .inject( diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java index 28e2c54c5ba..3d439475ef1 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java @@ -19,8 +19,9 @@ import io.grpc.Context; import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.propagation.HttpTextFormat.Getter; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.Scope; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.SpanContext; @@ -32,6 +33,8 @@ final class Server extends Thread { private final ArrayBlockingQueue queue; private final Tracer tracer; + // TODO (trask) should be injected + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); public Server(ArrayBlockingQueue queue, Tracer tracer) { this.queue = queue; @@ -57,9 +60,9 @@ public String get(Message carrier, String key) { tracer.spanBuilder("receive").setSpanKind(Kind.SERVER).setParent(spanContext).startSpan(); span.setAttribute("component", "example-server"); - try (Scope ignored = CurrentContext.withSpan(span)) { + try (Scope ignored = scopeManager.withSpan(span)) { // Simulate work. - CurrentContext.getSpan().addEvent("DoWork"); + scopeManager.getSpan().addEvent("DoWork"); } finally { span.end(); } diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/TestClientServerTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/TestClientServerTest.java index e681f1ac391..01e416f730b 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/TestClientServerTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/TestClientServerTest.java @@ -21,8 +21,9 @@ import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertEquals; -import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.exporters.inmemory.InMemoryTracing; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; @@ -38,6 +39,7 @@ public class TestClientServerTest { + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); private final TracerSdkProvider sdk = TracerSdkProvider.builder().build(); private final InMemoryTracing inMemoryTracing = InMemoryTracing.builder().setTracerProvider(sdk).build(); @@ -74,6 +76,6 @@ public void test() throws Exception { assertThat(finished.get(0).getKind()).isEqualTo(Kind.CLIENT); assertThat(finished.get(1).getKind()).isEqualTo(Kind.SERVER); - assertThat(CurrentContext.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); + assertThat(scopeManager.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); } } diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/concurrentcommonrequesthandler/HandlerTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/concurrentcommonrequesthandler/HandlerTest.java index b8ebe1a4db4..8d8b76a63cd 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/concurrentcommonrequesthandler/HandlerTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/concurrentcommonrequesthandler/HandlerTest.java @@ -18,9 +18,10 @@ import static com.google.common.truth.Truth.assertThat; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.Scope; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; @@ -41,6 +42,7 @@ */ public class HandlerTest { + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); private final TracerSdkProvider sdk = TracerSdkProvider.builder().build(); private final InMemoryTracing inMemoryTracing = InMemoryTracing.builder().setTracerProvider(sdk).build(); @@ -71,14 +73,14 @@ public void two_requests() throws Exception { assertThat(finished.get(0).getParentSpanId()).isEqualTo(SpanId.getInvalid()); assertThat(finished.get(1).getParentSpanId()).isEqualTo(SpanId.getInvalid()); - assertThat(CurrentContext.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); + assertThat(scopeManager.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); } /** Active parent is not picked up by child. */ @Test public void parent_not_picked_up() throws Exception { Span parentSpan = tracer.spanBuilder("parent").startSpan(); - try (Scope ignored = CurrentContext.withSpan(parentSpan)) { + try (Scope ignored = scopeManager.withSpan(parentSpan)) { String response = client.send("no_parent").get(15, TimeUnit.SECONDS); assertThat(response).isEqualTo("no_parent:response"); } finally { @@ -108,7 +110,7 @@ public void parent_not_picked_up() throws Exception { public void bad_solution_to_set_parent() throws Exception { Client client; Span parentSpan = tracer.spanBuilder("parent").startSpan(); - try (Scope ignored = CurrentContext.withSpan(parentSpan)) { + try (Scope ignored = scopeManager.withSpan(parentSpan)) { client = new Client(new RequestHandler(tracer, parentSpan.getContext())); String response = client.send("correct_parent").get(15, TimeUnit.SECONDS); assertThat(response).isEqualTo("correct_parent:response"); diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/errorreporting/ErrorReportingTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/errorreporting/ErrorReportingTest.java index 73d8ca7edc1..c89483eae87 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/errorreporting/ErrorReportingTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/errorreporting/ErrorReportingTest.java @@ -21,9 +21,10 @@ import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertEquals; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.Scope; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; @@ -41,6 +42,7 @@ @SuppressWarnings("FutureReturnValueIgnored") public final class ErrorReportingTest { + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); private final TracerSdkProvider sdk = TracerSdkProvider.builder().build(); private final InMemoryTracing inMemoryTracing = InMemoryTracing.builder().setTracerProvider(sdk).build(); @@ -51,7 +53,7 @@ public final class ErrorReportingTest { @Test public void testSimpleError() { Span span = tracer.spanBuilder("one").startSpan(); - try (Scope ignored = CurrentContext.withSpan(span)) { + try (Scope ignored = scopeManager.withSpan(span)) { throw new RuntimeException("Invalid state"); } catch (Exception e) { span.setStatus(Status.UNKNOWN); @@ -59,7 +61,7 @@ public void testSimpleError() { span.end(); } - assertThat(CurrentContext.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); + assertThat(scopeManager.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); List spans = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); assertThat(spans).hasSize(1); @@ -75,7 +77,7 @@ public void testCallbackError() { new Runnable() { @Override public void run() { - try (Scope ignored = CurrentContext.withSpan(span)) { + try (Scope ignored = scopeManager.withSpan(span)) { throw new RuntimeException("Invalid state"); } catch (Exception exc) { span.setStatus(Status.UNKNOWN); @@ -104,7 +106,7 @@ public void testErrorRecovery() { Object res = null; Span span = tracer.spanBuilder("one").startSpan(); - try (Scope ignored = CurrentContext.withSpan(span)) { + try (Scope ignored = scopeManager.withSpan(span)) { while (res == null && retries++ < maxRetries) { try { throw new RuntimeException("No url could be fetched"); @@ -119,7 +121,7 @@ public void testErrorRecovery() { } span.end(); - assertThat(CurrentContext.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); + assertThat(scopeManager.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); List spans = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); assertThat(spans).hasSize(1); @@ -136,7 +138,7 @@ public void testErrorRecovery() { @Test public void testInstrumentationLayer() { Span span = tracer.spanBuilder("one").startSpan(); - try (Scope ignored = CurrentContext.withSpan(span)) { + try (Scope ignored = scopeManager.withSpan(span)) { // ScopedRunnable captures the active Span at this time. executor.submit( new ScopedRunnable( @@ -146,13 +148,14 @@ public void run() { try { throw new RuntimeException("Invalid state"); } catch (Exception exc) { - CurrentContext.getSpan().setStatus(Status.UNKNOWN); + scopeManager.getSpan().setStatus(Status.UNKNOWN); } finally { - CurrentContext.getSpan().end(); + scopeManager.getSpan().end(); } } }, - tracer)); + tracer, + scopeManager)); } await() @@ -167,18 +170,20 @@ public void run() { private static class ScopedRunnable implements Runnable { Runnable runnable; Tracer tracer; + ScopeManager scopeManager; Span span; - private ScopedRunnable(Runnable runnable, Tracer tracer) { + private ScopedRunnable(Runnable runnable, Tracer tracer, ScopeManager scopeManager) { this.runnable = runnable; this.tracer = tracer; - this.span = CurrentContext.getSpan(); + this.scopeManager = scopeManager; + this.span = scopeManager.getSpan(); } @Override public void run() { // No error reporting is done, as we are a simple wrapper. - try (Scope ignored = CurrentContext.withSpan(span)) { + try (Scope ignored = scopeManager.withSpan(span)) { runnable.run(); } } diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/latespanfinish/LateSpanFinishTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/latespanfinish/LateSpanFinishTest.java index dc3122a83d3..661dd6c19f9 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/latespanfinish/LateSpanFinishTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/latespanfinish/LateSpanFinishTest.java @@ -18,9 +18,10 @@ import static com.google.common.truth.Truth.assertThat; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.Scope; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; @@ -35,6 +36,7 @@ @SuppressWarnings("FutureReturnValueIgnored") public final class LateSpanFinishTest { + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); private final TracerSdkProvider sdk = TracerSdkProvider.builder().build(); private final InMemoryTracing inMemoryTracing = InMemoryTracing.builder().setTracerProvider(sdk).build(); @@ -63,7 +65,7 @@ public void test() throws Exception { TestUtils.assertSameTrace(spans); - assertThat(CurrentContext.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); + assertThat(scopeManager.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); } /* @@ -78,9 +80,9 @@ private void submitTasks(final Span parentSpan) { public void run() { /* Alternative to calling activate() is to pass it manually to asChildOf() for each * created Span. */ - try (Scope scope = CurrentContext.withSpan(parentSpan)) { + try (Scope scope = scopeManager.withSpan(parentSpan)) { Span childSpan = tracer.spanBuilder("task1").startSpan(); - try (Scope childScope = CurrentContext.withSpan(childSpan)) { + try (Scope childScope = scopeManager.withSpan(childSpan)) { TestUtils.sleep(55); } finally { childSpan.end(); @@ -93,9 +95,9 @@ public void run() { new Runnable() { @Override public void run() { - try (Scope scope = CurrentContext.withSpan(parentSpan)) { + try (Scope scope = scopeManager.withSpan(parentSpan)) { Span childSpan = tracer.spanBuilder("task2").startSpan(); - try (Scope childScope = CurrentContext.withSpan(childSpan)) { + try (Scope childScope = scopeManager.withSpan(childSpan)) { TestUtils.sleep(85); } finally { childSpan.end(); diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/listenerperrequest/ListenerTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/listenerperrequest/ListenerTest.java index c8eace80149..5d0e5c3899c 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/listenerperrequest/ListenerTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/listenerperrequest/ListenerTest.java @@ -18,8 +18,9 @@ import static com.google.common.truth.Truth.assertThat; -import io.opentelemetry.currentcontext.CurrentContext; import io.opentelemetry.exporters.inmemory.InMemoryTracing; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.trace.DefaultSpan; @@ -30,6 +31,7 @@ /** Each request has own instance of ResponseListener. */ public class ListenerTest { + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); private final TracerSdkProvider sdk = TracerSdkProvider.builder().build(); private final InMemoryTracing inMemoryTracing = InMemoryTracing.builder().setTracerProvider(sdk).build(); @@ -45,6 +47,6 @@ public void test() throws Exception { assertThat(finished).hasSize(1); assertThat(finished.get(0).getKind()).isEqualTo(Kind.CLIENT); - assertThat(CurrentContext.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); + assertThat(scopeManager.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); } } diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/Client.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/Client.java index d10291191a2..a0be44098d9 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/Client.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/Client.java @@ -16,8 +16,9 @@ package io.opentelemetry.sdk.contrib.trace.testbed.multiplecallbacks; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.Scope; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Tracer; import java.util.concurrent.Callable; @@ -30,6 +31,8 @@ class Client { private final ExecutorService executor = Executors.newCachedThreadPool(); private final CountDownLatch parentDoneLatch; private final Tracer tracer; + // TODO (trask) should be injected + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); public Client(Tracer tracer, CountDownLatch parentDoneLatch) { this.tracer = tracer; @@ -37,14 +40,14 @@ public Client(Tracer tracer, CountDownLatch parentDoneLatch) { } public Future send(final Object message) { - final Span parent = CurrentContext.getSpan(); + final Span parent = scopeManager.getSpan(); return executor.submit( new Callable() { @Override public Object call() throws Exception { Span span = tracer.spanBuilder("subtask").setParent(parent).startSpan(); - try (Scope subtaskScope = CurrentContext.withSpan(span)) { + try (Scope subtaskScope = scopeManager.withSpan(span)) { // Simulate work - make sure we finish *after* the parent Span. parentDoneLatch.await(); } finally { diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/MultipleCallbacksTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/MultipleCallbacksTest.java index b5731baa472..7947e301d57 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/MultipleCallbacksTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/MultipleCallbacksTest.java @@ -20,9 +20,10 @@ import static org.awaitility.Awaitility.await; import static org.hamcrest.core.IsEqual.equalTo; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.Scope; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; @@ -42,6 +43,7 @@ */ @SuppressWarnings("FutureReturnValueIgnored") public class MultipleCallbacksTest { + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); private final TracerSdkProvider sdk = TracerSdkProvider.builder().build(); private final InMemoryTracing inMemoryTracing = InMemoryTracing.builder().setTracerProvider(sdk).build(); @@ -53,7 +55,7 @@ public void test() { Client client = new Client(tracer, parentDoneLatch); Span span = tracer.spanBuilder("parent").startSpan(); - try (Scope scope = CurrentContext.withSpan(span)) { + try (Scope scope = scopeManager.withSpan(span)) { client.send("task1"); client.send("task2"); client.send("task3"); @@ -76,6 +78,6 @@ public void test() { assertThat(spans.get(i).getParentSpanId()).isEqualTo(parentSpan.getSpanId()); } - assertThat(CurrentContext.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); + assertThat(scopeManager.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); } } diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/nestedcallbacks/NestedCallbacksTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/nestedcallbacks/NestedCallbacksTest.java index bf4de83a688..9bed14ae37d 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/nestedcallbacks/NestedCallbacksTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/nestedcallbacks/NestedCallbacksTest.java @@ -21,9 +21,10 @@ import static org.hamcrest.core.IsEqual.equalTo; import io.opentelemetry.common.AttributeValue; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.Scope; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; @@ -40,6 +41,7 @@ @SuppressWarnings("FutureReturnValueIgnored") public final class NestedCallbacksTest { + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); private final TracerSdkProvider sdk = TracerSdkProvider.builder().build(); private final InMemoryTracing inMemoryTracing = InMemoryTracing.builder().setTracerProvider(sdk).build(); @@ -66,7 +68,7 @@ public void test() { assertThat(attrs.get("key" + i).getStringValue()).isEqualTo(Integer.toString(i)); } - assertThat(CurrentContext.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); + assertThat(scopeManager.getSpan()).isSameInstanceAs(DefaultSpan.getInvalid()); } private void submitCallbacks(final Span span) { @@ -75,21 +77,21 @@ private void submitCallbacks(final Span span) { new Runnable() { @Override public void run() { - try (Scope ignored = CurrentContext.withSpan(span)) { + try (Scope ignored = scopeManager.withSpan(span)) { span.setAttribute("key1", "1"); executor.submit( new Runnable() { @Override public void run() { - try (Scope ignored = CurrentContext.withSpan(span)) { + try (Scope ignored = scopeManager.withSpan(span)) { span.setAttribute("key2", "2"); executor.submit( new Runnable() { @Override public void run() { - try (Scope ignored = CurrentContext.withSpan(span)) { + try (Scope ignored = scopeManager.withSpan(span)) { span.setAttribute("key3", "3"); } finally { span.end(); diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/Promise.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/Promise.java index e9d6239a55a..dea4214f56d 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/Promise.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/Promise.java @@ -16,8 +16,9 @@ package io.opentelemetry.sdk.contrib.trace.testbed.promisepropagation; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.Scope; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Tracer; import java.util.ArrayList; @@ -26,6 +27,8 @@ final class Promise { private final PromiseContext context; private final Tracer tracer; + // TODO (trask) should be injected + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); private final Span parentSpan; private final Collection> successCallbacks = new ArrayList<>(); @@ -36,7 +39,7 @@ final class Promise { // Passed along here for testing. Normally should be referenced via GlobalTracer.get(). this.tracer = tracer; - parentSpan = CurrentContext.getSpan(); + parentSpan = scopeManager.getSpan(); } void onSuccess(SuccessCallback successCallback) { @@ -56,7 +59,7 @@ void success(final T result) { public void run() { Span childSpan = tracer.spanBuilder("success").setParent(parentSpan).startSpan(); childSpan.setAttribute("component", "success"); - try (Scope ignored = CurrentContext.withSpan(childSpan)) { + try (Scope ignored = scopeManager.withSpan(childSpan)) { callback.accept(result); } finally { childSpan.end(); @@ -76,7 +79,7 @@ void error(final Throwable error) { public void run() { Span childSpan = tracer.spanBuilder("error").setParent(parentSpan).startSpan(); childSpan.setAttribute("component", "error"); - try (Scope ignored = CurrentContext.withSpan(childSpan)) { + try (Scope ignored = scopeManager.withSpan(childSpan)) { callback.accept(error); } finally { childSpan.end(); diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/PromisePropagationTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/PromisePropagationTest.java index 74b305e7e48..235d5ecf2e9 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/PromisePropagationTest.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/PromisePropagationTest.java @@ -18,9 +18,10 @@ import static com.google.common.truth.Truth.assertThat; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.Scope; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.sdk.contrib.trace.testbed.TestUtils; import io.opentelemetry.sdk.trace.TracerSdkProvider; import io.opentelemetry.sdk.trace.data.SpanData; @@ -41,6 +42,7 @@ * execution for the tests without sleeps. */ public class PromisePropagationTest { + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); private final TracerSdkProvider sdk = TracerSdkProvider.builder().build(); private final InMemoryTracing inMemoryTracing = InMemoryTracing.builder().setTracerProvider(sdk).build(); @@ -63,14 +65,14 @@ public void testPromiseCallback() { Span parentSpan = tracer.spanBuilder("promises").startSpan(); parentSpan.setAttribute("component", "example-promises"); - try (Scope ignored = CurrentContext.withSpan(parentSpan)) { + try (Scope ignored = scopeManager.withSpan(parentSpan)) { Promise successPromise = new Promise<>(context, tracer); successPromise.onSuccess( new Promise.SuccessCallback() { @Override public void accept(String s) { - CurrentContext.getSpan().addEvent("Promised 1 " + s); + scopeManager.getSpan().addEvent("Promised 1 " + s); successResult1.set(s); phaser.arriveAndAwaitAdvance(); // result set } @@ -79,7 +81,7 @@ public void accept(String s) { new Promise.SuccessCallback() { @Override public void accept(String s) { - CurrentContext.getSpan().addEvent("Promised 2 " + s); + scopeManager.getSpan().addEvent("Promised 2 " + s); successResult2.set(s); phaser.arriveAndAwaitAdvance(); // result set } diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/statelesscommonrequesthandler/RequestHandler.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/statelesscommonrequesthandler/RequestHandler.java index ae8fcac5bf1..7316fb82385 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/statelesscommonrequesthandler/RequestHandler.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/statelesscommonrequesthandler/RequestHandler.java @@ -16,8 +16,9 @@ package io.opentelemetry.sdk.contrib.trace.testbed.statelesscommonrequesthandler; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.Scope; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.Tracer; @@ -32,6 +33,8 @@ final class RequestHandler { static final String OPERATION_NAME = "send"; private final Tracer tracer; + // TODO (trask) should be injected + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); private static final ThreadLocal tlsScope = new ThreadLocal<>(); @@ -42,13 +45,13 @@ public RequestHandler(Tracer tracer) { /** beforeRequest handler....... */ public void beforeRequest(Object request) { Span span = tracer.spanBuilder(OPERATION_NAME).setSpanKind(Kind.SERVER).startSpan(); - tlsScope.set(CurrentContext.withSpan(span)); + tlsScope.set(scopeManager.withSpan(span)); } /** afterResponse handler....... */ public void afterResponse(Object response) { // Finish the Span - CurrentContext.getSpan().end(); + scopeManager.getSpan().end(); // Deactivate the Span tlsScope.get().close(); diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/suspendresumepropagation/SuspendResume.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/suspendresumepropagation/SuspendResume.java index fb607cbbba6..b76b280cd69 100644 --- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/suspendresumepropagation/SuspendResume.java +++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/suspendresumepropagation/SuspendResume.java @@ -16,26 +16,29 @@ package io.opentelemetry.sdk.contrib.trace.testbed.suspendresumepropagation; -import io.opentelemetry.currentcontext.CurrentContext; -import io.opentelemetry.currentcontext.Scope; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.Scope; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Tracer; final class SuspendResume { private final Span span; + // TODO (trask) should be injected + private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); public SuspendResume(int id, Tracer tracer) { // Tracer passed along here for testing. Normally should be referenced via GlobalTracer.get(). Span span = tracer.spanBuilder("job " + id).startSpan(); span.setAttribute("component", "suspend-resume"); - try (Scope scope = CurrentContext.withSpan(span)) { + try (Scope scope = scopeManager.withSpan(span)) { this.span = span; } } public void doPart(String name) { - try (Scope scope = CurrentContext.withSpan(span)) { + try (Scope scope = scopeManager.withSpan(span)) { span.addEvent("part: " + name); } } From ae5fd3813cb2b260617dc1aaa3a2ac43eafbf197 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Thu, 14 May 2020 18:01:32 -0700 Subject: [PATCH 22/22] More ScopeManager --- .../trace/DefaultTracerBenchmarks.java | 2 +- .../java/io/opentelemetry/OpenTelemetry.java | 21 ++++++++++++---- ...aultCorrelationContextManagerProvider.java | 3 ++- .../CorrelationContextManagerProvider.java | 3 ++- .../scope/DefaultScopeManager.java | 1 - .../trace/DefaultTraceProvider.java | 5 ++-- .../io/opentelemetry/trace/DefaultTracer.java | 21 ++++------------ .../trace/DefaultTracerProvider.java | 18 ++++---------- .../trace/spi/TraceProvider.java | 3 ++- .../io/opentelemetry/OpenTelemetryTest.java | 9 +++---- .../trace/DefaultTracerTest.java | 2 +- .../opentelemetry/trace/SpanBuilderTest.java | 3 ++- .../exporters/inmemory/InMemoryTracing.java | 2 +- .../inmemory/InMemorySpanExporterTest.java | 6 ++++- .../inmemory/InMemoryTracingTest.java | 6 ++++- .../opentracingshim/BaseShimObject.java | 5 ++++ .../opentracingshim/NoopSpanBuilderShim.java | 2 +- .../opentracingshim/ScopeManagerShim.java | 9 ++----- .../opentracingshim/TelemetryInfo.java | 12 +++++++++- .../opentracingshim/TraceShim.java | 7 +++++- .../opentracingshim/SpanBuilderShimTest.java | 12 ++++++++-- .../opentracingshim/SpanShimTest.java | 12 ++++++++-- .../opentracingshim/TraceShimTest.java | 24 +++++++++++++++---- .../opentracingshim/TracerShimTest.java | 1 + .../OpenTelemetryInteroperabilityTest.java | 4 ++-- .../ActiveSpanReplacementTest.java | 8 +++++-- .../ActorPropagationTest.java | 8 +++++-- .../baggagehandling/BaggageHandlingTest.java | 8 +++++-- .../clientserver/TestClientServerTest.java | 8 +++++-- .../HandlerTest.java | 8 +++++-- .../errorreporting/ErrorReportingTest.java | 8 +++++-- .../latespanfinish/LateSpanFinishTest.java | 8 +++++-- .../listenerperrequest/ListenerTest.java | 8 +++++-- .../MultipleCallbacksTest.java | 8 +++++-- .../nestedcallbacks/NestedCallbacksTest.java | 8 +++++-- .../PromisePropagationTest.java | 8 +++++-- .../HandlerTest.java | 8 +++++-- .../SuspendResumePropagationTest.java | 8 +++++-- .../CorrelationContextManagerSdk.java | 8 ++++--- .../CorrelationContextManagerProviderSdk.java | 5 ++-- .../io/opentelemetry/sdk/trace/TracerSdk.java | 8 ++----- .../sdk/trace/TracerSdkProvider.java | 17 ++++++++----- .../sdk/trace/TracerSharedState.java | 10 +++++++- .../sdk/trace/spi/TraceProviderSdk.java | 5 ++-- .../CorrelationContextSdkTest.java | 3 ++- .../ScopedCorrelationContextTest.java | 3 ++- .../sdk/trace/SpanBuilderSdkTest.java | 3 ++- .../sdk/trace/TracerSdkProviderTest.java | 13 ++++++---- .../sdk/trace/TracerSdkTest.java | 6 ++--- .../trace/export/BatchSpansProcessorTest.java | 6 ++++- .../export/SimpleSpansProcessorTest.java | 6 ++++- .../sdk/trace/spi/TraceProviderSdkTest.java | 5 +++- .../ActiveSpanReplacementTest.java | 2 +- .../trace/testbed/actorpropagation/Actor.java | 4 ++-- .../ActorPropagationTest.java | 2 +- .../trace/testbed/clientserver/Client.java | 3 +-- .../trace/testbed/clientserver/Server.java | 3 +-- .../clientserver/TestClientServerTest.java | 2 +- .../HandlerTest.java | 2 +- .../errorreporting/ErrorReportingTest.java | 2 +- .../latespanfinish/LateSpanFinishTest.java | 2 +- .../listenerperrequest/ListenerTest.java | 2 +- .../testbed/multiplecallbacks/Client.java | 4 ++-- .../MultipleCallbacksTest.java | 2 +- .../nestedcallbacks/NestedCallbacksTest.java | 2 +- .../testbed/promisepropagation/Promise.java | 4 ++-- .../PromisePropagationTest.java | 2 +- .../HandlerTest.java | 5 +++- .../RequestHandler.java | 4 ++-- .../SuspendResume.java | 4 ++-- .../SuspendResumePropagationTest.java | 5 +++- 71 files changed, 294 insertions(+), 157 deletions(-) diff --git a/api/src/jmh/java/io/opentelemetry/trace/DefaultTracerBenchmarks.java b/api/src/jmh/java/io/opentelemetry/trace/DefaultTracerBenchmarks.java index 930c954e44b..f2e682df6e9 100644 --- a/api/src/jmh/java/io/opentelemetry/trace/DefaultTracerBenchmarks.java +++ b/api/src/jmh/java/io/opentelemetry/trace/DefaultTracerBenchmarks.java @@ -35,8 +35,8 @@ @State(Scope.Thread) public class DefaultTracerBenchmarks { - private final Tracer tracer = DefaultTracer.getInstance(); private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); + private final Tracer tracer = new DefaultTracer(scopeManager); @Nullable private Span span = null; /** Benchmark the full span lifecycle. */ diff --git a/api/src/main/java/io/opentelemetry/OpenTelemetry.java b/api/src/main/java/io/opentelemetry/OpenTelemetry.java index 328deeae1fa..286e567744b 100644 --- a/api/src/main/java/io/opentelemetry/OpenTelemetry.java +++ b/api/src/main/java/io/opentelemetry/OpenTelemetry.java @@ -29,6 +29,8 @@ import io.opentelemetry.metrics.Meter; import io.opentelemetry.metrics.MeterProvider; import io.opentelemetry.metrics.spi.MetricsProvider; +import io.opentelemetry.scope.DefaultScopeManager; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.trace.DefaultTraceProvider; import io.opentelemetry.trace.DefaultTracerProvider; import io.opentelemetry.trace.Tracer; @@ -58,6 +60,7 @@ public final class OpenTelemetry { private final TracerProvider tracerProvider; private final MeterProvider meterProvider; private final CorrelationContextManager contextManager; + private final ScopeManager scopeManager; private volatile ContextPropagators propagators = DefaultContextPropagators.builder().addHttpTextFormat(new HttpTraceContext()).build(); @@ -65,7 +68,8 @@ public final class OpenTelemetry { /** * Returns a singleton {@link TracerProvider}. * - * @return registered TracerProvider or default via {@link DefaultTracerProvider#getInstance()}. + * @return registered TracerProvider or default via {@link + * DefaultTracerProvider#DefaultTracerProvider(ScopeManager)}. * @throws IllegalStateException if a specified TracerProvider (via system properties) could not * be found. * @since 0.1.0 @@ -160,6 +164,11 @@ public static CorrelationContextManager getCorrelationContextManager() { return getInstance().contextManager; } + // TODO (trask) javadoc + public static ScopeManager getScopeManager() { + return getInstance().scopeManager; + } + /** * Returns a {@link ContextPropagators} object, which can be used to access the set of registered * propagators for each supported format. @@ -202,11 +211,13 @@ private static OpenTelemetry getInstance() { } private OpenTelemetry() { + // TODO (trask) should this come from SPI? + scopeManager = DefaultScopeManager.getInstance(); TraceProvider traceProvider = loadSpi(TraceProvider.class); this.tracerProvider = traceProvider != null - ? new ObfuscatedTracerProvider(traceProvider.create()) - : DefaultTraceProvider.getInstance().create(); + ? new ObfuscatedTracerProvider(traceProvider.create(scopeManager)) + : DefaultTraceProvider.getInstance().create(scopeManager); MetricsProvider metricsProvider = loadSpi(MetricsProvider.class); meterProvider = @@ -217,8 +228,8 @@ private OpenTelemetry() { loadSpi(CorrelationContextManagerProvider.class); contextManager = contextManagerProvider != null - ? contextManagerProvider.create() - : DefaultCorrelationContextManagerProvider.getInstance().create(); + ? contextManagerProvider.create(scopeManager) + : DefaultCorrelationContextManagerProvider.getInstance().create(scopeManager); } /** diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerProvider.java b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerProvider.java index 885a200d23c..d8b44d2fa5c 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerProvider.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/DefaultCorrelationContextManagerProvider.java @@ -17,6 +17,7 @@ package io.opentelemetry.correlationcontext; import io.opentelemetry.correlationcontext.spi.CorrelationContextManagerProvider; +import io.opentelemetry.scope.ScopeManager; import javax.annotation.concurrent.ThreadSafe; @ThreadSafe @@ -37,7 +38,7 @@ public static CorrelationContextManagerProvider getInstance() { } @Override - public CorrelationContextManager create() { + public CorrelationContextManager create(ScopeManager scopeManager) { return DefaultCorrelationContextManager.getInstance(); } diff --git a/api/src/main/java/io/opentelemetry/correlationcontext/spi/CorrelationContextManagerProvider.java b/api/src/main/java/io/opentelemetry/correlationcontext/spi/CorrelationContextManagerProvider.java index 465f1268900..7c04513b048 100644 --- a/api/src/main/java/io/opentelemetry/correlationcontext/spi/CorrelationContextManagerProvider.java +++ b/api/src/main/java/io/opentelemetry/correlationcontext/spi/CorrelationContextManagerProvider.java @@ -17,6 +17,7 @@ package io.opentelemetry.correlationcontext.spi; import io.opentelemetry.correlationcontext.CorrelationContextManager; +import io.opentelemetry.scope.ScopeManager; import javax.annotation.concurrent.ThreadSafe; /** @@ -40,5 +41,5 @@ public interface CorrelationContextManagerProvider { * @return a {@code CorrelationContextManager} instance. * @since 0.1.0 */ - CorrelationContextManager create(); + CorrelationContextManager create(ScopeManager scopeManager); } diff --git a/api/src/main/java/io/opentelemetry/scope/DefaultScopeManager.java b/api/src/main/java/io/opentelemetry/scope/DefaultScopeManager.java index 04c28b21e8b..a22a2023316 100644 --- a/api/src/main/java/io/opentelemetry/scope/DefaultScopeManager.java +++ b/api/src/main/java/io/opentelemetry/scope/DefaultScopeManager.java @@ -20,7 +20,6 @@ import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.trace.Span; -/** Static methods for interacting with the current (thread-bound) context. */ // TODO (trask) javadoc class and methods public class DefaultScopeManager implements ScopeManager { private static final DefaultScopeManager INSTANCE = new DefaultScopeManager(); diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultTraceProvider.java b/api/src/main/java/io/opentelemetry/trace/DefaultTraceProvider.java index 0431938fb89..c442979b523 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultTraceProvider.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultTraceProvider.java @@ -16,6 +16,7 @@ package io.opentelemetry.trace; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.trace.spi.TraceProvider; import javax.annotation.concurrent.ThreadSafe; @@ -35,8 +36,8 @@ public static TraceProvider getInstance() { } @Override - public TracerProvider create() { - return DefaultTracerProvider.getInstance(); + public TracerProvider create(ScopeManager scopeManager) { + return new DefaultTracerProvider(scopeManager); } private DefaultTraceProvider() {} diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java index f8d3b363217..b5ee182f9b1 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java @@ -18,7 +18,6 @@ import io.opentelemetry.common.AttributeValue; import io.opentelemetry.internal.Utils; -import io.opentelemetry.scope.DefaultScopeManager; import io.opentelemetry.scope.ScopeManager; import java.util.Map; import javax.annotation.Nullable; @@ -31,28 +30,18 @@ */ @ThreadSafe public final class DefaultTracer implements Tracer { - private static final DefaultTracer INSTANCE = new DefaultTracer(); - - /** - * Returns a {@code Tracer} singleton that is the default implementations for {@link Tracer}. - * - * @return a {@code Tracer} singleton that is the default implementations for {@link Tracer}. - * @since 0.1.0 - */ - public static Tracer getInstance() { - return INSTANCE; - } - // TODO (trask) should be injected - private final ScopeManager scopeManager = DefaultScopeManager.getInstance(); + private final ScopeManager scopeManager; + + public DefaultTracer(ScopeManager scopeManager) { + this.scopeManager = scopeManager; + } @Override public Span.Builder spanBuilder(String spanName) { return NoopSpanBuilder.create(spanName, scopeManager); } - private DefaultTracer() {} - // Noop implementation of Span.Builder. private static final class NoopSpanBuilder implements Span.Builder { static NoopSpanBuilder create(String spanName, ScopeManager scopeManager) { diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultTracerProvider.java b/api/src/main/java/io/opentelemetry/trace/DefaultTracerProvider.java index f02ebf74b97..579249aca62 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultTracerProvider.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultTracerProvider.java @@ -16,22 +16,16 @@ package io.opentelemetry.trace; +import io.opentelemetry.scope.ScopeManager; import javax.annotation.concurrent.ThreadSafe; @ThreadSafe public class DefaultTracerProvider implements TracerProvider { - private static final TracerProvider instance = new DefaultTracerProvider(); + private final Tracer tracer; - /** - * Returns a {@code TracerProvider} singleton that is the default implementation for {@link - * TracerProvider}. - * - * @return a {@code TracerProvider} singleton that is the default implementation for {@link - * TracerProvider}. - */ - public static TracerProvider getInstance() { - return instance; + public DefaultTracerProvider(ScopeManager scopeManager) { + tracer = new DefaultTracer(scopeManager); } @Override @@ -41,8 +35,6 @@ public Tracer get(String instrumentationName) { @Override public Tracer get(String instrumentationName, String instrumentationVersion) { - return DefaultTracer.getInstance(); + return tracer; } - - private DefaultTracerProvider() {} } diff --git a/api/src/main/java/io/opentelemetry/trace/spi/TraceProvider.java b/api/src/main/java/io/opentelemetry/trace/spi/TraceProvider.java index 57ba982729d..95ba6d517a5 100644 --- a/api/src/main/java/io/opentelemetry/trace/spi/TraceProvider.java +++ b/api/src/main/java/io/opentelemetry/trace/spi/TraceProvider.java @@ -16,6 +16,7 @@ package io.opentelemetry.trace.spi; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.trace.TracerProvider; import javax.annotation.concurrent.ThreadSafe; @@ -38,5 +39,5 @@ public interface TraceProvider { * @return a new TracerProvider. * @since 0.1.0 */ - TracerProvider create(); + TracerProvider create(ScopeManager scopeManager); } diff --git a/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java b/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java index 973b7440418..92d6a2f506a 100644 --- a/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java +++ b/api/src/test/java/io/opentelemetry/OpenTelemetryTest.java @@ -36,6 +36,7 @@ import io.opentelemetry.metrics.Meter; import io.opentelemetry.metrics.MeterProvider; import io.opentelemetry.metrics.spi.MetricsProvider; +import io.opentelemetry.scope.ScopeManager; import io.opentelemetry.trace.DefaultTracerProvider; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Tracer; @@ -242,7 +243,7 @@ public Tracer get(String instrumentationName, String instrumentationVersion) { } @Override - public TracerProvider create() { + public TracerProvider create(ScopeManager scopeManager) { return new SecondTraceProvider(); } } @@ -265,7 +266,7 @@ public Span.Builder spanBuilder(String spanName) { } @Override - public TracerProvider create() { + public TracerProvider create(ScopeManager scopeManager) { return new FirstTraceProvider(); } } @@ -348,7 +349,7 @@ public Meter get(String instrumentationName, String instrumentationVersion) { public static class SecondCorrelationContextManager extends FirstCorrelationContextManager { @Override - public CorrelationContextManager create() { + public CorrelationContextManager create(ScopeManager scopeManager) { return new SecondCorrelationContextManager(); } } @@ -356,7 +357,7 @@ public CorrelationContextManager create() { public static class FirstCorrelationContextManager implements CorrelationContextManager, CorrelationContextManagerProvider { @Override - public CorrelationContextManager create() { + public CorrelationContextManager create(ScopeManager scopeManager) { return new FirstCorrelationContextManager(); } diff --git a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java index bbafefca484..b912ed516c5 100644 --- a/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java +++ b/api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java @@ -35,7 +35,7 @@ @SuppressWarnings("MustBeClosedChecker") public class DefaultTracerTest { private static final ScopeManager defaultScopeManager = DefaultScopeManager.getInstance(); - private static final Tracer defaultTracer = DefaultTracer.getInstance(); + private static final Tracer defaultTracer = new DefaultTracer(defaultScopeManager); private static final String SPAN_NAME = "MySpanName"; private static final byte[] firstBytes = new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'a'}; diff --git a/api/src/test/java/io/opentelemetry/trace/SpanBuilderTest.java b/api/src/test/java/io/opentelemetry/trace/SpanBuilderTest.java index 27094c41c5f..eda5df152c7 100644 --- a/api/src/test/java/io/opentelemetry/trace/SpanBuilderTest.java +++ b/api/src/test/java/io/opentelemetry/trace/SpanBuilderTest.java @@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat; import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.scope.DefaultScopeManager; import io.opentelemetry.trace.Span.Kind; import java.util.Collections; import java.util.Map; @@ -31,7 +32,7 @@ /** Unit tests for {@link Span.Builder}. */ @RunWith(JUnit4.class) public class SpanBuilderTest { - private final Tracer tracer = DefaultTracer.getInstance(); + private final Tracer tracer = new DefaultTracer(DefaultScopeManager.getInstance()); @Test public void doNotCrash_NoopImplementation() { diff --git a/exporters/inmemory/src/main/java/io/opentelemetry/exporters/inmemory/InMemoryTracing.java b/exporters/inmemory/src/main/java/io/opentelemetry/exporters/inmemory/InMemoryTracing.java index 2cb5c2b8858..788cd260785 100644 --- a/exporters/inmemory/src/main/java/io/opentelemetry/exporters/inmemory/InMemoryTracing.java +++ b/exporters/inmemory/src/main/java/io/opentelemetry/exporters/inmemory/InMemoryTracing.java @@ -31,7 +31,7 @@ *

  *   {@literal @}Test
  *   public void testCondition() {
- *     TracerSdkProvider tracerSdkProvider = TracerSdkProvider.builder().build()
+ *     TracerSdkProvider tracerSdkProvider = TracerSdkProvider.builder(scopeManager).build()
  *     InMemoryTracing tracing =
  *         InMemoryTracing.builder().setTracerSdkProvider(tracerSdkProvider).build();
  *     Tracer tracer = tracerSdkProvider.getTracer("MyTestClass");
diff --git a/exporters/inmemory/src/test/java/io/opentelemetry/exporters/inmemory/InMemorySpanExporterTest.java b/exporters/inmemory/src/test/java/io/opentelemetry/exporters/inmemory/InMemorySpanExporterTest.java
index 4d6237d9342..159c187bc00 100644
--- a/exporters/inmemory/src/test/java/io/opentelemetry/exporters/inmemory/InMemorySpanExporterTest.java
+++ b/exporters/inmemory/src/test/java/io/opentelemetry/exporters/inmemory/InMemorySpanExporterTest.java
@@ -18,6 +18,8 @@
 
 import static com.google.common.truth.Truth.assertThat;
 
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentelemetry.sdk.trace.data.SpanData;
 import io.opentelemetry.sdk.trace.export.SimpleSpansProcessor;
@@ -33,7 +35,9 @@
 /** Unit tests for {@link InMemorySpanExporter}. */
 @RunWith(JUnit4.class)
 public class InMemorySpanExporterTest {
-  private final TracerSdkProvider tracerSdkProvider = TracerSdkProvider.builder().build();
+  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final TracerSdkProvider tracerSdkProvider =
+      TracerSdkProvider.builder(scopeManager).build();
   private final Tracer tracer = tracerSdkProvider.get("InMemorySpanExporterTest");
   private final InMemorySpanExporter exporter = InMemorySpanExporter.create();
 
diff --git a/exporters/inmemory/src/test/java/io/opentelemetry/exporters/inmemory/InMemoryTracingTest.java b/exporters/inmemory/src/test/java/io/opentelemetry/exporters/inmemory/InMemoryTracingTest.java
index 91de4fa1d8a..c77564bda7a 100644
--- a/exporters/inmemory/src/test/java/io/opentelemetry/exporters/inmemory/InMemoryTracingTest.java
+++ b/exporters/inmemory/src/test/java/io/opentelemetry/exporters/inmemory/InMemoryTracingTest.java
@@ -18,6 +18,8 @@
 
 import static com.google.common.truth.Truth.assertThat;
 
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.trace.Samplers;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentelemetry.sdk.trace.config.TraceConfig;
@@ -33,7 +35,9 @@
 /** Unit tests for {@link InMemoryTracing}. */
 @RunWith(JUnit4.class)
 public class InMemoryTracingTest {
-  private final TracerSdkProvider tracerSdkProvider = TracerSdkProvider.builder().build();
+  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final TracerSdkProvider tracerSdkProvider =
+      TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing tracing =
       InMemoryTracing.builder().setTracerProvider(tracerSdkProvider).build();
   private final Tracer tracer = tracerSdkProvider.get("InMemoryTracing");
diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/BaseShimObject.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/BaseShimObject.java
index 53f9389197f..380dfeb653f 100644
--- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/BaseShimObject.java
+++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/BaseShimObject.java
@@ -18,6 +18,7 @@
 
 import io.opentelemetry.context.propagation.ContextPropagators;
 import io.opentelemetry.correlationcontext.CorrelationContextManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.trace.Tracer;
 
 abstract class BaseShimObject {
@@ -46,4 +47,8 @@ SpanContextShimTable spanContextTable() {
   ContextPropagators propagators() {
     return telemetryInfo.propagators();
   }
+
+  ScopeManager otelScopeManager() {
+    return telemetryInfo.scopeManager();
+  }
 }
diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/NoopSpanBuilderShim.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/NoopSpanBuilderShim.java
index 86f289cd569..9fbffa0ae8f 100644
--- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/NoopSpanBuilderShim.java
+++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/NoopSpanBuilderShim.java
@@ -78,6 +78,6 @@ public SpanBuilder withStartTimestamp(long microseconds) {
   @Override
   public Span start() {
     return new SpanShim(
-        telemetryInfo, DefaultTracer.getInstance().spanBuilder(spanName).startSpan());
+        telemetryInfo, new DefaultTracer(otelScopeManager()).spanBuilder(spanName).startSpan());
   }
 }
diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeManagerShim.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeManagerShim.java
index 79a855d02af..259fcdcf7c2 100644
--- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeManagerShim.java
+++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/ScopeManagerShim.java
@@ -16,16 +16,11 @@
 
 package io.opentelemetry.opentracingshim;
 
-import io.opentelemetry.scope.DefaultScopeManager;
-import io.opentelemetry.scope.ScopeManager;
 import io.opentracing.Scope;
 import io.opentracing.Span;
 
 final class ScopeManagerShim extends BaseShimObject implements io.opentracing.ScopeManager {
 
-  // TODO (trask) should be injected
-  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
-
   public ScopeManagerShim(TelemetryInfo telemetryInfo) {
     super(telemetryInfo);
   }
@@ -36,7 +31,7 @@ public Span activeSpan() {
     // As OpenTracing simply returns null when no active instance is available,
     // we need to do an explicit check against DefaultSpan,
     // which is used in OpenTelemetry for this very case.
-    io.opentelemetry.trace.Span span = scopeManager.getSpan();
+    io.opentelemetry.trace.Span span = otelScopeManager().getSpan();
     if (io.opentelemetry.trace.DefaultSpan.getInvalid().equals(span)) {
       return null;
     }
@@ -49,7 +44,7 @@ public Span activeSpan() {
   @SuppressWarnings("MustBeClosedChecker")
   public Scope activate(Span span) {
     io.opentelemetry.trace.Span actualSpan = getActualSpan(span);
-    return new ScopeShim(scopeManager.withSpan(actualSpan));
+    return new ScopeShim(otelScopeManager().withSpan(actualSpan));
   }
 
   static io.opentelemetry.trace.Span getActualSpan(Span span) {
diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TelemetryInfo.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TelemetryInfo.java
index 1af59880015..66d412e966f 100644
--- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TelemetryInfo.java
+++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TelemetryInfo.java
@@ -19,6 +19,7 @@
 import io.opentelemetry.context.propagation.ContextPropagators;
 import io.opentelemetry.correlationcontext.CorrelationContext;
 import io.opentelemetry.correlationcontext.CorrelationContextManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.trace.Tracer;
 
 /**
@@ -28,14 +29,19 @@
 final class TelemetryInfo {
   private final Tracer tracer;
   private final CorrelationContextManager contextManager;
+  private final ScopeManager scopeManager;
   private final CorrelationContext emptyCorrelationContext;
   private final ContextPropagators propagators;
   private final SpanContextShimTable spanContextTable;
 
   TelemetryInfo(
-      Tracer tracer, CorrelationContextManager contextManager, ContextPropagators propagators) {
+      Tracer tracer,
+      CorrelationContextManager contextManager,
+      ScopeManager scopeManager,
+      ContextPropagators propagators) {
     this.tracer = tracer;
     this.contextManager = contextManager;
+    this.scopeManager = scopeManager;
     this.propagators = propagators;
     this.emptyCorrelationContext = contextManager.contextBuilder().build();
     this.spanContextTable = new SpanContextShimTable();
@@ -49,6 +55,10 @@ CorrelationContextManager contextManager() {
     return contextManager;
   }
 
+  ScopeManager scopeManager() {
+    return scopeManager;
+  }
+
   SpanContextShimTable spanContextTable() {
     return spanContextTable;
   }
diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TraceShim.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TraceShim.java
index ae527d3b912..f7b130c90cf 100644
--- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TraceShim.java
+++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/TraceShim.java
@@ -18,6 +18,7 @@
 
 import io.opentelemetry.OpenTelemetry;
 import io.opentelemetry.correlationcontext.CorrelationContextManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.trace.Tracer;
 import io.opentelemetry.trace.TracerProvider;
 import java.util.Objects;
@@ -37,6 +38,7 @@ public static io.opentracing.Tracer createTracerShim() {
         new TelemetryInfo(
             getTracer(OpenTelemetry.getTracerProvider()),
             OpenTelemetry.getCorrelationContextManager(),
+            OpenTelemetry.getScopeManager(),
             OpenTelemetry.getPropagators()));
   }
 
@@ -50,11 +52,14 @@ public static io.opentracing.Tracer createTracerShim() {
    * @since 0.1.0
    */
   public static io.opentracing.Tracer createTracerShim(
-      TracerProvider tracerProvider, CorrelationContextManager contextManager) {
+      TracerProvider tracerProvider,
+      CorrelationContextManager contextManager,
+      ScopeManager scopeManager) {
     return new TracerShim(
         new TelemetryInfo(
             getTracer(Objects.requireNonNull(tracerProvider, "tracerProvider")),
             Objects.requireNonNull(contextManager, "contextManager"),
+            Objects.requireNonNull(scopeManager, "scopeManager"),
             OpenTelemetry.getPropagators()));
   }
 
diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanBuilderShimTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanBuilderShimTest.java
index 46dbe1fb88d..b50afbf9144 100644
--- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanBuilderShimTest.java
+++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanBuilderShimTest.java
@@ -21,16 +21,24 @@
 import static org.junit.Assert.assertFalse;
 
 import io.opentelemetry.OpenTelemetry;
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentelemetry.trace.Tracer;
 import org.junit.Test;
 
 public class SpanBuilderShimTest {
-  private final TracerSdkProvider tracerSdkFactory = TracerSdkProvider.builder().build();
+  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final TracerSdkProvider tracerSdkFactory =
+      TracerSdkProvider.builder(scopeManager).build();
   private final Tracer tracer = tracerSdkFactory.get("SpanShimTest");
   private final TelemetryInfo telemetryInfo =
-      new TelemetryInfo(tracer, new CorrelationContextManagerSdk(), OpenTelemetry.getPropagators());
+      new TelemetryInfo(
+          tracer,
+          new CorrelationContextManagerSdk(scopeManager),
+          scopeManager,
+          OpenTelemetry.getPropagators());
 
   private static final String SPAN_NAME = "Span";
 
diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanShimTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanShimTest.java
index 7eef430695d..7852f8c171a 100644
--- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanShimTest.java
+++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/SpanShimTest.java
@@ -24,6 +24,8 @@
 import static org.junit.Assert.assertTrue;
 
 import io.opentelemetry.OpenTelemetry;
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentelemetry.trace.Tracer;
@@ -33,10 +35,16 @@
 import org.junit.Test;
 
 public class SpanShimTest {
-  private final TracerSdkProvider tracerSdkFactory = TracerSdkProvider.builder().build();
+  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final TracerSdkProvider tracerSdkFactory =
+      TracerSdkProvider.builder(scopeManager).build();
   private final Tracer tracer = tracerSdkFactory.get("SpanShimTest");
   private final TelemetryInfo telemetryInfo =
-      new TelemetryInfo(tracer, new CorrelationContextManagerSdk(), OpenTelemetry.getPropagators());
+      new TelemetryInfo(
+          tracer,
+          new CorrelationContextManagerSdk(scopeManager),
+          scopeManager,
+          OpenTelemetry.getPropagators());
   private io.opentelemetry.trace.Span span;
 
   private static final String SPAN_NAME = "Span";
diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/TraceShimTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/TraceShimTest.java
index f7ef37e536b..070e964de81 100644
--- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/TraceShimTest.java
+++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/TraceShimTest.java
@@ -19,6 +19,8 @@
 import static org.junit.Assert.assertEquals;
 
 import io.opentelemetry.OpenTelemetry;
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import org.junit.Rule;
@@ -39,21 +41,33 @@ public void createTracerShim_default() {
   public void createTracerShim_nullTracer() {
     thrown.expect(NullPointerException.class);
     thrown.expectMessage("tracerProvider");
-    TraceShim.createTracerShim(null, OpenTelemetry.getCorrelationContextManager());
+    TraceShim.createTracerShim(
+        null, OpenTelemetry.getCorrelationContextManager(), OpenTelemetry.getScopeManager());
   }
 
   @Test
   public void createTracerShim_nullContextManager() {
     thrown.expect(NullPointerException.class);
     thrown.expectMessage("contextManager");
-    TraceShim.createTracerShim(OpenTelemetry.getTracerProvider(), null);
+    TraceShim.createTracerShim(
+        OpenTelemetry.getTracerProvider(), null, OpenTelemetry.getScopeManager());
+  }
+
+  @Test
+  public void createTracerShim_nullScopeManager() {
+    thrown.expect(NullPointerException.class);
+    thrown.expectMessage("scopeManager");
+    TraceShim.createTracerShim(
+        OpenTelemetry.getTracerProvider(), OpenTelemetry.getCorrelationContextManager(), null);
   }
 
   @Test
   public void createTracerShim() {
-    TracerSdkProvider sdk = TracerSdkProvider.builder().build();
-    CorrelationContextManagerSdk contextManager = new CorrelationContextManagerSdk();
-    TracerShim tracerShim = (TracerShim) TraceShim.createTracerShim(sdk, contextManager);
+    ScopeManager scopeManager = DefaultScopeManager.getInstance();
+    TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
+    CorrelationContextManagerSdk contextManager = new CorrelationContextManagerSdk(scopeManager);
+    TracerShim tracerShim =
+        (TracerShim) TraceShim.createTracerShim(sdk, contextManager, scopeManager);
     assertEquals(sdk.get("opentracingshim"), tracerShim.tracer());
     assertEquals(contextManager, tracerShim.contextManager());
   }
diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/TracerShimTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/TracerShimTest.java
index 8e8e9ed87e4..041d5173bf5 100644
--- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/TracerShimTest.java
+++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/TracerShimTest.java
@@ -44,6 +44,7 @@ public void setUp() {
             new TelemetryInfo(
                 OpenTelemetry.getTracer("opentracingshim"),
                 OpenTelemetry.getCorrelationContextManager(),
+                OpenTelemetry.getScopeManager(),
                 OpenTelemetry.getPropagators()));
   }
 
diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/OpenTelemetryInteroperabilityTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/OpenTelemetryInteroperabilityTest.java
index e96e647f6cd..32a669e8f99 100644
--- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/OpenTelemetryInteroperabilityTest.java
+++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/OpenTelemetryInteroperabilityTest.java
@@ -36,12 +36,12 @@
 
 public class OpenTelemetryInteroperabilityTest {
   private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final io.opentelemetry.trace.Tracer tracer = sdk.get("opentracingshim");
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
   private final Tracer otTracer =
-      TraceShim.createTracerShim(sdk, DefaultCorrelationContextManager.getInstance());
+      TraceShim.createTracerShim(sdk, DefaultCorrelationContextManager.getInstance(), scopeManager);
 
   @Before
   public void before() {
diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/activespanreplacement/ActiveSpanReplacementTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/activespanreplacement/ActiveSpanReplacementTest.java
index 9d962308385..e210d9d3018 100644
--- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/activespanreplacement/ActiveSpanReplacementTest.java
+++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/activespanreplacement/ActiveSpanReplacementTest.java
@@ -27,6 +27,8 @@
 
 import io.opentelemetry.exporters.inmemory.InMemoryTracing;
 import io.opentelemetry.opentracingshim.TraceShim;
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentelemetry.sdk.trace.data.SpanData;
@@ -42,10 +44,12 @@
 @SuppressWarnings("FutureReturnValueIgnored")
 public class ActiveSpanReplacementTest {
 
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
-  private final Tracer tracer = TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk());
+  private final Tracer tracer =
+      TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk(scopeManager), scopeManager);
   private final ExecutorService executor = Executors.newCachedThreadPool();
 
   @Test
diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/actorpropagation/ActorPropagationTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/actorpropagation/ActorPropagationTest.java
index 3961acd9580..2aaabddcfee 100644
--- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/actorpropagation/ActorPropagationTest.java
+++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/actorpropagation/ActorPropagationTest.java
@@ -22,6 +22,8 @@
 
 import io.opentelemetry.exporters.inmemory.InMemoryTracing;
 import io.opentelemetry.opentracingshim.TraceShim;
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentelemetry.sdk.trace.data.SpanData;
@@ -48,10 +50,12 @@
  */
 @SuppressWarnings("FutureReturnValueIgnored")
 public class ActorPropagationTest {
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
-  private final Tracer tracer = TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk());
+  private final Tracer tracer =
+      TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk(scopeManager), scopeManager);
   private Phaser phaser;
 
   @Before
diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/baggagehandling/BaggageHandlingTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/baggagehandling/BaggageHandlingTest.java
index c7add6fe5ad..06a1afa943a 100644
--- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/baggagehandling/BaggageHandlingTest.java
+++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/baggagehandling/BaggageHandlingTest.java
@@ -20,6 +20,8 @@
 
 import io.opentelemetry.exporters.inmemory.InMemoryTracing;
 import io.opentelemetry.opentracingshim.TraceShim;
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentracing.Span;
@@ -31,10 +33,12 @@
 import org.junit.Test;
 
 public final class BaggageHandlingTest {
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
-  private final Tracer tracer = TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk());
+  private final Tracer tracer =
+      TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk(scopeManager), scopeManager);
   private final ExecutorService executor = Executors.newCachedThreadPool();
 
   @Test
diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/clientserver/TestClientServerTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/clientserver/TestClientServerTest.java
index 4e70e57f3b7..c44cf0eda1b 100644
--- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/clientserver/TestClientServerTest.java
+++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/clientserver/TestClientServerTest.java
@@ -25,6 +25,8 @@
 
 import io.opentelemetry.exporters.inmemory.InMemoryTracing;
 import io.opentelemetry.opentracingshim.TraceShim;
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentelemetry.sdk.trace.data.SpanData;
@@ -39,10 +41,12 @@
 
 public class TestClientServerTest {
 
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
-  private final Tracer tracer = TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk());
+  private final Tracer tracer =
+      TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk(scopeManager), scopeManager);
   private final ArrayBlockingQueue queue = new ArrayBlockingQueue<>(10);
   private Server server;
 
diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/concurrentcommonrequesthandler/HandlerTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/concurrentcommonrequesthandler/HandlerTest.java
index 2507b0c6dd1..a110f6dcdad 100644
--- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/concurrentcommonrequesthandler/HandlerTest.java
+++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/concurrentcommonrequesthandler/HandlerTest.java
@@ -26,6 +26,8 @@
 
 import io.opentelemetry.exporters.inmemory.InMemoryTracing;
 import io.opentelemetry.opentracingshim.TraceShim;
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentelemetry.sdk.trace.data.SpanData;
@@ -46,10 +48,12 @@
  */
 public class HandlerTest {
 
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
-  private final Tracer tracer = TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk());
+  private final Tracer tracer =
+      TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk(scopeManager), scopeManager);
   private final Client client = new Client(new RequestHandler(tracer));
 
   @Before
diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/errorreporting/ErrorReportingTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/errorreporting/ErrorReportingTest.java
index 8ffad55313e..805f09943b5 100644
--- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/errorreporting/ErrorReportingTest.java
+++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/errorreporting/ErrorReportingTest.java
@@ -24,6 +24,8 @@
 
 import io.opentelemetry.exporters.inmemory.InMemoryTracing;
 import io.opentelemetry.opentracingshim.TraceShim;
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentelemetry.sdk.trace.data.SpanData;
@@ -45,10 +47,12 @@
 @SuppressWarnings("FutureReturnValueIgnored")
 public final class ErrorReportingTest {
 
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
-  private final Tracer tracer = TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk());
+  private final Tracer tracer =
+      TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk(scopeManager), scopeManager);
   private final ExecutorService executor = Executors.newCachedThreadPool();
 
   /* Very simple error handling **/
diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/latespanfinish/LateSpanFinishTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/latespanfinish/LateSpanFinishTest.java
index ec0627804df..90b445511c0 100644
--- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/latespanfinish/LateSpanFinishTest.java
+++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/latespanfinish/LateSpanFinishTest.java
@@ -24,6 +24,8 @@
 
 import io.opentelemetry.exporters.inmemory.InMemoryTracing;
 import io.opentelemetry.opentracingshim.TraceShim;
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentelemetry.sdk.trace.data.SpanData;
@@ -38,10 +40,12 @@
 
 @SuppressWarnings("FutureReturnValueIgnored")
 public final class LateSpanFinishTest {
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
-  private final Tracer tracer = TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk());
+  private final Tracer tracer =
+      TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk(scopeManager), scopeManager);
   private final ExecutorService executor = Executors.newCachedThreadPool();
 
   @Test
diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/listenerperrequest/ListenerTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/listenerperrequest/ListenerTest.java
index 7c994b3392d..7fd85a9818d 100644
--- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/listenerperrequest/ListenerTest.java
+++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/listenerperrequest/ListenerTest.java
@@ -21,6 +21,8 @@
 
 import io.opentelemetry.exporters.inmemory.InMemoryTracing;
 import io.opentelemetry.opentracingshim.TraceShim;
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentelemetry.sdk.trace.data.SpanData;
@@ -31,10 +33,12 @@
 
 /** Each request has own instance of ResponseListener. */
 public class ListenerTest {
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
-  private final Tracer tracer = TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk());
+  private final Tracer tracer =
+      TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk(scopeManager), scopeManager);
 
   @Test
   public void test() throws Exception {
diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/multiplecallbacks/MultipleCallbacksTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/multiplecallbacks/MultipleCallbacksTest.java
index 08454436d79..42a866c0082 100644
--- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/multiplecallbacks/MultipleCallbacksTest.java
+++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/multiplecallbacks/MultipleCallbacksTest.java
@@ -24,6 +24,8 @@
 
 import io.opentelemetry.exporters.inmemory.InMemoryTracing;
 import io.opentelemetry.opentracingshim.TraceShim;
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentelemetry.sdk.trace.data.SpanData;
@@ -43,10 +45,12 @@
  */
 @SuppressWarnings("FutureReturnValueIgnored")
 public class MultipleCallbacksTest {
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
-  private final Tracer tracer = TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk());
+  private final Tracer tracer =
+      TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk(scopeManager), scopeManager);
 
   @Test
   public void test() throws Exception {
diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/nestedcallbacks/NestedCallbacksTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/nestedcallbacks/NestedCallbacksTest.java
index 7b881d3cda9..f956f5dba49 100644
--- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/nestedcallbacks/NestedCallbacksTest.java
+++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/nestedcallbacks/NestedCallbacksTest.java
@@ -25,6 +25,8 @@
 import io.opentelemetry.common.AttributeValue;
 import io.opentelemetry.exporters.inmemory.InMemoryTracing;
 import io.opentelemetry.opentracingshim.TraceShim;
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentelemetry.sdk.trace.data.SpanData;
@@ -41,10 +43,12 @@
 @SuppressWarnings("FutureReturnValueIgnored")
 public final class NestedCallbacksTest {
 
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
-  private final Tracer tracer = TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk());
+  private final Tracer tracer =
+      TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk(scopeManager), scopeManager);
   private final ExecutorService executor = Executors.newCachedThreadPool();
 
   @Test
diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/promisepropagation/PromisePropagationTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/promisepropagation/PromisePropagationTest.java
index 57ff5d781d0..205ee336f21 100644
--- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/promisepropagation/PromisePropagationTest.java
+++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/promisepropagation/PromisePropagationTest.java
@@ -21,6 +21,8 @@
 
 import io.opentelemetry.exporters.inmemory.InMemoryTracing;
 import io.opentelemetry.opentracingshim.TraceShim;
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentelemetry.sdk.trace.data.SpanData;
@@ -45,10 +47,12 @@
  * @author tylerbenson
  */
 public class PromisePropagationTest {
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
-  private final Tracer tracer = TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk());
+  private final Tracer tracer =
+      TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk(scopeManager), scopeManager);
   private Phaser phaser;
 
   @Before
diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/statelesscommonrequesthandler/HandlerTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/statelesscommonrequesthandler/HandlerTest.java
index cb0880b88e0..b59609389b6 100644
--- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/statelesscommonrequesthandler/HandlerTest.java
+++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/statelesscommonrequesthandler/HandlerTest.java
@@ -20,6 +20,8 @@
 
 import io.opentelemetry.exporters.inmemory.InMemoryTracing;
 import io.opentelemetry.opentracingshim.TraceShim;
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentelemetry.sdk.trace.data.SpanData;
@@ -36,10 +38,12 @@
  */
 public final class HandlerTest {
 
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
-  private final Tracer tracer = TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk());
+  private final Tracer tracer =
+      TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk(scopeManager), scopeManager);
   private final Client client = new Client(new RequestHandler(tracer));
 
   @Before
diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/suspendresumepropagation/SuspendResumePropagationTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/suspendresumepropagation/SuspendResumePropagationTest.java
index 47c92018d93..4e31eb655f6 100644
--- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/suspendresumepropagation/SuspendResumePropagationTest.java
+++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/suspendresumepropagation/SuspendResumePropagationTest.java
@@ -20,6 +20,8 @@
 
 import io.opentelemetry.exporters.inmemory.InMemoryTracing;
 import io.opentelemetry.opentracingshim.TraceShim;
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentelemetry.sdk.trace.data.SpanData;
@@ -35,10 +37,12 @@
  * @author tylerbenson
  */
 public class SuspendResumePropagationTest {
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
-  private final Tracer tracer = TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk());
+  private final Tracer tracer =
+      TraceShim.createTracerShim(sdk, new CorrelationContextManagerSdk(scopeManager), scopeManager);
 
   @Before
   public void before() {}
diff --git a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java
index bade22efdc8..b9a903859c3 100644
--- a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java
+++ b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextManagerSdk.java
@@ -18,7 +18,6 @@
 
 import io.opentelemetry.correlationcontext.CorrelationContext;
 import io.opentelemetry.correlationcontext.CorrelationContextManager;
-import io.opentelemetry.scope.DefaultScopeManager;
 import io.opentelemetry.scope.ScopeManager;
 
 /**
@@ -26,8 +25,11 @@
  */
 public class CorrelationContextManagerSdk implements CorrelationContextManager {
 
-  // TODO (trask) should be injected
-  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final ScopeManager scopeManager;
+
+  public CorrelationContextManagerSdk(ScopeManager scopeManager) {
+    this.scopeManager = scopeManager;
+  }
 
   @Override
   public CorrelationContext.Builder contextBuilder() {
diff --git a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/spi/CorrelationContextManagerProviderSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/spi/CorrelationContextManagerProviderSdk.java
index f73014286b8..ad3fb9498f0 100644
--- a/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/spi/CorrelationContextManagerProviderSdk.java
+++ b/sdk/src/main/java/io/opentelemetry/sdk/correlationcontext/spi/CorrelationContextManagerProviderSdk.java
@@ -18,6 +18,7 @@
 
 import io.opentelemetry.correlationcontext.CorrelationContextManager;
 import io.opentelemetry.correlationcontext.spi.CorrelationContextManagerProvider;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk;
 
 /**
@@ -31,7 +32,7 @@ public final class CorrelationContextManagerProviderSdk
     implements CorrelationContextManagerProvider {
 
   @Override
-  public CorrelationContextManager create() {
-    return new CorrelationContextManagerSdk();
+  public CorrelationContextManager create(ScopeManager scopeManager) {
+    return new CorrelationContextManagerSdk(scopeManager);
   }
 }
diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java
index d29a3d7733b..c2611d8cca2 100644
--- a/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java
+++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdk.java
@@ -16,8 +16,6 @@
 
 package io.opentelemetry.sdk.trace;
 
-import io.opentelemetry.scope.DefaultScopeManager;
-import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
 import io.opentelemetry.trace.DefaultTracer;
 import io.opentelemetry.trace.Span;
@@ -27,8 +25,6 @@
 final class TracerSdk implements Tracer {
   private final TracerSharedState sharedState;
   private final InstrumentationLibraryInfo instrumentationLibraryInfo;
-  // TODO (trask) should be injected
-  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
 
   TracerSdk(TracerSharedState sharedState, InstrumentationLibraryInfo instrumentationLibraryInfo) {
     this.sharedState = sharedState;
@@ -38,7 +34,7 @@ final class TracerSdk implements Tracer {
   @Override
   public Span.Builder spanBuilder(String spanName) {
     if (sharedState.isStopped()) {
-      return DefaultTracer.getInstance().spanBuilder(spanName);
+      return new DefaultTracer(sharedState.getScopeManager()).spanBuilder(spanName);
     }
     return new SpanBuilderSdk(
         spanName,
@@ -48,7 +44,7 @@ public Span.Builder spanBuilder(String spanName) {
         sharedState.getResource(),
         sharedState.getIdsGenerator(),
         sharedState.getClock(),
-        scopeManager);
+        sharedState.getScopeManager());
   }
 
   /**
diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdkProvider.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdkProvider.java
index 0d8ad9f8ca0..8cb264277f8 100644
--- a/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdkProvider.java
+++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSdkProvider.java
@@ -16,6 +16,7 @@
 
 package io.opentelemetry.sdk.trace;
 
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.common.Clock;
 import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
 import io.opentelemetry.sdk.internal.ComponentRegistry;
@@ -46,12 +47,13 @@ public class TracerSdkProvider implements TracerProvider {
    *
    * @return a new {@link Builder} for {@link TracerSdkProvider}.
    */
-  public static Builder builder() {
-    return new Builder();
+  public static Builder builder(ScopeManager scopeManager) {
+    return new Builder(scopeManager);
   }
 
-  private TracerSdkProvider(Clock clock, IdsGenerator idsGenerator, Resource resource) {
-    this.sharedState = new TracerSharedState(clock, idsGenerator, resource);
+  private TracerSdkProvider(
+      Clock clock, IdsGenerator idsGenerator, Resource resource, ScopeManager scopeManager) {
+    this.sharedState = new TracerSharedState(clock, idsGenerator, resource, scopeManager);
     this.tracerSdkComponentRegistry = new TracerSdkComponentRegistry(sharedState);
   }
 
@@ -136,6 +138,7 @@ public void forceFlush() {
    */
   public static class Builder {
 
+    private final ScopeManager scopeManager;
     private Clock clock = MillisClock.getInstance();
     private IdsGenerator idsGenerator = new RandomIdsGenerator();
     private Resource resource = EnvVarResource.getResource();
@@ -183,10 +186,12 @@ public Builder setResource(Resource resource) {
      * @return An initialized TracerSdkFactory.
      */
     public TracerSdkProvider build() {
-      return new TracerSdkProvider(clock, idsGenerator, resource);
+      return new TracerSdkProvider(clock, idsGenerator, resource, scopeManager);
     }
 
-    private Builder() {}
+    private Builder(ScopeManager scopeManager) {
+      this.scopeManager = scopeManager;
+    }
   }
 
   private static final class TracerSdkComponentRegistry extends ComponentRegistry {
diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSharedState.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSharedState.java
index fc80f00ec40..982c49e6b07 100644
--- a/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSharedState.java
+++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/TracerSharedState.java
@@ -16,6 +16,7 @@
 
 package io.opentelemetry.sdk.trace;
 
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.common.Clock;
 import io.opentelemetry.sdk.resources.Resource;
 import io.opentelemetry.sdk.trace.config.TraceConfig;
@@ -29,6 +30,7 @@ final class TracerSharedState {
   private final Clock clock;
   private final IdsGenerator idsGenerator;
   private final Resource resource;
+  private final ScopeManager scopeManager;
 
   // Reads and writes are atomic for reference variables. Use volatile to ensure that these
   // operations are visible on other CPUs as well.
@@ -39,10 +41,12 @@ final class TracerSharedState {
   @GuardedBy("lock")
   private final List registeredSpanProcessors = new ArrayList<>();
 
-  TracerSharedState(Clock clock, IdsGenerator idsGenerator, Resource resource) {
+  TracerSharedState(
+      Clock clock, IdsGenerator idsGenerator, Resource resource, ScopeManager scopeManager) {
     this.clock = clock;
     this.idsGenerator = idsGenerator;
     this.resource = resource;
+    this.scopeManager = scopeManager;
   }
 
   Clock getClock() {
@@ -57,6 +61,10 @@ Resource getResource() {
     return resource;
   }
 
+  ScopeManager getScopeManager() {
+    return scopeManager;
+  }
+
   /**
    * Returns the active {@code TraceConfig}.
    *
diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/spi/TraceProviderSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/spi/TraceProviderSdk.java
index fd3a246b582..197ad748e96 100644
--- a/sdk/src/main/java/io/opentelemetry/sdk/trace/spi/TraceProviderSdk.java
+++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/spi/TraceProviderSdk.java
@@ -16,6 +16,7 @@
 
 package io.opentelemetry.sdk.trace.spi;
 
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentelemetry.trace.TracerProvider;
 import io.opentelemetry.trace.spi.TraceProvider;
@@ -23,7 +24,7 @@
 /** SDK implementation of the TracerProviderFactory for SPI. */
 public final class TraceProviderSdk implements TraceProvider {
   @Override
-  public TracerProvider create() {
-    return TracerSdkProvider.builder().build();
+  public TracerProvider create(ScopeManager scopeManager) {
+    return TracerSdkProvider.builder(scopeManager).build();
   }
 }
diff --git a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java
index 92bd45bdc4b..9e9e60c35a3 100644
--- a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java
+++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/CorrelationContextSdkTest.java
@@ -43,7 +43,8 @@
 @RunWith(JUnit4.class)
 public class CorrelationContextSdkTest {
   private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
-  private final CorrelationContextManager contextManager = new CorrelationContextManagerSdk();
+  private final CorrelationContextManager contextManager =
+      new CorrelationContextManagerSdk(scopeManager);
 
   private static final EntryMetadata TMD =
       EntryMetadata.create(EntryMetadata.EntryTtl.UNLIMITED_PROPAGATION);
diff --git a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java
index bae04674da4..bd1ab08a7f9 100644
--- a/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java
+++ b/sdk/src/test/java/io/opentelemetry/sdk/correlationcontext/ScopedCorrelationContextTest.java
@@ -55,7 +55,8 @@ public class ScopedCorrelationContextTest {
       EntryMetadata.create(EntryMetadata.EntryTtl.NO_PROPAGATION);
 
   private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
-  private final CorrelationContextManager contextManager = new CorrelationContextManagerSdk();
+  private final CorrelationContextManager contextManager =
+      new CorrelationContextManagerSdk(scopeManager);
 
   @Test
   public void emptyCorrelationContext() {
diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java
index 64eb94c57ad..3cb480ced64 100644
--- a/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java
+++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java
@@ -57,7 +57,8 @@ public class SpanBuilderSdkTest {
           TraceState.getDefault());
 
   private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
-  private final TracerSdkProvider tracerSdkFactory = TracerSdkProvider.builder().build();
+  private final TracerSdkProvider tracerSdkFactory =
+      TracerSdkProvider.builder(scopeManager).build();
   private final TracerSdk tracerSdk = tracerSdkFactory.get("SpanBuilderSdkTest");
 
   @Rule public final ExpectedException thrown = ExpectedException.none();
diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkProviderTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkProviderTest.java
index 56e6a28620b..be1151d16c7 100644
--- a/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkProviderTest.java
+++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkProviderTest.java
@@ -19,6 +19,8 @@
 import static com.google.common.truth.Truth.assertThat;
 import static org.mockito.Mockito.mock;
 
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.common.Clock;
 import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
 import io.opentelemetry.sdk.resources.Resource;
@@ -40,7 +42,8 @@
 public class TracerSdkProviderTest {
   @Mock private SpanProcessor spanProcessor;
   @Rule public final ExpectedException thrown = ExpectedException.none();
-  private final TracerSdkProvider tracerFactory = TracerSdkProvider.builder().build();
+  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final TracerSdkProvider tracerFactory = TracerSdkProvider.builder(scopeManager).build();
 
   @Before
   public void setUp() {
@@ -51,7 +54,7 @@ public void setUp() {
   @Test
   public void builder_HappyPath() {
     assertThat(
-            TracerSdkProvider.builder()
+            TracerSdkProvider.builder(scopeManager)
                 .setClock(mock(Clock.class))
                 .setResource(mock(Resource.class))
                 .setIdsGenerator(mock(IdsGenerator.class))
@@ -63,21 +66,21 @@ public void builder_HappyPath() {
   public void builder_NullClock() {
     thrown.expect(NullPointerException.class);
     thrown.expectMessage("clock");
-    TracerSdkProvider.builder().setClock(null);
+    TracerSdkProvider.builder(scopeManager).setClock(null);
   }
 
   @Test
   public void builder_NullResource() {
     thrown.expect(NullPointerException.class);
     thrown.expectMessage("resource");
-    TracerSdkProvider.builder().setResource(null);
+    TracerSdkProvider.builder(scopeManager).setResource(null);
   }
 
   @Test
   public void builder_NullIdsGenerator() {
     thrown.expect(NullPointerException.class);
     thrown.expectMessage("idsGenerator");
-    TracerSdkProvider.builder().setIdsGenerator(null);
+    TracerSdkProvider.builder(scopeManager).setIdsGenerator(null);
   }
 
   @Test
diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java
index 6ea15c6981d..a1d0dfb1948 100644
--- a/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java
+++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java
@@ -57,7 +57,7 @@ public class TracerSdkTest {
 
   private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
   private final TracerSdk tracer =
-      TracerSdkProvider.builder()
+      TracerSdkProvider.builder(scopeManager)
           .build()
           .get(INSTRUMENTATION_LIBRARY_NAME, INSTRUMENTATION_LIBRARY_VERSION);
 
@@ -117,7 +117,7 @@ public void propagatesInstrumentationLibraryInfoToSpan() {
   @Test
   public void stressTest() {
     CountingSpanProcessor spanProcessor = new CountingSpanProcessor();
-    TracerSdkProvider tracerSdkProvider = TracerSdkProvider.builder().build();
+    TracerSdkProvider tracerSdkProvider = TracerSdkProvider.builder(scopeManager).build();
     tracerSdkProvider.addSpanProcessor(spanProcessor);
     TracerSdk tracer =
         tracerSdkProvider.get(INSTRUMENTATION_LIBRARY_NAME, INSTRUMENTATION_LIBRARY_VERSION);
@@ -139,7 +139,7 @@ public void stressTest() {
   public void stressTest_withBatchSpanProcessor() {
     CountingSpanExporter countingSpanExporter = new CountingSpanExporter();
     SpanProcessor spanProcessor = BatchSpansProcessor.create(countingSpanExporter);
-    TracerSdkProvider tracerSdkProvider = TracerSdkProvider.builder().build();
+    TracerSdkProvider tracerSdkProvider = TracerSdkProvider.builder(scopeManager).build();
     tracerSdkProvider.addSpanProcessor(spanProcessor);
     TracerSdk tracer =
         tracerSdkProvider.get(INSTRUMENTATION_LIBRARY_NAME, INSTRUMENTATION_LIBRARY_VERSION);
diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/export/BatchSpansProcessorTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/export/BatchSpansProcessorTest.java
index 31a60a3f6a6..148aba39670 100644
--- a/sdk/src/test/java/io/opentelemetry/sdk/trace/export/BatchSpansProcessorTest.java
+++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/export/BatchSpansProcessorTest.java
@@ -19,6 +19,8 @@
 import static com.google.common.truth.Truth.assertThat;
 import static org.mockito.Mockito.doThrow;
 
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.common.export.ConfigBuilder;
 import io.opentelemetry.sdk.trace.ReadableSpan;
 import io.opentelemetry.sdk.trace.Samplers;
@@ -57,7 +59,9 @@ public class BatchSpansProcessorTest {
   private static final String SPAN_NAME_1 = "MySpanName/1";
   private static final String SPAN_NAME_2 = "MySpanName/2";
   private static final long MAX_SCHEDULE_DELAY_MILLIS = 500;
-  private final TracerSdkProvider tracerSdkFactory = TracerSdkProvider.builder().build();
+  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final TracerSdkProvider tracerSdkFactory =
+      TracerSdkProvider.builder(scopeManager).build();
   private final Tracer tracer = tracerSdkFactory.get("BatchSpansProcessorTest");
   private final BlockingSpanExporter blockingSpanExporter = new BlockingSpanExporter();
   @Mock private SpanExporter mockServiceHandler;
diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/export/SimpleSpansProcessorTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/export/SimpleSpansProcessorTest.java
index 728866c1e3d..71792da06f5 100644
--- a/sdk/src/test/java/io/opentelemetry/sdk/trace/export/SimpleSpansProcessorTest.java
+++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/export/SimpleSpansProcessorTest.java
@@ -22,6 +22,8 @@
 import static org.mockito.Mockito.verifyZeroInteractions;
 import static org.mockito.Mockito.when;
 
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.trace.ReadableSpan;
 import io.opentelemetry.sdk.trace.Samplers;
 import io.opentelemetry.sdk.trace.TestUtils;
@@ -50,7 +52,9 @@ public class SimpleSpansProcessorTest {
   private static final String SPAN_NAME = "MySpanName";
   @Mock private ReadableSpan readableSpan;
   @Mock private SpanExporter spanExporter;
-  private final TracerSdkProvider tracerSdkFactory = TracerSdkProvider.builder().build();
+  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final TracerSdkProvider tracerSdkFactory =
+      TracerSdkProvider.builder(scopeManager).build();
   private final Tracer tracer = tracerSdkFactory.get("SimpleSpansProcessor");
   private static final SpanContext SAMPLED_SPAN_CONTEXT =
       SpanContext.create(
diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/spi/TraceProviderSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/spi/TraceProviderSdkTest.java
index 45cd6d6dc2b..f1d349a95d4 100644
--- a/sdk/src/test/java/io/opentelemetry/sdk/trace/spi/TraceProviderSdkTest.java
+++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/spi/TraceProviderSdkTest.java
@@ -19,6 +19,8 @@
 import static com.google.common.truth.Truth.assertThat;
 
 import io.opentelemetry.OpenTelemetry;
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentelemetry.trace.Tracer;
 import org.junit.Test;
@@ -31,7 +33,8 @@ public class TraceProviderSdkTest {
 
   @Test
   public void testDefault() {
-    Tracer tracerSdk = TracerSdkProvider.builder().build().get("");
+    ScopeManager scopeManager = DefaultScopeManager.getInstance();
+    Tracer tracerSdk = TracerSdkProvider.builder(scopeManager).build().get("");
     assertThat(OpenTelemetry.getTracerProvider().get("")).isInstanceOf(tracerSdk.getClass());
   }
 }
diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/activespanreplacement/ActiveSpanReplacementTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/activespanreplacement/ActiveSpanReplacementTest.java
index d522e7f3c1e..34c52cadda0 100644
--- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/activespanreplacement/ActiveSpanReplacementTest.java
+++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/activespanreplacement/ActiveSpanReplacementTest.java
@@ -42,7 +42,7 @@
 public class ActiveSpanReplacementTest {
 
   private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
   private final Tracer tracer = sdk.get(ActiveSpanReplacementTest.class.getName());
diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/Actor.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/Actor.java
index e8b06e34271..66ea01c4565 100644
--- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/Actor.java
+++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/Actor.java
@@ -16,7 +16,7 @@
 
 package io.opentelemetry.sdk.contrib.trace.testbed.actorpropagation;
 
-import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.OpenTelemetry;
 import io.opentelemetry.scope.Scope;
 import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.trace.Span;
@@ -31,7 +31,7 @@
 final class Actor implements AutoCloseable {
 
   // TODO (trask) should be injected
-  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final ScopeManager scopeManager = OpenTelemetry.getScopeManager();
 
   private final ExecutorService executor;
   private final Tracer tracer;
diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/ActorPropagationTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/ActorPropagationTest.java
index ac66f1cce26..83af560679a 100644
--- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/ActorPropagationTest.java
+++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/actorpropagation/ActorPropagationTest.java
@@ -46,7 +46,7 @@
 @SuppressWarnings("FutureReturnValueIgnored")
 public class ActorPropagationTest {
   private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
   private final Tracer tracer = sdk.get(ActorPropagationTest.class.getName());
diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java
index 29c9340c2ab..d29d17a7668 100644
--- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java
+++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Client.java
@@ -19,7 +19,6 @@
 import io.grpc.Context;
 import io.opentelemetry.OpenTelemetry;
 import io.opentelemetry.context.propagation.HttpTextFormat.Setter;
-import io.opentelemetry.scope.DefaultScopeManager;
 import io.opentelemetry.scope.Scope;
 import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.trace.Span;
@@ -30,7 +29,7 @@
 final class Client {
 
   // TODO (trask) should be injected
-  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final ScopeManager scopeManager = OpenTelemetry.getScopeManager();
 
   private final ArrayBlockingQueue queue;
   private final Tracer tracer;
diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java
index 3d439475ef1..b0d4f82cbbd 100644
--- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java
+++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/Server.java
@@ -19,7 +19,6 @@
 import io.grpc.Context;
 import io.opentelemetry.OpenTelemetry;
 import io.opentelemetry.context.propagation.HttpTextFormat.Getter;
-import io.opentelemetry.scope.DefaultScopeManager;
 import io.opentelemetry.scope.Scope;
 import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.trace.Span;
@@ -34,7 +33,7 @@ final class Server extends Thread {
   private final ArrayBlockingQueue queue;
   private final Tracer tracer;
   // TODO (trask) should be injected
-  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final ScopeManager scopeManager = OpenTelemetry.getScopeManager();
 
   public Server(ArrayBlockingQueue queue, Tracer tracer) {
     this.queue = queue;
diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/TestClientServerTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/TestClientServerTest.java
index 01e416f730b..a644cd5a69e 100644
--- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/TestClientServerTest.java
+++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/clientserver/TestClientServerTest.java
@@ -40,7 +40,7 @@
 public class TestClientServerTest {
 
   private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
   private final Tracer tracer = sdk.get(TestClientServerTest.class.getName());
diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/concurrentcommonrequesthandler/HandlerTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/concurrentcommonrequesthandler/HandlerTest.java
index 8d8b76a63cd..bc9cc7ce369 100644
--- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/concurrentcommonrequesthandler/HandlerTest.java
+++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/concurrentcommonrequesthandler/HandlerTest.java
@@ -43,7 +43,7 @@
 public class HandlerTest {
 
   private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
   private final Tracer tracer = sdk.get(HandlerTest.class.getName());
diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/errorreporting/ErrorReportingTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/errorreporting/ErrorReportingTest.java
index c89483eae87..61c9002c4b9 100644
--- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/errorreporting/ErrorReportingTest.java
+++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/errorreporting/ErrorReportingTest.java
@@ -43,7 +43,7 @@
 public final class ErrorReportingTest {
 
   private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
   private final Tracer tracer = sdk.get(ErrorReportingTest.class.getName());
diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/latespanfinish/LateSpanFinishTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/latespanfinish/LateSpanFinishTest.java
index 661dd6c19f9..a5cd270236f 100644
--- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/latespanfinish/LateSpanFinishTest.java
+++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/latespanfinish/LateSpanFinishTest.java
@@ -37,7 +37,7 @@
 @SuppressWarnings("FutureReturnValueIgnored")
 public final class LateSpanFinishTest {
   private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
   private final Tracer tracer = sdk.get(LateSpanFinishTest.class.getName());
diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/listenerperrequest/ListenerTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/listenerperrequest/ListenerTest.java
index 5d0e5c3899c..44f496303bc 100644
--- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/listenerperrequest/ListenerTest.java
+++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/listenerperrequest/ListenerTest.java
@@ -32,7 +32,7 @@
 /** Each request has own instance of ResponseListener. */
 public class ListenerTest {
   private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
   private final Tracer tracer = sdk.get(ListenerTest.class.getName());
diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/Client.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/Client.java
index a0be44098d9..f10edbd70d5 100644
--- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/Client.java
+++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/Client.java
@@ -16,7 +16,7 @@
 
 package io.opentelemetry.sdk.contrib.trace.testbed.multiplecallbacks;
 
-import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.OpenTelemetry;
 import io.opentelemetry.scope.Scope;
 import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.trace.Span;
@@ -32,7 +32,7 @@ class Client {
   private final CountDownLatch parentDoneLatch;
   private final Tracer tracer;
   // TODO (trask) should be injected
-  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final ScopeManager scopeManager = OpenTelemetry.getScopeManager();
 
   public Client(Tracer tracer, CountDownLatch parentDoneLatch) {
     this.tracer = tracer;
diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/MultipleCallbacksTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/MultipleCallbacksTest.java
index 7947e301d57..ffb00dbe4de 100644
--- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/MultipleCallbacksTest.java
+++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/multiplecallbacks/MultipleCallbacksTest.java
@@ -44,7 +44,7 @@
 @SuppressWarnings("FutureReturnValueIgnored")
 public class MultipleCallbacksTest {
   private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
   private final Tracer tracer = sdk.get(MultipleCallbacksTest.class.getName());
diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/nestedcallbacks/NestedCallbacksTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/nestedcallbacks/NestedCallbacksTest.java
index 9bed14ae37d..7a69158c1a2 100644
--- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/nestedcallbacks/NestedCallbacksTest.java
+++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/nestedcallbacks/NestedCallbacksTest.java
@@ -42,7 +42,7 @@
 public final class NestedCallbacksTest {
 
   private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
   private final Tracer tracer = sdk.get(NestedCallbacksTest.class.getName());
diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/Promise.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/Promise.java
index dea4214f56d..9b42cf84d6b 100644
--- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/Promise.java
+++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/Promise.java
@@ -16,7 +16,7 @@
 
 package io.opentelemetry.sdk.contrib.trace.testbed.promisepropagation;
 
-import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.OpenTelemetry;
 import io.opentelemetry.scope.Scope;
 import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.trace.Span;
@@ -28,7 +28,7 @@ final class Promise {
   private final PromiseContext context;
   private final Tracer tracer;
   // TODO (trask) should be injected
-  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final ScopeManager scopeManager = OpenTelemetry.getScopeManager();
   private final Span parentSpan;
 
   private final Collection> successCallbacks = new ArrayList<>();
diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/PromisePropagationTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/PromisePropagationTest.java
index 235d5ecf2e9..fa6227f620a 100644
--- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/PromisePropagationTest.java
+++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/promisepropagation/PromisePropagationTest.java
@@ -43,7 +43,7 @@
  */
 public class PromisePropagationTest {
   private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
   private final Tracer tracer = sdk.get(PromisePropagationTest.class.getName());
diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/statelesscommonrequesthandler/HandlerTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/statelesscommonrequesthandler/HandlerTest.java
index cf71963c469..63f7a81735e 100644
--- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/statelesscommonrequesthandler/HandlerTest.java
+++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/statelesscommonrequesthandler/HandlerTest.java
@@ -19,6 +19,8 @@
 import static org.junit.Assert.assertEquals;
 
 import io.opentelemetry.exporters.inmemory.InMemoryTracing;
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentelemetry.sdk.trace.data.SpanData;
 import io.opentelemetry.trace.Tracer;
@@ -34,7 +36,8 @@
  */
 public final class HandlerTest {
 
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
   private final Tracer tracer = sdk.get(HandlerTest.class.getName());
diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/statelesscommonrequesthandler/RequestHandler.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/statelesscommonrequesthandler/RequestHandler.java
index 7316fb82385..5caaab04e6d 100644
--- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/statelesscommonrequesthandler/RequestHandler.java
+++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/statelesscommonrequesthandler/RequestHandler.java
@@ -16,7 +16,7 @@
 
 package io.opentelemetry.sdk.contrib.trace.testbed.statelesscommonrequesthandler;
 
-import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.OpenTelemetry;
 import io.opentelemetry.scope.Scope;
 import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.trace.Span;
@@ -34,7 +34,7 @@ final class RequestHandler {
 
   private final Tracer tracer;
   // TODO (trask) should be injected
-  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final ScopeManager scopeManager = OpenTelemetry.getScopeManager();
 
   private static final ThreadLocal tlsScope = new ThreadLocal<>();
 
diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/suspendresumepropagation/SuspendResume.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/suspendresumepropagation/SuspendResume.java
index b76b280cd69..dd4ea208150 100644
--- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/suspendresumepropagation/SuspendResume.java
+++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/suspendresumepropagation/SuspendResume.java
@@ -16,7 +16,7 @@
 
 package io.opentelemetry.sdk.contrib.trace.testbed.suspendresumepropagation;
 
-import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.OpenTelemetry;
 import io.opentelemetry.scope.Scope;
 import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.trace.Span;
@@ -25,7 +25,7 @@
 final class SuspendResume {
   private final Span span;
   // TODO (trask) should be injected
-  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final ScopeManager scopeManager = OpenTelemetry.getScopeManager();
 
   public SuspendResume(int id, Tracer tracer) {
     // Tracer passed along here for testing. Normally should be referenced via GlobalTracer.get().
diff --git a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/suspendresumepropagation/SuspendResumePropagationTest.java b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/suspendresumepropagation/SuspendResumePropagationTest.java
index fc538211a05..225930f219f 100644
--- a/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/suspendresumepropagation/SuspendResumePropagationTest.java
+++ b/sdk_contrib/testbed/src/test/java/io/opentelemetry/sdk/contrib/trace/testbed/suspendresumepropagation/SuspendResumePropagationTest.java
@@ -19,6 +19,8 @@
 import static com.google.common.truth.Truth.assertThat;
 
 import io.opentelemetry.exporters.inmemory.InMemoryTracing;
+import io.opentelemetry.scope.DefaultScopeManager;
+import io.opentelemetry.scope.ScopeManager;
 import io.opentelemetry.sdk.trace.TracerSdkProvider;
 import io.opentelemetry.sdk.trace.data.SpanData;
 import io.opentelemetry.trace.Tracer;
@@ -31,7 +33,8 @@
  * frameworks.
  */
 public class SuspendResumePropagationTest {
-  private final TracerSdkProvider sdk = TracerSdkProvider.builder().build();
+  private final ScopeManager scopeManager = DefaultScopeManager.getInstance();
+  private final TracerSdkProvider sdk = TracerSdkProvider.builder(scopeManager).build();
   private final InMemoryTracing inMemoryTracing =
       InMemoryTracing.builder().setTracerProvider(sdk).build();
   private final Tracer tracer = sdk.get(SuspendResumePropagationTest.class.getName());