Skip to content

Commit

Permalink
Update OpenCensus to 0.8.0 release.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebright committed Nov 1, 2017
1 parent 1c52536 commit 404abba
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 53 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ subprojects {
google_auth_credentials: 'com.google.auth:google-auth-library-credentials:0.4.0',
okhttp: 'com.squareup.okhttp:okhttp:2.5.0',
okio: 'com.squareup.okio:okio:1.6.0',
opencensus_api: 'io.opencensus:opencensus-api:0.8.0-SNAPSHOT',
opencensus_contrib_grpc_metrics: 'io.opencensus:opencensus-contrib-grpc-metrics:0.8.0-SNAPSHOT',
opencensus_impl: 'io.opencensus:opencensus-impl:0.8.0-SNAPSHOT',
opencensus_api: 'io.opencensus:opencensus-api:0.8.0',
opencensus_contrib_grpc_metrics: 'io.opencensus:opencensus-contrib-grpc-metrics:0.8.0',
opencensus_impl: 'io.opencensus:opencensus-impl:0.8.0',
instrumentation_api: 'com.google.instrumentation:instrumentation-api:0.4.3',
protobuf: "com.google.protobuf:protobuf-java:${protobufVersion}",
protobuf_lite: "com.google.protobuf:protobuf-lite:3.0.1",
Expand Down
25 changes: 15 additions & 10 deletions core/src/main/java/io/grpc/internal/CensusStatsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@
import io.grpc.Status;
import io.grpc.StreamTracer;
import io.opencensus.contrib.grpc.metrics.RpcMeasureConstants;
import io.opencensus.stats.StatsRecord;
import io.opencensus.stats.MeasureMap;
import io.opencensus.stats.StatsRecorder;
import io.opencensus.tags.TagContext;
import io.opencensus.tags.TagValue;
import io.opencensus.tags.Tagger;
import io.opencensus.tags.propagation.TagContextBinarySerializer;
import io.opencensus.tags.propagation.TagContextSerializationException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
Expand Down Expand Up @@ -94,7 +95,11 @@ final class CensusStatsModule {
public byte[] toBytes(TagContext context) {
// TODO(carl-mastrangelo): currently we only make sure the correctness. We may need to
// optimize out the allocation and copy in the future.
return tagCtxSerializer.toByteArray(context);
try {
return tagCtxSerializer.toByteArray(context);
} catch (TagContextSerializationException e) {
throw new RuntimeException(e);
}
}

@Override
Expand Down Expand Up @@ -245,7 +250,7 @@ void callEnded(Status status) {
if (tracer == null) {
tracer = BLANK_CLIENT_TRACER;
}
StatsRecord statsRecord = module.statsRecorder.newRecord()
MeasureMap measureMap = module.statsRecorder.newMeasureMap()
// The metrics are in double
.put(RpcMeasureConstants.RPC_CLIENT_ROUNDTRIP_LATENCY, roundtripNanos / NANOS_PER_MILLI)
.put(RpcMeasureConstants.RPC_CLIENT_REQUEST_COUNT, tracer.outboundMessageCount)
Expand All @@ -259,13 +264,13 @@ void callEnded(Status status) {
RpcMeasureConstants.RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES,
tracer.inboundUncompressedSize);
if (!status.isOk()) {
statsRecord.put(RpcMeasureConstants.RPC_CLIENT_ERROR_COUNT, 1);
measureMap.put(RpcMeasureConstants.RPC_CLIENT_ERROR_COUNT, 1);
}
statsRecord.record(
measureMap.record(
module
.tagger
.toBuilder(parentCtx)
.put(RpcMeasureConstants.RPC_CLIENT_METHOD, TagValue.create(fullMethodName))
.put(RpcMeasureConstants.RPC_METHOD, TagValue.create(fullMethodName))
.put(RpcMeasureConstants.RPC_STATUS, TagValue.create(status.getCode().toString()))
.build());
}
Expand Down Expand Up @@ -360,7 +365,7 @@ public void streamClosed(Status status) {
}
stopwatch.stop();
long elapsedTimeNanos = stopwatch.elapsed(TimeUnit.NANOSECONDS);
StatsRecord statsRecord = module.statsRecorder.newRecord()
MeasureMap measureMap = module.statsRecorder.newMeasureMap()
// The metrics are in double
.put(RpcMeasureConstants.RPC_SERVER_SERVER_LATENCY, elapsedTimeNanos / NANOS_PER_MILLI)
.put(RpcMeasureConstants.RPC_SERVER_RESPONSE_COUNT, outboundMessageCount)
Expand All @@ -370,10 +375,10 @@ public void streamClosed(Status status) {
.put(RpcMeasureConstants.RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES, outboundUncompressedSize)
.put(RpcMeasureConstants.RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES, inboundUncompressedSize);
if (!status.isOk()) {
statsRecord.put(RpcMeasureConstants.RPC_SERVER_ERROR_COUNT, 1);
measureMap.put(RpcMeasureConstants.RPC_SERVER_ERROR_COUNT, 1);
}
TagContext ctx = firstNonNull(parentCtx, tagger.empty());
statsRecord.record(
measureMap.record(
module
.tagger
.toBuilder(ctx)
Expand Down Expand Up @@ -401,7 +406,7 @@ public ServerStreamTracer newServerStreamTracer(String fullMethodName, Metadata
parentCtx =
tagger
.toBuilder(parentCtx)
.put(RpcMeasureConstants.RPC_SERVER_METHOD, TagValue.create(fullMethodName))
.put(RpcMeasureConstants.RPC_METHOD, TagValue.create(fullMethodName))
.build();
return new ServerTracer(
CensusStatsModule.this, fullMethodName, parentCtx, stopwatchSupplier, tagger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@
import io.grpc.MethodDescriptor;
import io.grpc.NameResolver;
import io.opencensus.common.Scope;
import io.opencensus.stats.StatsRecord;
import io.opencensus.stats.MeasureMap;
import io.opencensus.stats.StatsRecorder;
import io.opencensus.tags.TagContext;
import io.opencensus.tags.TagContextBuilder;
import io.opencensus.tags.Tagger;
import io.opencensus.tags.propagation.TagContextBinarySerializer;
import io.opencensus.tags.propagation.TagContextParseException;
import io.opencensus.tags.propagation.TagContextDeserializationException;
import io.opencensus.tags.propagation.TagContextSerializationException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.URI;
Expand Down Expand Up @@ -102,20 +103,20 @@ public Scope withTagContext(TagContext tags) {
private static final TagContextBinarySerializer DUMMY_TAG_CONTEXT_BINARY_SERIALIZER =
new TagContextBinarySerializer() {
@Override
public byte[] toByteArray(TagContext tags) {
public byte[] toByteArray(TagContext tags) throws TagContextSerializationException {
throw new UnsupportedOperationException();
}

@Override
public TagContext fromByteArray(byte[] bytes) throws TagContextParseException {
public TagContext fromByteArray(byte[] bytes) throws TagContextDeserializationException {
throw new UnsupportedOperationException();
}
};

private static final StatsRecorder DUMMY_STATS_RECORDER =
new StatsRecorder() {
@Override
public StatsRecord newRecord() {
public MeasureMap newMeasureMap() {
throw new UnsupportedOperationException();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
import io.grpc.Metadata;
import io.grpc.ServerStreamTracer;
import io.opencensus.common.Scope;
import io.opencensus.stats.StatsRecord;
import io.opencensus.stats.MeasureMap;
import io.opencensus.stats.StatsRecorder;
import io.opencensus.tags.TagContext;
import io.opencensus.tags.TagContextBuilder;
import io.opencensus.tags.Tagger;
import io.opencensus.tags.propagation.TagContextBinarySerializer;
import io.opencensus.tags.propagation.TagContextParseException;
import io.opencensus.tags.propagation.TagContextDeserializationException;
import io.opencensus.tags.propagation.TagContextSerializationException;
import java.io.File;
import java.util.List;
import org.junit.Test;
Expand Down Expand Up @@ -75,20 +76,20 @@ public Scope withTagContext(TagContext tags) {
private static final TagContextBinarySerializer DUMMY_TAG_CONTEXT_BINARY_SERIALIZER =
new TagContextBinarySerializer() {
@Override
public byte[] toByteArray(TagContext tags) {
public byte[] toByteArray(TagContext tags) throws TagContextSerializationException {
throw new UnsupportedOperationException();
}

@Override
public TagContext fromByteArray(byte[] bytes) throws TagContextParseException {
public TagContext fromByteArray(byte[] bytes) throws TagContextDeserializationException {
throw new UnsupportedOperationException();
}
};

private static final StatsRecorder DUMMY_STATS_RECORDER =
new StatsRecorder() {
@Override
public StatsRecord newRecord() {
public MeasureMap newMeasureMap() {
throw new UnsupportedOperationException();
}
};
Expand Down
16 changes: 8 additions & 8 deletions core/src/test/java/io/grpc/internal/CensusModulesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
// The intercepting listener calls callEnded() on ClientCallTracer, which records to Census.
StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
assertNotNull(record);
TagValue methodTag = record.tags.get(RpcMeasureConstants.RPC_CLIENT_METHOD);
TagValue methodTag = record.tags.get(RpcMeasureConstants.RPC_METHOD);
assertEquals(method.getFullMethodName(), methodTag.asString());
TagValue statusTag = record.tags.get(RpcMeasureConstants.RPC_STATUS);
assertEquals(Status.Code.PERMISSION_DENIED.toString(), statusTag.asString());
Expand Down Expand Up @@ -348,7 +348,7 @@ public void clientBasicStatsDefaultContext() {
StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
assertNotNull(record);
assertNoServerContent(record);
TagValue methodTag = record.tags.get(RpcMeasureConstants.RPC_CLIENT_METHOD);
TagValue methodTag = record.tags.get(RpcMeasureConstants.RPC_METHOD);
assertEquals(method.getFullMethodName(), methodTag.asString());
TagValue statusTag = record.tags.get(RpcMeasureConstants.RPC_STATUS);
assertEquals(Status.Code.OK.toString(), statusTag.asString());
Expand Down Expand Up @@ -420,7 +420,7 @@ public void clientStreamNeverCreatedStillRecordStats() {
StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
assertNotNull(record);
assertNoServerContent(record);
TagValue methodTag = record.tags.get(RpcMeasureConstants.RPC_CLIENT_METHOD);
TagValue methodTag = record.tags.get(RpcMeasureConstants.RPC_METHOD);
assertEquals(method.getFullMethodName(), methodTag.asString());
TagValue statusTag = record.tags.get(RpcMeasureConstants.RPC_STATUS);
assertEquals(Status.Code.DEADLINE_EXCEEDED.toString(), statusTag.asString());
Expand Down Expand Up @@ -511,7 +511,7 @@ private void subtestStatsHeadersPropagateTags(boolean propagate, boolean recordS
// It also put clientCtx in the Context seen by the call handler
assertEquals(
tagger.toBuilder(clientCtx).put(
RpcMeasureConstants.RPC_SERVER_METHOD,
RpcMeasureConstants.RPC_METHOD,
TagValue.create(method.getFullMethodName())).build(),
TAG_CONTEXT_KEY.get(serverContext));

Expand All @@ -522,7 +522,7 @@ private void subtestStatsHeadersPropagateTags(boolean propagate, boolean recordS
StatsTestUtils.MetricsRecord serverRecord = statsRecorder.pollRecord();
assertNotNull(serverRecord);
assertNoClientContent(serverRecord);
TagValue serverMethodTag = serverRecord.tags.get(RpcMeasureConstants.RPC_SERVER_METHOD);
TagValue serverMethodTag = serverRecord.tags.get(RpcMeasureConstants.RPC_METHOD);
assertEquals(method.getFullMethodName(), serverMethodTag.asString());
TagValue serverStatusTag = serverRecord.tags.get(RpcMeasureConstants.RPC_STATUS);
assertEquals(Status.Code.OK.toString(), serverStatusTag.asString());
Expand All @@ -539,7 +539,7 @@ private void subtestStatsHeadersPropagateTags(boolean propagate, boolean recordS
StatsTestUtils.MetricsRecord clientRecord = statsRecorder.pollRecord();
assertNotNull(clientRecord);
assertNoServerContent(clientRecord);
TagValue clientMethodTag = clientRecord.tags.get(RpcMeasureConstants.RPC_CLIENT_METHOD);
TagValue clientMethodTag = clientRecord.tags.get(RpcMeasureConstants.RPC_METHOD);
assertEquals(method.getFullMethodName(), clientMethodTag.asString());
TagValue clientStatusTag = clientRecord.tags.get(RpcMeasureConstants.RPC_STATUS);
assertEquals(Status.Code.OK.toString(), clientStatusTag.asString());
Expand Down Expand Up @@ -647,7 +647,7 @@ public void serverBasicStatsNoHeaders() {
tagger
.emptyBuilder()
.put(
RpcMeasureConstants.RPC_SERVER_METHOD,
RpcMeasureConstants.RPC_METHOD,
TagValue.create(method.getFullMethodName()))
.build(),
statsCtx);
Expand Down Expand Up @@ -676,7 +676,7 @@ public void serverBasicStatsNoHeaders() {
StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
assertNotNull(record);
assertNoClientContent(record);
TagValue methodTag = record.tags.get(RpcMeasureConstants.RPC_SERVER_METHOD);
TagValue methodTag = record.tags.get(RpcMeasureConstants.RPC_METHOD);
assertEquals(method.getFullMethodName(), methodTag.asString());
TagValue statusTag = record.tags.get(RpcMeasureConstants.RPC_STATUS);
assertEquals(Status.Code.CANCELLED.toString(), statusTag.asString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -722,8 +722,7 @@ public void cancelAfterBegin() throws Exception {
// in this test. Therefore we don't check the tracer stats.
MetricsRecord clientRecord = clientStatsRecorder.pollRecord(5, TimeUnit.SECONDS);
checkTags(
clientRecord, false, "grpc.testing.TestService/StreamingInputCall",
Status.CANCELLED.getCode());
clientRecord, "grpc.testing.TestService/StreamingInputCall", Status.CANCELLED.getCode());
// Do not check server-side metrics, because the status on the server side is undetermined.
}
}
Expand Down Expand Up @@ -1045,7 +1044,8 @@ public void deadlineExceeded() throws Exception {
// stats.
MetricsRecord clientRecord = clientStatsRecorder.pollRecord(5, TimeUnit.SECONDS);
checkTags(
clientRecord, false, "grpc.testing.TestService/StreamingOutputCall",
clientRecord,
"grpc.testing.TestService/StreamingOutputCall",
Status.Code.DEADLINE_EXCEEDED);
// Do not check server-side metrics, because the status on the server side is undetermined.
}
Expand Down Expand Up @@ -1078,7 +1078,8 @@ public void deadlineExceededServerStreaming() throws Exception {
// stats.
MetricsRecord clientRecord = clientStatsRecorder.pollRecord(5, TimeUnit.SECONDS);
checkTags(
clientRecord, false, "grpc.testing.TestService/StreamingOutputCall",
clientRecord,
"grpc.testing.TestService/StreamingOutputCall",
Status.Code.DEADLINE_EXCEEDED);
// Do not check server-side metrics, because the status on the server side is undetermined.
}
Expand All @@ -1102,8 +1103,7 @@ public void deadlineInPast() throws Exception {
if (metricsExpected()) {
MetricsRecord clientRecord = clientStatsRecorder.pollRecord(5, TimeUnit.SECONDS);
checkTags(
clientRecord, false, "grpc.testing.TestService/EmptyCall",
Status.DEADLINE_EXCEEDED.getCode());
clientRecord, "grpc.testing.TestService/EmptyCall", Status.DEADLINE_EXCEEDED.getCode());
}

// warm up the channel
Expand All @@ -1120,8 +1120,7 @@ public void deadlineInPast() throws Exception {
if (metricsExpected()) {
MetricsRecord clientRecord = clientStatsRecorder.pollRecord(5, TimeUnit.SECONDS);
checkTags(
clientRecord, false, "grpc.testing.TestService/EmptyCall",
Status.DEADLINE_EXCEEDED.getCode());
clientRecord, "grpc.testing.TestService/EmptyCall", Status.DEADLINE_EXCEEDED.getCode());
}
}

Expand Down Expand Up @@ -1496,7 +1495,8 @@ public void timeoutOnSleepingServer() throws Exception {
// in this test, thus we will not check that.
MetricsRecord clientRecord = clientStatsRecorder.pollRecord(5, TimeUnit.SECONDS);
checkTags(
clientRecord, false, "grpc.testing.TestService/FullDuplexCall",
clientRecord,
"grpc.testing.TestService/FullDuplexCall",
Status.DEADLINE_EXCEEDED.getCode());
}
}
Expand Down Expand Up @@ -1762,7 +1762,7 @@ private void assertClientStatsTrace(String method, Status.Code code,
// CensusStreamTracerModule records final status in interceptor, which is guaranteed to be
// done before application receives status.
MetricsRecord clientRecord = clientStatsRecorder.pollRecord();
checkTags(clientRecord, false, method, code);
checkTags(clientRecord, method, code);

if (requests != null && responses != null) {
checkCensus(clientRecord, false, requests, responses);
Expand Down Expand Up @@ -1801,7 +1801,7 @@ private void assertServerStatsTrace(String method, Status.Code code,
break;
}
try {
checkTags(serverRecord, true, method, code);
checkTags(serverRecord, method, code);
if (requests != null && responses != null) {
checkCensus(serverRecord, true, requests, responses);
}
Expand Down Expand Up @@ -1858,10 +1858,9 @@ private void assertServerStatsTrace(String method, Status.Code code,
}

private static void checkTags(
MetricsRecord record, boolean server, String methodName, Status.Code status) {
MetricsRecord record, String methodName, Status.Code status) {
assertNotNull("record is not null", record);
TagValue methodNameTag = record.tags.get(
server ? RpcMeasureConstants.RPC_SERVER_METHOD : RpcMeasureConstants.RPC_CLIENT_METHOD);
TagValue methodNameTag = record.tags.get(RpcMeasureConstants.RPC_METHOD);
assertNotNull("method name tagged", methodNameTag);
assertEquals("method names match", methodName, methodNameTag.asString());
TagValue statusTag = record.tags.get(RpcMeasureConstants.RPC_STATUS);
Expand Down
Loading

0 comments on commit 404abba

Please sign in to comment.