io.opentracing.propagation.TextMapAdapter Java Examples
The following examples show how to use
io.opentracing.propagation.TextMapAdapter.
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: OpenTracingBridgeTest.java From apm-agent-java with Apache License 2.0 | 6 votes |
@Test void testToIdOfExtractedContext() { final String traceIdString = "0af7651916cd43dd8448eb211c80319c"; final String parentIdString = "b9c7c989f97918e1"; // -------------------------------------------------------- final Id traceId = Id.new128BitId(); traceId.fromHexString(traceIdString, 0); assertThat(traceId.toString()).isEqualTo(traceIdString); // -------------------------------------------------------- final Id spanId = Id.new64BitId(); spanId.fromHexString(parentIdString, 0); assertThat(spanId.toString()).isEqualTo(parentIdString); // -------------------------------------------------------- TextMap textMapExtractAdapter = new TextMapAdapter(Map.of( TraceContext.W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME, "00-" + traceIdString + "-" + parentIdString + "-01", "User-Agent", "curl")); //ExternalProcessSpanContext SpanContext spanContext = apmTracer.extract(Format.Builtin.TEXT_MAP, textMapExtractAdapter); assertThat(spanContext).isNotNull(); assertThat(spanContext.toTraceId()).isEqualTo(traceIdString); assertThat(spanContext.toSpanId()).isEqualTo(parentIdString); }
Example #2
Source File: OpenTracingAdapterTest.java From brave with Apache License 2.0 | 6 votes |
@Test public void injectTraceContext() { TraceContext context = TraceContext.newBuilder() .traceId(1L) .spanId(2L) .sampled(true).build(); Map<String, String> map = new LinkedHashMap<>(); TextMapAdapter request = new TextMapAdapter(map); opentracing.inject(new BraveSpanContext(context), Format.Builtin.HTTP_HEADERS, request); assertThat(map).containsExactly( entry("X-B3-TraceId", "0000000000000001"), entry("X-B3-SpanId", "0000000000000002"), entry("X-B3-Sampled", "1") ); }
Example #3
Source File: OpenTracingAdapterTest.java From brave with Apache License 2.0 | 6 votes |
@Test public void extractTraceContext() { Map<String, String> map = new LinkedHashMap<>(); map.put("X-B3-TraceId", "0000000000000001"); map.put("X-B3-SpanId", "0000000000000002"); map.put("X-B3-Sampled", "1"); map.put("User-Id", "sammy"); BraveSpanContext openTracingContext = opentracing.extract(Format.Builtin.HTTP_HEADERS, new TextMapAdapter(map)); assertThat(openTracingContext.context) .isEqualTo(TraceContext.newBuilder() .traceId(1L) .spanId(2L) .sampled(true).build()); assertThat(openTracingContext.baggageItems()) .containsExactly(entry(BAGGAGE_FIELD.name(), "sammy")); }
Example #4
Source File: OpenTracing0_33_BraveSpanTest.java From brave-opentracing with Apache License 2.0 | 6 votes |
@Test public void extractDoesntDropBaggage() { Map<String, String> carrier = new LinkedHashMap<>(); carrier.put("client-id", "aloha"); SpanContext extractedContext = tracer.extract(TEXT_MAP, new TextMapAdapter(carrier)); assertThat(extractedContext.baggageItems()) .contains(entry("client-id", "aloha")); Span serverSpan = tracer.buildSpan("foo") .asChildOf(extractedContext) .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER) .start(); assertThat(serverSpan.getBaggageItem("client-id")) .isEqualTo("aloha"); serverSpan.finish(); }
Example #5
Source File: ThriftProtocolAgentIntercept.java From java-specialagent with Apache License 2.0 | 6 votes |
public static void writeFieldStop(final Object protocol) throws TException { if (injected.get()) return; final TProtocol tProtocol = (TProtocol)protocol; final Span span = spanHolder.get(); if (span == null) return; final Map<String,String> map = new HashMap<>(); GlobalTracer.get().inject(span.context(), Builtin.TEXT_MAP, new TextMapAdapter(map)); tProtocol.writeFieldBegin(new TField("span", TType.MAP, SPAN_FIELD_ID)); tProtocol.writeMapBegin(new TMap(TType.STRING, TType.STRING, map.size())); for (final Entry<String,String> entry : map.entrySet()) { tProtocol.writeString(entry.getKey()); tProtocol.writeString(entry.getValue()); } tProtocol.writeMapEnd(); tProtocol.writeFieldEnd(); injected.set(true); }
Example #6
Source File: PulsarClientAgentIntercept.java From java-specialagent with Apache License 2.0 | 6 votes |
private static void buildConsumerSpan(final Consumer<?> consumer, final Message<?> message) { final Tracer tracer = GlobalTracer.get(); final SpanContext parentContext = tracer.extract(Builtin.TEXT_MAP, new TextMapAdapter(message.getProperties())); final SpanBuilder spanBuilder = tracer .buildSpan("receive") .withTag(Tags.COMPONENT, COMPONENT_NAME) .withTag(Tags.SPAN_KIND, Tags.SPAN_KIND_CONSUMER) .withTag("topic", consumer.getTopic()) .withTag("subscription", consumer.getSubscription()) .withTag(Tags.PEER_SERVICE, "pulsar"); if (parentContext != null) spanBuilder.addReference(References.FOLLOWS_FROM, parentContext); spanBuilder.start().finish(); }
Example #7
Source File: MockTracerTest.java From opentracing-java with Apache License 2.0 | 6 votes |
@Test public void testTextMapPropagatorHttpHeaders() { MockTracer tracer = new MockTracer(MockTracer.Propagator.TEXT_MAP); { Span parentSpan = tracer.buildSpan("foo") .start(); parentSpan.finish(); HashMap<String, String> injectMap = new HashMap<>(); tracer.inject(parentSpan.context(), Format.Builtin.HTTP_HEADERS, new TextMapAdapter(injectMap)); SpanContext extract = tracer.extract(Format.Builtin.HTTP_HEADERS, new TextMapAdapter(injectMap)); tracer.buildSpan("bar") .asChildOf(extract) .start() .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()); }
Example #8
Source File: TracingServerInterceptorTest.java From java-grpc with Apache License 2.0 | 6 votes |
@Test public void testGetSpanFromHeaders() { long traceID = 1; long spanID = 2; MockTracer spyTracer = spy(serverTracer); doReturn(new MockSpan.MockContext(traceID, spanID, Collections.<String, String>emptyMap())) .when(spyTracer) .extract(eq(Format.Builtin.HTTP_HEADERS), any(TextMapAdapter.class)); Span span = TracingServerInterceptor.newBuilder() .withTracer(spyTracer) .build() .getSpanFromHeaders(Collections.<String, String>emptyMap(), "operationName"); assertNotNull("span is not null", span); MockSpan mockSpan = (MockSpan) span; assertEquals( "span parentID is set to extracted span context spanID", spanID, mockSpan.parentId()); List<MockSpan.LogEntry> logEntries = mockSpan.logEntries(); assertTrue("span contains no log entries", logEntries.isEmpty()); }
Example #9
Source File: TracingServerInterceptorTest.java From java-grpc with Apache License 2.0 | 6 votes |
@Test public void testGetSpanFromHeadersError() { MockTracer spyTracer = spy(serverTracer); doThrow(IllegalArgumentException.class) .when(spyTracer) .extract(eq(Format.Builtin.HTTP_HEADERS), any(TextMapAdapter.class)); Span span = TracingServerInterceptor.newBuilder() .withTracer(spyTracer) .build() .getSpanFromHeaders(Collections.<String, String>emptyMap(), "operationName"); assertNotNull("span is not null", span); List<MockSpan.LogEntry> logEntries = ((MockSpan) span).logEntries(); assertEquals("span contains 1 log entry", 1, logEntries.size()); assertEquals( "span log contains error field", GrpcFields.ERROR, logEntries.get(0).fields().get(Fields.EVENT)); assertThat( "span log contains error.object field", logEntries.get(0).fields().get(Fields.ERROR_OBJECT), instanceOf(RuntimeException.class)); }
Example #10
Source File: TracingFilterTest.java From java-web-servlet-filter with Apache License 2.0 | 6 votes |
@Test public void testSpanContextPropagation() throws IOException { MockSpan foo = mockTracer.buildSpan("foo").start(); { Map<String, String> injectMap = new HashMap<>(); mockTracer.inject(foo.context(), Format.Builtin.HTTP_HEADERS, new TextMapAdapter(injectMap)); OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(localRequestUrl("/hello")) .headers(Headers.of(injectMap)) .build(); client.newCall(request).execute(); Awaitility.await().until(reportedSpansSize(), IsEqual.equalTo(1)); } List<MockSpan> mockSpans = mockTracer.finishedSpans(); Assert.assertEquals(1, mockSpans.size()); assertOnErrors(mockSpans); MockSpan mockSpan = mockSpans.get(0); Assert.assertEquals(foo.context().spanId(), mockSpan.parentId()); Assert.assertEquals(foo.context().traceId(), mockSpan.context().traceId()); }
Example #11
Source File: TracePreZuulFilter.java From java-spring-cloud with Apache License 2.0 | 6 votes |
@Override public Object run() { RequestContext ctx = RequestContext.getCurrentContext(); // span is a child of one created in servlet-filter Span span = tracer.buildSpan(ctx.getRequest().getMethod()) .withTag(Tags.COMPONENT.getKey(), COMPONENT_NAME) .start(); tracer.inject(span.context(), Format.Builtin.HTTP_HEADERS, new TextMapAdapter(ctx.getZuulRequestHeaders())); ctx.set(CONTEXT_SPAN_KEY, span); return null; }
Example #12
Source File: Tracing.java From opentracing-tutorial with Apache License 2.0 | 6 votes |
public static Span startServerSpan(Tracer tracer, javax.ws.rs.core.HttpHeaders httpHeaders, String operationName) { // format the headers for extraction MultivaluedMap<String, String> rawHeaders = httpHeaders.getRequestHeaders(); final HashMap<String, String> headers = new HashMap<String, String>(); for (String key : rawHeaders.keySet()) { headers.put(key, rawHeaders.get(key).get(0)); } Tracer.SpanBuilder spanBuilder; try { SpanContext parentSpanCtx = tracer.extract(Format.Builtin.HTTP_HEADERS, new TextMapAdapter(headers)); if (parentSpanCtx == null) { spanBuilder = tracer.buildSpan(operationName); } else { spanBuilder = tracer.buildSpan(operationName).asChildOf(parentSpanCtx); } } catch (IllegalArgumentException e) { spanBuilder = tracer.buildSpan(operationName); } // TODO could add more tags like http.url return spanBuilder.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER).start(); }
Example #13
Source File: LogCorrelationTest.java From opentracing-toolbox with MIT License | 6 votes |
@Test void shouldCleanup() { final TextMapAdapter carrier = new TextMapAdapter(new HashMap<>()); unit.inject(context("request-id", "okPur4VJWZiKzA"), TEXT_MAP_INJECT, carrier); final Span span = unit.buildSpan("test") .asChildOf(unit.extract(TEXT_MAP_EXTRACT, carrier)) .start(); unit.activateSpan(span).close(); assertNull(MDC.get("trace_id")); assertNull(MDC.get("span_id")); assertNull(MDC.get("request-id")); }
Example #14
Source File: OpenTracingTest.java From spring-cloud-sleuth with Apache License 2.0 | 5 votes |
@Test public void extractTraceContext() { Map<String, String> map = singletonMap("b3", "0000000000000001-0000000000000002-1"); BraveSpanContext openTracingContext = this.opentracing .extract(Format.Builtin.HTTP_HEADERS, new TextMapAdapter(map)); assertThat(openTracingContext.unwrap()).isEqualTo( TraceContext.newBuilder().traceId(1L).spanId(2L).sampled(true).build()); }
Example #15
Source File: OpenTracing0_33_BraveTracerTest.java From brave-opentracing with Apache License 2.0 | 5 votes |
@Test public void canUseCustomFormatKeys() { Map<String, String> map = new LinkedHashMap<>(); TextMapAdapter carrier = new TextMapAdapter(map); Format<TextMap> B3 = new Format<TextMap>() { }; opentracing = BraveTracer.newBuilder(brave) .textMapPropagation(B3, Propagation.B3_SINGLE_STRING).build(); opentracing.inject(BraveSpanContext.create(context), B3, carrier); assertThat(map).containsEntry("b3", "0000000000000001-0000000000000002-1"); assertExtractedContext(B3, new TextMapAdapter(map)); }
Example #16
Source File: MockTracerTest.java From opentracing-java with Apache License 2.0 | 5 votes |
@Test public void testDefaultConstructor() { MockTracer mockTracer = new MockTracer(); Span span = mockTracer.buildSpan("foo").start(); Scope scope = mockTracer.activateSpan(span); assertEquals(span, mockTracer.scopeManager().activeSpan()); Map<String, String> propag = new HashMap<>(); mockTracer.inject(span.context(), Format.Builtin.TEXT_MAP, new TextMapAdapter(propag)); assertFalse(propag.isEmpty()); }
Example #17
Source File: DefaultInjection.java From riptide with MIT License | 5 votes |
@Override public RequestArguments inject( final Tracer tracer, final RequestArguments arguments, final SpanContext context) { final Map<String, String> headers = new HashMap<>(); tracer.inject(context, HTTP_HEADERS, new TextMapAdapter(headers)); return arguments.withHeaders(Multimaps.forMap(headers).asMap()); }
Example #18
Source File: OpenTracing0_33_BraveTracerTest.java From brave-opentracing with Apache License 2.0 | 5 votes |
@Test public void extract_only_baggage() { Map<String, String> map = new LinkedHashMap<>(); map.put(countryCodeField.name(), "NO"); TextMapAdapter request = new TextMapAdapter(map); BraveSpanContext partial = opentracing.extract(Format.Builtin.HTTP_HEADERS, request); assertThat(partial).isNotNull(); BraveSpan span = opentracing.buildSpan("next").asChildOf(partial).start(); assertThat(span.getBaggageItem(countryCodeField.name())).isEqualTo("NO"); span.finish(); }
Example #19
Source File: OpenTracing0_33_BraveTracerTest.java From brave-opentracing with Apache License 2.0 | 5 votes |
@Test public void injectRemoteSpanTraceContext() { BraveSpan openTracingSpan = opentracing.buildSpan("encode") .withTag("lc", "codec") .withTag(Tags.SPAN_KIND, Tags.SPAN_KIND_PRODUCER) .withStartTimestamp(1L).start(); Map<String, String> map = new LinkedHashMap<>(); TextMapAdapter request = new TextMapAdapter(map); opentracing.inject(openTracingSpan.context(), Format.Builtin.HTTP_HEADERS, request); assertThat(map).containsOnlyKeys("b3"); openTracingSpan.unwrap().abandon(); }
Example #20
Source File: OpenTracing0_33_BraveTracerTest.java From brave-opentracing with Apache License 2.0 | 5 votes |
@Test @UseDataProvider("dataProviderExtractTextFormats") public void extractOnlySampled(Format format) { Map<String, String> map = new LinkedHashMap<>(); map.put("X-B3-Sampled", "1"); BraveSpanContext otContext = opentracing.extract(format, new TextMapAdapter(map)); assertThat(otContext.toTraceId()).isNull(); assertThat(otContext.toSpanId()).isNull(); assertThat(otContext.unwrap()).isNull(); }
Example #21
Source File: OpenTracingTest.java From spring-cloud-sleuth with Apache License 2.0 | 5 votes |
@Test public void extractBaggage() { Map<String, String> map = new LinkedHashMap<>(); map.put("b3", "0000000000000001-0000000000000002-1"); map.put("country-code", "FO"); BraveSpanContext openTracingContext = this.opentracing .extract(Format.Builtin.HTTP_HEADERS, new TextMapAdapter(map)); assertThat(openTracingContext.baggageItems()) .containsExactly(entry("country-code", "FO")); }
Example #22
Source File: OpenTracingTest.java From spring-cloud-sleuth with Apache License 2.0 | 5 votes |
@Test public void extractTraceContextTextMap() { Map<String, String> map = singletonMap("b3", "0000000000000001-0000000000000002-1"); BraveSpanContext openTracingContext = this.opentracing .extract(Format.Builtin.TEXT_MAP, new TextMapAdapter(map)); assertThat(openTracingContext.unwrap()).isEqualTo( TraceContext.newBuilder().traceId(1L).spanId(2L).sampled(true).build()); }
Example #23
Source File: OpenTracingTest.java From spring-cloud-sleuth with Apache License 2.0 | 5 votes |
@Test public void extractTraceContextCaseInsensitive() { Map<String, String> map = new LinkedHashMap<>(); map.put("B3", "0000000000000001-0000000000000002-1"); map.put("other", "1"); BraveSpanContext openTracingContext = this.opentracing .extract(Format.Builtin.HTTP_HEADERS, new TextMapAdapter(map)); assertThat(openTracingContext.unwrap()).isEqualTo( TraceContext.newBuilder().traceId(1L).spanId(2L).sampled(true).build()); }
Example #24
Source File: OpenTracingTest.java From spring-cloud-sleuth with Apache License 2.0 | 5 votes |
@Test public void injectTraceContext_baggage() { BraveSpan span = this.opentracing.buildSpan("foo").start(); span.setBaggageItem("country-code", "FO"); Map<String, String> map = new LinkedHashMap<>(); TextMapAdapter carrier = new TextMapAdapter(map); this.opentracing.inject(span.context(), Format.Builtin.HTTP_HEADERS, carrier); assertThat(map).containsEntry("country-code", "FO"); }
Example #25
Source File: WorkItem.java From batfish with Apache License 2.0 | 5 votes |
@VisibleForTesting void setSourceSpan(@Nullable Span activeSpan, Tracer tracer) { if (activeSpan == null) { return; } tracer.inject(activeSpan.context(), Builtin.TEXT_MAP, new TextMapAdapter(_spanData)); }
Example #26
Source File: OpenTracingTracer.java From qpid-jms with Apache License 2.0 | 5 votes |
private SpanContext extract(TraceableMessage message) { SpanContext spanContext = null; @SuppressWarnings("unchecked") Map<String, String> headers = (Map<String, String>) message.getTracingAnnotation(ANNOTATION_KEY); if(headers != null && !headers.isEmpty()) { spanContext = tracer.extract(Format.Builtin.TEXT_MAP, new TextMapAdapter(headers)); } if(spanContext != null) { message.setTracingContext(ARRIVING_SPAN_CTX_CONTEXT_KEY, spanContext); } return spanContext; }
Example #27
Source File: AbstractOpenTracingProvider.java From cxf with Apache License 2.0 | 5 votes |
protected TraceScopeHolder<TraceScope> startTraceSpan(final Map<String, List<String>> requestHeaders, URI uri, String method) { SpanContext parent = tracer.extract(Builtin.HTTP_HEADERS, new TextMapAdapter( requestHeaders .entrySet() .stream() .collect(Collectors.toMap(Map.Entry::getKey, this::getFirstValueOrEmpty)) )); Span activeSpan = null; Scope scope = null; if (parent == null) { activeSpan = tracer.buildSpan(buildSpanDescription(uri.getPath(), method)).start(); scope = tracer.scopeManager().activate(activeSpan); } else { activeSpan = tracer.buildSpan(buildSpanDescription(uri.getPath(), method)).asChildOf(parent).start(); scope = tracer.scopeManager().activate(activeSpan); } // Set additional tags activeSpan.setTag(Tags.HTTP_METHOD.getKey(), method); activeSpan.setTag(Tags.HTTP_URL.getKey(), uri.toString()); // If the service resource is using asynchronous processing mode, the trace // scope will be closed in another thread and as such should be detached. Span span = null; if (isAsyncResponse()) { // Do not modify the current context span span = activeSpan; propagateContinuationSpan(span); scope.close(); } return new TraceScopeHolder<TraceScope>(new TraceScope(activeSpan, scope), span != null); }
Example #28
Source File: OpenTracingAdapterTest.java From brave with Apache License 2.0 | 5 votes |
@Test public void injectRemoteSpanTraceContext() { BraveSpan openTracingSpan = opentracing.buildSpan("encode") .withTag("lc", "codec") .withTag(Tags.SPAN_KIND, Tags.SPAN_KIND_PRODUCER) .withStartTimestamp(1L).start(); Map<String, String> map = new LinkedHashMap<>(); TextMapAdapter request = new TextMapAdapter(map); opentracing.inject(openTracingSpan.context(), Format.Builtin.HTTP_HEADERS, request); assertThat(map).containsOnlyKeys("b3"); openTracingSpan.unwrap().abandon(); }
Example #29
Source File: OpenTracing0_33_BraveTracerTest.java From brave-opentracing with Apache License 2.0 | 5 votes |
@Test @UseDataProvider("dataProviderInjectTextFormats") public void injectTraceContext(Format format) { Map<String, String> map = new LinkedHashMap<>(); TextMapAdapter carrier = new TextMapAdapter(map); opentracing.inject(BraveSpanContext.create(context), format, carrier); assertThat(map).containsExactly( entry("X-B3-TraceId", "0000000000000001"), entry("X-B3-SpanId", "0000000000000002"), entry("X-B3-Sampled", "1") ); }
Example #30
Source File: OpenTracingBridgeTest.java From apm-agent-java with Apache License 2.0 | 5 votes |
@Test void testInjectExtract() { final String traceId = "0af7651916cd43dd8448eb211c80319c"; final String parentId = "b9c7c989f97918e1"; Span otSpan = apmTracer.buildSpan("span") .asChildOf(apmTracer.extract(Format.Builtin.TEXT_MAP, new TextMapAdapter(Map.of( TraceContext.W3C_TRACE_PARENT_TEXTUAL_HEADER_NAME, "00-" + traceId + "-" + parentId + "-01", "User-Agent", "curl")))) .start(); final Scope scope = apmTracer.activateSpan(otSpan); Transaction transaction = tracer.currentTransaction(); assertThat(transaction).isNotNull(); assertThat(transaction.isSampled()).isTrue(); assertThat(transaction.getTraceContext().getTraceId().toString()).isEqualTo(traceId); assertThat(transaction.getTraceContext().getParentId().toString()).isEqualTo(parentId); Span span = apmTracer.activeSpan(); assertThat(span).isNotNull(); assertThat(span.getBaggageItem("User-Agent")).isNull(); final HashMap<String, String> map = new HashMap<>(); apmTracer.inject(otSpan.context(), Format.Builtin.TEXT_MAP, new TextMapAdapter(map)); final TraceContext injectedContext = TraceContext.with64BitId(tracer); assertThat(TraceContext.<Map<String, String>>getFromTraceContextTextHeaders().asChildOf(injectedContext, map, TextHeaderMapAccessor.INSTANCE)).isTrue(); assertThat(injectedContext.getTraceId().toString()).isEqualTo(traceId); assertThat(injectedContext.getParentId()).isEqualTo(transaction.getTraceContext().getId()); assertThat(injectedContext.isSampled()).isTrue(); assertThat(map.get("User-Agent")).isNull(); scope.close(); otSpan.finish(); assertThat(reporter.getTransactions()).hasSize(1); }