public void addSpanIdToRequest(@Nullable SpanId spanId) { Map<String,String> at = this.invocation.getAttachments(); if (spanId == null) { at.put("Sampled", "0"); } else { at.put("Sampled", "1"); at.put("TraceId", spanId.traceIdString()); at.put("SpanId", IdConversion.convertToString(spanId.spanId)); if (spanId.nullableParentId() != null) { at.put("ParentSpanId", IdConversion.convertToString(spanId.parentId)); } } }
@Test public void usesExistingTraceId_local() throws Exception { server.enqueue(new MockResponse()); SpanId parent = brave.localTracer().startNewSpan(getClass().getSimpleName(), "test"); try { get(client, "/foo"); } finally { brave.localTracer().finishSpan(); } RecordedRequest request = server.takeRequest(); assertThat(request.getHeader("x-b3-traceId")) .isEqualTo(parent.traceIdString()); assertThat(request.getHeader("x-b3-parentspanid")) .endsWith(IdConversion.convertToString(parent.spanId)); }
/** * This tests that the parent is determined at the time the request was made, not when the request * was executed. */ @Test public void usesParentFromInvocationTime_local() throws Exception { server.enqueue(new MockResponse().setBodyDelay(1, TimeUnit.SECONDS)); server.enqueue(new MockResponse()); SpanId parent = brave.localTracer().startNewSpan(getClass().getSimpleName(), "test"); try { getAsync(client, "/foo"); getAsync(client, "/foo"); } finally { brave.localTracer().finishSpan(); } // changing the local span after the fact! brave.localTracer().startNewSpan(getClass().getSimpleName(), "test"); try { for (int i = 0; i < 2; i++) { RecordedRequest request = server.takeRequest(); assertThat(request.getHeader("x-b3-traceId")) .isEqualTo(parent.traceIdString()); assertThat(request.getHeader("x-b3-parentspanid")) .endsWith(IdConversion.convertToString(parent.spanId)); } } finally { brave.localTracer().finishSpan(); } }
@Override public void addSpanIdToRequest(@Nullable SpanId spanId) { if (spanId == null) { request.addHeader(BraveHttpHeaders.Sampled.getName(), "0"); } else { request.addHeader(BraveHttpHeaders.Sampled.getName(), "1"); request.addHeader(BraveHttpHeaders.TraceId.getName(), spanId.traceIdString()); request.addHeader(BraveHttpHeaders.SpanId.getName(), IdConversion.convertToString(spanId.spanId)); if (spanId.nullableParentId() != null) { request.addHeader(BraveHttpHeaders.ParentSpanId.getName(), IdConversion.convertToString(spanId.parentId)); } } }