Span startSpan(HttpClientRequest request) { HttpClientRequestAdapter adapter = new HttpClientRequestAdapter(request, nameProvider); requestInterceptor.handle(adapter); Span span = clientSpanThreadBinder.getCurrentClientSpan(); clientSpanThreadBinder.setCurrentSpan(null); return span; }
/** * Sets the 'client received' event for current thread. This will also submit span because setting a client received * event means this span is finished. */ public void setClientReceived() { if (submitEndAnnotation("cr")) { currentSpan().setCurrentSpan(null); } }
@Override Span get() { return getCurrentClientSpan(); } }
public final Builder state(ServerClientAndLocalSpanState state) { this.currentLocalSpan = new LocalSpanThreadBinder(state); this.currentServerSpan = new ServerSpanThreadBinder(state); this.currentSpan = new ClientSpanThreadBinder(state); this.localEndpoint = state.endpoint(); return this; }
synchronized void complete(HttpResponse response) { ClientResponseAdapter adapter = new HttpClientResponseAdapter(response); if (span != null) { spanThreadBinder.setCurrentSpan(span); } responseInterceptor.handle(adapter); }
private Brave(Builder builder) { clock = builder.clock; serverSpanThreadBinder = new ServerSpanThreadBinder(builder.state); clientSpanThreadBinder = new ClientSpanThreadBinder(builder.state); localSpanThreadBinder = new LocalSpanThreadBinder(builder.state);
public Result invoke(Invoker<?> arg0, Invocation arg1) throws RpcException { clientRequestInterceptor.handle(new GrpcClientRequestAdapter(arg1)); Map<String,String> att = arg1.getAttachments(); final Span currentClientSpan = clientSpanThreadBinder.getCurrentClientSpan(); Result result ; try { result = arg0.invoke(arg1); clientSpanThreadBinder.setCurrentSpan(currentClientSpan); clientResponseInterceptor.handle(new GrpcClientResponseAdapter(result)); } finally { clientSpanThreadBinder.setCurrentSpan(null); } return result; }
/** * Start a new span for a new client request that will be bound to current thread. The ClientTracer can decide to return * <code>null</code> in case this request should not be traced (eg sampling). * * @param requestName Request name. Should be lowercase. Null or empty will defer to the server's name of the operation. * @return Span id for new request or <code>null</code> in case we should not trace this new client request. */ public SpanId startNewSpan(@Nullable String requestName) { // When a trace context is extracted from an incoming request, it may have only the // sampled header (no ids). If the header says unsampled, we must honor that. Since // we currently don't synthesize a fake span when a trace is unsampled, we have to // check sampled state explicitly. Boolean sample = currentServerSpan().sampled(); if (Boolean.FALSE.equals(sample)) { currentSpan().setCurrentSpan(null); return null; } Span newSpan = spanFactory().nextSpan(maybeParent()); SpanId nextContext = Brave.context(newSpan); if (Boolean.FALSE.equals(nextContext.sampled())) { currentSpan().setCurrentSpan(null); return null; } recorder().name(newSpan, requestName); currentSpan().setCurrentSpan(newSpan); return nextContext; }
private Brave(Builder builder) { serverTracer = ServerTracer.builder() .randomGenerator(builder.random) .spanCollector(builder.spanCollector) .state(builder.state) .traceSampler(builder.sampler).build(); clientTracer = ClientTracer.builder() .randomGenerator(builder.random) .spanCollector(builder.spanCollector) .state(builder.state) .traceSampler(builder.sampler).build(); localTracer = LocalTracer.builder() .randomGenerator(builder.random) .spanCollector(builder.spanCollector) .spanAndEndpoint(SpanAndEndpoint.LocalSpanAndEndpoint.create(builder.state)) .traceSampler(builder.sampler).build(); serverRequestInterceptor = new ServerRequestInterceptor(serverTracer); serverResponseInterceptor = new ServerResponseInterceptor(serverTracer); clientRequestInterceptor = new ClientRequestInterceptor(clientTracer); clientResponseInterceptor = new ClientResponseInterceptor(clientTracer); serverSpanAnnotationSubmitter = AnnotationSubmitter.create(SpanAndEndpoint.ServerSpanAndEndpoint.create(builder.state)); serverSpanThreadBinder = new ServerSpanThreadBinder(builder.state); clientSpanThreadBinder = new ClientSpanThreadBinder(builder.state); } }
@Override public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException { clientRequestInterceptor.handle(new DubboClientRequestAdapter(invoker,invocation)); try{ Result rpcResult = invoker.invoke(invocation); clientResponseInterceptor.handle(new DubboClientResponseAdapter(rpcResult)); return rpcResult; }catch (Exception ex){ clientResponseInterceptor.handle(new DubboClientResponseAdapter(ex)); throw ex; }finally { clientSpanThreadBinder.setCurrentSpan(null); } } }