Search in sources :

Example 1 with TraceContextOrSamplingFlags

use of brave.propagation.TraceContextOrSamplingFlags in project brave by openzipkin.

the class AWSPropagationTest method extract_containsMarker.

@Test
public void extract_containsMarker() {
    carrier.put("x-amzn-trace-id", sampledTraceId);
    TraceContextOrSamplingFlags extracted = extractor.extract(carrier);
    assertThat(extracted.context().extra()).containsExactly(AWSPropagation.MARKER);
}
Also used : TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) Test(org.junit.Test)

Example 2 with TraceContextOrSamplingFlags

use of brave.propagation.TraceContextOrSamplingFlags in project brave by openzipkin.

the class AWSPropagationTest method extract_fail_containsMarker.

/**
 * If invoked extract, a 128-bit trace ID will be created, compatible with AWS format
 */
@Test
public void extract_fail_containsMarker() {
    TraceContextOrSamplingFlags extracted = extractor.extract(carrier);
    assertThat(extracted.extra()).containsExactly(AWSPropagation.MARKER);
}
Also used : TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) Test(org.junit.Test)

Example 3 with TraceContextOrSamplingFlags

use of brave.propagation.TraceContextOrSamplingFlags in project brave by openzipkin.

the class AWSPropagationTest method contextWithPassThrough.

TraceContext contextWithPassThrough() {
    extractor = ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "x-amzn-trace-id").create(Propagation.KeyFactory.STRING).extractor(Map::get);
    TraceContextOrSamplingFlags extracted = extractor.extract(carrier);
    // sanity check
    assertThat(extracted.samplingFlags()).isEqualTo(SamplingFlags.EMPTY);
    assertThat(extracted.extra()).isNotEmpty();
    // Make a context that wasn't from AWSPropagation
    return TraceContext.newBuilder().traceId(1L).spanId(2L).sampled(true).extra(extracted.extra()).build();
}
Also used : TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags)

Example 4 with TraceContextOrSamplingFlags

use of brave.propagation.TraceContextOrSamplingFlags in project brave by openzipkin.

the class AWSPropagationTest method extract_skipsSelfField.

/**
 * Shows we skip whitespace and extra fields like self or custom ones
 */
// https://aws.amazon.com/blogs/aws/application-performance-percentiles-and-request-tracing-for-aws-application-load-balancer/
@Test
public void extract_skipsSelfField() {
    // TODO: check with AWS if it is valid to have arbitrary fields in front of standard ones.
    // we currently permit them
    carrier.put("x-amzn-trace-id", "Robot=Hello;Self=1-582113d1-1e48b74b3603af8479078ed6;  " + "Root=1-58211399-36d228ad5d99923122bbe354;  " + "TotalTimeSoFar=112ms;CalledFrom=Foo");
    TraceContextOrSamplingFlags extracted = extractor.extract(carrier);
    assertThat(extracted.traceIdContext()).isEqualTo(TraceIdContext.newBuilder().traceIdHigh(lowerHexToUnsignedLong("5821139936d228ad")).traceId(lowerHexToUnsignedLong("5d99923122bbe354")).build());
    assertThat(((AWSPropagation.Extra) extracted.extra().get(0)).fields).contains(new StringBuilder(";Robot=Hello;TotalTimeSoFar=112ms;CalledFrom=Foo"));
}
Also used : TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) Test(org.junit.Test)

Example 5 with TraceContextOrSamplingFlags

use of brave.propagation.TraceContextOrSamplingFlags in project brave by openzipkin.

the class TracingFilter method invoke.

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    if (tracer == null)
        return invoker.invoke(invocation);
    RpcContext rpcContext = RpcContext.getContext();
    Kind kind = rpcContext.isProviderSide() ? Kind.SERVER : Kind.CLIENT;
    final Span span;
    if (kind.equals(Kind.CLIENT)) {
        span = tracer.nextSpan();
        injector.inject(span.context(), invocation.getAttachments());
    } else {
        TraceContextOrSamplingFlags extracted = extractor.extract(invocation.getAttachments());
        span = extracted.context() != null ? tracer.joinSpan(extracted.context()) : tracer.nextSpan(extracted);
    }
    if (!span.isNoop()) {
        span.kind(kind).start();
        String service = invoker.getInterface().getSimpleName();
        String method = RpcUtils.getMethodName(invocation);
        span.kind(kind);
        span.name(service + "/" + method);
        InetSocketAddress remoteAddress = rpcContext.getRemoteAddress();
        Endpoint.Builder remoteEndpoint = Endpoint.newBuilder().port(remoteAddress.getPort());
        if (!remoteEndpoint.parseIp(remoteAddress.getAddress())) {
            remoteEndpoint.parseIp(remoteAddress.getHostName());
        }
        span.remoteEndpoint(remoteEndpoint.build());
    }
    boolean isOneway = false, deferFinish = false;
    try (Tracer.SpanInScope scope = tracer.withSpanInScope(span)) {
        Result result = invoker.invoke(invocation);
        if (result.hasException()) {
            onError(result.getException(), span.customizer());
        }
        isOneway = RpcUtils.isOneway(invoker.getUrl(), invocation);
        // the case on async client invocation
        Future<Object> future = rpcContext.getFuture();
        if (future instanceof FutureAdapter) {
            deferFinish = true;
            ((FutureAdapter) future).getFuture().setCallback(new FinishSpanCallback(span));
        }
        return result;
    } catch (Error | RuntimeException e) {
        onError(e, span.customizer());
        throw e;
    } finally {
        if (isOneway) {
            span.flush();
        } else if (!deferFinish) {
            span.finish();
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Tracer(brave.Tracer) FutureAdapter(com.alibaba.dubbo.rpc.protocol.dubbo.FutureAdapter) Span(brave.Span) Result(com.alibaba.dubbo.rpc.Result) RpcContext(com.alibaba.dubbo.rpc.RpcContext) Endpoint(zipkin2.Endpoint) Kind(brave.Span.Kind) TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags)

Aggregations

TraceContextOrSamplingFlags (brave.propagation.TraceContextOrSamplingFlags)27 Test (org.junit.Test)13 Span (brave.Span)10 ThreadLocalSpan (brave.propagation.ThreadLocalSpan)4 TraceContext (brave.propagation.TraceContext)4 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)4 StrictCurrentTraceContext (brave.propagation.StrictCurrentTraceContext)3 GenericMessage (org.springframework.messaging.support.GenericMessage)3 Tracer (brave.Tracer)2 TraceIdContext (brave.propagation.TraceIdContext)2 LinkedHashMap (java.util.LinkedHashMap)2 Endpoint (zipkin2.Endpoint)2 Kind (brave.Span.Kind)1 SpanInScope (brave.Tracer.SpanInScope)1 Result (com.alibaba.dubbo.rpc.Result)1 RpcContext (com.alibaba.dubbo.rpc.RpcContext)1 FutureAdapter (com.alibaba.dubbo.rpc.protocol.dubbo.FutureAdapter)1 SimpleForwardingServerCall (io.grpc.ForwardingServerCall.SimpleForwardingServerCall)1 ServerCall (io.grpc.ServerCall)1 TextMap (io.opentracing.propagation.TextMap)1