Skip to content

Commit

Permalink
Migrate usage of gRPC context to OTel context. (#1751)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anuraag Agrawal authored Oct 7, 2020
1 parent 1a73964 commit fd49b3f
Show file tree
Hide file tree
Showing 62 changed files with 120 additions and 259 deletions.
4 changes: 0 additions & 4 deletions QUICKSTART.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,6 @@ For more details how to read context from remote processes, see

### Context Propagation

In-process propagation leverages [gRPC Context](https://grpc.github.io/grpc-java/javadoc/io/grpc/Context.html),
a well established context propagation library, contained in a small artifact, which is non-dependent on the
entire gRPC engine.

OpenTelemetry provides a text-based approach to propagate context to remote services using the
[W3C Trace Context](https://www.w3.org/TR/trace-context/) HTTP headers.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package io.opentelemetry.trace.propagation;

import io.grpc.Context;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapPropagator.Getter;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -60,7 +60,7 @@ public String get(Map<String, String> 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.root(), carriers.get(i), getter);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package io.opentelemetry.trace.propagation;

import io.grpc.Context;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapPropagator.Setter;
import io.opentelemetry.trace.DefaultSpan;
import io.opentelemetry.trace.SpanContext;
Expand Down Expand Up @@ -75,7 +75,7 @@ private static SpanContext createTestSpanContext(String traceId, String spanId)
private static List<Context> createContexts(List<SpanContext> spanContexts) {
List<Context> contexts = new ArrayList<>();
for (SpanContext context : spanContexts) {
contexts.add(TracingContextUtils.withSpan(DefaultSpan.create(context), Context.ROOT));
contexts.add(TracingContextUtils.withSpan(DefaultSpan.create(context), Context.root()));
}
return contexts;
}
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/io/opentelemetry/baggage/Baggage.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package io.opentelemetry.baggage;

import io.grpc.Context;
import io.opentelemetry.context.Context;
import java.util.Collection;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
Expand Down
18 changes: 9 additions & 9 deletions api/src/main/java/io/opentelemetry/baggage/BaggageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@

package io.opentelemetry.baggage;

import io.grpc.Context;
import io.opentelemetry.context.ContextUtils;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;
import io.opentelemetry.context.Scope;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

/**
* Utility methods for accessing the {@link Baggage} contained in the {@link io.grpc.Context}.
* Utility methods for accessing the {@link Baggage} contained in the {@link Context}.
*
* @since 0.9.0
*/
@Immutable
public final class BaggageUtils {
private static final Context.Key<Baggage> CORR_CONTEXT_KEY =
Context.key("opentelemetry-corr-context-key");
private static final ContextKey<Baggage> CORR_CONTEXT_KEY =
ContextKey.named("opentelemetry-corr-context-key");

/**
* Creates a new {@code Context} with the given value set.
Expand All @@ -30,7 +30,7 @@ public final class BaggageUtils {
* @since 0.9.0
*/
public static Context withBaggage(Baggage baggage, Context context) {
return context.withValue(CORR_CONTEXT_KEY, baggage);
return context.withValues(CORR_CONTEXT_KEY, baggage);
}

/**
Expand All @@ -53,7 +53,7 @@ public static Baggage getCurrentBaggage() {
* @since 0.9.0
*/
public static Baggage getBaggage(Context context) {
Baggage baggage = CORR_CONTEXT_KEY.get(context);
Baggage baggage = context.getValue(CORR_CONTEXT_KEY);
return baggage == null ? EmptyBaggage.getInstance() : baggage;
}

Expand All @@ -67,7 +67,7 @@ public static Baggage getBaggage(Context context) {
*/
@Nullable
public static Baggage getBaggageWithoutDefault(Context context) {
return CORR_CONTEXT_KEY.get(context);
return context.getValue(CORR_CONTEXT_KEY);
}

/**
Expand All @@ -80,7 +80,7 @@ public static Baggage getBaggageWithoutDefault(Context context) {
*/
public static Scope currentContextWith(Baggage baggage) {
Context context = withBaggage(baggage, Context.current());
return ContextUtils.withScopedContext(context);
return context.makeCurrent();
}

private BaggageUtils() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package io.opentelemetry.baggage;

import io.grpc.Context;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import java.util.Objects;
import javax.annotation.concurrent.Immutable;
Expand Down
10 changes: 5 additions & 5 deletions api/src/main/java/io/opentelemetry/baggage/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
/**
* API for associating entries with scoped operations.
*
* <p>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.
* <p>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.
*
* <p>{@link io.opentelemetry.baggage.Entry Entrys} are key-value pairs of {@link
* java.lang.String}s. They are stored as a map in a {@link io.opentelemetry.baggage.Baggage}.
*
* <p>Note that entries are independent of the tracing data that is propagated in the {@code
* io.grpc.Context}, such as trace ID.
* <p>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.baggage;
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

package io.opentelemetry.trace;

import io.grpc.Context;
import io.opentelemetry.common.AttributeKey;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.internal.Utils;
import java.util.Objects;
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/io/opentelemetry/trace/Span.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

package io.opentelemetry.trace;

import io.grpc.Context;
import io.opentelemetry.common.AttributeKey;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.context.Context;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.ThreadSafe;

Expand Down
6 changes: 3 additions & 3 deletions api/src/main/java/io/opentelemetry/trace/Tracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
* <p>Users may choose to use manual or automatic Context propagation. Because of that this class
* offers APIs to facilitate both usages.
*
* <p>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
* <p>The automatic context propagation is done using {@link io.opentelemetry.context.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.
* {@link io.opentelemetry.context.Context} between different threads.
*
* <p>Example usage with automatic context propagation:
*
Expand Down
20 changes: 10 additions & 10 deletions api/src/main/java/io/opentelemetry/trace/TracingContextUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@

package io.opentelemetry.trace;

import io.grpc.Context;
import io.opentelemetry.context.ContextUtils;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;
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}.
* Util methods/functionality to interact with the {@link Context}.
*
* @since 0.1.0
*/
@Immutable
public final class TracingContextUtils {
private static final Context.Key<Span> CONTEXT_SPAN_KEY =
Context.key("opentelemetry-trace-span-key");
private static final ContextKey<Span> CONTEXT_SPAN_KEY =
ContextKey.named("opentelemetry-trace-span-key");

/**
* Creates a new {@code Context} with the given {@link Span} set.
Expand All @@ -30,7 +30,7 @@ public final class TracingContextUtils {
* @since 0.1.0
*/
public static Context withSpan(Span span, Context context) {
return context.withValue(CONTEXT_SPAN_KEY, span);
return context.withValues(CONTEXT_SPAN_KEY, span);
}

/**
Expand All @@ -41,7 +41,7 @@ public static Context withSpan(Span span, Context context) {
* @since 0.3.0
*/
public static Span getCurrentSpan() {
return getSpan(Context.current());
return getSpan(io.opentelemetry.context.Context.current());
}

/**
Expand All @@ -53,7 +53,7 @@ public static Span getCurrentSpan() {
* @since 0.3.0
*/
public static Span getSpan(Context context) {
Span span = CONTEXT_SPAN_KEY.get(context);
Span span = context.getValue(CONTEXT_SPAN_KEY);
return span == null ? DefaultSpan.getInvalid() : span;
}

Expand All @@ -67,7 +67,7 @@ public static Span getSpan(Context context) {
*/
@Nullable
public static Span getSpanWithoutDefault(Context context) {
return CONTEXT_SPAN_KEY.get(context);
return context.getValue(CONTEXT_SPAN_KEY);
}

/**
Expand All @@ -79,7 +79,7 @@ public static Span getSpanWithoutDefault(Context context) {
* @since 0.1.0
*/
public static Scope currentContextWith(Span span) {
return ContextUtils.withScopedContext(withSpan(span, Context.current()));
return withSpan(span, io.opentelemetry.context.Context.current()).makeCurrent();
}

private TracingContextUtils() {}
Expand Down
6 changes: 3 additions & 3 deletions api/src/main/java/io/opentelemetry/trace/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
*
* <p>{@link io.opentelemetry.trace.Span} represents a single operation within a trace.
*
* <p>{@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.
* <p>{@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;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import static io.opentelemetry.internal.Utils.checkArgument;

import io.grpc.Context;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.internal.TemporaryBuffers;
import io.opentelemetry.trace.DefaultSpan;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import static org.assertj.core.api.Assertions.assertThat;

import io.grpc.Context;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import org.junit.jupiter.api.Test;

class BaggageUtilsTest {
Expand All @@ -21,11 +22,8 @@ void testGetCurrentBaggage_Default() {
@Test
void testGetCurrentBaggage_SetCorrContext() {
Baggage baggage = DefaultBaggageManager.getInstance().baggageBuilder().build();
Context orig = BaggageUtils.withBaggage(baggage, Context.current()).attach();
try {
try (Scope ignored = BaggageUtils.withBaggage(baggage, Context.current()).makeCurrent()) {
assertThat(BaggageUtils.getCurrentBaggage()).isSameAs(baggage);
} finally {
Context.current().detach(orig);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import io.grpc.Context;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -46,13 +46,10 @@ void getCurrentContext_DefaultContext() {

@Test
void getCurrentContext_ContextSetToNull() {
Context orig = BaggageUtils.withBaggage(null, Context.current()).attach();
try {
try (Scope ignored = BaggageUtils.withBaggage(null, Context.current()).makeCurrent()) {
Baggage distContext = DEFAULT_BAGGAGE_MANAGER.getCurrentBaggage();
assertThat(distContext).isNotNull();
assertThat(distContext.getEntries()).isEmpty();
} finally {
Context.current().detach(orig);
}
}

Expand Down
10 changes: 5 additions & 5 deletions api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import io.grpc.Context;
import io.opentelemetry.context.ContextUtils;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -74,7 +73,8 @@ void testSpanContextPropagationExplicitParent() {
Span span =
defaultTracer
.spanBuilder(SPAN_NAME)
.setParent(TracingContextUtils.withSpan(DefaultSpan.create(spanContext), Context.ROOT))
.setParent(
TracingContextUtils.withSpan(DefaultSpan.create(spanContext), Context.root()))
.startSpan();
assertThat(span.getContext()).isSameAs(spanContext);
}
Expand All @@ -86,7 +86,7 @@ void testSpanContextPropagation() {
Span span =
defaultTracer
.spanBuilder(SPAN_NAME)
.setParent(TracingContextUtils.withSpan(parent, Context.ROOT))
.setParent(TracingContextUtils.withSpan(parent, Context.root()))
.startSpan();
assertThat(span.getContext()).isSameAs(spanContext);
}
Expand Down Expand Up @@ -141,7 +141,7 @@ void testSpanContextPropagationCurrentSpan() {
void testSpanContextPropagationCurrentSpanContext() {
Context context =
TracingContextUtils.withSpan(DefaultSpan.create(spanContext), Context.current());
try (Scope scope = ContextUtils.withScopedContext(context)) {
try (Scope scope = context.makeCurrent()) {
Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan();
assertThat(span.getContext()).isSameAs(spanContext);
}
Expand Down
6 changes: 3 additions & 3 deletions api/src/test/java/io/opentelemetry/trace/SpanBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import io.grpc.Context;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.context.Context;
import io.opentelemetry.trace.Span.Kind;
import org.junit.jupiter.api.Test;

Expand All @@ -22,8 +22,8 @@ class SpanBuilderTest {
void doNotCrash_NoopImplementation() {
Span.Builder spanBuilder = tracer.spanBuilder("MySpanName");
spanBuilder.setSpanKind(Kind.SERVER);
spanBuilder.setParent(TracingContextUtils.withSpan(DefaultSpan.create(null), Context.ROOT));
spanBuilder.setParent(Context.ROOT);
spanBuilder.setParent(TracingContextUtils.withSpan(DefaultSpan.create(null), Context.root()));
spanBuilder.setParent(Context.root());
spanBuilder.setNoParent();
spanBuilder.addLink(DefaultSpan.getInvalid().getContext());
spanBuilder.addLink(DefaultSpan.getInvalid().getContext(), Attributes.empty());
Expand Down
Loading

0 comments on commit fd49b3f

Please sign in to comment.