@Override public List<String> getServiceNames() { tracer.startNewSpan(component, "get-service-names"); try { return delegate.getServiceNames(); } finally { tracer.finishSpan(); } }
@Override public List<List<Span>> getTracesByIds(List<Long> traceIds) { tracer.startNewSpan(component, "get-traces-by-ids"); try { return delegate.getTracesByIds(traceIds); } finally { tracer.finishSpan(); } }
@Override public List<String> getSpanNames(String serviceName) { tracer.startNewSpan(component, "get-span-names"); try { return delegate.getSpanNames(serviceName); } finally { tracer.finishSpan(); } }
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); } }
@Override public List<DependencyLink> getDependencies(long endTs, @Nullable Long lookback) { tracer.startNewSpan(component, "get-dependencies"); try { return delegate.getDependencies(endTs, lookback); } finally { tracer.finishSpan(); } } }
/** * Completes the span, assigning the most precise duration possible. */ public void finishSpan() { long endTick = System.nanoTime(); Span span = spanAndEndpoint().span(); if (span == null) return; Long startTick = span.startTick; final long duration; if (startTick != null) { duration = (endTick - startTick) / 1000; } else { duration = currentTimeMicroseconds() - span.getTimestamp(); } finishSpan(duration); }
@Override public List<List<Span>> getTraces(QueryRequest request) { if (tracer.startNewSpan(component, "get-traces") != null) { tracer.submitBinaryAnnotation("request", request.toString()); } try { return delegate.getTraces(request); } finally { tracer.finishSpan(); } }
@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(); } }