use of brave.propagation.TraceContext in project brave by openzipkin.
the class ITTracingCallFactory method currentSpanVisibleToUserInterceptors.
@Test
public void currentSpanVisibleToUserInterceptors() throws IOException {
server.enqueue(new MockResponse());
closeClient(client);
client = TracingCallFactory.create(httpTracing, new OkHttpClient.Builder().addInterceptor(chain -> chain.proceed(chain.request().newBuilder().addHeader("my-id", currentTraceContext.get().traceIdString()).build())).dispatcher(dispatcher).build());
TraceContext parent = newTraceContext(SamplingFlags.SAMPLED);
try (Scope scope = currentTraceContext.newScope(parent)) {
get(client, "/foo");
}
RecordedRequest request = takeRequest();
assertThat(request.getHeader("x-b3-traceId")).isEqualTo(request.getHeader("my-id"));
testSpanHandler.takeRemoteSpan(Span.Kind.CLIENT);
}
use of brave.propagation.TraceContext in project brave by openzipkin.
the class TracingFilter method invoke.
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
if (!isInit)
return invoker.invoke(invocation);
TraceContext invocationContext = currentTraceContext.get();
RpcContext rpcContext = RpcContext.getContext();
Kind kind = rpcContext.isProviderSide() ? Kind.SERVER : Kind.CLIENT;
Span span;
DubboRequest request;
if (kind.equals(Kind.CLIENT)) {
// When A service invoke B service, then B service then invoke C service, the parentId of the
// C service span is A when read from invocation.getAttachments(). This is because
// AbstractInvoker adds attachments via RpcContext.getContext(), not the invocation.
// See com.alibaba.dubbo.rpc.protocol.AbstractInvoker(line 138) from v2.6.7
Map<String, String> attachments = RpcContext.getContext().getAttachments();
DubboClientRequest clientRequest = new DubboClientRequest(invoker, invocation, attachments);
request = clientRequest;
span = clientHandler.handleSendWithParent(clientRequest, invocationContext);
} else {
DubboServerRequest serverRequest = new DubboServerRequest(invoker, invocation);
request = serverRequest;
span = serverHandler.handleReceive(serverRequest);
}
boolean isSynchronous = true;
Scope scope = currentTraceContext.newScope(span.context());
Result result = null;
Throwable error = null;
try {
result = invoker.invoke(invocation);
error = result.getException();
// the case on async client invocation
Future<Object> future = rpcContext.getFuture();
if (future != null) {
if (!(future instanceof FutureAdapter)) {
assert false : "we can't defer the span finish unless we can access the ResponseFuture";
return result;
}
isSynchronous = false;
ResponseFuture original = ((FutureAdapter<Object>) future).getFuture();
// See instrumentation/RATIONALE.md for why the below response callbacks are invocation context
TraceContext callbackContext = kind == Kind.CLIENT ? invocationContext : span.context();
ResponseFuture wrapped = new FinishSpanResponseFuture(original, this, request, result, span, callbackContext);
RpcContext.getContext().setFuture(new FutureAdapter<>(wrapped));
}
return result;
} catch (Throwable e) {
propagateIfFatal(e);
error = e;
throw e;
} finally {
if (isSynchronous)
FinishSpan.finish(this, request, result, error, span);
scope.close();
}
}
use of brave.propagation.TraceContext in project brave by openzipkin.
the class ITTracingFilter_Provider method customParser.
@Test
public void customParser() {
Tag<DubboResponse> javaValue = new Tag<DubboResponse>("dubbo.result_value") {
@Override
protected String parseValue(DubboResponse input, TraceContext context) {
Result result = input.result();
if (result == null)
return null;
Object value = result.getValue();
if (value instanceof JavaBeanDescriptor) {
return String.valueOf(((JavaBeanDescriptor) value).getProperty("value"));
}
return null;
}
};
RpcTracing rpcTracing = RpcTracing.newBuilder(tracing).serverResponseParser((res, context, span) -> {
RpcResponseParser.DEFAULT.parse(res, context, span);
if (res instanceof DubboResponse) {
javaValue.tag((DubboResponse) res, span);
}
}).build();
init().setRpcTracing(rpcTracing);
String javaResult = client.get().sayHello("jorge");
assertThat(testSpanHandler.takeRemoteSpan(SERVER).tags()).containsEntry("dubbo.result_value", javaResult);
}
use of brave.propagation.TraceContext in project brave by openzipkin.
the class ITTracingFilter_Provider method createsChildWhenJoinDisabled.
@Test
public void createsChildWhenJoinDisabled() {
tracing = tracingBuilder(NEVER_SAMPLE).supportsJoin(false).build();
init();
TraceContext parent = newTraceContext(SamplingFlags.SAMPLED);
RpcContext.getContext().getAttachments().put("b3", B3SingleFormat.writeB3SingleFormat(parent));
client.get().sayHello("jorge");
assertChildOf(testSpanHandler.takeRemoteSpan(SERVER), parent);
}
use of brave.propagation.TraceContext in project brave by openzipkin.
the class ITTracingFilter_Consumer method propagatesUnsampledContext.
/**
* Unlike Brave 3, Brave 4 propagates trace ids even when unsampled
*/
@Test
public void propagatesUnsampledContext() {
TraceContext parent = newTraceContext(SamplingFlags.NOT_SAMPLED);
try (Scope scope = currentTraceContext.newScope(parent)) {
client.get().sayHello("jorge");
}
TraceContext extracted = server.takeRequest().context();
assertThat(extracted.sampled()).isFalse();
assertChildOf(extracted, parent);
}
Aggregations