use of brave.Tracer in project brave by openzipkin.
the class ITHttpClient method propagatesExtra_unsampledTrace.
@Test
public void propagatesExtra_unsampledTrace() throws Exception {
Tracer tracer = httpTracing.tracing().tracer();
server.enqueue(new MockResponse());
brave.Span parent = tracer.newTrace(SamplingFlags.NOT_SAMPLED).name("test").start();
try (SpanInScope ws = tracer.withSpanInScope(parent)) {
ExtraFieldPropagation.set(parent.context(), EXTRA_KEY, "joey");
get(client, "/foo");
} finally {
parent.finish();
}
assertThat(server.takeRequest().getHeader(EXTRA_KEY)).isEqualTo("joey");
}
use of brave.Tracer in project brave by openzipkin.
the class Log4JThreadContextTest method customCurrentTraceContext.
/**
* One common request is to support SLF4J or Log4J2 log correlation. This shows you can make a
* custom implementation
*/
@Test
public void customCurrentTraceContext() {
assertThat(ThreadContext.get("traceID")).isNull();
Tracer tracer = Tracing.newBuilder().currentTraceContext(new Log4J2CurrentTraceContext()).build().tracer();
Span parent = tracer.newTrace();
try (Tracer.SpanInScope wsParent = tracer.withSpanInScope(parent)) {
// the trace id is now in the logging context
assertThat(ThreadContext.get("traceId")).isEqualTo(parent.context().traceIdString());
// Clear a scope temporarily
try (Tracer.SpanInScope noScope = tracer.withSpanInScope(null)) {
assertThat(tracer.currentSpan()).isNull();
}
Span child = tracer.newChild(parent.context());
try (Tracer.SpanInScope wsChild = tracer.withSpanInScope(child)) {
// nesting worked
assertThat(ThreadContext.get("traceId")).isEqualTo(child.context().traceIdString());
}
// old parent reverted
assertThat(ThreadContext.get("traceId")).isEqualTo(parent.context().traceIdString());
}
assertThat(ThreadContext.get("traceId")).isNull();
Tracing.current().close();
}
use of brave.Tracer in project spring-cloud-gcp by spring-cloud.
the class StackdriverTraceAutoConfigurationTests method test.
@Test
public void test() {
this.contextRunner.run(context -> {
SleuthProperties sleuthProperties = context.getBean(SleuthProperties.class);
assertThat(sleuthProperties.isTraceId128()).isTrue();
assertThat(sleuthProperties.isSupportsJoin()).isFalse();
Reporter<zipkin2.Span> reporter = context.getBean(Reporter.class);
assertThat(reporter).isInstanceOf(StackdriverTraceReporter.class);
Tracer tracer = context.getBean(Tracer.class);
Span span = tracer.newTrace().start().kind(Span.Kind.CLIENT).name("test").start();
span.finish();
// There should be one trace received
MockConfiguration configuration = context.getBean(MockConfiguration.class);
assertThat(configuration.tracesList.size()).isEqualTo(1);
Traces traces = configuration.tracesList.get(0);
assertThat(traces.getTracesCount()).isEqualTo(1);
Trace trace = traces.getTraces(0);
assertThat(trace.getSpansCount()).isEqualTo(1);
TraceSpan traceSpan = trace.getSpans(0);
});
}
use of brave.Tracer in project spring-cloud-sleuth by spring-cloud.
the class WebClientTests method shouldPropagateNotSamplingHeader.
@Test
@Parameters
@SuppressWarnings("unchecked")
public void shouldPropagateNotSamplingHeader(ResponseEntityProvider provider) {
Span span = this.tracer.nextSpan(TraceContextOrSamplingFlags.create(SamplingFlags.NOT_SAMPLED)).name("foo").start();
try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(span)) {
ResponseEntity<Map<String, String>> response = provider.get(this);
then(response.getBody().get(TRACE_ID_NAME.toLowerCase())).isNotNull();
then(response.getBody().get(SAMPLED_NAME.toLowerCase())).isEqualTo("0");
} finally {
span.finish();
}
then(this.reporter.getSpans()).isEmpty();
then(Tracing.current().tracer().currentSpan()).isNull();
}
use of brave.Tracer in project spring-cloud-sleuth by spring-cloud.
the class TraceCommandTests method should_pass_tracing_information_when_using_Hystrix_commands.
@Test
public void should_pass_tracing_information_when_using_Hystrix_commands() {
Tracer tracer = this.tracer;
TraceKeys traceKeys = new TraceKeys();
HystrixCommand.Setter setter = withGroupKey(asKey("group")).andCommandKey(HystrixCommandKey.Factory.asKey("command"));
// tag::hystrix_command[]
HystrixCommand<String> hystrixCommand = new HystrixCommand<String>(setter) {
@Override
protected String run() throws Exception {
return someLogic();
}
};
// end::hystrix_command[]
// tag::trace_hystrix_command[]
TraceCommand<String> traceCommand = new TraceCommand<String>(tracer, traceKeys, setter) {
@Override
public String doRun() throws Exception {
return someLogic();
}
};
// end::trace_hystrix_command[]
String resultFromHystrixCommand = hystrixCommand.execute();
String resultFromTraceCommand = traceCommand.execute();
then(resultFromHystrixCommand).isEqualTo(resultFromTraceCommand);
}
Aggregations