Search in sources :

Example 6 with TagValue

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

the class CensusModulesTest method clientStreamNeverCreatedStillRecordStats.

@Test
public void clientStreamNeverCreatedStillRecordStats() {
    CensusStatsModule.CallAttemptsTracerFactory callAttemptsTracerFactory = new CensusStatsModule.CallAttemptsTracerFactory(censusStats, tagger.empty(), method.getFullMethodName());
    ClientStreamTracer streamTracer = callAttemptsTracerFactory.newClientStreamTracer(STREAM_INFO, new Metadata());
    fakeClock.forwardTime(3000, MILLISECONDS);
    Status status = Status.DEADLINE_EXCEEDED.withDescription("3 seconds");
    streamTracer.streamClosed(status);
    callAttemptsTracerFactory.callEnded(status);
    // Upstart record
    StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
    assertNotNull(record);
    assertNoServerContent(record);
    assertEquals(1, record.tags.size());
    TagValue methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
    assertEquals(method.getFullMethodName(), methodTag.asString());
    assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_STARTED_COUNT));
    // Completion record
    record = statsRecorder.pollRecord();
    assertNotNull(record);
    assertNoServerContent(record);
    methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
    assertEquals(method.getFullMethodName(), methodTag.asString());
    TagValue statusTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_STATUS);
    assertEquals(Status.Code.DEADLINE_EXCEEDED.toString(), statusTag.asString());
    assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_FINISHED_COUNT));
    assertEquals(1, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_ERROR_COUNT));
    assertEquals(0, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_REQUEST_COUNT));
    assertEquals(0, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_REQUEST_BYTES));
    assertEquals(0, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES));
    assertEquals(0, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_RESPONSE_COUNT));
    assertEquals(0, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_RESPONSE_BYTES));
    assertEquals(0, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES));
    assertEquals(3000, record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_ROUNDTRIP_LATENCY));
    assertNull(record.getMetric(DeprecatedCensusConstants.RPC_CLIENT_SERVER_ELAPSED_TIME));
    assertZeroRetryRecorded();
}
Also used : Status(io.grpc.Status) ClientStreamTracer(io.grpc.ClientStreamTracer) StatsTestUtils(io.grpc.internal.testing.StatsTestUtils) Metadata(io.grpc.Metadata) TagValue(io.opencensus.tags.TagValue) CallAttemptsTracerFactory(io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory) Test(org.junit.Test)

Example 7 with TagValue

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

the class AbstractInteropTest method censusContextsPropagated.

@Test(timeout = 10000)
public void censusContextsPropagated() {
    Assume.assumeTrue("Skip the test because server is not in the same process.", server != null);
    Assume.assumeTrue(customCensusModulePresent());
    Span clientParentSpan = Tracing.getTracer().spanBuilder("Test.interopTest").startSpan();
    // A valid ID is guaranteed to be unique, so we can verify it is actually propagated.
    assertTrue(clientParentSpan.getContext().getTraceId().isValid());
    Context ctx = io.opencensus.tags.unsafe.ContextUtils.withValue(Context.ROOT, tagger.emptyBuilder().putLocal(StatsTestUtils.EXTRA_TAG, TagValue.create("extra value")).build());
    ctx = ContextUtils.withValue(ctx, clientParentSpan);
    Context origCtx = ctx.attach();
    try {
        blockingStub.unaryCall(SimpleRequest.getDefaultInstance());
        Context serverCtx = contextCapture.get();
        assertNotNull(serverCtx);
        FakeTagContext statsCtx = (FakeTagContext) io.opencensus.tags.unsafe.ContextUtils.getValue(serverCtx);
        assertNotNull(statsCtx);
        Map<TagKey, TagValue> tags = statsCtx.getTags();
        boolean tagFound = false;
        for (Map.Entry<TagKey, TagValue> tag : tags.entrySet()) {
            if (tag.getKey().equals(StatsTestUtils.EXTRA_TAG)) {
                assertEquals(TagValue.create("extra value"), tag.getValue());
                tagFound = true;
            }
        }
        assertTrue("tag not found", tagFound);
        Span span = ContextUtils.getValue(serverCtx);
        assertNotNull(span);
        SpanContext spanContext = span.getContext();
        assertEquals(clientParentSpan.getContext().getTraceId(), spanContext.getTraceId());
    } finally {
        ctx.detach(origCtx);
    }
}
Also used : SpanContext(io.opencensus.trace.SpanContext) Context(io.grpc.Context) FakeTagContext(io.grpc.internal.testing.StatsTestUtils.FakeTagContext) SpanContext(io.opencensus.trace.SpanContext) TagKey(io.opencensus.tags.TagKey) TagValue(io.opencensus.tags.TagValue) Span(io.opencensus.trace.Span) Map(java.util.Map) FakeTagContext(io.grpc.internal.testing.StatsTestUtils.FakeTagContext) Test(org.junit.Test)

Example 8 with TagValue

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

the class RetryTest method transparentRetryStatsRecorded.

@Test
public void transparentRetryStatsRecorded() throws Exception {
    startNewServer();
    createNewChannel();
    final AtomicBoolean originalAttemptFailed = new AtomicBoolean();
    class TransparentRetryTriggeringTracer extends ClientStreamTracer {

        @Override
        public void streamCreated(Attributes transportAttrs, Metadata metadata) {
            if (originalAttemptFailed.get()) {
                return;
            }
            // Send GOAWAY from server. The client may either receive GOAWAY or create the underlying
            // netty stream and write headers first, even we await server termination as below.
            // In the latter case, we rerun the test. We can also call localServer.shutdown() to trigger
            // GOAWAY, but it takes a lot longer time to gracefully shut down.
            localServer.shutdownNow();
            try {
                localServer.awaitTermination();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new AssertionError(e);
            }
        }

        @Override
        public void streamClosed(Status status) {
            if (originalAttemptFailed.get()) {
                return;
            }
            originalAttemptFailed.set(true);
            try {
                startNewServer();
                channel.resetConnectBackoff();
            } catch (Exception e) {
                throw new AssertionError("local server can not be restarted", e);
            }
        }
    }
    class TransparentRetryTracerFactory extends ClientStreamTracer.Factory {

        @Override
        public ClientStreamTracer newClientStreamTracer(StreamInfo info, Metadata headers) {
            return new TransparentRetryTriggeringTracer();
        }
    }
    CallOptions callOptions = CallOptions.DEFAULT.withWaitForReady().withStreamTracerFactory(new TransparentRetryTracerFactory());
    while (true) {
        ClientCall<String, Integer> call = channel.newCall(clientStreamingMethod, callOptions);
        call.start(mockCallListener, new Metadata());
        // original attempt
        assertRpcStartedRecorded();
        MetricsRecord record = clientStatsRecorder.pollRecord(5, SECONDS);
        assertThat(record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_FINISHED_COUNT)).isEqualTo(1);
        TagValue statusTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_STATUS);
        if (statusTag.asString().equals(Code.UNAVAILABLE.toString())) {
            break;
        } else {
            // Due to race condition, GOAWAY is not received/processed before the stream is closed due
            // to connection error. Rerun the test.
            assertThat(statusTag.asString()).isEqualTo(Code.UNKNOWN.toString());
            assertRetryStatsRecorded(0, 0, 0);
            originalAttemptFailed.set(false);
        }
    }
    // retry attempt
    assertRpcStartedRecorded();
    ServerCall<String, Integer> serverCall = serverCalls.poll(5, SECONDS);
    serverCall.close(Status.INVALID_ARGUMENT, new Metadata());
    assertRpcStatusRecorded(Code.INVALID_ARGUMENT, 0, 0);
    assertRetryStatsRecorded(0, 1, 0);
}
Also used : Status(io.grpc.Status) ClientStreamTracer(io.grpc.ClientStreamTracer) MetricsRecord(io.grpc.internal.testing.StatsTestUtils.MetricsRecord) Attributes(io.grpc.Attributes) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StreamInfo(io.grpc.ClientStreamTracer.StreamInfo) TagValue(io.opencensus.tags.TagValue) Test(org.junit.Test)

Example 9 with TagValue

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

the class RetryTest method assertRpcStatusRecorded.

private void assertRpcStatusRecorded(Status.Code code, long roundtripLatencyMs, long outboundMessages) throws Exception {
    MetricsRecord record = clientStatsRecorder.pollRecord(5, SECONDS);
    TagValue statusTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_STATUS);
    assertThat(statusTag.asString()).isEqualTo(code.toString());
    assertThat(record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_FINISHED_COUNT)).isEqualTo(1);
    assertThat(record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_ROUNDTRIP_LATENCY)).isEqualTo(roundtripLatencyMs);
    assertThat(record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_REQUEST_COUNT)).isEqualTo(outboundMessages);
}
Also used : MetricsRecord(io.grpc.internal.testing.StatsTestUtils.MetricsRecord) TagValue(io.opencensus.tags.TagValue)

Example 10 with TagValue

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

the class AbstractInteropTest method checkStartTags.

private static void checkStartTags(MetricsRecord record, String methodName, boolean clientSide) {
    assertNotNull("record is not null", record);
    TagKey methodNameTagKey = clientSide ? RpcMeasureConstants.GRPC_CLIENT_METHOD : RpcMeasureConstants.GRPC_SERVER_METHOD;
    TagValue methodNameTag = record.tags.get(methodNameTagKey);
    assertNotNull("method name tagged", methodNameTag);
    assertEquals("method names match", methodName, methodNameTag.asString());
}
Also used : TagKey(io.opencensus.tags.TagKey) TagValue(io.opencensus.tags.TagValue)

Aggregations

TagValue (io.opencensus.tags.TagValue)22 StatsTestUtils (io.grpc.internal.testing.StatsTestUtils)8 Metadata (io.grpc.Metadata)7 TagKey (io.opencensus.tags.TagKey)7 Test (org.junit.Test)7 ClientStreamTracer (io.grpc.ClientStreamTracer)5 CallAttemptsTracerFactory (io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory)5 Context (io.grpc.Context)4 SpanContext (io.opencensus.trace.SpanContext)4 Status (io.grpc.Status)3 AggregationData (io.opencensus.stats.AggregationData)3 View (io.opencensus.stats.View)3 TagContext (io.opencensus.tags.TagContext)3 CallOptions (io.grpc.CallOptions)2 ServerStreamTracer (io.grpc.ServerStreamTracer)2 MetricsRecord (io.grpc.internal.testing.StatsTestUtils.MetricsRecord)2 TagValueWithMetadata (io.opencensus.implcore.tags.TagValueWithMetadata)2 ViewData (io.opencensus.stats.ViewData)2 List (java.util.List)2 Map (java.util.Map)2