opentelemetry-java-instrume.../README.md

16 KiB

OpenTelemetry Auto-Instrumentation for Java

Introduction

This project provides a Java agent JAR that can be attached to any Java 7+ application and dynamically injects bytecode to capture telemetry from a number of popular libraries and frameworks. The telemetry data can exported in a variety of formats each provided as their own independent JAR. In addition, the agent and exporter can be configured via command line arguments or environment variables. The net result is the ability to gather telemetry data from a Java application without code changes.

Getting Started

Download the latest release of the Java agent and available exporters.

The instrumentation agent is enabled using the -javaagent flag to the JVM. Configuration parameters are passed as Java system properties (-D flags) or as environment variables. Both the Java agent and exporter configuration must be defined before the application JAR. For example:

java -javaagent:path/to/opentelemetry-auto-<version>.jar \
     -Dota.exporter.jar=path/to/opentelemetry-auto-exporters-jaeger-<version>.jar \
     -Dota.exporter.jaeger.endpoint=localhost:14250 \
     -Dota.exporter.jaeger.service.name=shopping \
     -jar myapp.jar

Configuration parameters (subject to change!)

Note: These parameter names are very likely to change over time, so please check back here when trying out a new version! Please report any bugs or unexpected behavior you may find.

Jaeger exporter

A simple wrapper for the Jaeger exporter of opentelemetry-java. It currently only supports gRPC as its communications protocol.

System property Environment variable Purpose
ota.exporter.jaeger.endpoint OTA_EXPORTER_JAEGER_ENDPOINT The Jaeger endpoint to connect to. Currently only gRPC is supported.
ota.exporter.jaeger.service.name OTA_EXPORTER_JAEGER_SERVICE_NAME The service name of this JVM instance

Zipkin exporter

A simple wrapper for the Zipkin exporter of opentelemetry-java. It POSTs json in Zipkin format to a specified HTTP URL.

System property Environment variable Purpose
ota.exporter.zipkin.endpoint OTA_EXPORTER_ZIPKIN_ENDPOINT The Zipkin endpoint to connect to. Currently only HTTP is supported.
ota.exporter.zipkin.service.name OTA_EXPORTER_ZIPKIN_SERVICE_NAME The service name of this JVM instance

OTLP exporter

A simple wrapper for the OTLP exporter of opentelemetry-java.

System property Environment variable Purpose
ota.exporter.jar OTA_EXPORTER_JAR Path to the exporter fat-jar that you want to use
ota.exporter.otlp.endpoint OTA_EXPORTER_OTLP_ENDPOINT The OTLP endpoint to connect to.

Logging Exporter

The logging exporter simply prints the name of the span along with its attributes to stdout. It is used manly for testing and debugging.

System property Environment variable Purpose
ota.exporter.logging.prefix OTA_EXPORTER_LOGGING_PREFIX An optional string that is printed in front of the span name and attributes.

Supported Java libraries and frameworks

Library/Framework Versions
Akka HTTP 10.0+
Apache HttpAsyncClient 4.0+
Apache HttpClient 2.0+
AWS SDK 1.11.x and 2.2.0+
Cassandra Driver 3.0+ (not including 4.x yet)
Couchbase Client 2.0+ (not including 3.x yet)
Dropwizard Views 0.7+
Elasticsearch API 2.0+ (not including 7.x yet)
Elasticsearch REST Client 5.0+
Finatra 2.9+
Geode Client 1.4+
Google HTTP Client 1.19+
Grizzly 2.0+ (disabled by default, see below)
gRPC 1.5+
Hibernate 3.3+
HttpURLConnection Java 7+
Hystrix 1.4+
java.util.logging Java 7+
JAX-RS 0.5+
JAX-RS Client 2.0+
JDBC Java 7+
Jedis 1.4+
Jetty 8.0+
JMS 1.1+
JSP 2.3+
Kafka 0.11+
khttp 0.1.0+
Lettuce 4.0+
Log4j 1.1+
Logback 1.0+
MongoDB Drivers 3.3+
Netty 3.8+
OkHttp 3.0+
Play 2.3+ (not including 2.8.x yet)
Play WS 1.0+
RabbitMQ Client 2.7+
Ratpack 1.5+
Reactor 3.1+
Rediscala 1.8+
RMI Java 7+
RxJava 1.0+
Servlet 2.3+
Spark Web Framework 2.3+
Spring Data 1.8+
Spring Scheduling 3.1+
Spring Servlet MVC 3.1+
Spring Webflux 5.0+
Spymemcached 2.12+
Twilio 6.6+

Disabled instrumentations

Some instrumentations can produce too many spans and make traces very noisy. For this reason the following instrumentations are disabled by default:

  • jdbc-datasource which creates spans whenever java.sql.DataSource#getConnection method is called.
  • servlet-filter which creates spans around Servlet Filter methods.
  • servlet-service which creates spans around Servlet methods.

To enable them, add ota.integration.<name>.enabled system property: -Dota.integration.jdbc-datasource.enabled=true

Grizzly instrumentation

Whenever you use Grizzly for Servlet-based applications, you get better experience from Servlet-specific support. As these two instrumentations conflict with each other, more generic instrumentation for Grizzly http server is disabled by default. If needed, you can enable it by add the following system property: -Dota.integration.grizzly.enabled=true

Manually instrumenting

You can use the OpenTelemetry getTracer or the @WithSpan annotation to manually instrument your Java application.

Configure the OpenTelemetry getTracer

OpenTelemetry offers a tracer to easily enable custom instrumentation throughout your application. See the OpenTelemetry Java QuickStart for an example of how to configure it.

Configure a WithSpan annotation

If you want to configure custom instrumentation and don't want to use the OpenTelemetry getTracer and API directly, configure a @WithSpan annotation. Add the trace annotation to your application's code:

import io.opentelemetry.contrib.auto.annotations.WithSpan;

public class MyClass {
  @WithSpan
  public void MyLogic() {
      <...>
  }
}

Each time the application invokes the annotated method, it creates a span that denote its duration and provides any thrown exceptions.

Configuration

The @WithSpan annotation requires code changes to implement. You can disable the annotation at runtime via the exclude configuration or environment variables:

System property Environment variable Purpose
trace.classes.exclude TRACE_CLASSES_EXCLUDE Exclude classes with the @WithSpan annotation
trace.methods.exclude TRACE_METHODS_EXCLUDE Exclude methods with the @WithSpan annotation

Troubleshooting

To turn on the agent's internal debug logging:

-Dio.opentelemetry.auto.slf4j.simpleLogger.defaultLogLevel=debug

Note these logs are extremely verbose. Enable debug logging only when needed. Debug logging negatively impacts the performance of your application.