-
Notifications
You must be signed in to change notification settings - Fork 846
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an OpenTelemetry context mechanism. (#1658)
* Add an OpenTelemetry context mechanism. * Moar * Extracted interfaces * More context * Cleanup / tests * Move and cleanups * Brackets * Add example for brave context interop * Brave in OTel * Spotless * Missing folder * Another * Spotless
- Loading branch information
Anuraag Agrawal
authored
Oct 5, 2020
1 parent
ed16964
commit 21fbb36
Showing
29 changed files
with
2,574 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
plugins { | ||
id "java" | ||
|
||
id "org.unbroken-dome.test-sets" | ||
id "ru.vyarus.animalsniffer" | ||
} | ||
|
||
description = 'OpenTelemetry Context (Incubator)' | ||
ext.moduleName = "io.opentelemetry.context" | ||
|
||
testSets { | ||
grpcInOtelTest | ||
otelInGrpcTest | ||
otelAsGrpcTest | ||
|
||
braveInOtelTest | ||
otelAsBraveTest | ||
} | ||
|
||
dependencies { | ||
grpcInOtelTestImplementation libraries.grpc_context | ||
otelAsGrpcTestImplementation libraries.grpc_context | ||
otelInGrpcTestImplementation libraries.grpc_context | ||
|
||
braveInOtelTestImplementation "io.zipkin.brave:brave:5.12.6" | ||
otelAsBraveTestImplementation "io.zipkin.brave:brave:5.12.6" | ||
|
||
testImplementation libraries.awaitility | ||
|
||
signature "org.codehaus.mojo.signature:java18:1.0@signature" | ||
signature "net.sf.androidscents.signature:android-api-level-24:7.0_r2@signature" | ||
} |
125 changes: 125 additions & 0 deletions
125
context/src/braveInOtelTest/java/io/opentelemetry/context/BraveInOtelTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.context; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import brave.Tracing; | ||
import brave.propagation.CurrentTraceContext; | ||
import brave.propagation.TraceContext; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class BraveInOtelTest { | ||
|
||
private static final ContextKey<String> ANIMAL = ContextKey.named("animal"); | ||
|
||
private static final Tracing TRACING = | ||
Tracing.newBuilder().currentTraceContext(new OpenTelemetryCurrentTraceContext()).build(); | ||
|
||
private static final TraceContext TRACE_CONTEXT = | ||
TraceContext.newBuilder().traceId(1).spanId(1).addExtra("japan").build(); | ||
|
||
private static ExecutorService otherThread; | ||
|
||
@BeforeAll | ||
static void setUp() { | ||
otherThread = Executors.newSingleThreadExecutor(); | ||
} | ||
|
||
@AfterAll | ||
static void tearDown() { | ||
otherThread.shutdown(); | ||
} | ||
|
||
@Test | ||
void braveOtelMix() { | ||
try (CurrentTraceContext.Scope ignored = | ||
TRACING.currentTraceContext().newScope(TRACE_CONTEXT)) { | ||
assertThat(Tracing.current().currentTraceContext().get().extra()).contains("japan"); | ||
try (Scope ignored2 = Context.current().withValues(ANIMAL, "cat").makeCurrent()) { | ||
assertThat(Tracing.current().currentTraceContext().get().extra()).contains("japan"); | ||
assertThat(Context.current().getValue(ANIMAL)).isEqualTo("cat"); | ||
|
||
TraceContext context2 = | ||
Tracing.current().currentTraceContext().get().toBuilder().addExtra("cheese").build(); | ||
try (CurrentTraceContext.Scope ignored3 = | ||
TRACING.currentTraceContext().newScope(context2)) { | ||
assertThat(Tracing.current().currentTraceContext().get().extra()).contains("japan"); | ||
assertThat(Tracing.current().currentTraceContext().get().extra()).contains("cheese"); | ||
assertThat(Context.current().getValue(ANIMAL)).isEqualTo("cat"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
void braveWrap() throws Exception { | ||
try (CurrentTraceContext.Scope ignored = | ||
TRACING.currentTraceContext().newScope(TRACE_CONTEXT)) { | ||
try (Scope ignored2 = Context.current().withValues(ANIMAL, "cat").makeCurrent()) { | ||
assertThat(Tracing.current().currentTraceContext().get().extra()).contains("japan"); | ||
assertThat(Context.current().getValue(ANIMAL)).isEqualTo("cat"); | ||
AtomicReference<Boolean> braveContainsJapan = new AtomicReference<>(); | ||
AtomicReference<String> otelValue = new AtomicReference<>(); | ||
Runnable runnable = | ||
() -> { | ||
TraceContext traceContext = Tracing.current().currentTraceContext().get(); | ||
if (traceContext != null && traceContext.extra().contains("japan")) { | ||
braveContainsJapan.set(true); | ||
} else { | ||
braveContainsJapan.set(false); | ||
} | ||
otelValue.set(Context.current().getValue(ANIMAL)); | ||
}; | ||
otherThread.submit(runnable).get(); | ||
assertThat(braveContainsJapan).hasValue(false); | ||
assertThat(otelValue).hasValue(null); | ||
|
||
otherThread.submit(TRACING.currentTraceContext().wrap(runnable)).get(); | ||
assertThat(braveContainsJapan).hasValue(true); | ||
|
||
// Since Brave context is inside the OTel context, propagating the Brave context does not | ||
// propagate the OTel context. | ||
assertThat(otelValue).hasValue(null); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
void otelWrap() throws Exception { | ||
try (CurrentTraceContext.Scope ignored = | ||
TRACING.currentTraceContext().newScope(TRACE_CONTEXT)) { | ||
try (Scope ignored2 = Context.current().withValues(ANIMAL, "cat").makeCurrent()) { | ||
assertThat(Tracing.current().currentTraceContext().get().extra()).contains("japan"); | ||
assertThat(Context.current().getValue(ANIMAL)).isEqualTo("cat"); | ||
AtomicReference<Boolean> braveContainsJapan = new AtomicReference<>(false); | ||
AtomicReference<String> otelValue = new AtomicReference<>(); | ||
Runnable runnable = | ||
() -> { | ||
TraceContext traceContext = Tracing.current().currentTraceContext().get(); | ||
if (traceContext != null && traceContext.extra().contains("japan")) { | ||
braveContainsJapan.set(true); | ||
} else { | ||
braveContainsJapan.set(false); | ||
} | ||
otelValue.set(Context.current().getValue(ANIMAL)); | ||
}; | ||
otherThread.submit(runnable).get(); | ||
assertThat(braveContainsJapan).hasValue(false); | ||
assertThat(otelValue).hasValue(null); | ||
|
||
otherThread.submit(Context.current().wrap(runnable)).get(); | ||
assertThat(braveContainsJapan).hasValue(true); | ||
assertThat(otelValue).hasValue("cat"); | ||
} | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...t/src/braveInOtelTest/java/io/opentelemetry/context/OpenTelemetryCurrentTraceContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.context; | ||
|
||
import brave.propagation.CurrentTraceContext; | ||
import brave.propagation.TraceContext; | ||
|
||
public class OpenTelemetryCurrentTraceContext extends CurrentTraceContext { | ||
|
||
private static final ContextKey<TraceContext> TRACE_CONTEXT_KEY = | ||
ContextKey.named("brave-tracecontext"); | ||
|
||
@Override | ||
public TraceContext get() { | ||
return Context.current().getValue(TRACE_CONTEXT_KEY); | ||
} | ||
|
||
@SuppressWarnings("ReferenceEquality") | ||
@Override | ||
public Scope newScope(TraceContext context) { | ||
Context currentOtel = Context.current(); | ||
TraceContext currentBrave = currentOtel.getValue(TRACE_CONTEXT_KEY); | ||
if (currentBrave == context) { | ||
return Scope.NOOP; | ||
} | ||
|
||
Context newOtel = currentOtel.withValues(TRACE_CONTEXT_KEY, context); | ||
io.opentelemetry.context.Scope otelScope = newOtel.makeCurrent(); | ||
return otelScope::close; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
context/src/grpcInOtelTest/java/io/grpc/override/ContextStorageOverride.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.grpc.override; | ||
|
||
import io.grpc.Context; | ||
import io.opentelemetry.context.ContextKey; | ||
import io.opentelemetry.context.Scope; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
// This exact package / class name indicates to gRPC to use this override. | ||
public class ContextStorageOverride extends Context.Storage { | ||
|
||
private static final Logger log = Logger.getLogger(ContextStorageOverride.class.getName()); | ||
|
||
private static final ContextKey<Context> GRPC_CONTEXT = ContextKey.named("grpc-context"); | ||
private static final Context.Key<Scope> OTEL_SCOPE = Context.key("otel-scope"); | ||
|
||
@Override | ||
public Context doAttach(Context toAttach) { | ||
io.opentelemetry.context.Context otelContext = io.opentelemetry.context.Context.current(); | ||
Context current = otelContext.getValue(GRPC_CONTEXT); | ||
|
||
if (current == toAttach) { | ||
return toAttach; | ||
} | ||
|
||
if (current == null) { | ||
current = Context.ROOT; | ||
} | ||
|
||
io.opentelemetry.context.Context newOtelContext = | ||
otelContext.withValues(GRPC_CONTEXT, toAttach); | ||
Scope scope = newOtelContext.makeCurrent(); | ||
return current.withValue(OTEL_SCOPE, scope); | ||
} | ||
|
||
@Override | ||
public 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()); | ||
} | ||
|
||
Scope otelScope = OTEL_SCOPE.get(toRestore); | ||
otelScope.close(); | ||
} | ||
|
||
@Override | ||
public Context current() { | ||
return io.opentelemetry.context.Context.current().getValue(GRPC_CONTEXT); | ||
} | ||
} |
Oops, something went wrong.