/** Creates a potentially noop span representing this request */ Span nextSpan(TraceContextOrSamplingFlags extracted, Req request) { Boolean sampled = extracted.sampled(); // only recreate the context if the http sampler made a decision if (sampled == null && (sampled = sampler.trySample(adapter, request)) != null) { extracted = extracted.sampled(sampled.booleanValue()); } return extracted.context() != null ? tracer.joinSpan(extracted.context()) : tracer.nextSpan(extracted); }
/** Creates a potentially noop span representing this request */ // copy/pasted from HttpServerHandler.nextSpan Span nextSpan(TraceContextOrSamplingFlags extracted, HttpRequest request) { Boolean sampled = extracted.sampled(); // only recreate the context if the http sampler made a decision if (sampled == null && (sampled = sampler.trySample(adapter, request)) != null) { extracted = extracted.sampled(sampled.booleanValue()); } return extracted.context() != null ? tracer.joinSpan(extracted.context()) : tracer.nextSpan(extracted); }
void joinWithSpanInScope(Tracer tracer, TraceContext context) { Span span = tracer.joinSpan(context).name("encode").start(); try (Tracer.SpanInScope scope = tracer.withSpanInScope(span)) { span.tag("foo", "bar"); span.tag("baz", "qux"); } finally { span.finish(); } }
@Override public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(final ServerCall<ReqT, RespT> call, final Metadata headers, final ServerCallHandler<ReqT, RespT> next) { TraceContextOrSamplingFlags extracted = extractor.extract(headers); Span span = extracted.context() != null ? tracer.joinSpan(extracted.context()) : tracer.nextSpan(extracted); // If grpc propagation is enabled, make sure we refresh the server method if (grpcPropagationFormatEnabled) { Tags tags = span.context().findExtra(Tags.class); if (tags != null) tags.put(RPC_METHOD, call.getMethodDescriptor().getFullMethodName()); } span.kind(Span.Kind.SERVER); parser.onStart(call, headers, span.customizer()); // startCall invokes user interceptors, so we place the span in scope here ServerCall.Listener<ReqT> result; SpanInScope scope = tracer.withSpanInScope(span); try { // retrolambda can't resolve this try/finally result = next.startCall(new TracingServerCall<>(span, call, parser), headers); } catch (RuntimeException | Error e) { span.error(e); span.finish(); throw e; } finally { scope.close(); } // This ensures the server implementation can see the span in scope return new ScopingServerCallListener<>(tracer, span, result, parser); }
@Override public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { final TraceContextOrSamplingFlags contextOrFlags = extractor.extract(req.headers()); final Span span = contextOrFlags.context() != null ? tracer.joinSpan(contextOrFlags.context()) : tracer.nextSpan(contextOrFlags); // For no-op spans, nothing special to do. if (span.isNoop()) { return delegate().serve(ctx, req); } final String method = ctx.method().name(); span.kind(Kind.SERVER).name(method); ctx.log().addListener(log -> SpanContextUtil.startSpan(span, log), RequestLogAvailability.REQUEST_START); // Ensure the trace context propagates to children ctx.onChild(RequestContextCurrentTraceContext::copy); ctx.log().addListener(log -> { SpanTags.logWireReceive(span, log.requestFirstBytesTransferredTimeNanos(), log); SpanTags.logWireSend(span, log.responseFirstBytesTransferredTimeNanos(), log); SpanContextUtil.closeSpan(span, log); }, RequestLogAvailability.COMPLETE); try (SpanInScope ignored = tracer.withSpanInScope(span)) { return delegate().serve(ctx, req); } } }
TraceContextOrSamplingFlags extracted = extractor.extract(invocation.getAttachments()); span = extracted.context() != null ? tracer.joinSpan(extracted.context()) : tracer.nextSpan(extracted);
@Override Span joinSpan(SpanId spanId) { TraceContext context = toTraceContext(spanId); return Brave.toSpan(toSpanId(tracer.joinSpan(context).context())); } }
span = server ? braveTracer.joinSpan(context) : braveTracer.newChild(context); } else { span = braveTracer.nextSpan(((BraveSpanContext.Incomplete) reference).extractionResult());
@Override public void createChildSpan(TracingWrapper<ZipkinWrapper> parent) { operationName = UUID.randomUUID().toString().replace("-",""); if (parent == null) { // root node we start a new trace span = tracing.tracer().newTrace().name(operationName + "-root") .start(); } else { brave.Span parentClient = parent.get().tracing.tracer().newChild(parent.get().span.context()) .kind(Kind.CLIENT) .name(operationName + "-client") .start(); // TODO if I finish this later the span is cached // and joined with server span and reported as a single span. // to properly solve this we have to look into the tags. // However there is another problem jaeger adds only one span.kind // (even if span contains cs,cr,sr,ss) // And it filters out core annotations, so there is no way how to find out // that there is a dependency link in this span. // https://github.com/jaegertracing/jaeger/issues/451 parentClient.finish(); span = tracing.tracer().joinSpan(parentClient.context()) .name(operationName + "-server") .kind(Kind.SERVER) .start(); } }
protected void stopTraceSpan(final TraceScopeHolder<TraceScope> holder, final int responseStatus) { if (holder == null) { return; } final TraceScope scope = holder.getScope(); if (scope != null) { try { // If the client invocation was asynchronous , the trace span has been created // in another thread and should be re-attached to the current one. if (holder.isDetached()) { brave.tracing().tracer().joinSpan(scope.getSpan().context()); } final Response response = HttpAdapterFactory.response(responseStatus); final HttpClientAdapter<?, Response> adapter = HttpClientAdapterFactory.create(response); final HttpClientHandler<?, Response> handler = HttpClientHandler.create(brave, adapter); handler.handleReceive(response, null, scope.getSpan()); } finally { scope.close(); } } } }
protected void stopTraceSpan(final TraceScopeHolder<TraceScope> holder, final int responseStatus) { if (holder == null) { return; } final TraceScope scope = holder.getScope(); if (scope != null) { try { // If the client invocation was asynchronous , the trace span has been created // in another thread and should be re-attached to the current one. if (holder.isDetached()) { brave.tracing().tracer().joinSpan(scope.getSpan().context()); } final Response response = HttpAdapterFactory.response(responseStatus); final HttpClientAdapter<?, Response> adapter = HttpClientAdapterFactory.create(response); final HttpClientHandler<?, Response> handler = HttpClientHandler.create(brave, adapter); handler.handleReceive(response, null, scope.getSpan()); } finally { scope.close(); } } } }
} else { TraceContextOrSamplingFlags extracted = extractor.extract(invocation.getAttachments()); span = extracted.context() != null ? tracer.joinSpan(extracted.context()) : tracer.nextSpan(extracted);
span = brave.tracing().tracer().joinSpan(scope.getSpan().context());
span = brave.tracing().tracer().joinSpan(scope.getSpan().context());
@Override public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(final ServerCall<ReqT, RespT> call, final Metadata headers, final ServerCallHandler<ReqT, RespT> next) { TraceContextOrSamplingFlags extracted = extractor.extract(headers); Span span = extracted.context() != null ? tracer.joinSpan(extracted.context()) : tracer.nextSpan(extracted); // If grpc propagation is enabled, make sure we refresh the server method if (grpcPropagationFormatEnabled) { Tags tags = span.context().findExtra(Tags.class); if (tags != null) tags.put(RPC_METHOD, call.getMethodDescriptor().getFullMethodName()); } span.kind(Span.Kind.SERVER); parser.onStart(call, headers, span.customizer()); // startCall invokes user interceptors, so we place the span in scope here ServerCall.Listener<ReqT> result; SpanInScope scope = tracer.withSpanInScope(span); try { // retrolambda can't resolve this try/finally result = next.startCall(new TracingServerCall<>(span, call, parser), headers); } catch (RuntimeException | Error e) { span.error(e); span.finish(); throw e; } finally { scope.close(); } // This ensures the server implementation can see the span in scope return new ScopingServerCallListener<>(tracer, span, result, parser); }