io.opentracing.propagation.BinaryInject Java Examples

The following examples show how to use io.opentracing.propagation.BinaryInject. 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: BraveTracer.java    From brave-opentracing with Apache License 2.0 6 votes vote down vote up
/**
 * Injects the underlying context using B3 encoding by default.
 */
@Override public <C> void inject(SpanContext spanContext, Format<C> format, C carrier) {
  BraveSpanContext braveContext = ((BraveSpanContext) spanContext);
  if (carrier instanceof BinaryInject) {
    BinaryCodec.INSTANCE.inject(braveContext.unwrap(), (BinaryInject) carrier);
    return;
  }
  if (!(carrier instanceof TextMapInject)) {
    throw new UnsupportedOperationException(carrier + " not instanceof TextMapInject");
  }
  Kind kind = braveContext.kind;
  Injector<TextMapInject> injector = null;
  if (Kind.CLIENT.equals(kind)) {
    injector = formatToClientInjector.get(format);
  } else if (Kind.PRODUCER.equals(kind)) {
    injector = formatToProducerInjector.get(format);
  } else if (Kind.CONSUMER.equals(kind)) {
    injector = formatToConsumerInjector.get(format);
  }
  if (injector == null) injector = formatToInjector.get(format);
  if (injector == null) {
    throw new UnsupportedOperationException(format + " not in " + formatToInjector.keySet());
  }
  injector.inject(braveContext.unwrap(), (TextMapInject) carrier);
}
 
Example #2
Source File: MockTracerTest.java    From opentracing-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testBinaryPropagator() {
    MockTracer tracer = new MockTracer(MockTracer.Propagator.BINARY);
    {
        Span parentSpan = tracer.buildSpan("foo")
                .start();
        parentSpan.setBaggageItem("foobag", "fooitem");
        parentSpan.finish();

        ByteBuffer buffer = ByteBuffer.allocate(128);
        BinaryInject binary = BinaryAdapters.injectionCarrier(buffer);
        tracer.inject(parentSpan.context(), Format.Builtin.BINARY_INJECT, binary);

        buffer.rewind();
        SpanContext extract = tracer.extract(Format.Builtin.BINARY_EXTRACT, BinaryAdapters.extractionCarrier(buffer));

        Span childSpan = tracer.buildSpan("bar")
                .asChildOf(extract)
                .start();
        childSpan.setBaggageItem("barbag", "baritem");
        childSpan.finish();
    }
    List<MockSpan> finishedSpans = tracer.finishedSpans();

    Assert.assertEquals(2, finishedSpans.size());
    Assert.assertEquals(finishedSpans.get(0).context().traceId(), finishedSpans.get(1).context().traceId());
    Assert.assertEquals(finishedSpans.get(0).context().spanId(), finishedSpans.get(1).parentId());
    Assert.assertEquals("fooitem", finishedSpans.get(0).getBaggageItem("foobag"));
    Assert.assertNull(finishedSpans.get(0).getBaggageItem("barbag"));
    Assert.assertEquals("fooitem", finishedSpans.get(1).getBaggageItem("foobag"));
    Assert.assertEquals("baritem", finishedSpans.get(1).getBaggageItem("barbag"));
}
 
Example #3
Source File: BraveTracer.java    From brave-opentracing with Apache License 2.0 4 votes vote down vote up
@Override public void inject(TraceContext traceContext, BinaryInject binaryInject) {
  byte[] injected = B3SingleFormat.writeB3SingleFormatAsBytes(traceContext);
  binaryInject.injectionBuffer(injected.length).put(injected);
}