From 8d0034a429c5bf7c719e8a9985f2dfffa00e02b2 Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Sat, 20 Jul 2019 23:53:26 -0400 Subject: [PATCH] Add a test for the opencensus view hack --- core/build.gradle | 3 +- .../io/grpc/internal/CensusModulesTest.java | 101 ++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) diff --git a/core/build.gradle b/core/build.gradle index b02e42a01ea..1707b3f19dd 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -26,7 +26,8 @@ dependencies { project(':grpc-api').sourceSets.test.output, project(':grpc-testing'), project(':grpc-grpclb'), - libraries.guava_testlib + libraries.guava_testlib, + libraries.opencensus_impl signature "org.codehaus.mojo.signature:java17:1.0@signature" signature "net.sf.androidscents.signature:android-api-level-14:4.0_r4@signature" diff --git a/core/src/test/java/io/grpc/internal/CensusModulesTest.java b/core/src/test/java/io/grpc/internal/CensusModulesTest.java index db210d49e44..75678b90a22 100644 --- a/core/src/test/java/io/grpc/internal/CensusModulesTest.java +++ b/core/src/test/java/io/grpc/internal/CensusModulesTest.java @@ -17,6 +17,7 @@ package io.grpc.internal; import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; import static java.util.concurrent.TimeUnit.MILLISECONDS; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -40,6 +41,7 @@ import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.when; +import com.google.common.collect.ImmutableList; import io.grpc.Attributes; import io.grpc.CallOptions; import io.grpc.Channel; @@ -61,8 +63,19 @@ import io.grpc.internal.testing.StatsTestUtils.FakeTagger; import io.grpc.internal.testing.StatsTestUtils.MockableSpan; import io.grpc.testing.GrpcServerRule; +import io.opencensus.common.Function; +import io.opencensus.common.Functions; import io.opencensus.contrib.grpc.metrics.RpcMeasureConstants; +import io.opencensus.contrib.grpc.metrics.RpcViewConstants; +import io.opencensus.impl.stats.StatsComponentImpl; +import io.opencensus.stats.AggregationData; +import io.opencensus.stats.AggregationData.CountData; +import io.opencensus.stats.AggregationData.LastValueDataDouble; +import io.opencensus.stats.AggregationData.LastValueDataLong; +import io.opencensus.stats.AggregationData.SumDataDouble; import io.opencensus.stats.Measure; +import io.opencensus.stats.StatsComponent; +import io.opencensus.stats.View; import io.opencensus.tags.TagContext; import io.opencensus.tags.TagValue; import io.opencensus.trace.BlankSpan; @@ -1153,4 +1166,92 @@ private static void assertNoClientContent(StatsTestUtils.MetricsRecord record) { assertNull(record.getMetric(DeprecatedCensusConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES)); assertNull(record.getMetric(DeprecatedCensusConstants.RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES)); } + + @SuppressWarnings("deprecation") + @Test + public void newTagsPopulateOldViews() throws InterruptedException { + StatsComponent localStats = new StatsComponentImpl(); + + // Test views that contain both of the remap tags: method & status. + localStats.getViewManager().registerView(RpcViewConstants.RPC_CLIENT_ERROR_COUNT_VIEW); + localStats.getViewManager().registerView(RpcViewConstants.GRPC_CLIENT_COMPLETED_RPC_VIEW); + + CensusStatsModule localCensusStats = new CensusStatsModule( + tagger, tagCtxSerializer, localStats.getStatsRecorder(), fakeClock.getStopwatchSupplier(), + false, false, true, false /* real-time */); + + CensusStatsModule.ClientCallTracer callTracer = + localCensusStats.newClientCallTracer( + tagger.empty(), method.getFullMethodName()); + + callTracer.newClientStreamTracer(STREAM_INFO, new Metadata()); + fakeClock.forwardTime(30, MILLISECONDS); + callTracer.callEnded(Status.PERMISSION_DENIED.withDescription("No you don't")); + + // Give OpenCensus a chance to update the views asynchronously. + Thread.sleep(100); + + assertWithMessage("Legacy error count view had unexpected count") + .that( + getAggregationValueAsLong( + localStats, + RpcViewConstants.RPC_CLIENT_ERROR_COUNT_VIEW, + ImmutableList.of( + TagValue.create("PERMISSION_DENIED"), + TagValue.create(method.getFullMethodName())))) + .isEqualTo(1); + + assertWithMessage("New error count view had unexpected count") + .that( + getAggregationValueAsLong( + localStats, + RpcViewConstants.GRPC_CLIENT_COMPLETED_RPC_VIEW, + ImmutableList.of( + TagValue.create(method.getFullMethodName()), + TagValue.create("PERMISSION_DENIED")))) + .isEqualTo(1); + } + + @SuppressWarnings("deprecation") + private long getAggregationValueAsLong(StatsComponent localStats, View view, + List dimension) { + AggregationData aggregationData = localStats.getViewManager() + .getView(view.getName()) + .getAggregationMap() + .get(dimension); + + return aggregationData.match( + new Function() { + @Override + public Long apply(SumDataDouble arg) { + return (long) arg.getSum(); + } + }, + Functions.throwAssertionError(), + new Function() { + @Override + public Long apply(CountData arg) { + return arg.getCount(); + } + }, + Functions.throwAssertionError(), + new Function() { + @Override + public Long apply(LastValueDataDouble arg) { + return (long) arg.getLastValue(); + } + }, + new Function() { + @Override + public Long apply(LastValueDataLong arg) { + return arg.getLastValue(); + } + }, + new Function() { + @Override + public Long apply(AggregationData arg) { + return ((io.opencensus.stats.AggregationData.MeanData) arg).getCount(); + } + }); + } }