Java Code Examples for brave.propagation.CurrentTraceContext.Scope#close()

The following examples show how to use brave.propagation.CurrentTraceContext.Scope#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CurrentTraceContextTest.java    From brave with Apache License 2.0 6 votes vote down vote up
void noticesDifferentContext(Scope scope) {
  try (Scope scope2 = currentTraceContext.maybeScope(unsampledContext)) {
    assertThat(scope2).isNotEqualTo(Scope.NOOP);
    assertThat(currentTraceContext.get())
      .isEqualTo(unsampledContext);
    verifyImplicitContext(unsampledContext);
    try (Scope scope3 = currentTraceContext.maybeScope(notYetSampledContext)) {
      assertThat(scope3).isNotEqualTo(Scope.NOOP);
      assertThat(currentTraceContext.get())
        .isEqualTo(notYetSampledContext);
      verifyImplicitContext(notYetSampledContext);
    }
  } finally {
    scope.close();
  }
}
 
Example 2
Source File: JfrScopeDecorator.java    From brave with Apache License 2.0 6 votes vote down vote up
@Override public Scope decorateScope(@Nullable TraceContext context, Scope scope) {
  if (scope == Scope.NOOP) return scope; // we only scope fields constant in the context

  ScopeEvent event = new ScopeEvent();
  if (!event.isEnabled()) return scope;

  if (context != null) {
    event.traceId = context.traceIdString();
    event.parentId = context.parentIdString();
    event.spanId = context.spanIdString();
  }

  event.begin();

  class JfrCurrentTraceContextScope implements Scope {
    @Override public void close() {
      scope.close();
      event.commit();
    }
  }
  return new JfrCurrentTraceContextScope();
}
 
Example 3
Source File: TracingHttpServerHandler.java    From brave with Apache License 2.0 6 votes vote down vote up
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) {
  if (!(msg instanceof HttpRequest)) {
    ctx.fireChannelRead(msg); // superclass does not throw
    return;
  }

  HttpRequestWrapper request =
    new HttpRequestWrapper((HttpRequest) msg, (InetSocketAddress) ctx.channel().remoteAddress());

  ctx.channel().attr(NettyHttpTracing.REQUEST_ATTRIBUTE).set(request);
  Span span = handler.handleReceive(request);
  ctx.channel().attr(NettyHttpTracing.SPAN_ATTRIBUTE).set(span);
  Scope scope = currentTraceContext.newScope(span.context());

  // Place the span in scope so that downstream code can read trace IDs
  Throwable error = null;
  try {
    ctx.fireChannelRead(msg);
  } catch (Throwable e) {
    error = e;
    throw e;
  } finally {
    if (error != null) span.error(error).finish();
    scope.close();
  }
}
 
Example 4
Source File: CurrentTraceContextTest.java    From brave with Apache License 2.0 5 votes vote down vote up
void noticesDifferentSpanId(Scope scope) {
  TraceContext differentSpanId = context.toBuilder().spanId(context.spanId() + 1L).build();
  try (Scope scope2 = currentTraceContext.maybeScope(differentSpanId)) {
    assertThat(scope2).isNotEqualTo(Scope.NOOP);
    assertThat(currentTraceContext.get())
      .isEqualTo(differentSpanId);
    verifyImplicitContext(differentSpanId);
  } finally {
    scope.close();
  }
}
 
Example 5
Source File: TraceContextSingleObserver.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override public void onSuccess(T value) {
  Scope scope = contextScoper.maybeScope(assembled);
  try { // retrolambda can't resolve this try/finally
    downstream.onSuccess(value);
  } finally {
    scope.close();
  }
}
 
Example 6
Source File: TraceContextSingleObserver.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override public void onError(Throwable t) {
  Scope scope = contextScoper.maybeScope(assembled);
  try { // retrolambda can't resolve this try/finally
    downstream.onError(t);
  } finally {
    scope.close();
  }
}
 
Example 7
Source File: TraceContextObserver.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override public void onComplete() {
  if (done) return;
  done = true;

  Scope scope = contextScoper.maybeScope(assembled);
  try { // retrolambda can't resolve this try/finally
    downstream.onComplete();
  } finally {
    scope.close();
  }
}
 
Example 8
Source File: TraceContextObserver.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override public void onError(Throwable t) {
  if (done) {
    RxJavaPlugins.onError(t);
    return;
  }
  done = true;

  Scope scope = contextScoper.maybeScope(assembled);
  try { // retrolambda can't resolve this try/finally
    downstream.onError(t);
  } finally {
    scope.close();
  }
}
 
Example 9
Source File: TraceContextObserver.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override public void onNext(T t) {
  Scope scope = contextScoper.maybeScope(assembled);
  try { // retrolambda can't resolve this try/finally
    downstream.onNext(t);
  } finally {
    scope.close();
  }
}
 
Example 10
Source File: HttpRequestParserAdapters.java    From brave with Apache License 2.0 5 votes vote down vote up
<Req> void parseInScope(TraceContext context, HttpAdapter<Req, ?> adapter, Req req,
  SpanCustomizer span) {
  Scope ws = currentTraceContext.maybeScope(context);
  try {
    parser.request(adapter, req, span);
  } finally {
    ws.close();
  }
}
 
Example 11
Source File: TraceContextConnectableObservable.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override public void connect(Consumer<? super Disposable> connection) {
  Scope scope = contextScoper.maybeScope(assembled);
  try { // retrolambda can't resolve this try/finally
    source.connect(connection);
  } finally {
    scope.close();
  }
}
 
Example 12
Source File: TraceContextMaybeObserver.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override public void onComplete() {
  Scope scope = contextScoper.maybeScope(assembled);
  try { // retrolambda can't resolve this try/finally
    downstream.onComplete();
  } finally {
    scope.close();
  }
}
 
Example 13
Source File: TraceContextMaybeObserver.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override public void onSuccess(T value) {
  Scope scope = contextScoper.maybeScope(assembled);
  try { // retrolambda can't resolve this try/finally
    downstream.onSuccess(value);
  } finally {
    scope.close();
  }
}
 
Example 14
Source File: TraceContextMaybeObserver.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override public void onError(Throwable t) {
  Scope scope = contextScoper.maybeScope(assembled);
  try { // retrolambda can't resolve this try/finally
    downstream.onError(t);
  } finally {
    scope.close();
  }
}
 
Example 15
Source File: TraceContextSubscriber.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override public void onComplete() {
  if (done) return;
  done = true;

  Scope scope = contextScoper.maybeScope(assembled);
  try { // retrolambda can't resolve this try/finally
    downstream.onComplete();
  } finally {
    scope.close();
  }
}
 
Example 16
Source File: TracingApplicationEventListener.java    From brave with Apache License 2.0 5 votes vote down vote up
/**
 * This keeps the span in scope as long as possible. In synchronous methods, the span remains in
 * scope for the whole request/response lifecycle. {@linkplain ManagedAsync} and {@linkplain
 * Suspended} requests are the worst case: the span is only visible until request filters
 * complete.
 */
@Override
public void onEvent(RequestEvent event) {
  Scope maybeScope;
  switch (event.getType()) {
    // Note: until REQUEST_MATCHED, we don't know metadata such as if the request is async or not
    case REQUEST_MATCHED:
      parser.requestMatched(event, span);
      async = async(event);
      break;
    case REQUEST_FILTERED:
    case RESOURCE_METHOD_FINISHED:
      // If we scoped above, we have to close that to avoid leaks.
      // Jersey-specific @ManagedAsync stays on the request thread until REQUEST_FILTERED
      // Normal async methods sometimes stay on a thread until RESOURCE_METHOD_FINISHED, but
      // this is not reliable. So, we eagerly close the scope from request filters, and re-apply
      // it later when the resource method starts.
      if (!async || (maybeScope = getAndSet(null)) == null) break;
      maybeScope.close();
      break;
    case RESOURCE_METHOD_START:
      // If we are async, we have to re-scope the span as the resource method invocation is
      // is likely on a different thread than the request filtering.
      if (!async || get() != null) break;
      set(currentTraceContext.newScope(span.context()));
      break;
    case FINISHED:
      handler.handleSend(new RequestEventWrapper(event), span);
      // In async FINISHED can happen before RESOURCE_METHOD_FINISHED, and on different threads!
      // Don't close the scope unless it is a synchronous method.
      if (!async && (maybeScope = getAndSet(null)) != null) {
        maybeScope.close();
      }
      break;
    default:
  }
}
 
Example 17
Source File: TraceContextCompletableObserver.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override public void onComplete() {
  Scope scope = contextScoper.maybeScope(assembled);
  try { // retrolambda can't resolve this try/finally
    downstream.onComplete();
  } finally {
    scope.close();
  }
}
 
Example 18
Source File: HttpResponseParserAdapters.java    From brave with Apache License 2.0 5 votes vote down vote up
<Resp> void parseInScope(TraceContext context, HttpAdapter<?, Resp> adapter, @Nullable Resp res,
  @Nullable Throwable error, SpanCustomizer customizer) {
  Scope ws = currentTraceContext.maybeScope(context);
  try {
    parser.response(adapter, res, error, customizer);
  } finally {
    ws.close();
  }
}
 
Example 19
Source File: CurrentTraceContextTest.java    From brave with Apache License 2.0 5 votes vote down vote up
void retainsContext(Scope scope) {
  try {
    assertThat(scope).isNotEqualTo(Scope.NOOP);
    assertThat(currentTraceContext.get())
      .isEqualTo(context);
    verifyImplicitContext(context);
  } finally {
    scope.close();
  }
}
 
Example 20
Source File: TracingHttpAsyncClientBuilder.java    From brave with Apache License 2.0 4 votes vote down vote up
static void removeScope(HttpContext context) {
  Scope scope = (Scope) context.removeAttribute(Scope.class.getName());
  if (scope == null) return;
  context.removeAttribute(Scope.class.getName());
  scope.close();
}