Search in sources :

Example 36 with TagContext

use of io.opencensus.tags.TagContext in project grpc-java by grpc.

the class CensusModulesTest method subtestStatsHeadersPropagateTags.

private void subtestStatsHeadersPropagateTags(boolean propagate, boolean recordStats) {
    // EXTRA_TAG is propagated by the FakeStatsContextFactory. Note that not all tags are
    // propagated.  The StatsContextFactory decides which tags are to propagated.  gRPC facilitates
    // the propagation by putting them in the headers.
    TagContext clientCtx = tagger.emptyBuilder().putLocal(StatsTestUtils.EXTRA_TAG, TagValue.create("extra-tag-value-897")).build();
    CensusStatsModule census = new CensusStatsModule(tagger, tagCtxSerializer, statsRecorder, fakeClock.getStopwatchSupplier(), propagate, recordStats, recordStats, recordStats, recordStats);
    Metadata headers = new Metadata();
    CensusStatsModule.CallAttemptsTracerFactory callAttemptsTracerFactory = new CensusStatsModule.CallAttemptsTracerFactory(census, clientCtx, method.getFullMethodName());
    // This propagates clientCtx to headers if propagates==true
    ClientStreamTracer streamTracer = callAttemptsTracerFactory.newClientStreamTracer(STREAM_INFO, headers);
    streamTracer.streamCreated(Attributes.EMPTY, headers);
    if (recordStats) {
        // Client upstart record
        StatsTestUtils.MetricsRecord clientRecord = statsRecorder.pollRecord();
        assertNotNull(clientRecord);
        assertNoServerContent(clientRecord);
        assertEquals(2, 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);
        assertEquals("extra-tag-value-897", clientPropagatedTag.asString());
    }
    if (propagate) {
        assertTrue(headers.containsKey(census.statsHeader));
    } else {
        assertFalse(headers.containsKey(census.statsHeader));
        return;
    }
    ServerStreamTracer serverTracer = census.getServerTracerFactory().newServerStreamTracer(method.getFullMethodName(), headers);
    // Server tracer deserializes clientCtx from the headers, so that it records stats with the
    // propagated tags.
    Context serverContext = serverTracer.filterContext(Context.ROOT);
    // It also put clientCtx in the Context seen by the call handler
    assertEquals(tagger.toBuilder(clientCtx).putLocal(RpcMeasureConstants.GRPC_SERVER_METHOD, TagValue.create(method.getFullMethodName())).build(), io.opencensus.tags.unsafe.ContextUtils.getValue(serverContext));
    // Verifies that the server tracer records the status with the propagated tag
    serverTracer.streamClosed(Status.OK);
    if (recordStats) {
        // Server upstart record
        StatsTestUtils.MetricsRecord serverRecord = statsRecorder.pollRecord();
        assertNotNull(serverRecord);
        assertNoClientContent(serverRecord);
        assertEquals(2, 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);
        assertEquals("extra-tag-value-897", serverPropagatedTag.asString());
        // Server completion record
        serverRecord = statsRecorder.pollRecord();
        assertNotNull(serverRecord);
        assertNoClientContent(serverRecord);
        serverMethodTag = serverRecord.tags.get(RpcMeasureConstants.GRPC_SERVER_METHOD);
        assertEquals(method.getFullMethodName(), serverMethodTag.asString());
        TagValue serverStatusTag = serverRecord.tags.get(RpcMeasureConstants.GRPC_SERVER_STATUS);
        assertEquals(Status.Code.OK.toString(), serverStatusTag.asString());
        assertNull(serverRecord.getMetric(DeprecatedCensusConstants.RPC_SERVER_ERROR_COUNT));
        serverPropagatedTag = serverRecord.tags.get(StatsTestUtils.EXTRA_TAG);
        assertEquals("extra-tag-value-897", serverPropagatedTag.asString());
    }
    // Verifies that the client tracer factory uses clientCtx, which includes the custom tags, to
    // record stats.
    streamTracer.streamClosed(Status.OK);
    callAttemptsTracerFactory.callEnded(Status.OK);
    if (recordStats) {
        // Client completion record
        StatsTestUtils.MetricsRecord clientRecord = statsRecorder.pollRecord();
        assertNotNull(clientRecord);
        assertNoServerContent(clientRecord);
        TagValue clientMethodTag = clientRecord.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
        assertEquals(method.getFullMethodName(), clientMethodTag.asString());
        TagValue clientStatusTag = clientRecord.tags.get(RpcMeasureConstants.GRPC_CLIENT_STATUS);
        assertEquals(Status.Code.OK.toString(), clientStatusTag.asString());
        assertNull(clientRecord.getMetric(DeprecatedCensusConstants.RPC_CLIENT_ERROR_COUNT));
        TagValue clientPropagatedTag = clientRecord.tags.get(StatsTestUtils.EXTRA_TAG);
        assertEquals("extra-tag-value-897", clientPropagatedTag.asString());
        assertZeroRetryRecorded();
    }
    if (!recordStats) {
        assertNull(statsRecorder.pollRecord());
    }
}
Also used : SpanContext(io.opencensus.trace.SpanContext) Context(io.grpc.Context) TagContext(io.opencensus.tags.TagContext) ClientStreamTracer(io.grpc.ClientStreamTracer) StatsTestUtils(io.grpc.internal.testing.StatsTestUtils) ServerStreamTracer(io.grpc.ServerStreamTracer) TagContext(io.opencensus.tags.TagContext) Metadata(io.grpc.Metadata) TagValue(io.opencensus.tags.TagValue) CallAttemptsTracerFactory(io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory)

Example 37 with TagContext

use of io.opencensus.tags.TagContext in project grpc-java by grpc.

the class CensusModulesTest method subtestServerBasicStatsNoHeaders.

private void subtestServerBasicStatsNoHeaders(boolean recordStarts, boolean recordFinishes, boolean recordRealTime) {
    CensusStatsModule localCensusStats = new CensusStatsModule(tagger, tagCtxSerializer, statsRecorder, fakeClock.getStopwatchSupplier(), true, recordStarts, recordFinishes, recordRealTime, true);
    ServerStreamTracer.Factory tracerFactory = localCensusStats.getServerTracerFactory();
    ServerStreamTracer tracer = tracerFactory.newServerStreamTracer(method.getFullMethodName(), new Metadata());
    if (recordStarts) {
        StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
        assertNotNull(record);
        assertNoClientContent(record);
        assertEquals(1, record.tags.size());
        TagValue methodTag = record.tags.get(RpcMeasureConstants.GRPC_SERVER_METHOD);
        assertEquals(method.getFullMethodName(), methodTag.asString());
        assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_STARTED_COUNT));
    } else {
        assertNull(statsRecorder.pollRecord());
    }
    Context filteredContext = tracer.filterContext(Context.ROOT);
    TagContext statsCtx = io.opencensus.tags.unsafe.ContextUtils.getValue(filteredContext);
    assertEquals(tagger.emptyBuilder().putLocal(RpcMeasureConstants.GRPC_SERVER_METHOD, TagValue.create(method.getFullMethodName())).build(), statsCtx);
    tracer.inboundMessage(0);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_RECEIVED_MESSAGES_PER_METHOD, 1, recordRealTime, false);
    tracer.inboundWireSize(34);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_RECEIVED_BYTES_PER_METHOD, 34, recordRealTime, false);
    tracer.inboundUncompressedSize(67);
    fakeClock.forwardTime(100, MILLISECONDS);
    tracer.outboundMessage(0);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_SENT_MESSAGES_PER_METHOD, 1, recordRealTime, false);
    tracer.outboundWireSize(1028);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_SENT_BYTES_PER_METHOD, 1028, recordRealTime, false);
    tracer.outboundUncompressedSize(1128);
    fakeClock.forwardTime(16, MILLISECONDS);
    tracer.inboundMessage(1);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_RECEIVED_MESSAGES_PER_METHOD, 1, recordRealTime, false);
    tracer.inboundWireSize(154);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_RECEIVED_BYTES_PER_METHOD, 154, recordRealTime, false);
    tracer.inboundUncompressedSize(552);
    tracer.outboundMessage(1);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_SENT_MESSAGES_PER_METHOD, 1, recordRealTime, false);
    tracer.outboundWireSize(99);
    assertRealTimeMetric(RpcMeasureConstants.GRPC_SERVER_SENT_BYTES_PER_METHOD, 99, recordRealTime, false);
    tracer.outboundUncompressedSize(865);
    fakeClock.forwardTime(24, MILLISECONDS);
    tracer.streamClosed(Status.CANCELLED);
    if (recordFinishes) {
        StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
        assertNotNull(record);
        assertNoClientContent(record);
        TagValue methodTag = record.tags.get(RpcMeasureConstants.GRPC_SERVER_METHOD);
        assertEquals(method.getFullMethodName(), methodTag.asString());
        TagValue statusTag = record.tags.get(RpcMeasureConstants.GRPC_SERVER_STATUS);
        assertEquals(Status.Code.CANCELLED.toString(), statusTag.asString());
        assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_FINISHED_COUNT));
        assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_ERROR_COUNT));
        assertEquals(2, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_RESPONSE_COUNT));
        assertEquals(1028 + 99, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_RESPONSE_BYTES));
        assertEquals(1128 + 865, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES));
        assertEquals(2, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_REQUEST_COUNT));
        assertEquals(34 + 154, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_REQUEST_BYTES));
        assertEquals(67 + 552, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES));
        assertEquals(100 + 16 + 24, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_SERVER_SERVER_LATENCY));
    } else {
        assertNull(statsRecorder.pollRecord());
    }
}
Also used : SpanContext(io.opencensus.trace.SpanContext) Context(io.grpc.Context) TagContext(io.opencensus.tags.TagContext) StatsTestUtils(io.grpc.internal.testing.StatsTestUtils) ServerStreamTracer(io.grpc.ServerStreamTracer) TagContext(io.opencensus.tags.TagContext) Metadata(io.grpc.Metadata) TagValue(io.opencensus.tags.TagValue)

Example 38 with TagContext

use of io.opencensus.tags.TagContext in project instrumentation-java by census-instrumentation.

the class JaxrsClientFilterTest method testResponseFilter.

@Test
public void testResponseFilter() throws Exception {
    Span span = new FakeSpan(SpanContext.INVALID, null);
    TagContext tagContext = mock(TagContext.class);
    HttpRequestContext context = createHttpRequestContext(span, tagContext);
    ClientRequestContext requestContext = mock(ClientRequestContext.class);
    when(requestContext.getProperty("opencensus.context")).thenReturn(context);
    ClientResponseContext responseContext = mock(ClientResponseContext.class);
    filter.filter(requestContext, responseContext);
    verify(requestContext).getProperty("opencensus.context");
    verify(responseContext, times(1)).getStatus();
}
Also used : ClientRequestContext(javax.ws.rs.client.ClientRequestContext) TagContext(io.opencensus.tags.TagContext) HttpRequestContext(io.opencensus.contrib.http.HttpRequestContext) Span(io.opencensus.trace.Span) ClientResponseContext(javax.ws.rs.client.ClientResponseContext) Test(org.junit.Test)

Example 39 with TagContext

use of io.opencensus.tags.TagContext in project instrumentation-java by census-instrumentation.

the class JaxrsContainerFilterTest method testResponseFilter.

@Test
public void testResponseFilter() throws Exception {
    Span span = new FakeSpan(SpanContext.INVALID, null);
    TagContext tagContext = mock(TagContext.class);
    HttpRequestContext context = JaxrsClientFilterTest.createHttpRequestContext(span, tagContext);
    UriInfo uriInfo = mock(UriInfo.class);
    when(uriInfo.getMatchedURIs()).thenReturn(Collections.singletonList("/resource/{route}"));
    ContainerRequestContext requestContext = mock(ContainerRequestContext.class);
    when(requestContext.getProperty("opencensus.context")).thenReturn(context);
    when(requestContext.getUriInfo()).thenReturn(uriInfo);
    ContainerResponseContext responseContext = mock(ContainerResponseContext.class);
    filter.filter(requestContext, responseContext);
    verify(requestContext).getProperty("opencensus.context");
    verify(responseContext, times(1)).getStatus();
}
Also used : FakeSpan(io.opencensus.contrib.http.jaxrs.JaxrsClientFilterTest.FakeSpan) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) TagContext(io.opencensus.tags.TagContext) ContainerResponseContext(javax.ws.rs.container.ContainerResponseContext) HttpRequestContext(io.opencensus.contrib.http.HttpRequestContext) FakeSpan(io.opencensus.contrib.http.jaxrs.JaxrsClientFilterTest.FakeSpan) Span(io.opencensus.trace.Span) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 40 with TagContext

use of io.opencensus.tags.TagContext in project instrumentation-java by census-instrumentation.

the class HttpClientHandler method recordStats.

private void recordStats(HttpRequestContext context, @Nullable Q request, int httpCode) {
    double requestLatency = NANOSECONDS.toMillis(System.nanoTime() - context.requestStartTime);
    String methodStr = request == null ? "" : extractor.getMethod(request);
    String host = request == null ? "null_request" : extractor.getHost(request);
    TagContext startCtx = tagger.toBuilder(context.tagContext).put(HTTP_CLIENT_HOST, TagValue.create(host == null ? "null_host" : host), METADATA_NO_PROPAGATION).put(HTTP_CLIENT_METHOD, TagValue.create(methodStr == null ? "" : methodStr), METADATA_NO_PROPAGATION).put(HTTP_CLIENT_STATUS, TagValue.create(httpCode == 0 ? "error" : Integer.toString(httpCode)), METADATA_NO_PROPAGATION).build();
    statsRecorder.newMeasureMap().put(HTTP_CLIENT_ROUNDTRIP_LATENCY, requestLatency).put(HTTP_CLIENT_SENT_BYTES, context.sentMessageSize.get()).put(HTTP_CLIENT_RECEIVED_BYTES, context.receiveMessageSize.get()).record(startCtx);
}
Also used : TagContext(io.opencensus.tags.TagContext)

Aggregations

TagContext (io.opencensus.tags.TagContext)76 Test (org.junit.Test)56 Tag (io.opencensus.tags.Tag)10 TagContextBuilder (io.opencensus.tags.TagContextBuilder)9 ByteArrayDataOutput (com.google.common.io.ByteArrayDataOutput)7 Context (io.grpc.Context)6 Scope (io.opencensus.common.Scope)5 StatsTestUtil.createEmptyViewData (io.opencensus.implcore.stats.StatsTestUtil.createEmptyViewData)5 View (io.opencensus.stats.View)5 ViewData (io.opencensus.stats.ViewData)5 Metadata (io.grpc.Metadata)3 StatsTestUtils (io.grpc.internal.testing.StatsTestUtils)3 MeasureMap (io.opencensus.stats.MeasureMap)3 TagValue (io.opencensus.tags.TagValue)3 SpanContext (io.opencensus.trace.SpanContext)3 HashMap (java.util.HashMap)3 ServerStreamTracer (io.grpc.ServerStreamTracer)2 CallAttemptsTracerFactory (io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory)2 HttpRequestContext (io.opencensus.contrib.http.HttpRequestContext)2 SimpleTagContext (io.opencensus.implcore.stats.StatsTestUtil.SimpleTagContext)2