Java Code Examples for io.opentracing.Span#setBaggageItem()

The following examples show how to use io.opentracing.Span#setBaggageItem() . 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: DefaultConfigurationTest.java    From opentracing-toolbox with MIT License 6 votes vote down vote up
@Test
void correlatesWithTraceSpanAndFlowId() {
    final Span span = tracer.buildSpan("test").start();
    final String flowId = UUID.randomUUID().toString();
    span.setBaggageItem("flow_id", flowId);

    try (final Scope ignored = tracer.activateSpan(span)) {
        log.info("Correlating...");
    } finally {
        span.finish();
    }

    final LoggingEvent event = getOnlyElement(log.getLoggingEvents());
    final ImmutableMap<String, String> mdc = event.getMdc();

    assertThat(mdc, aMapWithSize(3));
    assertThat(mdc, hasEntry("trace_id", span.context().toTraceId()));
    assertThat(mdc, hasEntry("span_id", span.context().toSpanId()));
    assertThat(mdc, hasEntry("flow_id", flowId));
}
 
Example 2
Source File: CustomConfigurationTest.java    From opentracing-toolbox with MIT License 6 votes vote down vote up
@Test
void correlatesWithTraceSpanAndRequestId() {
    final Span span = tracer.buildSpan("test").start();
    final String requestId = UUID.randomUUID().toString();
    span.setBaggageItem("request_id", requestId);

    try (final Scope ignored = tracer.activateSpan(span)) {
        log.info("Correlating...");
    } finally {
        span.finish();
    }

    final LoggingEvent event = getOnlyElement(log.getLoggingEvents());
    final ImmutableMap<String, String> mdc = event.getMdc();

    assertThat(mdc, aMapWithSize(3));
    assertThat(mdc, hasEntry("trace", span.context().toTraceId()));
    assertThat(mdc, hasEntry("span", span.context().toSpanId()));
    assertThat(mdc, hasEntry("request_id", requestId));
}
 
Example 3
Source File: OpenTracingBridgeTest.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
void testOperationsAfterFinish() {
    final Span span = apmTracer.buildSpan("test").start();

    span.finish();

    assertThat(reporter.getTransactions()).hasSize(1);

    // subsequent calls have undefined behavior but should not throw exceptions
    span.setOperationName("");
    span.setTag("foo", "bar");
    span.setBaggageItem("foo", "bar");
    span.getBaggageItem("foo");
    span.log("foo");
}
 
Example 4
Source File: LogCorrelationTest.java    From opentracing-toolbox with MIT License 5 votes vote down vote up
@Test
void correlatesBaggageWhenSpanIsForwarding() {
    final Span span = unit.buildSpan("test").start();
    final ForwardingSpan wrapped = () -> span;

    try (final Scope ignored = unit.activateSpan(wrapped)) {
        span.setBaggageItem("request-id", "P6QQkzZHfza9GO");
        assertEquals("P6QQkzZHfza9GO", MDC.get("request_id"));
    }
}
 
Example 5
Source File: LogCorrelationTest.java    From opentracing-toolbox with MIT License 5 votes vote down vote up
@Test
void doesntCorrelateWhenSpanIsNotActive() {
    final Span parent = unit.buildSpan("parent").start();

    try (final Scope ignored = unit.activateSpan(parent)) {
        final Span child = unit.buildSpan("child").start();
        child.setBaggageItem("request-id", "P6QQkzZHfza9GO");
        assertNull(MDC.get("request_id"));
    }
}
 
Example 6
Source File: LogCorrelationTest.java    From opentracing-toolbox with MIT License 5 votes vote down vote up
@Test
void doesntCorrelateWithoutScope() {
    final Span span = unit.buildSpan("test").start();

    span.setBaggageItem("request-id", "P6QQkzZHfza9GO");

    assertNull(MDC.get("request_id"));
}
 
Example 7
Source File: LogCorrelationTest.java    From opentracing-toolbox with MIT License 5 votes vote down vote up
@Test
void correlatesNewBaggage() {
    final Span span = unit.buildSpan("test").start();

    try (final Scope ignored = unit.activateSpan(span)) {
        span.setBaggageItem("request-id", "P6QQkzZHfza9GO");
        assertEquals("P6QQkzZHfza9GO", MDC.get("request_id"));
    }
}
 
Example 8
Source File: AutoTaggingTest.java    From opentracing-toolbox with MIT License 5 votes vote down vote up
@Test
void shouldNotAutoTagOrdinaryKeyOnStartChild() {
    final Span parent = unit.buildSpan("parent").start();
    parent.setBaggageItem("ordinary", "yes");

    unit.buildSpan("child").asChildOf(parent).start().finish();

    final MockSpan span = getOnlyElement(tracer.finishedSpans());
    assertNull(span.tags().get("ordinary"));
}
 
Example 9
Source File: AutoTaggingTest.java    From opentracing-toolbox with MIT License 5 votes vote down vote up
@Test
void shouldAutoTagSpecialKeyOnStartChild() {
    final Span parent = unit.buildSpan("parent").start();
    parent.setBaggageItem("special", "yes");

    unit.buildSpan("child").asChildOf(parent).start().finish();

    final MockSpan span = getOnlyElement(tracer.finishedSpans());
    assertEquals("yes", span.tags().get("special"));
}
 
Example 10
Source File: AutoBaggage.java    From opentracing-toolbox with MIT License 5 votes vote down vote up
@Override
public <T> void onTag(final Span span, final Tag<T> tag, final T value) {
    if (baggageExists(span)) {
        return;
    }

    if (tagKey.equals(tag.getKey())) {
        span.setBaggageItem(baggageKey, String.valueOf(value));
    }
}
 
Example 11
Source File: MockSpanTest.java    From opentracing-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddBaggageAfterFinish() {
    MockTracer tracer = new MockTracer();
    Span span = tracer.buildSpan("foo").start();
    span.finish();

    try {
        span.setBaggageItem("foo", "bar");
        Assert.fail();
    } catch (RuntimeException ex) {
    }
    Assert.assertEquals(1, tracer.finishedSpans().get(0).generatedErrors().size());
}
 
Example 12
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 13
Source File: MockTracerTest.java    From opentracing-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testTextMapPropagatorTextMap() {
    MockTracer tracer = new MockTracer(MockTracer.Propagator.TEXT_MAP);
    HashMap<String, String> injectMap = new HashMap<>();
    injectMap.put("foobag", "donttouch");
    {
        Span parentSpan = tracer.buildSpan("foo")
                .start();
        parentSpan.setBaggageItem("foobag", "fooitem");
        parentSpan.finish();

        tracer.inject(parentSpan.context(), Format.Builtin.TEXT_MAP_INJECT,
                new TextMapInjectAdapter(injectMap));

        SpanContext extract = tracer.extract(Format.Builtin.TEXT_MAP_EXTRACT, new TextMapExtractAdapter(injectMap));

        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"));
    Assert.assertEquals("donttouch", injectMap.get("foobag"));
}
 
Example 14
Source File: TracingUtil.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public static Span getSpan(Tracer tracer, String operationName, SpanContext spanContext, String flowId, String flowChainId, String flowTriggerUserCrn) {
    Span span = tracer
            .buildSpan(operationName)
            .addReference(References.FOLLOWS_FROM, spanContext)
            .ignoreActiveSpan()
            .start();
    span.setBaggageItem(FlowConstants.OPERATION_NAME, operationName);
    MDCBuilder.addSpanId(span.context().toSpanId());
    MDCBuilder.addTraceId(span.context().toTraceId());
    span.setTag(FlowConstants.FLOW_ID, flowId);
    span.setTag(FlowConstants.FLOW_CHAIN_ID, flowChainId);
    span.setTag(FlowConstants.FLOW_TRIGGER_USERCRN, flowTriggerUserCrn);
    span.setTag("RESOURCE_CRN", MDC.get("resourceCrn"));
    return span;
}
 
Example 15
Source File: Hello.java    From opentracing-tutorial with Apache License 2.0 5 votes vote down vote up
private void sayHello(String helloTo, String greeting) {
    Span span = tracer.buildSpan("say-hello").start();
    try (Scope scope = tracer.scopeManager().activate(span)) {
        span.setTag("hello-to", helloTo);
        span.setBaggageItem("greeting", greeting);

        String helloStr = formatString(helloTo);
        printHello(helloStr);
    } finally {
        span.finish();
    }
}
 
Example 16
Source File: HelloController.java    From Mastering-Distributed-Tracing with MIT License 5 votes vote down vote up
@GetMapping("/sayHello/{name}")
public String sayHello(@PathVariable String name, @RequestHeader HttpHeaders headers) {
    System.out.println("Headers: " + headers);

    Span span = tracer.activeSpan();
    if (span != null) {
        span.setBaggageItem("user-agent", headers.getFirst(HttpHeaders.USER_AGENT));
    }

    String response = formatGreeting(name);
    return response;
}
 
Example 17
Source File: OpenTracingBridgeTest.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
void testGetBaggageItem() {
    final Span span = apmTracer.buildSpan("span")
        .start();

    // baggage is not supported yet
    span.setBaggageItem("foo", "bar");
    assertThat(span.getBaggageItem("foo")).isNull();
    span.finish();
}
 
Example 18
Source File: BaggageHandlingTest.java    From opentelemetry-java with Apache License 2.0 5 votes vote down vote up
@Test
public void test_multithreaded() throws Exception {
  final Span span = tracer.buildSpan("one").start();
  span.setBaggageItem("key1", "value1");

  Future<?> f =
      executor.submit(
          () -> {
            /* Override the previous value... */
            span.setBaggageItem("key1", "value2");

            /* add a new baggage item... */
            span.setBaggageItem("newkey", "newvalue");

            /* have a child that updates its own baggage
             * (should not be reflected in the original Span). */
            Span childSpan = tracer.buildSpan("child").start();
            try {
              childSpan.setBaggageItem("key1", "childvalue");
            } finally {
              childSpan.finish();
            }

            /* and finish the Span. */
            span.finish();
          });

  /* Single call, no need to use await() */
  f.get(5, TimeUnit.SECONDS);

  assertEquals(2, inMemoryTracing.getSpanExporter().getFinishedSpanItems().size());
  assertEquals(span.getBaggageItem("key1"), "value2");
  assertEquals(span.getBaggageItem("newkey"), "newvalue");
}
 
Example 19
Source File: TraceBehaviorTest.java    From tchannel-java with MIT License 4 votes vote down vote up
@Test
public void TestTraceBehavior() throws Exception {
    logger.info("Starting test encoding1={}, encoding2={}, sampled={}", encoding1, encoding2, sampled);

    String host = tchannel.getListeningHost();
    Downstream request = new Downstream(
            SERVER_NAME,
            "s3",
            encoding1,
            host + ":" + tchannel.getPort(),
            new Downstream(
                    SERVER_NAME,
                    "s4",
                    encoding2,
                    host + ":" + tchannel.getPort(),
                    null
            )
    );

    String baggage = "luggage-" + System.currentTimeMillis();
    Span span = tracer.buildSpan("root").startManual();
    span.setBaggageItem(BAGGAGE_KEY, baggage);
    if (sampled) {
        Tags.SAMPLING_PRIORITY.set(span, 1);
    }
    tchannel.getTracingContext().pushSpan(span);

    Response response = traceBehavior.handleRequest(new Request("s2", request));
    logger.info("Response: {}", response);
    span.finish();

    String traceId = span.context().toTraceId();

    validate(response, traceId, baggage, 2);
    if (sampled) {
        for (int i = 0; i < 100; i++) {
            if (reporter.getSpans().size() == 5) {
                break;
            }
            Thread.sleep(10);
        }
        assertEquals(5, reporter.getSpans().size());
    }
}
 
Example 20
Source File: CustomerCamelRoute.java    From istio-tutorial with Apache License 2.0 4 votes vote down vote up
public void addTracer(Exchange exchange){
    String userAgent = (String) exchange.getIn().getHeader("user-agent");
    Span span = ActiveSpanManager.getSpan(exchange);
    span.setBaggageItem("user-agent",userAgent);
}