Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add instrumentation tags to opencensus metrics #11322

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions census/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ java_library(
visibility = ["//visibility:public"],
deps = [
"//api",
"//core:internal",
"//context",
artifact("com.google.code.findbugs:jsr305"),
artifact("com.google.guava:guava"),
Expand Down
1 change: 1 addition & 0 deletions census/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies {
runtimeOnly project(":grpc-context")
implementation libraries.guava,
project(":grpc-context"), // Override opencensus dependency with our newer version
project(":grpc-core"),
libraries.opencensus.api,
libraries.opencensus.contrib.grpc.metrics

Expand Down
14 changes: 14 additions & 0 deletions census/src/main/java/io/grpc/census/CensusStatsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

import static com.google.common.base.Preconditions.checkNotNull;
import static io.grpc.census.internal.ObservabilityCensusConstants.API_LATENCY_PER_CALL;
import static io.grpc.census.internal.ObservabilityCensusConstants.INSTRUMENTATION_SOURCE;
import static io.grpc.census.internal.ObservabilityCensusConstants.INSTRUMENTATION_VERSION;
import static io.grpc.census.internal.ObservabilityCensusConstants.LIBRARY_NAME;
import static io.grpc.census.internal.ObservabilityCensusConstants.LIBRARY_VERSION;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Stopwatch;
Expand Down Expand Up @@ -396,6 +400,8 @@ void recordFinishedAttempt() {
.tagger
.toBuilder(startCtx)
.putLocal(RpcMeasureConstants.GRPC_CLIENT_STATUS, statusTag)
.putLocal(INSTRUMENTATION_SOURCE, LIBRARY_NAME)
.putLocal(INSTRUMENTATION_VERSION, LIBRARY_VERSION)
.build());
}
}
Expand Down Expand Up @@ -447,6 +453,8 @@ static final class CallAttemptsTracerFactory extends
TagValue methodTag = TagValue.create(fullMethodName);
startCtx = module.tagger.toBuilder(parentCtx)
.putLocal(RpcMeasureConstants.GRPC_CLIENT_METHOD, methodTag)
.putLocal(INSTRUMENTATION_SOURCE, LIBRARY_NAME)
.putLocal(INSTRUMENTATION_VERSION, LIBRARY_VERSION)
.build();
if (module.recordStartedRpcs) {
// Record here in case newClientStreamTracer() would never be called.
Expand Down Expand Up @@ -556,6 +564,8 @@ void recordFinishedCall() {
.toBuilder(parentCtx)
.putLocal(RpcMeasureConstants.GRPC_CLIENT_METHOD, methodTag)
.putLocal(RpcMeasureConstants.GRPC_CLIENT_STATUS, statusTag)
.putLocal(INSTRUMENTATION_SOURCE, LIBRARY_NAME)
.putLocal(INSTRUMENTATION_VERSION, LIBRARY_VERSION)
.build());
}
}
Expand Down Expand Up @@ -765,6 +775,8 @@ public void streamClosed(Status status) {
.tagger
.toBuilder(parentCtx)
.putLocal(RpcMeasureConstants.GRPC_SERVER_STATUS, statusTag)
.putLocal(INSTRUMENTATION_SOURCE, LIBRARY_NAME)
.putLocal(INSTRUMENTATION_VERSION, LIBRARY_VERSION)
.build());
}

Expand All @@ -787,6 +799,8 @@ public ServerStreamTracer newServerStreamTracer(String fullMethodName, Metadata
tagger
.toBuilder(parentCtx)
.putLocal(RpcMeasureConstants.GRPC_SERVER_METHOD, methodTag)
.putLocal(INSTRUMENTATION_SOURCE, LIBRARY_NAME)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the looks of it, we should just do something like this on client-side as well and not update the individual record sites.

.putLocal(INSTRUMENTATION_VERSION, LIBRARY_VERSION)
.build();
return new ServerTracer(CensusStatsModule.this, parentCtx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@

import com.google.common.annotations.VisibleForTesting;
import io.grpc.CallOptions;
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.GrpcUtil.GrpcBuildVersion;
import io.opencensus.contrib.grpc.metrics.RpcViewConstants;
import io.opencensus.stats.Aggregation;
import io.opencensus.stats.Measure;
import io.opencensus.stats.Measure.MeasureDouble;
import io.opencensus.stats.View;
import io.opencensus.tags.TagKey;
import io.opencensus.tags.TagValue;
import io.opencensus.trace.SpanContext;
import java.util.Arrays;

Expand All @@ -47,6 +51,16 @@ public final class ObservabilityCensusConstants {
public static CallOptions.Key<SpanContext> CLIENT_TRACE_SPAN_CONTEXT_KEY
= CallOptions.Key.createWithDefault("Client span context for tracing", SpanContext.INVALID);

public static final TagKey INSTRUMENTATION_SOURCE = TagKey.create("instrumentation_source");

public static final TagKey INSTRUMENTATION_VERSION = TagKey.create("instrumentation_version");

public static final TagValue LIBRARY_NAME = TagValue.create("grpc-java");

static GrpcBuildVersion buildVersion = GrpcUtil.getGrpcBuildVersion();
public static final TagValue LIBRARY_VERSION = TagValue.create(
"v" + buildVersion.getImplementationVersion());

static final Aggregation AGGREGATION_WITH_BYTES_HISTOGRAM =
RpcViewConstants.GRPC_CLIENT_SENT_BYTES_PER_RPC_VIEW.getAggregation();

Expand All @@ -65,39 +79,44 @@ public final class ObservabilityCensusConstants {
"Time taken by gRPC to complete an RPC from application's perspective",
API_LATENCY_PER_CALL,
AGGREGATION_WITH_MILLIS_HISTOGRAM,
Arrays.asList(GRPC_CLIENT_METHOD, GRPC_CLIENT_STATUS));
Arrays.asList(GRPC_CLIENT_METHOD, GRPC_CLIENT_STATUS,
INSTRUMENTATION_SOURCE, INSTRUMENTATION_VERSION));

public static final View GRPC_CLIENT_SENT_COMPRESSED_MESSAGE_BYTES_PER_RPC_VIEW =
View.create(
View.Name.create("grpc.io/client/sent_compressed_message_bytes_per_rpc"),
"Compressed message bytes sent per client RPC attempt",
GRPC_CLIENT_SENT_BYTES_PER_RPC,
AGGREGATION_WITH_BYTES_HISTOGRAM,
Arrays.asList(GRPC_CLIENT_METHOD, GRPC_CLIENT_STATUS));
Arrays.asList(GRPC_CLIENT_METHOD, GRPC_CLIENT_STATUS,
INSTRUMENTATION_SOURCE, INSTRUMENTATION_VERSION));

public static final View GRPC_CLIENT_RECEIVED_COMPRESSED_MESSAGE_BYTES_PER_RPC_VIEW =
View.create(
View.Name.create("grpc.io/client/received_compressed_message_bytes_per_rpc"),
"Compressed message bytes received per client RPC attempt",
GRPC_CLIENT_RECEIVED_BYTES_PER_RPC,
AGGREGATION_WITH_BYTES_HISTOGRAM,
Arrays.asList(GRPC_CLIENT_METHOD, GRPC_CLIENT_STATUS));
Arrays.asList(GRPC_CLIENT_METHOD, GRPC_CLIENT_STATUS,
INSTRUMENTATION_SOURCE, INSTRUMENTATION_VERSION));

public static final View GRPC_SERVER_SENT_COMPRESSED_MESSAGE_BYTES_PER_RPC_VIEW =
View.create(
View.Name.create("grpc.io/server/sent_compressed_message_bytes_per_rpc"),
"Compressed message bytes sent per server RPC",
GRPC_SERVER_SENT_BYTES_PER_RPC,
AGGREGATION_WITH_BYTES_HISTOGRAM,
Arrays.asList(GRPC_SERVER_METHOD, GRPC_SERVER_STATUS));
Arrays.asList(GRPC_SERVER_METHOD, GRPC_SERVER_STATUS,
INSTRUMENTATION_SOURCE, INSTRUMENTATION_VERSION));

public static final View GRPC_SERVER_RECEIVED_COMPRESSED_MESSAGE_BYTES_PER_RPC_VIEW =
View.create(
View.Name.create("grpc.io/server/received_compressed_message_bytes_per_rpc"),
"Compressed message bytes received per server RPC",
GRPC_SERVER_RECEIVED_BYTES_PER_RPC,
AGGREGATION_WITH_BYTES_HISTOGRAM,
Arrays.asList(GRPC_SERVER_METHOD, GRPC_SERVER_STATUS));
Arrays.asList(GRPC_SERVER_METHOD, GRPC_SERVER_STATUS,
INSTRUMENTATION_SOURCE, INSTRUMENTATION_VERSION));

private ObservabilityCensusConstants() {}
}
33 changes: 21 additions & 12 deletions census/src/test/java/io/grpc/census/CensusModulesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
import static io.grpc.census.CensusStatsModule.CallAttemptsTracerFactory.TRANSPARENT_RETRIES_PER_CALL;
import static io.grpc.census.internal.ObservabilityCensusConstants.API_LATENCY_PER_CALL;
import static io.grpc.census.internal.ObservabilityCensusConstants.CLIENT_TRACE_SPAN_CONTEXT_KEY;
import static io.grpc.census.internal.ObservabilityCensusConstants.INSTRUMENTATION_SOURCE;
import static io.grpc.census.internal.ObservabilityCensusConstants.INSTRUMENTATION_VERSION;
import static io.grpc.census.internal.ObservabilityCensusConstants.LIBRARY_NAME;
import static io.grpc.census.internal.ObservabilityCensusConstants.LIBRARY_VERSION;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -335,10 +339,10 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
if (nonDefaultContext) {
TagValue extraTag = record.tags.get(StatsTestUtils.EXTRA_TAG);
assertEquals("extra value", extraTag.asString());
assertEquals(2, record.tags.size());
assertEquals(4, record.tags.size());
} else {
assertNull(record.tags.get(StatsTestUtils.EXTRA_TAG));
assertEquals(1, record.tags.size());
assertEquals(3, record.tags.size());
}

if (nonDefaultContext) {
Expand Down Expand Up @@ -427,7 +431,7 @@ private void subtestClientBasicStatsDefaultContext(
StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
assertNotNull(record);
assertNoServerContent(record);
assertEquals(1, record.tags.size());
assertEquals(3, record.tags.size());
TagValue methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
assertEquals(method.getFullMethodName(), methodTag.asString());
assertEquals(1, record.getMetricAsLongOrFail(RpcMeasureConstants.GRPC_CLIENT_STARTED_RPCS));
Expand Down Expand Up @@ -537,7 +541,7 @@ public void recordRetryStats() {
callAttemptsTracerFactory.newClientStreamTracer(STREAM_INFO, new Metadata());

StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
assertEquals(1, record.tags.size());
assertEquals(3, record.tags.size());
TagValue methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
assertEquals(method.getFullMethodName(), methodTag.asString());
assertEquals(1, record.getMetricAsLongOrFail(RpcMeasureConstants.GRPC_CLIENT_STARTED_RPCS));
Expand Down Expand Up @@ -581,7 +585,7 @@ record = statsRecorder.pollRecord();
fakeClock.forwardTime(1000, MILLISECONDS);
tracer = callAttemptsTracerFactory.newClientStreamTracer(STREAM_INFO, new Metadata());
record = statsRecorder.pollRecord();
assertEquals(1, record.tags.size());
assertEquals(3, record.tags.size());
methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
assertEquals(method.getFullMethodName(), methodTag.asString());
assertEquals(1, record.getMetricAsLongOrFail(RpcMeasureConstants.GRPC_CLIENT_STARTED_RPCS));
Expand Down Expand Up @@ -623,7 +627,7 @@ record = statsRecorder.pollRecord();
tracer = callAttemptsTracerFactory.newClientStreamTracer(
STREAM_INFO.toBuilder().setIsTransparentRetry(true).build(), new Metadata());
record = statsRecorder.pollRecord();
assertEquals(1, record.tags.size());
assertEquals(3, record.tags.size());
methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
assertEquals(method.getFullMethodName(), methodTag.asString());
assertEquals(1, record.getMetricAsLongOrFail(RpcMeasureConstants.GRPC_CLIENT_STARTED_RPCS));
Expand All @@ -644,7 +648,7 @@ record = statsRecorder.pollRecord();
tracer = callAttemptsTracerFactory.newClientStreamTracer(
STREAM_INFO.toBuilder().setIsTransparentRetry(true).build(), new Metadata());
record = statsRecorder.pollRecord();
assertEquals(1, record.tags.size());
assertEquals(3, record.tags.size());
assertEquals(1, record.getMetricAsLongOrFail(RpcMeasureConstants.GRPC_CLIENT_STARTED_RPCS));
tracer.outboundHeaders();
tracer.outboundMessage(0);
Expand Down Expand Up @@ -831,7 +835,7 @@ public void clientStreamNeverCreatedStillRecordStats() {
StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
assertNotNull(record);
assertNoServerContent(record);
assertEquals(1, record.tags.size());
assertEquals(3, record.tags.size());
TagValue methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
assertEquals(method.getFullMethodName(), methodTag.asString());
assertEquals(1, record.getMetricAsLongOrFail(RpcMeasureConstants.GRPC_CLIENT_STARTED_RPCS));
Expand Down Expand Up @@ -934,7 +938,7 @@ private void subtestStatsHeadersPropagateTags(boolean propagate, boolean recordS
StatsTestUtils.MetricsRecord clientRecord = statsRecorder.pollRecord();
assertNotNull(clientRecord);
assertNoServerContent(clientRecord);
assertEquals(2, clientRecord.tags.size());
assertEquals(4, clientRecord.tags.size());
TagValue clientMethodTag = clientRecord.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
assertEquals(method.getFullMethodName(), clientMethodTag.asString());
TagValue clientPropagatedTag = clientRecord.tags.get(StatsTestUtils.EXTRA_TAG);
Expand All @@ -959,6 +963,8 @@ private void subtestStatsHeadersPropagateTags(boolean propagate, boolean recordS
.putLocal(
RpcMeasureConstants.GRPC_SERVER_METHOD,
TagValue.create(method.getFullMethodName()))
.putLocal(INSTRUMENTATION_SOURCE, LIBRARY_NAME)
.putLocal(INSTRUMENTATION_VERSION, LIBRARY_VERSION)
.build(),
io.opencensus.tags.unsafe.ContextUtils.getValue(serverContext));

Expand All @@ -970,7 +976,7 @@ private void subtestStatsHeadersPropagateTags(boolean propagate, boolean recordS
StatsTestUtils.MetricsRecord serverRecord = statsRecorder.pollRecord();
assertNotNull(serverRecord);
assertNoClientContent(serverRecord);
assertEquals(2, serverRecord.tags.size());
assertEquals(4, serverRecord.tags.size());
TagValue serverMethodTag = serverRecord.tags.get(RpcMeasureConstants.GRPC_SERVER_METHOD);
assertEquals(method.getFullMethodName(), serverMethodTag.asString());
TagValue serverPropagatedTag = serverRecord.tags.get(StatsTestUtils.EXTRA_TAG);
Expand Down Expand Up @@ -1187,7 +1193,7 @@ private void subtestServerBasicStatsNoHeaders(
StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
assertNotNull(record);
assertNoClientContent(record);
assertEquals(1, record.tags.size());
assertEquals(3, record.tags.size());
TagValue methodTag = record.tags.get(RpcMeasureConstants.GRPC_SERVER_METHOD);
assertEquals(method.getFullMethodName(), methodTag.asString());
assertEquals(1, record.getMetricAsLongOrFail(RpcMeasureConstants.GRPC_SERVER_STARTED_RPCS));
Expand All @@ -1203,6 +1209,8 @@ private void subtestServerBasicStatsNoHeaders(
.putLocal(
RpcMeasureConstants.GRPC_SERVER_METHOD,
TagValue.create(method.getFullMethodName()))
.putLocal(INSTRUMENTATION_SOURCE, LIBRARY_NAME)
.putLocal(INSTRUMENTATION_VERSION, LIBRARY_VERSION)
.build(),
statsCtx);

Expand Down Expand Up @@ -1556,7 +1564,8 @@ public void callLatencyView() throws InterruptedException {
assertDistributionData(
localStats,
ObservabilityCensusConstants.GRPC_CLIENT_API_LATENCY_VIEW,
ImmutableList.of(TagValue.create(method.getFullMethodName()), TagValue.create("OK")),
ImmutableList.of(TagValue.create(method.getFullMethodName()), TagValue.create("OK"),
LIBRARY_NAME, LIBRARY_VERSION),
50.0, 1, 0.0,
ImmutableList.of(
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,
Expand Down
Loading