Replace TracingContextUtils.currentContextWith with ImplicitContextKeyed.makeCurrent (#1855)
* Replace TracingContextUtils.currentContextWith with ImplicitContextKeyed.makeCurrent * Dont add MustBeClosed this time since affects other code.
This commit is contained in:
parent
c1b3b28e18
commit
a8975e83d4
|
|
@ -34,7 +34,7 @@ public class DefaultTracerBenchmarks {
|
|||
@Warmup(iterations = 5, time = 1)
|
||||
public void measureFullSpanLifecycle() {
|
||||
span = tracer.spanBuilder("span").startSpan();
|
||||
try (io.opentelemetry.context.Scope ignored = TracingContextUtils.currentContextWith(span)) {
|
||||
try (io.opentelemetry.context.Scope ignored = span.makeCurrent()) {
|
||||
// no-op
|
||||
} finally {
|
||||
span.end();
|
||||
|
|
@ -59,7 +59,7 @@ public class DefaultTracerBenchmarks {
|
|||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
@Warmup(iterations = 5, time = 1)
|
||||
public void measureScopeLifecycle() {
|
||||
try (io.opentelemetry.context.Scope ignored = TracingContextUtils.currentContextWith(span)) {
|
||||
try (io.opentelemetry.context.Scope ignored = span.makeCurrent()) {
|
||||
// no-op
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,10 +5,8 @@
|
|||
|
||||
package io.opentelemetry.trace;
|
||||
|
||||
import com.google.errorprone.annotations.MustBeClosed;
|
||||
import io.opentelemetry.context.Context;
|
||||
import io.opentelemetry.context.ContextKey;
|
||||
import io.opentelemetry.context.Scope;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
|
|
@ -63,32 +61,5 @@ public final class TracingContextUtils {
|
|||
return context.get(CONTEXT_SPAN_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new {@link Scope} encapsulating the provided {@link Span} added to the current {@code
|
||||
* Context}.
|
||||
*
|
||||
* <p>Example of usage:
|
||||
*
|
||||
* <pre>{@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 = TracingContextUtils.currentContextWith(span)) {
|
||||
* TracingContextUtils.getCurrentSpan().addEvent("my event");
|
||||
* doSomeOtherWork(); // Here "span" is the current Span.
|
||||
* }
|
||||
* span.end();
|
||||
* }
|
||||
* }</pre>
|
||||
*
|
||||
* @param span the {@link Span} to be added to the current {@code Context}.
|
||||
* @return the {@link Scope} for the updated {@code Context}.
|
||||
*/
|
||||
@MustBeClosed
|
||||
public static Scope currentContextWith(Span span) {
|
||||
return Context.current().with(span).makeCurrent();
|
||||
}
|
||||
|
||||
private TracingContextUtils() {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,10 +55,10 @@ class TracingContextUtilsTest {
|
|||
@Test
|
||||
void testInProcessContext() {
|
||||
Span span = Span.wrap(SpanContext.getInvalid());
|
||||
try (Scope scope = TracingContextUtils.currentContextWith(span)) {
|
||||
try (Scope scope = span.makeCurrent()) {
|
||||
assertThat(Span.current()).isSameAs(span);
|
||||
Span secondSpan = Span.wrap(SpanContext.getInvalid());
|
||||
try (Scope secondScope = TracingContextUtils.currentContextWith(secondSpan)) {
|
||||
try (Scope secondScope = secondSpan.makeCurrent()) {
|
||||
assertThat(Span.current()).isSameAs(secondSpan);
|
||||
} finally {
|
||||
assertThat(Span.current()).isSameAs(span);
|
||||
|
|
|
|||
|
|
@ -5,12 +5,28 @@
|
|||
|
||||
package io.opentelemetry.context;
|
||||
|
||||
import com.google.errorprone.annotations.MustBeClosed;
|
||||
|
||||
/**
|
||||
* A value that can be stored inside {@link Context}. Types will generally use this interface to
|
||||
* allow storing themselves in {@link Context} without exposing a {@link ContextKey}.
|
||||
*/
|
||||
public interface ImplicitContextKeyed {
|
||||
|
||||
/**
|
||||
* Adds this {@link ImplicitContextKeyed} value to the {@link Context#current() current context}
|
||||
* and makes the new {@link Context} the current context. {@link Scope#close()} must be called to
|
||||
* properly restore the previous context from before this scope of execution or context will not
|
||||
* work correctly. It is recommended to use try-with-resources to call {@link Scope#close()}
|
||||
* automatically.
|
||||
*
|
||||
* <p>This method is equivalent to {@code Context.current().with(value).makeCurrent()}.
|
||||
*/
|
||||
@MustBeClosed
|
||||
default Scope makeCurrent() {
|
||||
return Context.current().with(this).makeCurrent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new {@link Context} created by setting {@code this} into the provided {@link
|
||||
* Context}. It is generally recommended to call {@link Context#with(ImplicitContextKeyed)}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@
|
|||
|
||||
package io.opentelemetry.opentracingshim;
|
||||
|
||||
import io.opentelemetry.trace.TracingContextUtils;
|
||||
import io.opentracing.Scope;
|
||||
import io.opentracing.ScopeManager;
|
||||
import io.opentracing.Span;
|
||||
|
|
@ -34,7 +33,7 @@ final class ScopeManagerShim extends BaseShimObject implements ScopeManager {
|
|||
@SuppressWarnings("MustBeClosedChecker")
|
||||
public Scope activate(Span span) {
|
||||
io.opentelemetry.trace.Span actualSpan = getActualSpan(span);
|
||||
return new ScopeShim(TracingContextUtils.currentContextWith(actualSpan));
|
||||
return new ScopeShim(actualSpan.makeCurrent());
|
||||
}
|
||||
|
||||
static io.opentelemetry.trace.Span getActualSpan(Span span) {
|
||||
|
|
|
|||
|
|
@ -14,7 +14,6 @@ import io.opentelemetry.exporters.inmemory.InMemoryTracing;
|
|||
import io.opentelemetry.opentracingshim.TraceShim;
|
||||
import io.opentelemetry.sdk.OpenTelemetrySdk;
|
||||
import io.opentelemetry.sdk.trace.data.SpanData;
|
||||
import io.opentelemetry.trace.TracingContextUtils;
|
||||
import io.opentracing.Scope;
|
||||
import io.opentracing.Span;
|
||||
import io.opentracing.Tracer;
|
||||
|
|
@ -56,7 +55,7 @@ class OpenTelemetryInteroperabilityTest {
|
|||
@Test
|
||||
void openTracingContinuesSdkTrace() {
|
||||
io.opentelemetry.trace.Span otelSpan = tracer.spanBuilder("otel_span").startSpan();
|
||||
try (io.opentelemetry.context.Scope scope = TracingContextUtils.currentContextWith(otelSpan)) {
|
||||
try (io.opentelemetry.context.Scope scope = otelSpan.makeCurrent()) {
|
||||
otTracer.buildSpan("ot_span").start().finish();
|
||||
} finally {
|
||||
otelSpan.end();
|
||||
|
|
|
|||
|
|
@ -24,7 +24,6 @@ import io.opentelemetry.sdk.metrics.export.MetricExporter;
|
|||
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
|
||||
import io.opentelemetry.trace.Span;
|
||||
import io.opentelemetry.trace.Tracer;
|
||||
import io.opentelemetry.trace.TracingContextUtils;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
|
@ -207,7 +206,7 @@ public class OtlpPipelineStressTest {
|
|||
: i < numberOfSpans) {
|
||||
// for (int i = 0; i < 10000; i++) {
|
||||
Span exampleSpan = tracer.spanBuilder("exampleSpan").startSpan();
|
||||
try (Scope scope = TracingContextUtils.currentContextWith(exampleSpan)) {
|
||||
try (Scope scope = exampleSpan.makeCurrent()) {
|
||||
exampleSpan.setAttribute("exampleNumber", i++);
|
||||
exampleSpan.setAttribute("attribute0", "attvalue-0");
|
||||
exampleSpan.setAttribute("attribute1", "attvalue-1");
|
||||
|
|
|
|||
|
|
@ -33,7 +33,6 @@ 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.Collections;
|
||||
import java.util.List;
|
||||
|
|
@ -603,7 +602,7 @@ class SpanBuilderSdkTest {
|
|||
@Test
|
||||
void noParent() {
|
||||
Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan();
|
||||
try (Scope ignored = TracingContextUtils.currentContextWith(parent)) {
|
||||
try (Scope ignored = parent.makeCurrent()) {
|
||||
Span span = tracerSdk.spanBuilder(SPAN_NAME).setNoParent().startSpan();
|
||||
try {
|
||||
assertThat(span.getSpanContext().getTraceIdAsHexString())
|
||||
|
|
@ -726,7 +725,7 @@ class SpanBuilderSdkTest {
|
|||
Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan();
|
||||
try {
|
||||
RecordEventsReadableSpan span;
|
||||
try (Scope scope = TracingContextUtils.currentContextWith(parent)) {
|
||||
try (Scope scope = parent.makeCurrent()) {
|
||||
span =
|
||||
(RecordEventsReadableSpan)
|
||||
tracerSdk.spanBuilder(SPAN_NAME).setParent(emptyContext).startSpan();
|
||||
|
|
@ -750,7 +749,7 @@ class SpanBuilderSdkTest {
|
|||
@Test
|
||||
void parentCurrentSpan() {
|
||||
Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan();
|
||||
try (Scope ignored = TracingContextUtils.currentContextWith(parent)) {
|
||||
try (Scope ignored = parent.makeCurrent()) {
|
||||
final Context implicitParent = Context.current();
|
||||
RecordEventsReadableSpan span =
|
||||
(RecordEventsReadableSpan) tracerSdk.spanBuilder(SPAN_NAME).startSpan();
|
||||
|
|
@ -799,7 +798,7 @@ class SpanBuilderSdkTest {
|
|||
@Test
|
||||
void parent_clockIsSame() {
|
||||
Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan();
|
||||
try (Scope scope = TracingContextUtils.currentContextWith(parent)) {
|
||||
try (Scope scope = parent.makeCurrent()) {
|
||||
RecordEventsReadableSpan span =
|
||||
(RecordEventsReadableSpan) tracerSdk.spanBuilder(SPAN_NAME).startSpan();
|
||||
|
||||
|
|
@ -812,7 +811,7 @@ class SpanBuilderSdkTest {
|
|||
@Test
|
||||
void parentCurrentSpan_clockIsSame() {
|
||||
Span parent = tracerSdk.spanBuilder(SPAN_NAME).startSpan();
|
||||
try (Scope ignored = TracingContextUtils.currentContextWith(parent)) {
|
||||
try (Scope ignored = parent.makeCurrent()) {
|
||||
RecordEventsReadableSpan span =
|
||||
(RecordEventsReadableSpan) tracerSdk.spanBuilder(SPAN_NAME).startSpan();
|
||||
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@ import io.opentelemetry.sdk.trace.data.SpanData;
|
|||
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
|
||||
import io.opentelemetry.sdk.trace.export.SpanExporter;
|
||||
import io.opentelemetry.trace.Span;
|
||||
import io.opentelemetry.trace.TracingContextUtils;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
|
|
@ -161,7 +160,7 @@ class TracerSdkTest {
|
|||
@Override
|
||||
public void update() {
|
||||
Span span = tracer.spanBuilder("testSpan").startSpan();
|
||||
try (Scope ignored = TracingContextUtils.currentContextWith(span)) {
|
||||
try (Scope ignored = span.makeCurrent()) {
|
||||
span.setAttribute("testAttribute", "testValue");
|
||||
} finally {
|
||||
span.end();
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@ import io.opentelemetry.sdk.trace.data.SpanData;
|
|||
import io.opentelemetry.trace.Span;
|
||||
import io.opentelemetry.trace.SpanId;
|
||||
import io.opentelemetry.trace.Tracer;
|
||||
import io.opentelemetry.trace.TracingContextUtils;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
|
@ -38,7 +37,7 @@ class ActiveSpanReplacementTest {
|
|||
void test() {
|
||||
// Start an isolated task and query for its result in another task/thread
|
||||
Span span = tracer.spanBuilder("initial").startSpan();
|
||||
try (Scope scope = TracingContextUtils.currentContextWith(span)) {
|
||||
try (Scope scope = span.makeCurrent()) {
|
||||
// Explicitly pass a Span to be finished once a late calculation is done.
|
||||
submitAnotherTask(span);
|
||||
}
|
||||
|
|
@ -70,11 +69,11 @@ class ActiveSpanReplacementTest {
|
|||
() -> {
|
||||
// Create a new Span for this task
|
||||
Span taskSpan = tracer.spanBuilder("task").startSpan();
|
||||
try (Scope scope = TracingContextUtils.currentContextWith(taskSpan)) {
|
||||
try (Scope scope = taskSpan.makeCurrent()) {
|
||||
|
||||
// Simulate work strictly related to the initial Span
|
||||
// and finish it.
|
||||
try (Scope initialScope = TracingContextUtils.currentContextWith(initialSpan)) {
|
||||
try (Scope initialScope = initialSpan.makeCurrent()) {
|
||||
sleep(50);
|
||||
} finally {
|
||||
initialSpan.end();
|
||||
|
|
@ -82,7 +81,7 @@ class ActiveSpanReplacementTest {
|
|||
|
||||
// Restore the span for this task and create a subspan
|
||||
Span subTaskSpan = tracer.spanBuilder("subtask").startSpan();
|
||||
try (Scope subTaskScope = TracingContextUtils.currentContextWith(subTaskSpan)) {
|
||||
try (Scope subTaskScope = subTaskSpan.makeCurrent()) {
|
||||
sleep(50);
|
||||
} finally {
|
||||
subTaskSpan.end();
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ import io.opentelemetry.context.Scope;
|
|||
import io.opentelemetry.trace.Span;
|
||||
import io.opentelemetry.trace.Span.Kind;
|
||||
import io.opentelemetry.trace.Tracer;
|
||||
import io.opentelemetry.trace.TracingContextUtils;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
|
@ -45,7 +44,7 @@ final class Actor implements AutoCloseable {
|
|||
.setParent(parent)
|
||||
.setSpanKind(Kind.CONSUMER)
|
||||
.startSpan();
|
||||
try (Scope ignored = TracingContextUtils.currentContextWith(child)) {
|
||||
try (Scope ignored = child.makeCurrent()) {
|
||||
phaser.arriveAndAwaitAdvance(); // child tracer started
|
||||
child.addEvent("received " + message);
|
||||
phaser.arriveAndAwaitAdvance(); // assert size
|
||||
|
|
|
|||
|
|
@ -15,7 +15,6 @@ import io.opentelemetry.sdk.trace.data.SpanData;
|
|||
import io.opentelemetry.trace.Span;
|
||||
import io.opentelemetry.trace.Span.Kind;
|
||||
import io.opentelemetry.trace.Tracer;
|
||||
import io.opentelemetry.trace.TracingContextUtils;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
|
@ -49,7 +48,7 @@ class ActorPropagationTest {
|
|||
phaser.register();
|
||||
Span parent = tracer.spanBuilder("actorTell").setSpanKind(Kind.PRODUCER).startSpan();
|
||||
parent.setAttribute("component", "example-actor");
|
||||
try (Scope ignored = TracingContextUtils.currentContextWith(parent)) {
|
||||
try (Scope ignored = parent.makeCurrent()) {
|
||||
actor.tell("my message 1");
|
||||
actor.tell("my message 2");
|
||||
} finally {
|
||||
|
|
@ -86,7 +85,7 @@ class ActorPropagationTest {
|
|||
Span span = tracer.spanBuilder("actorAsk").setSpanKind(Kind.PRODUCER).startSpan();
|
||||
span.setAttribute("component", "example-actor");
|
||||
|
||||
try (Scope ignored = TracingContextUtils.currentContextWith(span)) {
|
||||
try (Scope ignored = span.makeCurrent()) {
|
||||
future1 = actor.ask("my message 1");
|
||||
future2 = actor.ask("my message 2");
|
||||
} finally {
|
||||
|
|
|
|||
|
|
@ -11,7 +11,6 @@ import io.opentelemetry.context.Scope;
|
|||
import io.opentelemetry.trace.Span;
|
||||
import io.opentelemetry.trace.Span.Kind;
|
||||
import io.opentelemetry.trace.Tracer;
|
||||
import io.opentelemetry.trace.TracingContextUtils;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
|
||||
final class Client {
|
||||
|
|
@ -30,7 +29,7 @@ final class Client {
|
|||
Span span = tracer.spanBuilder("send").setSpanKind(Kind.CLIENT).startSpan();
|
||||
span.setAttribute("component", "example-client");
|
||||
|
||||
try (Scope ignored = TracingContextUtils.currentContextWith(span)) {
|
||||
try (Scope ignored = span.makeCurrent()) {
|
||||
OpenTelemetry.getGlobalPropagators()
|
||||
.getTextMapPropagator()
|
||||
.inject(Context.current(), message, Message::put);
|
||||
|
|
|
|||
|
|
@ -12,7 +12,6 @@ import io.opentelemetry.context.propagation.TextMapPropagator.Getter;
|
|||
import io.opentelemetry.trace.Span;
|
||||
import io.opentelemetry.trace.Span.Kind;
|
||||
import io.opentelemetry.trace.Tracer;
|
||||
import io.opentelemetry.trace.TracingContextUtils;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
|
|
@ -44,7 +43,7 @@ final class Server extends Thread {
|
|||
tracer.spanBuilder("receive").setSpanKind(Kind.SERVER).setParent(context).startSpan();
|
||||
span.setAttribute("component", "example-server");
|
||||
|
||||
try (Scope ignored = TracingContextUtils.currentContextWith(span)) {
|
||||
try (Scope ignored = span.makeCurrent()) {
|
||||
// Simulate work.
|
||||
Span.current().addEvent("DoWork");
|
||||
} finally {
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@ import io.opentelemetry.sdk.trace.data.SpanData;
|
|||
import io.opentelemetry.trace.Span;
|
||||
import io.opentelemetry.trace.SpanId;
|
||||
import io.opentelemetry.trace.Tracer;
|
||||
import io.opentelemetry.trace.TracingContextUtils;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
|
@ -67,7 +66,7 @@ class HandlerTest {
|
|||
@Test
|
||||
void parent_not_picked_up() throws Exception {
|
||||
Span parentSpan = tracer.spanBuilder("parent").startSpan();
|
||||
try (Scope ignored = TracingContextUtils.currentContextWith(parentSpan)) {
|
||||
try (Scope ignored = parentSpan.makeCurrent()) {
|
||||
String response = client.send("no_parent").get(15, TimeUnit.SECONDS);
|
||||
assertThat(response).isEqualTo("no_parent:response");
|
||||
} finally {
|
||||
|
|
@ -97,7 +96,7 @@ class HandlerTest {
|
|||
void bad_solution_to_set_parent() throws Exception {
|
||||
Client client;
|
||||
Span parentSpan = tracer.spanBuilder("parent").startSpan();
|
||||
try (Scope ignored = TracingContextUtils.currentContextWith(parentSpan)) {
|
||||
try (Scope ignored = parentSpan.makeCurrent()) {
|
||||
client = new Client(new RequestHandler(tracer, Context.current().with(parentSpan)));
|
||||
String response = client.send("correct_parent").get(15, TimeUnit.SECONDS);
|
||||
assertThat(response).isEqualTo("correct_parent:response");
|
||||
|
|
|
|||
|
|
@ -19,7 +19,6 @@ import io.opentelemetry.sdk.trace.data.SpanData.Event;
|
|||
import io.opentelemetry.trace.Span;
|
||||
import io.opentelemetry.trace.StatusCode;
|
||||
import io.opentelemetry.trace.Tracer;
|
||||
import io.opentelemetry.trace.TracingContextUtils;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
|
@ -39,7 +38,7 @@ public final class ErrorReportingTest {
|
|||
@Test
|
||||
void testSimpleError() {
|
||||
Span span = tracer.spanBuilder("one").startSpan();
|
||||
try (Scope ignored = TracingContextUtils.currentContextWith(span)) {
|
||||
try (Scope ignored = span.makeCurrent()) {
|
||||
throw new RuntimeException("Invalid state");
|
||||
} catch (Exception e) {
|
||||
span.setStatus(StatusCode.ERROR);
|
||||
|
|
@ -60,7 +59,7 @@ public final class ErrorReportingTest {
|
|||
final Span span = tracer.spanBuilder("one").startSpan();
|
||||
executor.submit(
|
||||
() -> {
|
||||
try (Scope ignored = TracingContextUtils.currentContextWith(span)) {
|
||||
try (Scope ignored = span.makeCurrent()) {
|
||||
throw new RuntimeException("Invalid state");
|
||||
} catch (Exception exc) {
|
||||
span.setStatus(StatusCode.ERROR);
|
||||
|
|
@ -85,7 +84,7 @@ public final class ErrorReportingTest {
|
|||
final int maxRetries = 1;
|
||||
int retries = 0;
|
||||
Span span = tracer.spanBuilder("one").startSpan();
|
||||
try (Scope ignored = TracingContextUtils.currentContextWith(span)) {
|
||||
try (Scope ignored = span.makeCurrent()) {
|
||||
while (retries++ < maxRetries) {
|
||||
try {
|
||||
throw new RuntimeException("No url could be fetched");
|
||||
|
|
@ -114,7 +113,7 @@ public final class ErrorReportingTest {
|
|||
@Test
|
||||
void testInstrumentationLayer() {
|
||||
Span span = tracer.spanBuilder("one").startSpan();
|
||||
try (Scope ignored = TracingContextUtils.currentContextWith(span)) {
|
||||
try (Scope ignored = span.makeCurrent()) {
|
||||
// ScopedRunnable captures the active Span at this time.
|
||||
executor.submit(
|
||||
new ScopedRunnable(
|
||||
|
|
@ -153,7 +152,7 @@ public final class ErrorReportingTest {
|
|||
@Override
|
||||
public void run() {
|
||||
// No error reporting is done, as we are a simple wrapper.
|
||||
try (Scope ignored = TracingContextUtils.currentContextWith(span)) {
|
||||
try (Scope ignored = span.makeCurrent()) {
|
||||
runnable.run();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,6 @@ import io.opentelemetry.sdk.trace.TracerSdkProvider;
|
|||
import io.opentelemetry.sdk.trace.data.SpanData;
|
||||
import io.opentelemetry.trace.Span;
|
||||
import io.opentelemetry.trace.Tracer;
|
||||
import io.opentelemetry.trace.TracingContextUtils;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
|
@ -64,9 +63,9 @@ public final class LateSpanFinishTest {
|
|||
() -> {
|
||||
/* Alternative to calling activate() is to pass it manually to asChildOf() for each
|
||||
* created Span. */
|
||||
try (Scope scope = TracingContextUtils.currentContextWith(parentSpan)) {
|
||||
try (Scope scope = parentSpan.makeCurrent()) {
|
||||
Span childSpan = tracer.spanBuilder("task1").startSpan();
|
||||
try (Scope childScope = TracingContextUtils.currentContextWith(childSpan)) {
|
||||
try (Scope childScope = childSpan.makeCurrent()) {
|
||||
TestUtils.sleep(55);
|
||||
} finally {
|
||||
childSpan.end();
|
||||
|
|
@ -76,9 +75,9 @@ public final class LateSpanFinishTest {
|
|||
|
||||
executor.submit(
|
||||
() -> {
|
||||
try (Scope scope = TracingContextUtils.currentContextWith(parentSpan)) {
|
||||
try (Scope scope = parentSpan.makeCurrent()) {
|
||||
Span childSpan = tracer.spanBuilder("task2").startSpan();
|
||||
try (Scope childScope = TracingContextUtils.currentContextWith(childSpan)) {
|
||||
try (Scope childScope = childSpan.makeCurrent()) {
|
||||
TestUtils.sleep(85);
|
||||
} finally {
|
||||
childSpan.end();
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ import io.opentelemetry.context.Context;
|
|||
import io.opentelemetry.context.Scope;
|
||||
import io.opentelemetry.trace.Span;
|
||||
import io.opentelemetry.trace.Tracer;
|
||||
import io.opentelemetry.trace.TracingContextUtils;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
|
@ -31,7 +30,7 @@ class Client {
|
|||
return executor.submit(
|
||||
() -> {
|
||||
Span span = tracer.spanBuilder("subtask").setParent(parent).startSpan();
|
||||
try (Scope subtaskScope = TracingContextUtils.currentContextWith(span)) {
|
||||
try (Scope subtaskScope = span.makeCurrent()) {
|
||||
// Simulate work - make sure we finish *after* the parent Span.
|
||||
parentDoneLatch.await();
|
||||
} finally {
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@ import io.opentelemetry.sdk.trace.TracerSdkProvider;
|
|||
import io.opentelemetry.sdk.trace.data.SpanData;
|
||||
import io.opentelemetry.trace.Span;
|
||||
import io.opentelemetry.trace.Tracer;
|
||||
import io.opentelemetry.trace.TracingContextUtils;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
|
@ -41,7 +40,7 @@ class MultipleCallbacksTest {
|
|||
Client client = new Client(tracer, parentDoneLatch);
|
||||
|
||||
Span span = tracer.spanBuilder("parent").startSpan();
|
||||
try (Scope scope = TracingContextUtils.currentContextWith(span)) {
|
||||
try (Scope scope = span.makeCurrent()) {
|
||||
client.send("task1");
|
||||
client.send("task2");
|
||||
client.send("task3");
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@ import io.opentelemetry.sdk.trace.TracerSdkProvider;
|
|||
import io.opentelemetry.sdk.trace.data.SpanData;
|
||||
import io.opentelemetry.trace.Span;
|
||||
import io.opentelemetry.trace.Tracer;
|
||||
import io.opentelemetry.trace.TracingContextUtils;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
|
@ -61,17 +60,17 @@ public final class NestedCallbacksTest {
|
|||
|
||||
executor.submit(
|
||||
() -> {
|
||||
try (Scope ignored = TracingContextUtils.currentContextWith(span)) {
|
||||
try (Scope ignored = span.makeCurrent()) {
|
||||
span.setAttribute("key1", "1");
|
||||
|
||||
executor.submit(
|
||||
() -> {
|
||||
try (Scope ignored12 = TracingContextUtils.currentContextWith(span)) {
|
||||
try (Scope ignored12 = span.makeCurrent()) {
|
||||
span.setAttribute("key2", "2");
|
||||
|
||||
executor.submit(
|
||||
() -> {
|
||||
try (Scope ignored1 = TracingContextUtils.currentContextWith(span)) {
|
||||
try (Scope ignored1 = span.makeCurrent()) {
|
||||
span.setAttribute("key3", "3");
|
||||
} finally {
|
||||
span.end();
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ import io.opentelemetry.context.Context;
|
|||
import io.opentelemetry.context.Scope;
|
||||
import io.opentelemetry.trace.Span;
|
||||
import io.opentelemetry.trace.Tracer;
|
||||
import io.opentelemetry.trace.TracingContextUtils;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
|
|
@ -44,7 +43,7 @@ final class Promise<T> {
|
|||
() -> {
|
||||
Span childSpan = tracer.spanBuilder("success").setParent(parent).startSpan();
|
||||
childSpan.setAttribute("component", "success");
|
||||
try (Scope ignored = TracingContextUtils.currentContextWith(childSpan)) {
|
||||
try (Scope ignored = childSpan.makeCurrent()) {
|
||||
callback.accept(result);
|
||||
} finally {
|
||||
childSpan.end();
|
||||
|
|
@ -61,7 +60,7 @@ final class Promise<T> {
|
|||
() -> {
|
||||
Span childSpan = tracer.spanBuilder("error").setParent(parent).startSpan();
|
||||
childSpan.setAttribute("component", "error");
|
||||
try (Scope ignored = TracingContextUtils.currentContextWith(childSpan)) {
|
||||
try (Scope ignored = childSpan.makeCurrent()) {
|
||||
callback.accept(error);
|
||||
} finally {
|
||||
childSpan.end();
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@ import io.opentelemetry.sdk.trace.data.SpanData;
|
|||
import io.opentelemetry.trace.Span;
|
||||
import io.opentelemetry.trace.SpanId;
|
||||
import io.opentelemetry.trace.Tracer;
|
||||
import io.opentelemetry.trace.TracingContextUtils;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Phaser;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
|
@ -54,7 +53,7 @@ class PromisePropagationTest {
|
|||
Span parentSpan = tracer.spanBuilder("promises").startSpan();
|
||||
parentSpan.setAttribute("component", "example-promises");
|
||||
|
||||
try (Scope ignored = TracingContextUtils.currentContextWith(parentSpan)) {
|
||||
try (Scope ignored = parentSpan.makeCurrent()) {
|
||||
Promise<String> successPromise = new Promise<>(context, tracer);
|
||||
|
||||
successPromise.onSuccess(
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ import io.opentelemetry.context.Scope;
|
|||
import io.opentelemetry.trace.Span;
|
||||
import io.opentelemetry.trace.Span.Kind;
|
||||
import io.opentelemetry.trace.Tracer;
|
||||
import io.opentelemetry.trace.TracingContextUtils;
|
||||
|
||||
/**
|
||||
* One instance per Client. 'beforeRequest' and 'afterResponse' are executed in the same thread for
|
||||
|
|
@ -31,7 +30,7 @@ final class RequestHandler {
|
|||
/** beforeRequest handler....... */
|
||||
public void beforeRequest(Object request) {
|
||||
Span span = tracer.spanBuilder(OPERATION_NAME).setSpanKind(Kind.SERVER).startSpan();
|
||||
tlsScope.set(TracingContextUtils.currentContextWith(span));
|
||||
tlsScope.set(span.makeCurrent());
|
||||
}
|
||||
|
||||
/** afterResponse handler....... */
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ package io.opentelemetry.sdk.extensions.trace.testbed.suspendresumepropagation;
|
|||
import io.opentelemetry.context.Scope;
|
||||
import io.opentelemetry.trace.Span;
|
||||
import io.opentelemetry.trace.Tracer;
|
||||
import io.opentelemetry.trace.TracingContextUtils;
|
||||
|
||||
final class SuspendResume {
|
||||
private final Span span;
|
||||
|
|
@ -16,13 +15,13 @@ final class SuspendResume {
|
|||
public SuspendResume(int id, Tracer tracer) {
|
||||
Span span = tracer.spanBuilder("job " + id).startSpan();
|
||||
span.setAttribute("component", "suspend-resume");
|
||||
try (Scope scope = TracingContextUtils.currentContextWith(span)) {
|
||||
try (Scope scope = span.makeCurrent()) {
|
||||
this.span = span;
|
||||
}
|
||||
}
|
||||
|
||||
public void doPart(String name) {
|
||||
try (Scope scope = TracingContextUtils.currentContextWith(span)) {
|
||||
try (Scope scope = span.makeCurrent()) {
|
||||
span.addEvent("part: " + name);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue