public TraceWritesSpanStore(Brave brave, SpanStore delegate) { this.tracer = brave.localTracer(); this.delegate = delegate; this.component = delegate.getClass().getSimpleName(); }
public void record(ApplicationEvent event) { annotations.put(event.getClass().getSimpleName().replace("Event", ""), timestamp + microsSinceInit()); // record duration and flush the trace if we're done if (event instanceof ApplicationReadyEvent) { long duration = microsSinceInit(); // get duration now, as below logic might skew things. ApplicationReadyEvent ready = (ApplicationReadyEvent) event; LocalTracer tracer = ready.getApplicationContext().getBeanFactory() .getBean(Brave.class).localTracer(); tracer.startNewSpan("spring-boot", "bootstrap", timestamp); annotations.forEach(tracer::submitAnnotation); tracer.finishSpan(duration); } }
@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(); } }