Java Code Examples for io.opentracing.Tracer#activeSpan()

The following examples show how to use io.opentracing.Tracer#activeSpan() . 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: Http2Client.java    From light-4j with Apache License 2.0 6 votes vote down vote up
/**
 * Add Authorization Code grant token the caller app gets from OAuth2 server and inject OpenTracing context
 *
 * This is the method called from client like web server that want to have Tracer context pass through.
 *
 * @param request the http request
 * @param token the bearer token
 * @param tracer the OpenTracing tracer
 */
public void addAuthTokenTrace(ClientRequest request, String token, Tracer tracer) {
    if(token != null && !token.startsWith("Bearer ")) {
        if(token.toUpperCase().startsWith("BEARER ")) {
            // other cases of Bearer
            token = "Bearer " + token.substring(7);
        } else {
            token = "Bearer " + token;
        }
    }
    request.getRequestHeaders().put(Headers.AUTHORIZATION, token);
    if(tracer != null && tracer.activeSpan() != null) {
        Tags.SPAN_KIND.set(tracer.activeSpan(), Tags.SPAN_KIND_CLIENT);
        Tags.HTTP_METHOD.set(tracer.activeSpan(), request.getMethod().toString());
        Tags.HTTP_URL.set(tracer.activeSpan(), request.getPath());
        tracer.inject(tracer.activeSpan().context(), Format.Builtin.HTTP_HEADERS, new ClientRequestCarrier(request));
    }
}
 
Example 2
Source File: ScheduledRunnableAgentRule.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
@Advice.OnMethodEnter
public static void exit(final @ClassName String className, final @Advice.Origin String origin, @Advice.Argument(value = 0, readOnly = false, typing = Typing.DYNAMIC) Runnable arg) throws Exception {
  if (!isAllowed(className, origin))
    return;

  final Tracer tracer = GlobalTracer.get();
  if (isVerbose(className)) {
    final Span span = tracer
      .buildSpan("schedule")
      .withTag(Tags.COMPONENT, "java-concurrent")
      .start();
    arg = WrapperProxy.wrap(arg, new TracedRunnable(arg, span, true));
    span.finish();
  }
  else if (tracer.activeSpan() != null) {
    arg = WrapperProxy.wrap(arg, new TracedRunnable(arg, tracer.activeSpan(), false));
  }
}
 
Example 3
Source File: FixedDelayAgentRule.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
@Advice.OnMethodEnter
public static void exit(final @ClassName String className, final @Advice.Origin String origin, @Advice.Argument(value = 0, readOnly = false, typing = Typing.DYNAMIC) Runnable arg) throws Exception {
  if (!isAllowed(className, origin))
    return;

  final Tracer tracer = GlobalTracer.get();
  if (isVerbose(className)) {
    final Span span = tracer
      .buildSpan("scheduleWithFixedDelay")
      .withTag(Tags.COMPONENT, "java-concurrent")
      .start();
    arg = WrapperProxy.wrap(arg, new TracedRunnable(arg, span, true));
    span.finish();
  }
  else if (tracer.activeSpan() != null) {
    arg = WrapperProxy.wrap(arg, new TracedRunnable(arg, tracer.activeSpan(), false));
  }
}
 
Example 4
Source File: ScheduledCallableAgentRule.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
@Advice.OnMethodEnter
public static void exit(final @ClassName String className, final @Advice.Origin String origin, @Advice.Argument(value = 0, readOnly = false, typing = Typing.DYNAMIC) Callable<?> arg) throws Exception {
  if (!isAllowed(className, origin))
    return;

  final Tracer tracer = GlobalTracer.get();
  if (isVerbose(className)) {
    final Span span = tracer
      .buildSpan("schedule")
      .withTag(Tags.COMPONENT, "java-concurrent")
      .start();
    arg = new TracedCallable<>(arg, span, true);
    span.finish();
  }
  else if (tracer.activeSpan() != null) {
    arg = new TracedCallable<>(arg, tracer.activeSpan(), false);
  }
}
 
Example 5
Source File: FixedRateAgentRule.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
@Advice.OnMethodEnter
public static void exit(final @ClassName String className, final @Advice.Origin String origin, @Advice.Argument(value = 0, readOnly = false, typing = Typing.DYNAMIC) Runnable arg) throws Exception {
  if (!isAllowed(className, origin))
    return;

  final Tracer tracer = GlobalTracer.get();
  if (isVerbose(className)) {
    final Span span = tracer
      .buildSpan("scheduleAtFixedRate")
      .withTag(Tags.COMPONENT, "java-concurrent")
      .start();
    arg = WrapperProxy.wrap(arg, new TracedRunnable(arg, span, true));
    span.finish();
  }
  else if (tracer.activeSpan() != null) {
    arg = WrapperProxy.wrap(arg, new TracedRunnable(arg, tracer.activeSpan(), false));
  }
}
 
Example 6
Source File: ExecutorAgentRule.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
@Advice.OnMethodEnter
public static void exit(final @ClassName String className, final @Advice.Origin String origin, @Advice.Argument(value = 0, readOnly = false, typing = Typing.DYNAMIC) Runnable arg) throws Exception {
  if (!isAllowed(className, origin))
    return;

  final Tracer tracer = GlobalTracer.get();
  if (isVerbose(className)) {
    final Span span = tracer
      .buildSpan("execute")
      .withTag(Tags.COMPONENT, "java-concurrent")
      .start();
    arg = WrapperProxy.wrap(arg, new TracedRunnable(arg, span, true));
    span.finish();
  }
  else if (tracer.activeSpan() != null) {
    arg = WrapperProxy.wrap(arg, new TracedRunnable(arg, tracer.activeSpan(), false));
  }
}
 
Example 7
Source File: JdbcTracingUtils.java    From java-jdbc with Apache License 2.0 6 votes vote down vote up
static Span buildSpan(String operationName,
    String sql,
    ConnectionInfo connectionInfo,
    boolean withActiveSpanOnly,
    Set<String> ignoredStatements,
    Tracer tracer) {
  if (!TracingDriver.isTraceEnabled() || (withActiveSpanOnly && tracer.activeSpan() == null)) {
    return NoopSpan.INSTANCE;
  } else if (ignoredStatements != null && ignoredStatements.contains(sql)) {
    return NoopSpan.INSTANCE;
  }

  Tracer.SpanBuilder spanBuilder = tracer.buildSpan(operationName)
      .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT);

  Span span = spanBuilder.start();
  decorate(span, sql, connectionInfo);

  return span;
}
 
Example 8
Source File: OpenTracingDecorator.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
private Span getParentSpan(Tracer tracer, final DataFetchingEnvironment env) {
    final GraphQLContext localContext = env.getLocalContext();
    if (localContext != null && localContext.hasKey(PARENT_SPAN_KEY)) {
        return localContext.get(PARENT_SPAN_KEY);
    }

    final GraphQLContext rootContext = env.getContext();
    if (rootContext != null && rootContext.hasKey(PARENT_SPAN_KEY)) {
        return rootContext.get(PARENT_SPAN_KEY);
    }

    return tracer.activeSpan();
}
 
Example 9
Source File: TracingRedisAsyncCommands.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
private <T> RedisFuture<T> continueScopeSpan(RedisFuture<T> redisFuture) {
  Tracer tracer = tracingConfiguration.getTracer();
  Span span = tracer.activeSpan();
  CompletableRedisFuture<T> customRedisFuture = new CompletableRedisFuture<>(redisFuture);
  redisFuture.whenComplete((v, throwable) -> {
    try (Scope ignored = tracer.scopeManager().activate(span)) {
      if (throwable != null) {
        customRedisFuture.completeExceptionally(throwable);
      } else {
        customRedisFuture.complete(v);
      }
    }
  });
  return customRedisFuture;
}
 
Example 10
Source File: TracingRedisAdvancedClusterAsyncCommands.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
private <T> RedisFuture<T> continueScopeSpan(RedisFuture<T> redisFuture) {
  Tracer tracer = tracingConfiguration.getTracer();
  Span span = tracer.activeSpan();
  CompletableRedisFuture<T> customRedisFuture = new CompletableRedisFuture<>(redisFuture);
  redisFuture.whenComplete((v, throwable) -> {
    try (Scope ignored = tracer.scopeManager().activate(span)) {
      if (throwable != null) {
        customRedisFuture.completeExceptionally(throwable);
      } else {
        customRedisFuture.complete(v);
      }
    }
  });
  return customRedisFuture;
}
 
Example 11
Source File: TracingRedisAsyncCommands.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
private <T> RedisFuture<T> continueScopeSpan(RedisFuture<T> redisFuture) {
  Tracer tracer = tracingConfiguration.getTracer();
  Span span = tracer.activeSpan();
  CompletableRedisFuture<T> customRedisFuture = new CompletableRedisFuture<>(redisFuture);
  redisFuture.whenComplete((v, throwable) -> {
    try (Scope ignored = tracer.scopeManager().activate(span)) {
      if (throwable != null) {
        customRedisFuture.completeExceptionally(throwable);
      } else {
        customRedisFuture.complete(v);
      }
    }
  });
  return customRedisFuture;
}
 
Example 12
Source File: TracingRedisAdvancedClusterAsyncCommands.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
private <T> RedisFuture<T> continueScopeSpan(RedisFuture<T> redisFuture) {
  Tracer tracer = tracingConfiguration.getTracer();
  Span span = tracer.activeSpan();
  CompletableRedisFuture<T> customRedisFuture = new CompletableRedisFuture<>(redisFuture);
  redisFuture.whenComplete((v, throwable) -> {
    try (Scope ignored = tracer.scopeManager().activate(span)) {
      if (throwable != null) {
        customRedisFuture.completeExceptionally(throwable);
      } else {
        customRedisFuture.complete(v);
      }
    }
  });
  return customRedisFuture;
}
 
Example 13
Source File: TracingRedisAsyncCommands.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
private <T> RedisFuture<T> continueScopeSpan(RedisFuture<T> redisFuture) {
  Tracer tracer = tracingConfiguration.getTracer();
  Span span = tracer.activeSpan();
  CompletableRedisFuture<T> customRedisFuture = new CompletableRedisFuture<>(redisFuture);
  redisFuture.whenComplete((v, throwable) -> {
    try (Scope ignored = tracer.scopeManager().activate(span)) {
      if (throwable != null) {
        customRedisFuture.completeExceptionally(throwable);
      } else {
        customRedisFuture.complete(v);
      }
    }
  });
  return customRedisFuture;
}
 
Example 14
Source File: TracingRedisAdvancedClusterAsyncCommands.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
private <T> RedisFuture<T> continueScopeSpan(RedisFuture<T> redisFuture) {
  Tracer tracer = tracingConfiguration.getTracer();
  Span span = tracer.activeSpan();
  CompletableRedisFuture<T> customRedisFuture = new CompletableRedisFuture<>(redisFuture);
  redisFuture.whenComplete((v, throwable) -> {
    try (Scope ignored = tracer.scopeManager().activate(span)) {
      if (throwable != null) {
        customRedisFuture.completeExceptionally(throwable);
      } else {
        customRedisFuture.complete(v);
      }
    }
  });
  return customRedisFuture;
}
 
Example 15
Source File: Sample01.java    From java-span-reporter with Apache License 2.0 5 votes vote down vote up
private static void run0(Tracer tracer) throws Exception {
    // start a span
    try(Scope scope0 = tracer.activateSpan(tracer.buildSpan("span-0")
            .withTag("description", "top level initial span in the original process").start())) {
        Span span0 = tracer.activeSpan();
        Tags.HTTP_URL.set(span0, "/orders"); //span0.setTag(Tags.HTTP_URL.getKey(), "/orders");
        //Tags.HTTP_METHOD.set(span0, "POST");
        //Tags.PEER_SERVICE.set(span0, "OrderManager");
        //Tags.SPAN_KIND.set(span0, Tags.SPAN_KIND_SERVER);
        try(Scope scope1 = tracer.activateSpan(tracer.buildSpan("span-1")
                .withTag("description", "the first inner span in the original process")
                .start())) {
            Span span1 = tracer.activeSpan();

            // do something

            // start another span

            try(Scope scope2 = tracer.activateSpan(tracer.buildSpan("span-2")
                    .asChildOf(span1)
                    .withTag("description", "the second inner span in the original process")
                    .start())) {
                Span span2 = tracer.activeSpan();

                // do something
                span2.log("blablabala");
                span2.log(MapMaker.fields(LogLevel.FIELD_NAME, LogLevel.DEBUG, "k0", "v0", "k1", 42));
                span2.log(MapMaker.fields(LogLevel.FIELD_NAME, LogLevel.WARN, "k0", "v0", "ex", new Exception("boom !")));

                // cross process boundary
                //Map<String, String> map = new java.util.HashMap<>();
                //tracer.inject(span2.context(), Format.Builtin.HTTP_HEADERS, new TextMapInjectAdapter(map))
                Thread.currentThread().sleep(10);
                // request.addHeaders(map);
                // request.doGet();
            }
        }
    }
}
 
Example 16
Source File: TracedRunnable.java    From java-concurrent with Apache License 2.0 4 votes vote down vote up
public TracedRunnable(Runnable delegate, Tracer tracer) {
  this(delegate, tracer, tracer.activeSpan());
}
 
Example 17
Source File: TracedCallable.java    From java-concurrent with Apache License 2.0 4 votes vote down vote up
public TracedCallable(Callable<V> delegate, Tracer tracer) {
  this(delegate, tracer, tracer.activeSpan());
}
 
Example 18
Source File: ErrorReportingTest.java    From opentelemetry-java with Apache License 2.0 4 votes vote down vote up
public ScopedRunnable(Runnable runnable, Tracer tracer) {
  this.runnable = runnable;
  this.tracer = tracer;
  this.span = tracer.activeSpan();
}
 
Example 19
Source File: SolrCmdDistributor.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void submit(final Req req, boolean isCommit) throws IOException {
  // Copy user principal from the original request to the new update request, for later authentication interceptor use
  if (SolrRequestInfo.getRequestInfo() != null) {
    req.uReq.setUserPrincipal(SolrRequestInfo.getRequestInfo().getReq().getUserPrincipal());
  }

  Tracer tracer = GlobalTracer.getTracer();
  Span parentSpan = tracer.activeSpan();
  if (parentSpan != null) {
    tracer.inject(parentSpan.context(), Format.Builtin.HTTP_HEADERS,
        new SolrRequestCarrier(req.uReq));
  }

  if (req.synchronous) {
    blockAndDoRetries();

    try {
      req.uReq.setBasePath(req.node.getUrl());
      clients.getHttpClient().request(req.uReq);
    } catch (Exception e) {
      SolrException.log(log, e);
      Error error = new Error();
      error.e = e;
      error.req = req;
      if (e instanceof SolrException) {
        error.statusCode = ((SolrException) e).code();
      }
      errors.add(error);
    }
    
    return;
  }
  
  if (log.isDebugEnabled()) {
    log.debug("sending update to {} retry: {} {} params {}"
        , req.node.getUrl(), req.retries, req.cmd, req.uReq.getParams());
  }
  
  if (isCommit) {
    // a commit using ConncurrentUpdateSolrServer is not async,
    // so we make it async to prevent commits from happening
    // serially across multiple nodes
    pending.add(completionService.submit(() -> {
      doRequest(req);
      return null;
    }));
  } else {
    doRequest(req);
  }
}
 
Example 20
Source File: ErrorReportingTest.java    From opentracing-java with Apache License 2.0 4 votes vote down vote up
public ScopedRunnable(Runnable runnable, Tracer tracer) {
    this.runnable = runnable;
    this.tracer = tracer;
    this.span = tracer.activeSpan();
}