Automatically instruments 3rd-party libraries in Java applications
Note: The coverage statistic is not correct, because Jacoco cannot properly instrument code that is instrumented at the bytecode level.
SpecialAgent automatically instruments 3rd-party libraries in Java applications. The architecture of SpecialAgent was designed to involve contributions from the community, whereby its platform integrates and automates OpenTracing Instrumentation Plugins written by individual contributors. In addition to Instrumentation Plugins, the SpecialAgent also supports Tracer Plugins, which connect an instrumented runtime to OpenTracing-compliant tracer vendors, such as LightStep or Jaeger. Both the Instrumentation Plugins and the Tracer Plugins are decoupled from SpecialAgent -- i.e. neither kinds of plugins need to know anything about SpecialAgent. At its core, the SpecialAgent is itself nothing more than an engine that abstracts the functionality for automatic installation of Instrumentation Plugins, and then connecting them to Tracer Plugins. A benefit of this approach is that the SpecialAgent intrinsically embodies and encourages community involvement.
In additiona to its engine, the SpecialAgent packages a set of pre-supported Instrumentation Plugins and Tracer Plugins.
1 Introduction
2 Quick Start
2.1 Installation
2.1.1 In Application
2.1.1.1 Stable
2.1.1.2 Development
2.1.2 For Development
2.1.2.1 Building
2.2 Usage
2.2.1 Static Attach
2.2.2 Dynamic Attach
2.2.3 Static Deferred Attach
3 Configuration
3.1 Overview
3.2 Properties
3.3 Selecting the Tracer Plugin
3.4 Disabling Instrumentation Plugins
3.4.1 Disabling All Instrumentation Plugins
3.4.2 Disabling One Instrumentation Plugin
3.4.3 Disabling AgentRules of an Instrumentation Plugin
3.5 Disabling Tracer Plugins
4 Definitions
4.1 SpecialAgent
4.2 Tracer
4.3 Tracer Plugin
4.4 Instrumentation Plugin
4.5 Instrumentation Rule
5 Objectives
5.1 Goals
5.2 Non-Goals
6 Supported Plugins
6.1 Instrumentation Plugins
6.2 Tracer Plugins
6.3 Instrumented libraries by existing rules
7 Credits
8 Contributing
9 License
This file contains the operational instructions for the use and development of SpecialAgent.
When SpecialAgent attaches to an application, either statically or dynamically, it will automatically load the Instrumentation Plugins explicitly specified as dependencies in its POM (Project Object Model).
Any exception that occurs during the execution of the bootstrap process will not adversely affect the stability of the target application. It is, however, possible that the Instrumentation Plugin code may result in exceptions that are not properly handled, and could destabilize the target application.
The SpecialAgent has 2 artifacts: main and test. These artifacts are built by Maven, and can be obtained by cloning this repository and following the Building instructions, or downloading directly from Maven's Central Repository.
To use the SpecialAgent on an application, first download the JAR:
The latest stable release is: 1.3.6.
wget -O opentracing-specialagent-1.3.6.jar "http://central.maven.org/maven2/io/opentracing/contrib/specialagent/opentracing-specialagent/1.3.6/opentracing-specialagent-1.3.6.jar"The latest development release is: 1.3.7-SNAPSHOT.
wget -O opentracing-specialagent-1.3.7-SNAPSHOT.jar "https://oss.sonatype.org/service/local/artifact/maven/redirect?r=snapshots&g=io.opentracing.contrib.specialagent&a=opentracing-specialagent&v=LATEST"This is the main artifact that contains within it the Instrumentation Plugins from the opentracing-contrib organization for which Instrumentation Rules have been implemented. This JAR can be specified as the -javaagent target for Static Attach to an application. This JAR can also be executed, standalone, with an argument representing the PID of a target process to which it should dynamically attach. Please refer to Usage section for usage instructions.
For development of Instrumentation Plugins, import the opentracing-specialagent-api and test-jar of the opentracing-specialagent.
<dependency>
<groupId>io.opentracing.contrib.specialagent</groupId>
<artifactId>opentracing-specialagent-api</artifactId>
<version>1.3.6</version> <!--version>1.3.7-SNAPSHOT<version-->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.opentracing.contrib.specialagent</groupId>
<artifactId>opentracing-specialagent</artifactId>
<version>1.3.6</version> <!--version>1.3.7-SNAPSHOT<version-->
<type>test-jar</type>
<scope>test</scope>
</dependency>This is the test artifact that contains within it the AgentRunner, which is a JUnit runner class provided for testing of the ByteBuddy auto-instrumentation rules. This JAR does not contain within it any Instrumentation Plugins themselves, and is only intended to be applied to the test phase of the build lifecycle of a single plugin for an Instrumentation Plugin implementation. For direction with the AgentRunner, please refer to the opentracing-specialagent-api module.
Prerequisite: The SpecialAgent requires Oracle Java to build. Thought the SpecialAgent supports OpenJDK for general application use, it only supports Oracle Java for building and testing.
The SpecialAgent is built in 2 passes that utilize different profiles:
-
The
defaultprofile is used for development of Instrumentation Rules. It builds and runs tests for each rule, but does not bundle the rules intoopentracing-specialagent-1.3.6.jarTo run this profile:
mvn clean install
-
The
assembleprofile is used to bundle the Instrumentation Rules intoopentracing-specialagent-1.3.6.jar. It builds each rule, but does not run tests. Once the build with theassembleprofile is finished, theopentracing-specialagent-1.3.6.jarwill contain the built rules inside it.Note: If you do not run this step, the
opentracing-specialagent-1.3.6.jarfrom the previous step will not contain any Instrumentation Plugins within it!Note: It is important to not run Maven's
cleanlifecycle when executing theassembleprofile.To run this profile:
mvn -Dassemble package
-
For a one-line build command to build SpecialAgent, its rules, run all tests, and create the
assemblepackage:mvn clean install && mvn -Dassemble package
The SpecialAgent uses Java’s Instrumentation mechanism to transform the behavior of a target application. The entrypoint into the target application is performed via Java’s Agent convention. SpecialAgent supports both Static Attach and Dynamic Attach.
Statically attaching to a Java application involves the use of the -javaagent vm argument at the time of startup of the target Java application. The following command can be used as an example:
java -javaagent:opentracing-specialagent-1.3.6.jar -jar MyApp.jarThis command statically attaches SpecialAgent into the application in myapp.jar.
Dynamically attaching to a Java application involves the use of a running application’s PID, after the application’s startup. The following commands can be used as an example:
-
Call this to obtain the
PIDof the target application:jps
-
For jdk1.8
java -Xbootclasspath/a:$JAVA_HOME/lib/tools.jar -jar opentracing-specialagent-1.3.6.jar <PID>
-
For jdk9+
java -jar opentracing-specialagent-1.3.6.jar <PID>
Note: If you encounter an exception stating Unable to open socket file, make sure the attaching VM is executed with the same permissions as the target VM.
With Static Attach, the application is executed with the -javaagent argument, and the agent initialization occurs before the application is started. This mode requires 1 command from the command line.
With Dynamic Attach, the application is allowed to start first, afterwhich an agent VM is dynamically attached to the application's PID. This mode requires 2 commands from the command line: the first for the application, and the second for the agent VM.
With Static Deferred Attach, the application is executed with the -javaagent argument, but the agent initialization is deferred until the application is started. This mode requires 1 command from the command line, and is designed specifically for Spring runtimes that have complex initialization lifecycles. The SpecialAgent relies on the ContextRefreshedEvent to signify that the application is ready, and thus to cue agent initialization. This approach works for all versions of Spring and Spring Boot.
The following command can be used as an example:
java -javaagent:opentracing-specialagent-1.3.6.jar -Dsa.spring -jar MySpringApp.jarThe SpecialAgent exposes a simple pattern for configuration of SpecialAgent, the Instrumentation Plugins, as well as Tracer Plugins. The configuration pattern is based on system properties, which can be defined on the command-line, in a properties file, or in @AgentRunner.Config for JUnit tests:
-
Properties passed on the command-line via
-D${PROPERTY}=...override same-named properties defined in layers below... -
The @AgentRunner.Config annotation allows one to define log level and transformation event logging settings. Properties defined in the
@Configannotation override same-named properties defined in layers below... -
The
-Dsa.config=${PROPERTIES_FILE}command-line argument can be specified for SpecialAgent to load property names from a${PROPERTIES_FILE}. Properties defined in the${PROPERTIES_FILE}override same-named properties defined in the layer below... -
The SpecialAgent has a
default.propertiesfile that defines default values for properties that need to be defined.
The following properties are supported by all instrumentation plugins:
-
Logging:
The
-Dsa.log.levelsystem property can be used to set the logging level for SpecialAgent. Acceptable values are:SEVERE,WARNING,INFO,CONFIG,FINE,FINER, orFINEST, or any numerical log level value is accepted also. The default logging level is set toWARNING.The
-Dsa.log.eventssystem property can be used to set the re/transformation events to log:DISCOVERY,IGNORED,TRANSFORMATION,ERROR,COMPLETE. The property accepts a comma-delimited list of event names. By default, theERRORevent is logged (only when run withAgentRunner).The
-Dsa.log.filesystem property can be used to set the logging output file for SpecialAgent. -
Verbose Mode:
-Dsa.instrumentation.plugins.verbose,-Dsa.instrumentation.plugin.${PLUGIN_NAME}.verboseSets verbose mode for all or one plugin (Default: false). This property can also be set in an
AgentRunnerJUnit test with the@AgentRunner.Config(verbose=true)for all tests in a JUnit class, or@AgentRunner.TestConfig(verbose=true)for an individual JUnit test method.Concurrent plugin supports verbose mode which is disabled by default. To enable set
sa.concurrent.verbose=true. In non verbose mode parent span context (if exists) is propagating to task execution. In verbose mode parent span is always created on task submission to executor and child span is created when task is started. -
Skip fingerprint verification:
-Dsa.fingerprint.skipTells the SpecialAgent to skip the fingerprint verification when linking Instrumentation Plugins into class loaders. This option allows one to work around an unexpected fingerprint verification failure, which can happen in complex runtimes that do not contain all class definitions on the class path. It must be noted, however, that if the fingerprint verification is disabled, the SpecialAgent will indiscriminately install all plugins regardless of library version compatibility issues, which may lead to
NoClassDefFoundError,IllegalAccessError,AbstractMethodError,LinkageError, etc.
3.3 Selecting the Tracer Plugin
The SpecialAgent supports OpenTracing-compatible tracers. There are 2 ways to connect a tracer to the SpecialAgent runtime:
-
Internal Tracer Plugins
The SpecialAgent includes the following Tracer Plugins:
The
-Dsa.tracer=${TRACER_PLUGIN}property is used on the command-line to specify which Tracer Plugin will be used. The value of${TRACER_PLUGIN}is the short name of the Tracer Plugin, i.e.jaegerorlightstep. -
External Tracer Plugins
The SpecialAgent allows external Tracer Plugins to be attached to the runtime.
The
-Dsa.tracer=${TRACER_JAR}property is used on the command-line to specify the JAR path of the Tracer Plugin to be used. The${TRACER_JAR}must be a JAR that conforms to theTracerFactoryAPI of the TracerResolver project.
NOTE: If a tracer is not specified with the -Dsa.tracer=... property, the SpecialAgent will present a warning in the log that states: Tracer NOT RESOLVED.
3.4 Disabling Instrumentation Plugins
The SpecialAgent has all of its Instrumentation Plugins enabled by default, and allows them to be disabled.
To disable all instrumentation plugins, specify a system property, either on the command-line or in the properties file referenced by -Dconfig=${PROPERTIES_FILE}.
sa.instrumentation.plugins.disable
To disable an individual instrumentation plugin, specify a system property, either on the command-line or in the properties file referenced by -Dconfig=${PROPERTIES_FILE}.
sa.instrumentation.plugin.${PLUGIN_NAME}.disable
The value of ${PLUGIN_NAME} is the name of the plugin as declared in the plugin's POM (Project Object Model). The names follow a consice pattern, such as okhttp for the specialagent-okhttp plugin artifactId, and web-servlet-filter for the specialagent-web-servlet-filter plugin artifactId.
To disable an individual AgentRule of an instrumentation plugin, specify a system property, either on the command-line or in the properties file referenced by -Dconfig=${PROPERTIES_FILE}.
sa.instrumentation.plugin.${PLUGIN_NAME}.${AGENT_RULE_SIMPLE_CLASS_NAME}.disable
The value of ${AGENT_RULE_SIMPLE_CLASS_NAME} is the simple class name of the AgentRule subclass that is to be disabled.
3.5 Disabling Tracer Plugins
The SpecialAgent has all of its Tracer Plugins enabled by default, and allows them to be disabled.
To disable all tracer plugins, specify a system property, either on the command-line or in the properties file referenced by -Dconfig=${PROPERTIES_FILE}.
sa.tracer.plugins.disable
To disable an individual tracer plugin, specify a system property, either on the command-line or in the properties file referenced by -Dconfig=${PROPERTIES_FILE}.
sa.tracer.plugin.${SHORT_NAME}.disable
The value of ${SHORT_NAME} is the short name of the plugin, such as lightstep or jaeger.
The following terms are used throughout this documentation.
The SpecialAgent is software that attaches to Java applications, and automatically instruments 3rd-party libraries integrated in the application. The SpecialAgent uses the OpenTracing API for Instrumentation Plugins that instrument 3rd-party libraries, as well as Tracer Plugins that implement OpenTracing tracer service providers. Both the Instrumentation Plugins, as well as the Tracer Plugins are open-source, and are developed and supported by the OpenTracing community.
The SpecialAgent supports Oracle Java and OpenJDK. When building SpecialAgent from source, only Oracle Java is supported.
4.2 Tracer
Service provider of the OpenTracing standard, providing an implementation of the io.opentracing.Tracer interface.
Examples:
Tracers are not coupled to the SpecialAgent.
4.3 Tracer Plugin
A bridge providing automatic discovery of Tracers in a runtime instrumented with the OpenTracing API. This bridge implements the TracerFactory interface of TracerResolver, and is distributed as a single "fat JAR" that can be conveniently added to the classpath of a Java process.
Tracer Plugins are not coupled to the SpecialAgent.
An OpenTracing Instrumentation project that exist as individual repositories under opentracing-contrib.
Examples:
Instrumentation Plugins are not coupled to the SpecialAgent.
A submodule of the SpecialAgent that implements the auto-instrumentation rules for Instrumentation Plugins via the opentracing-specialagent-api.
Examples:
Instrumentation Rules are coupled to the SpecialAgent.
- The SpecialAgent must allow any Instrumentation Plugin available in opentracing-contrib to be automatically installable in applications that utilize a 3rd-party library for which an Instrumentation Plugin exists.
- The SpecialAgent must automatically install the Instrumentation Plugin for each 3rd-party library for which a module exists, regardless in which class loader the 3rd-party library is loaded.
- The SpecialAgent must not adversely affect the runtime stability of the application on which it is intended to be used. This goal applies only to the code in the SpecialAgent, and cannot apply to the code of the Instrumentation Plugins made available in opentracing-contrib.
- The SpecialAgent must Static Attach and Dynamic Attach to applications running on JVM versions 1.7, 1.8, 9, and 11.
- The SpecialAgent must implement a lightweight test methodology that can be easily applied to a module that implements instrumentation for a 3rd-party library. This test must simulate:
- Launch the test in a process simulating the
-javaagentvm argument that points to the SpecialAgent (in order to test auto-instrumentation functionality). - Elevate the test code to be executed from a custom class loader that is disconnected from the system class loader (in order to test bytecode injection into an isolated class loader that cannot resolve classes on the system classpath).
- Allow tests to specify their own
Tracerinstances viaGlobalTracer, or initialize aMockTracerif no instance is specified. The test must provide a reference to theTracerinstance in the test method for assertions with JUnit.
- Launch the test in a process simulating the
- The SpecialAgent must provide a means by which Instrumentation Plugins can be configured before use on a target application.
- The SpecialAgent is not designed to modify application code, beyond the installation of Instrumentation Plugins. For example, there is no facility for dynamically tracing arbitrary code.
The following plugins have Instrumentation Rules implemented.
- OkHttp3 [3.5.0,]
- JDBC API (
java.sql) [3.1,] - Concurrent API (
java.util.concurrent) - Java Servlet API (
javax.servlet) [2.3,] [] - Mongo Driver [3.9.0,]
- Apache Camel [2.24.0,]
- AWS SDK [1.11.79,]
- AWS SDK 2 [2.1.4,]
- Cassandra Driver [3.0.0,]
- JMS API (
javax.jmsv1 & v2) - JMS Spring [5.0.0.RELEASE,]
- Elasticsearch Client [6.4.0,]
- RxJava 2 [2.1.0,]
- Kafka Client [1.1.0,]
- Kafka Spring [2.2.0.RELEASE,]
- AsyncHttpClient [2.7.0,]
- RabbitMQ Client [5.0.0,]
- RabbitMQ Spring [2.0.0.RELEASE,]
- Thrift [0.12.0,]
- GRPC [1.6.0,]
- Jedis Client [2.7.0,]
- Apache HttpClient [4.4,]
- Lettuce Client [5.1.0.RELEASE,]
- Spring Web [5.0.0.RELEASE,]
- Spring Web MVC [5.0.0.RELEASE,]
- Spring WebFlux [5.1.0.RELEASE,]
- Spring WebSocket STOMP [5.1.0.RELEASE,]
- Redisson [3.6.0,]
- Grizzly HTTP Server [2.3.35,]
- Grizzly AsyncHttpClient [1.15,]
- Reactor [3.2.3.RELEASE,]
- Hazelcast [3.7,]
- Spymemcached [2.11.0,]
- Feign [9.0.0,]
- Zuul [1.0.0,]
- Spring @Async and @Scheduled [5.0.0.RELEASE,]
- Spring Messaging [5.1.1.RELEASE,]
- Spring 3 Web [3.0.3.RELEASE,]
- Spring 3 Web MVC [3.0.2.RELEASE,]
- JAX-RS Client [2.0,]
6.2 Tracer Plugins
The following OpenTracing tracer service providers have Tracer Plugins implemented. Here is a demo.
The following libraries are instrumented by existing Instrumentation Rules.
Thank you to the following contributors for developing instrumentation plugins:
Thank you to the following contributors for developing tracer plugins:
Thank you to the following developers for filing issues and helping us fix them:
Thank you to the following individuals for noticing typographic errors and sending PRs:
Finally, thanks for all of the feedback! Please share your comments as an issue!
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
This project is licensed under the Apache 2 License - see the LICENSE.txt file for details.
