Skip to content

Commit ea4e2bf

Browse files
songy23zhangkun83
authored andcommitted
all: Upgrade OpenCensus versions. (grpc#5657)
Also updated CensusModule to use the new helper methods ContextUtils.withValue() instead of directly manipulating the context keys. See census-instrumentation/opencensus-java#1864.
1 parent f8d0868 commit ea4e2bf

File tree

7 files changed

+54
-44
lines changed

7 files changed

+54
-44
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ subprojects {
112112
protobufVersion = '3.7.1'
113113
protocVersion = protobufVersion
114114
protobufNanoVersion = '3.0.0-alpha-5'
115-
opencensusVersion = '0.19.2'
115+
opencensusVersion = '0.21.0'
116116

117117
configureProtoCompilation = {
118118
String generatedSourcePath = "${projectDir}/src/generated"

core/src/main/java/io/grpc/internal/CensusStatsModule.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
2020
import static com.google.common.base.Preconditions.checkState;
21-
import static io.opencensus.tags.unsafe.ContextUtils.TAG_CONTEXT_KEY;
2221

2322
import com.google.common.annotations.VisibleForTesting;
2423
import com.google.common.base.Stopwatch;
@@ -48,6 +47,7 @@
4847
import io.opencensus.tags.Tags;
4948
import io.opencensus.tags.propagation.TagContextBinarySerializer;
5049
import io.opencensus.tags.propagation.TagContextSerializationException;
50+
import io.opencensus.tags.unsafe.ContextUtils;
5151
import java.util.concurrent.TimeUnit;
5252
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
5353
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
@@ -356,7 +356,7 @@ static final class ClientCallTracer extends ClientStreamTracer.Factory {
356356
this.parentCtx = checkNotNull(parentCtx);
357357
TagValue methodTag = TagValue.create(fullMethodName);
358358
this.startCtx = module.tagger.toBuilder(parentCtx)
359-
.put(DeprecatedCensusConstants.RPC_METHOD, methodTag)
359+
.putPropagating(DeprecatedCensusConstants.RPC_METHOD, methodTag)
360360
.build();
361361
this.stopwatch = module.stopwatchSupplier.get().start();
362362
if (module.recordStartedRpcs) {
@@ -442,7 +442,7 @@ void callEnded(Status status) {
442442
module
443443
.tagger
444444
.toBuilder(startCtx)
445-
.put(DeprecatedCensusConstants.RPC_STATUS, statusTag)
445+
.putPropagating(DeprecatedCensusConstants.RPC_STATUS, statusTag)
446446
.build());
447447
}
448448
}
@@ -647,14 +647,14 @@ public void streamClosed(Status status) {
647647
module
648648
.tagger
649649
.toBuilder(parentCtx)
650-
.put(DeprecatedCensusConstants.RPC_STATUS, statusTag)
650+
.putPropagating(DeprecatedCensusConstants.RPC_STATUS, statusTag)
651651
.build());
652652
}
653653

654654
@Override
655655
public Context filterContext(Context context) {
656656
if (!module.tagger.empty().equals(parentCtx)) {
657-
return context.withValue(TAG_CONTEXT_KEY, parentCtx);
657+
return ContextUtils.withValue(context, parentCtx);
658658
}
659659
return context;
660660
}
@@ -672,7 +672,7 @@ public ServerStreamTracer newServerStreamTracer(String fullMethodName, Metadata
672672
parentCtx =
673673
tagger
674674
.toBuilder(parentCtx)
675-
.put(DeprecatedCensusConstants.RPC_METHOD, methodTag)
675+
.putPropagating(DeprecatedCensusConstants.RPC_METHOD, methodTag)
676676
.build();
677677
return new ServerTracer(CensusStatsModule.this, parentCtx);
678678
}

core/src/main/java/io/grpc/internal/CensusTracingModule.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package io.grpc.internal;
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
20-
import static io.opencensus.trace.unsafe.ContextUtils.CONTEXT_SPAN_KEY;
2120

2221
import com.google.common.annotations.VisibleForTesting;
2322
import io.grpc.CallOptions;
@@ -41,6 +40,7 @@
4140
import io.opencensus.trace.Status;
4241
import io.opencensus.trace.Tracer;
4342
import io.opencensus.trace.propagation.BinaryFormat;
43+
import io.opencensus.trace.unsafe.ContextUtils;
4444
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
4545
import java.util.logging.Level;
4646
import java.util.logging.Logger;
@@ -342,7 +342,7 @@ public Context filterContext(Context context) {
342342
// Access directly the unsafe trace API to create the new Context. This is a safe usage
343343
// because gRPC always creates a new Context for each of the server calls and does not
344344
// inherit from the parent Context.
345-
return context.withValue(CONTEXT_SPAN_KEY, span);
345+
return ContextUtils.withValue(context, span);
346346
}
347347

348348
@Override
@@ -382,7 +382,8 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
382382
// Safe usage of the unsafe trace API because CONTEXT_SPAN_KEY.get() returns the same value
383383
// as Tracer.getCurrentSpan() except when no value available when the return value is null
384384
// for the direct access and BlankSpan when Tracer API is used.
385-
final ClientCallTracer tracerFactory = newClientCallTracer(CONTEXT_SPAN_KEY.get(), method);
385+
final ClientCallTracer tracerFactory =
386+
newClientCallTracer(ContextUtils.getValue(Context.current()), method);
386387
ClientCall<ReqT, RespT> call =
387388
next.newCall(
388389
method,

core/src/test/java/io/grpc/internal/CensusModulesTest.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package io.grpc.internal;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20-
import static io.opencensus.tags.unsafe.ContextUtils.TAG_CONTEXT_KEY;
2120
import static java.util.concurrent.TimeUnit.MILLISECONDS;
2221
import static org.junit.Assert.assertEquals;
2322
import static org.junit.Assert.assertFalse;
@@ -252,21 +251,24 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
252251
ClientCall<String, String> call;
253252
if (nonDefaultContext) {
254253
Context ctx =
255-
Context.ROOT.withValues(
256-
TAG_CONTEXT_KEY,
257-
tagger.emptyBuilder().put(
258-
StatsTestUtils.EXTRA_TAG, TagValue.create("extra value")).build(),
259-
ContextUtils.CONTEXT_SPAN_KEY,
260-
fakeClientParentSpan);
254+
io.opencensus.tags.unsafe.ContextUtils.withValue(
255+
Context.ROOT,
256+
tagger
257+
.emptyBuilder()
258+
.putPropagating(StatsTestUtils.EXTRA_TAG, TagValue.create("extra value"))
259+
.build());
260+
ctx = ContextUtils.withValue(ctx, fakeClientParentSpan);
261261
Context origCtx = ctx.attach();
262262
try {
263263
call = interceptedChannel.newCall(method, CALL_OPTIONS);
264264
} finally {
265265
ctx.detach(origCtx);
266266
}
267267
} else {
268-
assertEquals(TAG_CONTEXT_KEY.get(Context.ROOT), TAG_CONTEXT_KEY.get());
269-
assertNull(ContextUtils.CONTEXT_SPAN_KEY.get());
268+
assertEquals(
269+
io.opencensus.tags.unsafe.ContextUtils.getValue(Context.ROOT),
270+
io.opencensus.tags.unsafe.ContextUtils.getValue(Context.current()));
271+
assertEquals(ContextUtils.getValue(Context.current()), BlankSpan.INSTANCE);
270272
call = interceptedChannel.newCall(method, CALL_OPTIONS);
271273
}
272274

@@ -303,7 +305,7 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
303305
verify(spyClientSpanBuilder).setRecordEvents(eq(true));
304306
} else {
305307
verify(tracer).spanBuilderWithExplicitParent(
306-
eq("Sent.package1.service2.method3"), ArgumentMatchers.<Span>isNull());
308+
eq("Sent.package1.service2.method3"), ArgumentMatchers.<Span>isNotNull());
307309
verify(spyClientSpanBuilder).setRecordEvents(eq(true));
308310
}
309311
verify(spyClientSpan, never()).end(any(EndSpanOptions.class));
@@ -646,7 +648,7 @@ private void subtestStatsHeadersPropagateTags(boolean propagate, boolean recordS
646648
// EXTRA_TAG is propagated by the FakeStatsContextFactory. Note that not all tags are
647649
// propagated. The StatsContextFactory decides which tags are to propagated. gRPC facilitates
648650
// the propagation by putting them in the headers.
649-
TagContext clientCtx = tagger.emptyBuilder().put(
651+
TagContext clientCtx = tagger.emptyBuilder().putPropagating(
650652
StatsTestUtils.EXTRA_TAG, TagValue.create("extra-tag-value-897")).build();
651653
CensusStatsModule census =
652654
new CensusStatsModule(
@@ -687,11 +689,11 @@ private void subtestStatsHeadersPropagateTags(boolean propagate, boolean recordS
687689
// It also put clientCtx in the Context seen by the call handler
688690
assertEquals(
689691
tagger.toBuilder(clientCtx)
690-
.put(
692+
.putPropagating(
691693
DeprecatedCensusConstants.RPC_METHOD,
692694
TagValue.create(method.getFullMethodName()))
693695
.build(),
694-
TAG_CONTEXT_KEY.get(serverContext));
696+
io.opencensus.tags.unsafe.ContextUtils.getValue(serverContext));
695697

696698
// Verifies that the server tracer records the status with the propagated tag
697699
serverTracer.streamClosed(Status.OK);
@@ -798,7 +800,7 @@ public void traceHeadersPropagateSpanContext() throws Exception {
798800
verify(spyServerSpanBuilder).setRecordEvents(eq(true));
799801

800802
Context filteredContext = serverTracer.filterContext(Context.ROOT);
801-
assertSame(spyServerSpan, ContextUtils.CONTEXT_SPAN_KEY.get(filteredContext));
803+
assertSame(spyServerSpan, ContextUtils.getValue(filteredContext));
802804
}
803805

804806
@Test
@@ -919,11 +921,11 @@ private void subtestServerBasicStatsNoHeaders(
919921
}
920922

921923
Context filteredContext = tracer.filterContext(Context.ROOT);
922-
TagContext statsCtx = TAG_CONTEXT_KEY.get(filteredContext);
924+
TagContext statsCtx = io.opencensus.tags.unsafe.ContextUtils.getValue(filteredContext);
923925
assertEquals(
924926
tagger
925927
.emptyBuilder()
926-
.put(
928+
.putPropagating(
927929
DeprecatedCensusConstants.RPC_METHOD,
928930
TagValue.create(method.getFullMethodName()))
929931
.build(),
@@ -1021,7 +1023,7 @@ public void serverBasicTracingNoHeaders() {
10211023
verify(spyServerSpanBuilder).setRecordEvents(eq(true));
10221024

10231025
Context filteredContext = serverStreamTracer.filterContext(Context.ROOT);
1024-
assertSame(spyServerSpan, ContextUtils.CONTEXT_SPAN_KEY.get(filteredContext));
1026+
assertSame(spyServerSpan, ContextUtils.getValue(filteredContext));
10251027

10261028
serverStreamTracer.serverCallStarted(
10271029
new ServerCallInfoImpl<>(method, Attributes.EMPTY, null));

interop-testing/src/main/java/io/grpc/testing/integration/AbstractInteropTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020
import static com.google.common.truth.Truth.assertWithMessage;
2121
import static io.grpc.stub.ClientCalls.blockingServerStreamingCall;
22-
import static io.opencensus.tags.unsafe.ContextUtils.TAG_CONTEXT_KEY;
23-
import static io.opencensus.trace.unsafe.ContextUtils.CONTEXT_SPAN_KEY;
2422
import static org.junit.Assert.assertEquals;
2523
import static org.junit.Assert.assertFalse;
2624
import static org.junit.Assert.assertNotEquals;
@@ -1448,19 +1446,21 @@ public void censusContextsPropagated() {
14481446
// A valid ID is guaranteed to be unique, so we can verify it is actually propagated.
14491447
assertTrue(clientParentSpan.getContext().getTraceId().isValid());
14501448
Context ctx =
1451-
Context.ROOT.withValues(
1452-
TAG_CONTEXT_KEY,
1453-
tagger.emptyBuilder().put(
1454-
StatsTestUtils.EXTRA_TAG, TagValue.create("extra value")).build(),
1455-
ContextUtils.CONTEXT_SPAN_KEY,
1456-
clientParentSpan);
1449+
io.opencensus.tags.unsafe.ContextUtils.withValue(
1450+
Context.ROOT,
1451+
tagger
1452+
.emptyBuilder()
1453+
.putPropagating(StatsTestUtils.EXTRA_TAG, TagValue.create("extra value"))
1454+
.build());
1455+
ctx = ContextUtils.withValue(ctx, clientParentSpan);
14571456
Context origCtx = ctx.attach();
14581457
try {
14591458
blockingStub.unaryCall(SimpleRequest.getDefaultInstance());
14601459
Context serverCtx = contextCapture.get();
14611460
assertNotNull(serverCtx);
14621461

1463-
FakeTagContext statsCtx = (FakeTagContext) TAG_CONTEXT_KEY.get(serverCtx);
1462+
FakeTagContext statsCtx =
1463+
(FakeTagContext) io.opencensus.tags.unsafe.ContextUtils.getValue(serverCtx);
14641464
assertNotNull(statsCtx);
14651465
Map<TagKey, TagValue> tags = statsCtx.getTags();
14661466
boolean tagFound = false;
@@ -1472,7 +1472,7 @@ public void censusContextsPropagated() {
14721472
}
14731473
assertTrue("tag not found", tagFound);
14741474

1475-
Span span = CONTEXT_SPAN_KEY.get(serverCtx);
1475+
Span span = ContextUtils.getValue(serverCtx);
14761476
assertNotNull(span);
14771477
SpanContext spanContext = span.getContext();
14781478
assertEquals(clientParentSpan.getContext().getTraceId(), spanContext.getTraceId());

repositories.bzl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,18 +399,18 @@ def io_netty_transport():
399399
def io_opencensus_api():
400400
jvm_maven_import_external(
401401
name = "io_opencensus_opencensus_api",
402-
artifact = "io.opencensus:opencensus-api:0.19.2",
402+
artifact = "io.opencensus:opencensus-api:0.21.0",
403403
server_urls = ["http://central.maven.org/maven2"],
404-
artifact_sha256 = "0e2e5d3f4f6fd296017a00b1cd8fb8e4261331cc0c3b6818c0533b01bf7945dc",
404+
artifact_sha256 = "8e2cb0f6391d8eb0a1bcd01e7748883f0033b1941754f4ed3f19d2c3e4276fc8",
405405
licenses = ["notice"], # Apache 2.0
406406
)
407407

408408
def io_opencensus_grpc_metrics():
409409
jvm_maven_import_external(
410410
name = "io_opencensus_opencensus_contrib_grpc_metrics",
411-
artifact = "io.opencensus:opencensus-contrib-grpc-metrics:0.19.2",
411+
artifact = "io.opencensus:opencensus-contrib-grpc-metrics:0.21.0",
412412
server_urls = ["http://central.maven.org/maven2"],
413-
artifact_sha256 = "0e23c03414612c7fbef1fdb347076eb69368e596de768cd4b98e081d92206f15",
413+
artifact_sha256 = "29fc79401082301542cab89d7054d2f0825f184492654c950020553ef4ff0ef8",
414414
licenses = ["notice"], # Apache 2.0
415415
)
416416

testing/src/main/java/io/grpc/internal/testing/StatsTestUtils.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.common.collect.ImmutableMap;
2424
import com.google.common.collect.Iterators;
2525
import com.google.common.collect.Maps;
26+
import io.grpc.Context;
2627
import io.opencensus.common.Scope;
2728
import io.opencensus.stats.Measure;
2829
import io.opencensus.stats.MeasureMap;
@@ -31,6 +32,8 @@
3132
import io.opencensus.tags.TagContext;
3233
import io.opencensus.tags.TagContextBuilder;
3334
import io.opencensus.tags.TagKey;
35+
import io.opencensus.tags.TagMetadata;
36+
import io.opencensus.tags.TagMetadata.TagTtl;
3437
import io.opencensus.tags.TagValue;
3538
import io.opencensus.tags.Tagger;
3639
import io.opencensus.tags.propagation.TagContextBinarySerializer;
@@ -168,7 +171,7 @@ public FakeTagContext empty() {
168171

169172
@Override
170173
public TagContext getCurrentTagContext() {
171-
return ContextUtils.TAG_CONTEXT_KEY.get();
174+
return ContextUtils.getValue(Context.current());
172175
}
173176

174177
@Override
@@ -201,7 +204,7 @@ public TagContext fromByteArray(byte[] bytes) throws TagContextDeserializationEx
201204
String serializedString = new String(bytes, UTF_8);
202205
if (serializedString.startsWith(EXTRA_TAG_HEADER_VALUE_PREFIX)) {
203206
return tagger.emptyBuilder()
204-
.put(EXTRA_TAG,
207+
.putPropagating(EXTRA_TAG,
205208
TagValue.create(serializedString.substring(EXTRA_TAG_HEADER_VALUE_PREFIX.length())))
206209
.build();
207210
} else {
@@ -256,6 +259,9 @@ public static final class FakeTagContext extends TagContext {
256259
private static final FakeTagContext EMPTY =
257260
new FakeTagContext(ImmutableMap.<TagKey, TagValue>of());
258261

262+
private static final TagMetadata METADATA_PROPAGATING =
263+
TagMetadata.create(TagTtl.UNLIMITED_PROPAGATION);
264+
259265
private final ImmutableMap<TagKey, TagValue> tags;
260266

261267
private FakeTagContext(ImmutableMap<TagKey, TagValue> tags) {
@@ -278,7 +284,7 @@ protected Iterator<Tag> getIterator() {
278284
new Function<Map.Entry<TagKey, TagValue>, Tag>() {
279285
@Override
280286
public Tag apply(@Nullable Map.Entry<TagKey, TagValue> entry) {
281-
return Tag.create(entry.getKey(), entry.getValue());
287+
return Tag.create(entry.getKey(), entry.getValue(), METADATA_PROPAGATING);
282288
}
283289
});
284290
}
@@ -292,6 +298,7 @@ private FakeTagContextBuilder(Map<TagKey, TagValue> tags) {
292298
tagsBuilder.putAll(tags);
293299
}
294300

301+
@SuppressWarnings("deprecation")
295302
@Override
296303
public TagContextBuilder put(TagKey key, TagValue value) {
297304
tagsBuilder.put(key, value);

0 commit comments

Comments
 (0)