Search in sources :

Example 11 with SpanContext

use of io.opencensus.trace.SpanContext in project instrumentation-java by census-instrumentation.

the class SpanBuilderImpl method startSpan.

@Override
public Span startSpan() {
    if (remoteParentSpanContext != null) {
        return startSpanInternal(remoteParentSpanContext, Boolean.TRUE, name, sampler, parentLinks, recordEvents, kind, null);
    } else {
        // This is not a child of a remote Span. Get the parent SpanContext from the parent Span if
        // any.
        SpanContext parentContext = null;
        Boolean hasRemoteParent = null;
        if (parent != null) {
            parentContext = parent.getContext();
            hasRemoteParent = Boolean.FALSE;
        }
        return startSpanInternal(parentContext, hasRemoteParent, name, sampler, parentLinks, recordEvents, kind, parent);
    }
}
Also used : SpanContext(io.opencensus.trace.SpanContext)

Example 12 with SpanContext

use of io.opencensus.trace.SpanContext in project instrumentation-java by census-instrumentation.

the class B3FormatTest method parseMissingSampledAndMissingFlag.

@Test
public void parseMissingSampledAndMissingFlag() throws SpanContextParseException {
    Map<String, String> headersNotSampled = new HashMap<String, String>();
    headersNotSampled.put(X_B3_TRACE_ID, TRACE_ID_BASE16);
    headersNotSampled.put(X_B3_SPAN_ID, SPAN_ID_BASE16);
    SpanContext spanContext = SpanContext.create(TRACE_ID, SPAN_ID, TraceOptions.DEFAULT);
    assertThat(b3Format.extract(headersNotSampled, getter)).isEqualTo(spanContext);
}
Also used : SpanContext(io.opencensus.trace.SpanContext) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 13 with SpanContext

use of io.opencensus.trace.SpanContext in project grpc-java by grpc.

the class CensusModulesTest method serverBasicTracingNoHeaders.

@Test
public void serverBasicTracingNoHeaders() {
    ServerStreamTracer.Factory tracerFactory = censusTracing.getServerTracerFactory();
    ServerStreamTracer serverStreamTracer = tracerFactory.newServerStreamTracer(method.getFullMethodName(), new Metadata());
    verifyNoInteractions(mockTracingPropagationHandler);
    verify(tracer).spanBuilderWithRemoteParent(eq("Recv.package1.service2.method3"), ArgumentMatchers.<SpanContext>isNull());
    verify(spyServerSpanBuilder).setRecordEvents(eq(true));
    Context filteredContext = serverStreamTracer.filterContext(Context.ROOT);
    assertSame(spyServerSpan, ContextUtils.getValue(filteredContext));
    serverStreamTracer.serverCallStarted(new CallInfo<>(method, Attributes.EMPTY, null));
    verify(spyServerSpan, never()).end(any(EndSpanOptions.class));
    serverStreamTracer.outboundMessage(0);
    serverStreamTracer.outboundMessageSent(0, 882, -1);
    serverStreamTracer.inboundMessage(0);
    serverStreamTracer.outboundMessage(1);
    serverStreamTracer.outboundMessageSent(1, -1, 27);
    serverStreamTracer.inboundMessageRead(0, 255, 90);
    serverStreamTracer.streamClosed(Status.CANCELLED);
    InOrder inOrder = inOrder(spyServerSpan);
    inOrder.verify(spyServerSpan, times(3)).addMessageEvent(messageEventCaptor.capture());
    List<MessageEvent> events = messageEventCaptor.getAllValues();
    assertEquals(MessageEvent.builder(MessageEvent.Type.SENT, 0).setCompressedMessageSize(882).build(), events.get(0));
    assertEquals(MessageEvent.builder(MessageEvent.Type.SENT, 1).setUncompressedMessageSize(27).build(), events.get(1));
    assertEquals(MessageEvent.builder(MessageEvent.Type.RECEIVED, 0).setCompressedMessageSize(255).setUncompressedMessageSize(90).build(), events.get(2));
    inOrder.verify(spyServerSpan).end(EndSpanOptions.builder().setStatus(io.opencensus.trace.Status.CANCELLED).setSampleToLocalSpanStore(false).build());
    verifyNoMoreInteractions(spyServerSpan);
}
Also used : SpanContext(io.opencensus.trace.SpanContext) Context(io.grpc.Context) TagContext(io.opencensus.tags.TagContext) InOrder(org.mockito.InOrder) ServerStreamTracer(io.grpc.ServerStreamTracer) MessageEvent(io.opencensus.trace.MessageEvent) EndSpanOptions(io.opencensus.trace.EndSpanOptions) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 14 with SpanContext

use of io.opencensus.trace.SpanContext 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 15 with SpanContext

use of io.opencensus.trace.SpanContext in project instrumentation-java by census-instrumentation.

the class HttpServerHandler method handleStart.

/**
 * Instrument an incoming request before it is handled.
 *
 * <p>This method will create a span under the deserialized propagated parent context. If the
 * parent context is not present, the span will be created under the current context.
 *
 * <p>The generated span will NOT be set as current context. User can control when to enter the
 * scope of this span. Use {@link AbstractHttpHandler#getSpanFromContext} to retrieve the span.
 *
 * @param carrier the entity that holds the HTTP information.
 * @param request the request entity.
 * @return the {@link HttpRequestContext} that contains stats and trace data associated with the
 *     request.
 * @since 0.19
 */
public HttpRequestContext handleStart(C carrier, Q request) {
    checkNotNull(carrier, "carrier");
    checkNotNull(request, "request");
    SpanBuilder spanBuilder = null;
    String spanName = getSpanName(request, extractor);
    // de-serialize the context
    SpanContext spanContext = null;
    try {
        spanContext = textFormat.extract(carrier, getter);
    } catch (SpanContextParseException e) {
    // TODO: Currently we cannot distinguish between context parse error and missing context.
    // Logging would be annoying so we just ignore this error and do not even log a message.
    }
    if (spanContext == null || publicEndpoint) {
        spanBuilder = tracer.spanBuilder(spanName);
    } else {
        spanBuilder = tracer.spanBuilderWithRemoteParent(spanName, spanContext);
    }
    Span span = spanBuilder.setSpanKind(Kind.SERVER).startSpan();
    if (publicEndpoint && spanContext != null) {
        span.addLink(Link.fromSpanContext(spanContext, Type.PARENT_LINKED_SPAN));
    }
    if (span.getOptions().contains(Options.RECORD_EVENTS)) {
        addSpanRequestAttributes(span, request, extractor);
    }
    return getNewContext(span, tagger.getCurrentTagContext());
}
Also used : SpanBuilder(io.opencensus.trace.SpanBuilder) SpanContext(io.opencensus.trace.SpanContext) SpanContextParseException(io.opencensus.trace.propagation.SpanContextParseException) Span(io.opencensus.trace.Span)

Aggregations

SpanContext (io.opencensus.trace.SpanContext)21 Test (org.junit.Test)8 Timestamp (io.opencensus.common.Timestamp)6 AttributeValue (io.opencensus.trace.AttributeValue)4 Status (io.opencensus.trace.Status)4 SpanData (io.opencensus.trace.export.SpanData)4 Span (io.opencensus.trace.Span)3 SpanId (io.opencensus.trace.SpanId)3 ArrayList (java.util.ArrayList)3 CloudTraceContext (com.google.apphosting.api.CloudTraceContext)2 Context (io.grpc.Context)2 Annotation (io.opencensus.trace.Annotation)2 SpanBuilder (io.opencensus.trace.SpanBuilder)2 Map (java.util.Map)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Span (com.google.devtools.cloudtrace.v2.Span)1 SpanName (com.google.devtools.cloudtrace.v2.SpanName)1 TruncatableString (com.google.devtools.cloudtrace.v2.TruncatableString)1 Metadata (io.grpc.Metadata)1 ServerStreamTracer (io.grpc.ServerStreamTracer)1