Search in sources :

Example 91 with TraceContext

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);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) OkHttpClient(okhttp3.OkHttpClient) Scope(brave.propagation.CurrentTraceContext.Scope) TraceContext(brave.propagation.TraceContext) Test(org.junit.Test)

Example 92 with TraceContext

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();
    }
}
Also used : FutureAdapter(com.alibaba.dubbo.rpc.protocol.dubbo.FutureAdapter) ResponseFuture(com.alibaba.dubbo.remoting.exchange.ResponseFuture) Span(brave.Span) Result(com.alibaba.dubbo.rpc.Result) RpcContext(com.alibaba.dubbo.rpc.RpcContext) Scope(brave.propagation.CurrentTraceContext.Scope) Kind(brave.Span.Kind) CurrentTraceContext(brave.propagation.CurrentTraceContext) TraceContext(brave.propagation.TraceContext)

Example 93 with TraceContext

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);
}
Also used : ReferenceConfig(com.alibaba.dubbo.config.ReferenceConfig) Result(com.alibaba.dubbo.rpc.Result) RpcResponseParser(brave.rpc.RpcResponseParser) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ALWAYS_SAMPLE(brave.sampler.Sampler.ALWAYS_SAMPLE) JavaBeanDescriptor(com.alibaba.dubbo.common.beanutil.JavaBeanDescriptor) ApplicationConfig(com.alibaba.dubbo.config.ApplicationConfig) Test(org.junit.Test) TraceContext(brave.propagation.TraceContext) RpcTracing(brave.rpc.RpcTracing) B3SingleFormat(brave.propagation.B3SingleFormat) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) RpcRuleSampler(brave.rpc.RpcRuleSampler) NEVER_SAMPLE(brave.sampler.Sampler.NEVER_SAMPLE) Tag(brave.Tag) RpcContext(com.alibaba.dubbo.rpc.RpcContext) SamplingFlags(brave.propagation.SamplingFlags) SERVER(brave.Span.Kind.SERVER) RpcRequestMatchers.methodEquals(brave.rpc.RpcRequestMatchers.methodEquals) RpcRequestMatchers.serviceEquals(brave.rpc.RpcRequestMatchers.serviceEquals) Before(org.junit.Before) JavaBeanDescriptor(com.alibaba.dubbo.common.beanutil.JavaBeanDescriptor) TraceContext(brave.propagation.TraceContext) RpcTracing(brave.rpc.RpcTracing) Tag(brave.Tag) Result(com.alibaba.dubbo.rpc.Result) Test(org.junit.Test)

Example 94 with TraceContext

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);
}
Also used : TraceContext(brave.propagation.TraceContext) Test(org.junit.Test)

Example 95 with TraceContext

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);
}
Also used : Scope(brave.propagation.CurrentTraceContext.Scope) TraceContext(brave.propagation.TraceContext) Test(org.junit.Test)

Aggregations

TraceContext (brave.propagation.TraceContext)200 Test (org.junit.Test)163 CurrentTraceContext (brave.propagation.CurrentTraceContext)77 Scope (brave.propagation.CurrentTraceContext.Scope)52 StrictCurrentTraceContext (brave.propagation.StrictCurrentTraceContext)52 MutableSpan (brave.handler.MutableSpan)38 Span (brave.Span)17 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)13 MockResponse (okhttp3.mockwebserver.MockResponse)12 SpanHandler (brave.handler.SpanHandler)9 Clock (brave.Clock)7 SamplingFlags (brave.propagation.SamplingFlags)7 TraceContextOrSamplingFlags (brave.propagation.TraceContextOrSamplingFlags)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 TestSpanHandler (brave.test.TestSpanHandler)6 AssertableCallback (brave.test.util.AssertableCallback)6 Message (javax.jms.Message)6 After (org.junit.After)6