Search in sources :

Example 6 with SpanContextParseException

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

the class B3Format method extract.

@Override
public <C> /*>>> extends @NonNull Object*/
SpanContext extract(C carrier, Getter<C> getter) throws SpanContextParseException {
    checkNotNull(carrier, "carrier");
    checkNotNull(getter, "getter");
    try {
        TraceId traceId;
        String traceIdStr = getter.get(carrier, X_B3_TRACE_ID);
        if (traceIdStr != null) {
            if (traceIdStr.length() == TraceId.SIZE) {
                // This is an 8-byte traceID.
                traceIdStr = UPPER_TRACE_ID + traceIdStr;
            }
            traceId = TraceId.fromLowerBase16(traceIdStr);
        } else {
            throw new SpanContextParseException("Missing X_B3_TRACE_ID.");
        }
        SpanId spanId;
        String spanIdStr = getter.get(carrier, X_B3_SPAN_ID);
        if (spanIdStr != null) {
            spanId = SpanId.fromLowerBase16(spanIdStr);
        } else {
            throw new SpanContextParseException("Missing X_B3_SPAN_ID.");
        }
        TraceOptions traceOptions = TraceOptions.DEFAULT;
        if (SAMPLED_VALUE.equals(getter.get(carrier, X_B3_SAMPLED)) || FLAGS_VALUE.equals(getter.get(carrier, X_B3_FLAGS))) {
            traceOptions = TraceOptions.builder().setIsSampled(true).build();
        }
        return SpanContext.create(traceId, spanId, traceOptions, TRACESTATE_DEFAULT);
    } catch (IllegalArgumentException e) {
        throw new SpanContextParseException("Invalid input.", e);
    }
}
Also used : SpanContextParseException(io.opencensus.trace.propagation.SpanContextParseException) TraceId(io.opencensus.trace.TraceId) TraceOptions(io.opencensus.trace.TraceOptions) SpanId(io.opencensus.trace.SpanId)

Example 7 with SpanContextParseException

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

the class HttpServerHandlerTest method handleStartShouldIgnoreContextParseException.

@Test
public void handleStartShouldIgnoreContextParseException() throws Exception {
    when(textFormat.extract(same(carrier), same(textFormatGetter))).thenThrow(new SpanContextParseException("test"));
    HttpRequestContext context = handler.handleStart(carrier, request);
    verify(tracer).spanBuilderWithExplicitParent(any(String.class), any(Span.class));
    assertThat(context.span).isEqualTo(spanWithLocalParent);
}
Also used : SpanContextParseException(io.opencensus.trace.propagation.SpanContextParseException) FakeSpan(io.opencensus.contrib.http.util.testing.FakeSpan) Span(io.opencensus.trace.Span) Test(org.junit.Test)

Aggregations

SpanContextParseException (io.opencensus.trace.propagation.SpanContextParseException)7 SpanId (io.opencensus.trace.SpanId)4 TraceId (io.opencensus.trace.TraceId)4 TraceOptions (io.opencensus.trace.TraceOptions)4 Span (io.opencensus.trace.Span)2 Test (org.junit.Test)2 Metadata (io.grpc.Metadata)1 FakeSpan (io.opencensus.contrib.http.util.testing.FakeSpan)1 SpanBuilder (io.opencensus.trace.SpanBuilder)1 SpanContext (io.opencensus.trace.SpanContext)1 Tracestate (io.opencensus.trace.Tracestate)1