io.opentracing.contrib.common.WrapperProxy Java Examples

The following examples show how to use io.opentracing.contrib.common.WrapperProxy. 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: TracingDataSource.java    From java-jdbc with Apache License 2.0 6 votes vote down vote up
@Override
public Connection getConnection() throws SQLException {
  final Span span = buildSpan("AcquireConnection", "", connectionInfo, withActiveSpanOnly,
      ignoreStatements, tracer);
  final Connection connection;
  try (Scope ignored = tracer.activateSpan(span)) {
    connection = underlying.getConnection();
  } catch (Exception e) {
    JdbcTracingUtils.onError(e, span);
    throw e;
  } finally {
    span.finish();
  }

  return WrapperProxy
      .wrap(connection, new TracingConnection(connection, connectionInfo, withActiveSpanOnly,
          ignoreStatements, tracer));
}
 
Example #2
Source File: TracingDataSource.java    From java-jdbc with Apache License 2.0 6 votes vote down vote up
@Override
public Connection getConnection(final String username, final String password)
    throws SQLException {
  final Span span = buildSpan("AcquireConnection", "", connectionInfo, withActiveSpanOnly,
      ignoreStatements, tracer);
  final Connection connection;
  try (Scope ignored = tracer.activateSpan(span)) {
    connection = underlying.getConnection(username, password);
  } catch (Exception e) {
    JdbcTracingUtils.onError(e, span);
    throw e;
  } finally {
    span.finish();
  }

  return WrapperProxy
      .wrap(connection, new TracingConnection(connection, connectionInfo, withActiveSpanOnly,
          ignoreStatements, tracer));
}
 
Example #3
Source File: AsyncHttpClientAgentIntercept.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
public static Object enter(final Object request, final Object handler) {
  final Request req = (Request)request;
  final Tracer tracer = GlobalTracer.get();
  final Span span = tracer
    .buildSpan(req.getMethod())
    .withTag(Tags.COMPONENT.getKey(), COMPONENT_NAME)
    .withTag(Tags.HTTP_METHOD.getKey(), req.getMethod())
    .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
    .withTag(Tags.HTTP_URL.getKey(), req.getUrl()).start();

  tracer.inject(span.context(), Format.Builtin.HTTP_HEADERS, new TextMap() {
    @Override
    public Iterator<Entry<String,String>> iterator() {
      throw new UnsupportedOperationException("iterator not supported with Tracer.inject()");
    }

    @Override
    public void put(final String key, final String value) {
      req.getHeaders().add(key, value);
    }
  });

  return WrapperProxy.wrap(handler, new TracingAsyncHandler(tracer, (AsyncHandler<?>)handler, span));
}
 
Example #4
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 #5
Source File: SpymemcachedAgentIntercept.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static Object get(final Object key, final Object callback) {
  final SpanBuilder spanBuilder = spanBuilder("get");
  if (key instanceof Collection) {
    final Iterator<? extends CharSequence> iterator = ((Collection<? extends CharSequence>)key).iterator();
    final StringBuilder builder = new StringBuilder();
    for (int i = 0; iterator.hasNext(); ++i) {
      if (i > 0)
        builder.append(',');

      builder.append(iterator.next());
    }

    spanBuilder.withTag("keys", builder.toString());
  }
  else {
    spanBuilder.withTag("key", key.toString()).start();
  }

  final Span span = spanBuilder.start();
  return WrapperProxy.wrap(callback, new TracingGetOperationCallback((GetOperation.Callback)callback, span));
}
 
Example #6
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 #7
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 #8
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 #9
Source File: Jms1Test.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
@Test
public void sendAndReceive(final MockTracer tracer) throws Exception {
  final Destination destination = session.createQueue("TEST.JMS1.RECEIVE");

  final MessageConsumer consumer = session.createConsumer(destination);
  assertTrue(WrapperProxy.isWrapper(consumer, TracingMessageConsumer.class));

  final TextMessage message = session.createTextMessage("Hello world");

  final MessageProducer producer = session.createProducer(destination);
  producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
  assertTrue(WrapperProxy.isWrapper(producer, TracingMessageProducer.class));
  producer.send(message);

  final TextMessage received = (TextMessage)consumer.receive(5000);
  assertEquals("Hello world", received.getText());

  final List<MockSpan> finishedSpans = tracer.finishedSpans();
  assertEquals(2, finishedSpans.size());

  producer.close();
  consumer.close();
}
 
Example #10
Source File: TracingConnection.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
    throws SQLException {
  final PreparedStatement statement = connection
      .prepareStatement(sql, resultSetType, resultSetConcurrency);
  return WrapperProxy.wrap(statement, new TracingPreparedStatement(statement, sql, connectionInfo,
      withActiveSpanOnly, ignoredStatements, tracer));
}
 
Example #11
Source File: PlayWSAgentIntercept.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
public static Object executeStart(final Object arg0, final Object arg1) {
  final Request request = (Request)arg0;
  final AsyncHandler<?> asyncHandler = (AsyncHandler<?>)arg1;

  final Tracer tracer = GlobalTracer.get();
  final Span span = tracer.buildSpan(request.getMethod())
    .withTag(Tags.COMPONENT, COMPONENT_NAME)
    .withTag(Tags.SPAN_KIND, Tags.SPAN_KIND_CLIENT)
    .withTag(Tags.HTTP_METHOD, request.getMethod())
    .withTag(Tags.HTTP_URL, request.getUrl())
    .start();

  tracer.inject(span.context(), Builtin.HTTP_HEADERS, new HttpHeadersInjectAdapter(request.getHeaders()));
  return WrapperProxy.wrap(asyncHandler, new TracingAsyncHandler(asyncHandler, span));
}
 
Example #12
Source File: HttpClientAgentIntercept.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
public static Object[] enter(final Object arg0, final Object arg1, final Object arg2) {
  final HttpRequest request = arg0 instanceof HttpRequest ? (HttpRequest)arg0 : arg1 instanceof HttpRequest ? (HttpRequest)arg1 : null;
  if (request == null)
    return null;

  if (request.getHeaders("amz-sdk-invocation-id").length > 0) {
    // skip embedded Apache HttpClient in AWS SDK Client, because it breaks
    // request signature and AWS SDK gets traced by the aws-sdk rule
    return null;
  }

  final LocalSpanContext context = LocalSpanContext.get(COMPONENT_NAME);
  if (context != null) {
    context.increment();
    return null;
  }

  final Tracer tracer = GlobalTracer.get();
  final Span span = tracer
    .buildSpan(request.getRequestLine().getMethod())
    .withTag(Tags.COMPONENT, COMPONENT_NAME)
    .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT)
    .withTag(Tags.HTTP_METHOD, request.getRequestLine().getMethod())
    .withTag(Tags.HTTP_URL, request.getRequestLine().getUri()).start();

  for (final ApacheClientSpanDecorator decorator : Configuration.spanDecorators)
    decorator.onRequest(request, arg0 instanceof HttpHost ? (HttpHost)arg0 : null, span);

  LocalSpanContext.set(COMPONENT_NAME, span, null);

  tracer.inject(span.context(), Builtin.HTTP_HEADERS, new HttpHeadersInjectAdapter(request));
  if (arg1 instanceof ResponseHandler)
    return new Object[] {WrapperProxy.wrap(arg1, new TracingResponseHandler<>((ResponseHandler<?>)arg1, span))};

  if (arg2 instanceof ResponseHandler)
    return new Object[] {null, WrapperProxy.wrap(arg2, new TracingResponseHandler<>((ResponseHandler<?>)arg2, span))};

  return null;
}
 
Example #13
Source File: TracingDriver.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Override
public Connection connect(String url, Properties info) throws SQLException {
  // if there is no url, we have problems
  if (url == null) {
    throw new SQLException("url is required");
  }

  final Set<String> ignoreStatements;
  final boolean withActiveSpanOnly;
  if (interceptorMode) {
    withActiveSpanOnly = TracingDriver.withActiveSpanOnly;
    ignoreStatements = TracingDriver.ignoreStatements;
  } else if (acceptsURL(url)) {
    withActiveSpanOnly = url.contains(WITH_ACTIVE_SPAN_ONLY);
    ignoreStatements = extractIgnoredStatements(url);
  } else {
    return null;
  }

  url = extractRealUrl(url);

  // find the real driver for the URL
  final Driver wrappedDriver = findDriver(url);

  final Tracer currentTracer = getTracer();
  final ConnectionInfo connectionInfo = URLParser.parser(url);
  final Span span = buildSpan("AcquireConnection", "", connectionInfo, withActiveSpanOnly,
      Collections.<String>emptySet(), currentTracer);
  final Connection connection;
  try (Scope ignored = currentTracer.activateSpan(span)) {
    connection = wrappedDriver.connect(url, info);
  } finally {
    span.finish();
  }

  return WrapperProxy
      .wrap(connection, new TracingConnection(connection, connectionInfo, withActiveSpanOnly,
          ignoreStatements, currentTracer));
}
 
Example #14
Source File: TracingConnection.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Override
public Statement createStatement() throws SQLException {
  final Statement statement = connection.createStatement();
  return WrapperProxy
      .wrap(statement, new TracingStatement(statement, connectionInfo, withActiveSpanOnly,
          ignoredStatements, tracer));
}
 
Example #15
Source File: TracingConnection.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Override
public CallableStatement prepareCall(String sql) throws SQLException {
  final CallableStatement statement = connection.prepareCall(sql);
  return WrapperProxy.wrap(statement,
      new TracingCallableStatement(connection.prepareCall(sql), sql, connectionInfo,
          withActiveSpanOnly, ignoredStatements, tracer));
}
 
Example #16
Source File: TracingConnection.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency)
    throws SQLException {
  final Statement statement = connection.createStatement(resultSetType, resultSetConcurrency);
  return WrapperProxy.wrap(statement, new TracingStatement(statement,
      connectionInfo, withActiveSpanOnly, ignoredStatements, tracer));
}
 
Example #17
Source File: TracingConnection.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency)
    throws SQLException {
  final CallableStatement statement = connection
      .prepareCall(sql, resultSetType, resultSetConcurrency);
  return WrapperProxy.wrap(statement, new TracingCallableStatement(statement, sql, connectionInfo,
      withActiveSpanOnly, ignoredStatements, tracer));
}
 
Example #18
Source File: SpymemcachedAgentIntercept.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
public static Object tracingCallback(final String operation, final Object key, final Object callback) {
  final SpanBuilder spanBuilder = spanBuilder(operation);
  if (key != null)
    spanBuilder.withTag("key", key.toString());

  final Span span = spanBuilder.start();
  return WrapperProxy.wrap(callback, new TracingOperationCallback((OperationCallback)callback, span));
}
 
Example #19
Source File: TracingConnection.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency,
    int resultSetHoldability) throws SQLException {
  final Statement statement = connection
      .createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
  return WrapperProxy.wrap(statement, new TracingStatement(statement,
      connectionInfo, withActiveSpanOnly, ignoredStatements, tracer));
}
 
Example #20
Source File: TracingConnection.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
    int resultSetHoldability) throws SQLException {
  final PreparedStatement statement = connection
      .prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
  return WrapperProxy.wrap(statement, new TracingPreparedStatement(statement,
      sql, connectionInfo, withActiveSpanOnly, ignoredStatements, tracer));
}
 
Example #21
Source File: TracingConnection.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
    int resultSetHoldability) throws SQLException {
  final CallableStatement statement = connection
      .prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
  return WrapperProxy.wrap(statement, new TracingCallableStatement(statement, sql,
      connectionInfo, withActiveSpanOnly, ignoredStatements, tracer));
}
 
Example #22
Source File: SpringRabbitMQAgentIntercept.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
public static void handleDeliveryStart(Object thiz, Object props) {
  if (WrapperProxy.isWrapper(thiz, TracingConsumer.class))
    return;

  if (AgentRuleUtil.callerEquals(1, 3, "io.opentracing.contrib.rabbitmq.TracingConsumer.handleDelivery"))
    return;

  final AMQP.BasicProperties properties = (AMQP.BasicProperties)props;
  final Tracer tracer = GlobalTracer.get();
  final Span span = TracingUtils.buildChildSpan(properties, null, tracer);
  final Scope scope = tracer.activateSpan(span);
  LocalSpanContext.set(COMPONENT_NAME, span, scope);
}
 
Example #23
Source File: JdbcAgentRule.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Advice.OnMethodExit(onThrowable = Throwable.class)
public static void exit(@Advice.Return(readOnly = false, typing = Typing.DYNAMIC) Object returned, @Advice.Thrown(readOnly = false, typing = Typing.DYNAMIC) Throwable thrown) throws Exception {
  if (thrown instanceof EarlyReturnException) {
    returned = WrapperProxy.wrap(returned, ((EarlyReturnException)thrown).getReturnValue());
    thrown = null;
  }
}
 
Example #24
Source File: ElasticsearchTransportClientAgentIntercept.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
public static Object transport(final Object request, final Object listener) {
  final Tracer.SpanBuilder spanBuilder = GlobalTracer.get()
    .buildSpan(request.getClass().getSimpleName())
    .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT);

  final Span span = spanBuilder.start();
  SpanDecorator.onRequest(span);
  return WrapperProxy.wrap(listener, new TracingResponseListener<>((ActionListener)listener, span));
}
 
Example #25
Source File: JdbcAspect.java    From java-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * Intercepts calls to {@link DataSource#getConnection()} (and related), wrapping the outcome in a
 * {@link TracingConnection} proxy
 *
 * @param pjp the intercepted join point
 * @return a new {@link TracingConnection} proxy wrapping the result of the joint point
 * @throws Throwable in case of wrong JDBC URL
 */
@Around("execution(java.sql.Connection *.getConnection(..)) && target(javax.sql.DataSource)")
public Object getConnection(final ProceedingJoinPoint pjp) throws Throwable {
  Connection conn = (Connection) pjp.proceed();
  if (WrapperProxy.isWrapper(conn, TracingConnection.class)) {
    return conn;
  }
  String url = conn.getMetaData().getURL();
  ConnectionInfo connectionInfo = URLParser.parse(url);
  return WrapperProxy.wrap(conn, new TracingConnection(conn, connectionInfo,
      withActiveSpanOnly, ignoredStatements, GlobalTracer.get()));
}
 
Example #26
Source File: KafkaAgentIntercept.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
public static Object onProducerEnter(final Object record, final Object callback) {
  final Tracer tracer = GlobalTracer.get();
  final Span span = TracingKafkaUtils.buildAndInjectSpan((ProducerRecord<?,?>)record, tracer);
  return WrapperProxy.wrap(callback, new TracingCallback((Callback)callback, span, tracer));
}
 
Example #27
Source File: TracingConnection.java    From java-jdbc with Apache License 2.0 4 votes vote down vote up
@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
  final PreparedStatement statement = connection.prepareStatement(sql);
  return WrapperProxy.wrap(statement, new TracingPreparedStatement(statement, sql, connectionInfo,
      withActiveSpanOnly, ignoredStatements, tracer));
}
 
Example #28
Source File: TracingConnection.java    From java-jdbc with Apache License 2.0 4 votes vote down vote up
@Override
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
  final PreparedStatement statement = connection.prepareStatement(sql, autoGeneratedKeys);
  return WrapperProxy.wrap(statement, new TracingPreparedStatement(statement, sql,
      connectionInfo, withActiveSpanOnly, ignoredStatements, tracer));
}
 
Example #29
Source File: TracingConnection.java    From java-jdbc with Apache License 2.0 4 votes vote down vote up
@Override
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
  final PreparedStatement statement = connection.prepareStatement(sql, columnIndexes);
  return WrapperProxy.wrap(statement, new TracingPreparedStatement(statement, sql,
      connectionInfo, withActiveSpanOnly, ignoredStatements, tracer));
}
 
Example #30
Source File: TracingConnection.java    From java-jdbc with Apache License 2.0 4 votes vote down vote up
@Override
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
  final PreparedStatement statement = connection.prepareStatement(sql, columnNames);
  return WrapperProxy.wrap(statement, new TracingPreparedStatement(statement, sql, connectionInfo,
      withActiveSpanOnly, ignoredStatements, tracer));
}