Search in sources :

Example 1 with FakeTagContext

use of io.grpc.internal.testing.StatsTestUtils.FakeTagContext 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)

Aggregations

Context (io.grpc.Context)1 FakeTagContext (io.grpc.internal.testing.StatsTestUtils.FakeTagContext)1 TagKey (io.opencensus.tags.TagKey)1 TagValue (io.opencensus.tags.TagValue)1 Span (io.opencensus.trace.Span)1 SpanContext (io.opencensus.trace.SpanContext)1 Map (java.util.Map)1 Test (org.junit.Test)1