Skip to content

Commit fd49b3f

Browse files
author
Anuraag Agrawal
authored
Migrate usage of gRPC context to OTel context. (open-telemetry#1751)
1 parent 1a73964 commit fd49b3f

62 files changed

Lines changed: 120 additions & 259 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

QUICKSTART.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,6 @@ For more details how to read context from remote processes, see
167167

168168
### Context Propagation
169169

170-
In-process propagation leverages [gRPC Context](https://grpc.github.io/grpc-java/javadoc/io/grpc/Context.html),
171-
a well established context propagation library, contained in a small artifact, which is non-dependent on the
172-
entire gRPC engine.
173-
174170
OpenTelemetry provides a text-based approach to propagate context to remote services using the
175171
[W3C Trace Context](https://www.w3.org/TR/trace-context/) HTTP headers.
176172

api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextExtractBenchmark.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package io.opentelemetry.trace.propagation;
77

8-
import io.grpc.Context;
8+
import io.opentelemetry.context.Context;
99
import io.opentelemetry.context.propagation.TextMapPropagator.Getter;
1010
import java.util.ArrayList;
1111
import java.util.Arrays;
@@ -60,7 +60,7 @@ public String get(Map<String, String> carrier, String key) {
6060
public Context measureExtract() {
6161
Context result = null;
6262
for (int i = 0; i < COUNT; i++) {
63-
result = httpTraceContext.extract(Context.ROOT, carriers.get(i), getter);
63+
result = httpTraceContext.extract(Context.root(), carriers.get(i), getter);
6464
}
6565
return result;
6666
}

api/src/jmh/java/io/opentelemetry/trace/propagation/HttpTraceContextInjectBenchmark.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package io.opentelemetry.trace.propagation;
77

8-
import io.grpc.Context;
8+
import io.opentelemetry.context.Context;
99
import io.opentelemetry.context.propagation.TextMapPropagator.Setter;
1010
import io.opentelemetry.trace.DefaultSpan;
1111
import io.opentelemetry.trace.SpanContext;
@@ -75,7 +75,7 @@ private static SpanContext createTestSpanContext(String traceId, String spanId)
7575
private static List<Context> createContexts(List<SpanContext> spanContexts) {
7676
List<Context> contexts = new ArrayList<>();
7777
for (SpanContext context : spanContexts) {
78-
contexts.add(TracingContextUtils.withSpan(DefaultSpan.create(context), Context.ROOT));
78+
contexts.add(TracingContextUtils.withSpan(DefaultSpan.create(context), Context.root()));
7979
}
8080
return contexts;
8181
}

api/src/main/java/io/opentelemetry/baggage/Baggage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package io.opentelemetry.baggage;
77

8-
import io.grpc.Context;
8+
import io.opentelemetry.context.Context;
99
import java.util.Collection;
1010
import javax.annotation.Nullable;
1111
import javax.annotation.concurrent.Immutable;

api/src/main/java/io/opentelemetry/baggage/BaggageUtils.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55

66
package io.opentelemetry.baggage;
77

8-
import io.grpc.Context;
9-
import io.opentelemetry.context.ContextUtils;
8+
import io.opentelemetry.context.Context;
9+
import io.opentelemetry.context.ContextKey;
1010
import io.opentelemetry.context.Scope;
1111
import javax.annotation.Nullable;
1212
import javax.annotation.concurrent.Immutable;
1313

1414
/**
15-
* Utility methods for accessing the {@link Baggage} contained in the {@link io.grpc.Context}.
15+
* Utility methods for accessing the {@link Baggage} contained in the {@link Context}.
1616
*
1717
* @since 0.9.0
1818
*/
1919
@Immutable
2020
public final class BaggageUtils {
21-
private static final Context.Key<Baggage> CORR_CONTEXT_KEY =
22-
Context.key("opentelemetry-corr-context-key");
21+
private static final ContextKey<Baggage> CORR_CONTEXT_KEY =
22+
ContextKey.named("opentelemetry-corr-context-key");
2323

2424
/**
2525
* Creates a new {@code Context} with the given value set.
@@ -30,7 +30,7 @@ public final class BaggageUtils {
3030
* @since 0.9.0
3131
*/
3232
public static Context withBaggage(Baggage baggage, Context context) {
33-
return context.withValue(CORR_CONTEXT_KEY, baggage);
33+
return context.withValues(CORR_CONTEXT_KEY, baggage);
3434
}
3535

3636
/**
@@ -53,7 +53,7 @@ public static Baggage getCurrentBaggage() {
5353
* @since 0.9.0
5454
*/
5555
public static Baggage getBaggage(Context context) {
56-
Baggage baggage = CORR_CONTEXT_KEY.get(context);
56+
Baggage baggage = context.getValue(CORR_CONTEXT_KEY);
5757
return baggage == null ? EmptyBaggage.getInstance() : baggage;
5858
}
5959

@@ -67,7 +67,7 @@ public static Baggage getBaggage(Context context) {
6767
*/
6868
@Nullable
6969
public static Baggage getBaggageWithoutDefault(Context context) {
70-
return CORR_CONTEXT_KEY.get(context);
70+
return context.getValue(CORR_CONTEXT_KEY);
7171
}
7272

7373
/**
@@ -80,7 +80,7 @@ public static Baggage getBaggageWithoutDefault(Context context) {
8080
*/
8181
public static Scope currentContextWith(Baggage baggage) {
8282
Context context = withBaggage(baggage, Context.current());
83-
return ContextUtils.withScopedContext(context);
83+
return context.makeCurrent();
8484
}
8585

8686
private BaggageUtils() {}

api/src/main/java/io/opentelemetry/baggage/DefaultBaggageManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package io.opentelemetry.baggage;
77

8-
import io.grpc.Context;
8+
import io.opentelemetry.context.Context;
99
import io.opentelemetry.context.Scope;
1010
import java.util.Objects;
1111
import javax.annotation.concurrent.Immutable;

api/src/main/java/io/opentelemetry/baggage/package-info.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
/**
77
* API for associating entries with scoped operations.
88
*
9-
* <p>This package manages a set of entries in the {@code io.grpc.Context}. The entries can be used
10-
* to label anything that is associated with a specific operation. For example, the {@code
11-
* opentelemetry.stats} package labels all stats with the current entries.
9+
* <p>This package manages a set of entries in the {@link io.opentelemetry.context.Context}. The
10+
* entries can be used to label anything that is associated with a specific operation. For example,
11+
* the {@code opentelemetry.stats} package labels all stats with the current entries.
1212
*
1313
* <p>{@link io.opentelemetry.baggage.Entry Entrys} are key-value pairs of {@link
1414
* java.lang.String}s. They are stored as a map in a {@link io.opentelemetry.baggage.Baggage}.
1515
*
16-
* <p>Note that entries are independent of the tracing data that is propagated in the {@code
17-
* io.grpc.Context}, such as trace ID.
16+
* <p>Note that entries are independent of the tracing data that is propagated in the {@link
17+
* io.opentelemetry.context.Context}, such as trace ID.
1818
*/
1919
// TODO: Add code examples.
2020
package io.opentelemetry.baggage;

api/src/main/java/io/opentelemetry/trace/DefaultTracer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
package io.opentelemetry.trace;
77

8-
import io.grpc.Context;
98
import io.opentelemetry.common.AttributeKey;
109
import io.opentelemetry.common.Attributes;
10+
import io.opentelemetry.context.Context;
1111
import io.opentelemetry.context.Scope;
1212
import io.opentelemetry.internal.Utils;
1313
import java.util.Objects;

api/src/main/java/io/opentelemetry/trace/Span.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
package io.opentelemetry.trace;
77

8-
import io.grpc.Context;
98
import io.opentelemetry.common.AttributeKey;
109
import io.opentelemetry.common.Attributes;
10+
import io.opentelemetry.context.Context;
1111
import javax.annotation.Nonnull;
1212
import javax.annotation.concurrent.ThreadSafe;
1313

api/src/main/java/io/opentelemetry/trace/Tracer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
* <p>Users may choose to use manual or automatic Context propagation. Because of that this class
1616
* offers APIs to facilitate both usages.
1717
*
18-
* <p>The automatic context propagation is done using {@link io.grpc.Context} which is a gRPC
19-
* independent implementation for in-process Context propagation mechanism which can carry
18+
* <p>The automatic context propagation is done using {@link io.opentelemetry.context.Context} which
19+
* is a gRPC independent implementation for in-process Context propagation mechanism which can carry
2020
* scoped-values across API boundaries and between threads. Users of the library must propagate the
21-
* {@link io.grpc.Context} between different threads.
21+
* {@link io.opentelemetry.context.Context} between different threads.
2222
*
2323
* <p>Example usage with automatic context propagation:
2424
*

0 commit comments

Comments
 (0)