io.opencensus.trace.propagation.SpanContextParseException Java Examples

The following examples show how to use io.opencensus.trace.propagation.SpanContextParseException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpServerHandlerTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws SpanContextParseException {
  MockitoAnnotations.initMocks(this);
  handler =
      new HttpServerHandler<Object, Object, Object>(
          tracer, extractor, textFormat, textFormatGetter, false);
  handlerForPublicEndpoint =
      new HttpServerHandler<Object, Object, Object>(
          tracer, extractor, textFormat, textFormatGetter, true);

  when(tracer.spanBuilderWithRemoteParent(any(String.class), same(spanContextRemote)))
      .thenReturn(spanBuilderWithRemoteParent);
  when(tracer.spanBuilderWithExplicitParent(any(String.class), any(Span.class)))
      .thenReturn(spanBuilderWithLocalParent);
  when(spanBuilderWithRemoteParent.setSpanKind(any(Kind.class)))
      .thenReturn(spanBuilderWithRemoteParent);
  when(spanBuilderWithLocalParent.setSpanKind(any(Kind.class)))
      .thenReturn(spanBuilderWithLocalParent);
  when(spanBuilderWithRemoteParent.startSpan()).thenReturn(spanWithRemoteParent);
  when(spanBuilderWithLocalParent.startSpan()).thenReturn(spanWithLocalParent);

  when(textFormat.extract(same(carrier), same(textFormatGetter))).thenReturn(spanContextRemote);
}
 
Example #2
Source File: B3FormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseInvalidSpanId() throws SpanContextParseException {
  Map<String, String> invalidHeaders = new HashMap<String, String>();
  invalidHeaders.put(X_B3_TRACE_ID, TRACE_ID_BASE16);
  invalidHeaders.put(X_B3_SPAN_ID, "abcdefghijklmnop");
  thrown.expect(SpanContextParseException.class);
  thrown.expectMessage("Invalid input.");
  b3Format.extract(invalidHeaders, getter);
}
 
Example #3
Source File: HttpServerHandlerTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void handleStartShouldSetKindToServer() throws SpanContextParseException {
  handler.handleStart(carrier, request);
  verify(spanBuilderWithRemoteParent).setSpanKind(kindArgumentCaptor.capture());

  Kind kind = kindArgumentCaptor.getValue();
  assertThat(kind).isEqualTo(Kind.SERVER);
}
 
Example #4
Source File: HttpServerHandlerTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #5
Source File: CloudTraceFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseInvalidTraceOptionsShouldFail() throws SpanContextParseException {
  parseFailure(
      constructHeader(TRACE_ID_BASE16, SPAN_ID_BASE10, OPTIONS_INVALID),
      SpanContextParseException.class,
      "Invalid input");
}
 
Example #6
Source File: CloudTraceFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseMissingHeaderShouldFail() throws SpanContextParseException {
  Map<String, String> headerMissing = new HashMap<String, String>();
  thrown.expect(SpanContextParseException.class);
  thrown.expectMessage("Missing or too short header: X-Cloud-Trace-Context");
  cloudTraceFormat.extract(headerMissing, getter);
}
 
Example #7
Source File: CloudTraceFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseShortHeaderShouldFail() throws SpanContextParseException {
  parseFailure(
      constructHeader(TRACE_ID_BASE16, ""),
      SpanContextParseException.class,
      "Missing or too short header: X-Cloud-Trace-Context");
}
 
Example #8
Source File: CloudTraceFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseWithShortSpanIdAndSamplingShouldSucceed() throws SpanContextParseException {
  final String spanId = "1";
  ByteBuffer buffer = ByteBuffer.allocate(SpanId.SIZE);
  buffer.putLong(Long.parseLong(spanId));
  SpanId expectedSpanId = SpanId.fromBytes(buffer.array());
  parseSuccess(
      constructHeader(TRACE_ID_BASE16, spanId, SAMPLED),
      SpanContext.create(TRACE_ID, expectedSpanId, TRACE_OPTIONS_SAMPLED));
}
 
Example #9
Source File: CloudTraceFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseLongTraceIdShouldFail() throws SpanContextParseException {
  parseFailure(
      constructHeader(TRACE_ID_BASE16_LONG, SPAN_ID_BASE10, SAMPLED),
      SpanContextParseException.class,
      "Invalid input");
}
 
Example #10
Source File: CloudTraceFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseMissingTraceIdShouldFail() throws SpanContextParseException {
  parseFailure(
      constructHeader("", SPAN_ID_BASE10_VERY_LONG, SAMPLED),
      SpanContextParseException.class,
      "Invalid input");
}
 
Example #11
Source File: CloudTraceFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseInvalidSpanIdShouldFail() throws SpanContextParseException {
  parseFailure(
      constructHeader(TRACE_ID_BASE16, SPAN_ID_BASE10_INVALID, SAMPLED),
      SpanContextParseException.class,
      "Invalid input");
}
 
Example #12
Source File: CloudTraceFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseNegativeSpanIdShouldFail() throws SpanContextParseException {
  parseFailure(
      constructHeader(TRACE_ID_BASE16, SPAN_ID_BASE10_NEGATIVE, SAMPLED),
      SpanContextParseException.class,
      "Invalid input");
}
 
Example #13
Source File: CloudTraceFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseNegativeTraceOptionsShouldFail() throws SpanContextParseException {
  parseFailure(
      constructHeader(TRACE_ID_BASE16, SPAN_ID_BASE10, OPTIONS_NEGATIVE),
      SpanContextParseException.class,
      "Invalid input");
}
 
Example #14
Source File: B3FormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseMissingSpanId() throws SpanContextParseException {
  Map<String, String> invalidHeaders = new HashMap<String, String>();
  invalidHeaders.put(X_B3_TRACE_ID, TRACE_ID_BASE16);
  thrown.expect(SpanContextParseException.class);
  thrown.expectMessage("Missing X_B3_SPAN_ID.");
  b3Format.extract(invalidHeaders, getter);
}
 
Example #15
Source File: B3FormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseInvalidSpanId_Size() throws SpanContextParseException {
  Map<String, String> invalidHeaders = new HashMap<String, String>();
  invalidHeaders.put(X_B3_TRACE_ID, TRACE_ID_BASE16);
  invalidHeaders.put(X_B3_SPAN_ID, "0123456789abcdef00");
  thrown.expect(SpanContextParseException.class);
  thrown.expectMessage("Invalid input.");
  b3Format.extract(invalidHeaders, getter);
}
 
Example #16
Source File: HttpServerHandler.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
}
 
Example #17
Source File: B3FormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseMissingTraceId() throws SpanContextParseException {
  Map<String, String> invalidHeaders = new HashMap<String, String>();
  invalidHeaders.put(X_B3_SPAN_ID, SPAN_ID_BASE16);
  thrown.expect(SpanContextParseException.class);
  thrown.expectMessage("Missing X_B3_TRACE_ID.");
  b3Format.extract(invalidHeaders, getter);
}
 
Example #18
Source File: B3FormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseInvalidTraceId_Size() throws SpanContextParseException {
  Map<String, String> invalidHeaders = new HashMap<String, String>();
  invalidHeaders.put(X_B3_TRACE_ID, "0123456789abcdef00");
  invalidHeaders.put(X_B3_SPAN_ID, SPAN_ID_BASE16);
  thrown.expect(SpanContextParseException.class);
  thrown.expectMessage("Invalid input.");
  b3Format.extract(invalidHeaders, getter);
}
 
Example #19
Source File: B3FormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseInvalidTraceId() throws SpanContextParseException {
  Map<String, String> invalidHeaders = new HashMap<String, String>();
  invalidHeaders.put(X_B3_TRACE_ID, "abcdefghijklmnop");
  invalidHeaders.put(X_B3_SPAN_ID, SPAN_ID_BASE16);
  thrown.expect(SpanContextParseException.class);
  thrown.expectMessage("Invalid input.");
  b3Format.extract(invalidHeaders, getter);
}
 
Example #20
Source File: B3FormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseEightBytesTraceId_NotSampledSpanContext() throws SpanContextParseException {
  Map<String, String> headersEightBytes = new HashMap<String, String>();
  headersEightBytes.put(X_B3_TRACE_ID, TRACE_ID_BASE16_EIGHT_BYTES);
  headersEightBytes.put(X_B3_SPAN_ID, SPAN_ID_BASE16);
  assertThat(b3Format.extract(headersEightBytes, getter))
      .isEqualTo(SpanContext.create(TRACE_ID_EIGHT_BYTES, SPAN_ID, TraceOptions.DEFAULT));
}
 
Example #21
Source File: CloudTraceFormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseMissingSpanIdShouldFail() throws SpanContextParseException {
  parseFailure(
      constructHeader(TRACE_ID_BASE16, "", SAMPLED),
      SpanContextParseException.class,
      "Invalid input");
}
 
Example #22
Source File: B3FormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseZeroFlag() throws SpanContextParseException {
  Map<String, String> headersFlagNotSampled = new HashMap<String, String>();
  headersFlagNotSampled.put(X_B3_TRACE_ID, TRACE_ID_BASE16);
  headersFlagNotSampled.put(X_B3_SPAN_ID, SPAN_ID_BASE16);
  headersFlagNotSampled.put(X_B3_FLAGS, "0");
  assertThat(b3Format.extract(headersFlagNotSampled, getter))
      .isEqualTo(SpanContext.create(TRACE_ID, SPAN_ID, TraceOptions.DEFAULT));
}
 
Example #23
Source File: B3FormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseFlag() throws SpanContextParseException {
  Map<String, String> headersFlagSampled = new HashMap<String, String>();
  headersFlagSampled.put(X_B3_TRACE_ID, TRACE_ID_BASE16);
  headersFlagSampled.put(X_B3_SPAN_ID, SPAN_ID_BASE16);
  headersFlagSampled.put(X_B3_FLAGS, "1");
  assertThat(b3Format.extract(headersFlagSampled, getter))
      .isEqualTo(SpanContext.create(TRACE_ID, SPAN_ID, TRACE_OPTIONS));
}
 
Example #24
Source File: B3FormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseZeroSampled() 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);
  headersNotSampled.put(X_B3_SAMPLED, "0");
  assertThat(b3Format.extract(headersNotSampled, getter))
      .isEqualTo(SpanContext.create(TRACE_ID, SPAN_ID, TraceOptions.DEFAULT));
}
 
Example #25
Source File: B3FormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseSampled() throws SpanContextParseException {
  Map<String, String> headersSampled = new HashMap<String, String>();
  headersSampled.put(X_B3_TRACE_ID, TRACE_ID_BASE16);
  headersSampled.put(X_B3_SPAN_ID, SPAN_ID_BASE16);
  headersSampled.put(X_B3_SAMPLED, "1");
  assertThat(b3Format.extract(headersSampled, getter))
      .isEqualTo(SpanContext.create(TRACE_ID, SPAN_ID, TRACE_OPTIONS));
}
 
Example #26
Source File: B3FormatTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #27
Source File: BasicOperationsBenchmark.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/** Decode a span using binary format. */
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public SpanContext decodeSpanBinary(Data data) throws SpanContextParseException {
  return data.propagation.getBinaryFormat().fromByteArray(data.spanToDecodeBinary);
}
 
Example #28
Source File: BinaryFormatImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void fromBinaryValue_ShorterTraceOptions() throws SpanContextParseException {
  expectedException.expect(SpanContextParseException.class);
  expectedException.expectMessage("Invalid input: truncated");
  binaryFormat.fromByteArray(
      new byte[] {
        0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 1, 97, 98, 99, 100,
        101, 102, 103, 104, 2
      });
}
 
Example #29
Source File: BinaryFormatImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void fromBinaryValue_ShorterTraceId() throws SpanContextParseException {
  expectedException.expect(SpanContextParseException.class);
  expectedException.expectMessage("Invalid input: truncated");
  binaryFormat.fromByteArray(
      new byte[] {0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76});
}
 
Example #30
Source File: BinaryFormatImplTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void fromBinaryValue_UnsupportedFieldIdThird_skipped() throws SpanContextParseException {
  assertThat(
          binaryFormat
              .fromByteArray(
                  new byte[] {
                    0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 1, 97,
                    98, 99, 100, 101, 102, 103, 104, 0, 1
                  })
              .isValid())
      .isTrue();
}