java agent代码

Tyler Benson 6f985c15bf Remove Byteman now that instrumentation is migrated před 7 roky
.circleci bea17a436a Separate CI building and testing steps, and increase max memory. před 7 roky
buildSrc fd8f6a940e Exclude draft version. před 7 roky
dd-java-agent 6f985c15bf Remove Byteman now that instrumentation is migrated před 7 roky
dd-java-agent-ittests 6f985c15bf Remove Byteman now that instrumentation is migrated před 7 roky
dd-trace d933a4049a Move OkHttp instrumentation to byte buddy před 7 roky
dd-trace-annotations 3bc153241b Abstract core dependencies for central management před 7 roky
dd-trace-examples 6f985c15bf Remove Byteman now that instrumentation is migrated před 7 roky
gradle d933a4049a Move OkHttp instrumentation to byte buddy před 7 roky
.editorconfig e16ad373ad Apply and enforce google checkstyle and formatting před 7 roky
.gitignore 78210d6e42 Add additional cases for benchmarking. před 7 roky
LICENSE c882d1d3c8 Create LICENSE před 7 roky
LICENSE-3rdparty.csv 6f985c15bf Remove Byteman now that instrumentation is migrated před 7 roky
NOTICE d572bb5f85 Create NOTICE před 7 roky
README.md f44b50f6d4 Fix maven doc. před 7 roky
dd-trace-java.gradle 2a3ea4aab6 Upgrade gradle version to latest. před 7 roky
gradlew 5bfff0bece Add gradlew to examples and update docs před 7 roky
gradlew.bat 5a6bd2d002 Add gradle build scripts. před 7 roky
settings.gradle d933a4049a Move OkHttp instrumentation to byte buddy před 7 roky

README.md

What is Datadog APM?

Datadog APM traces the path of each request through your application stack, recording the latency of each step along the way. It sends all tracing data to Datadog, where you can easily identify which services or calls are slowing down your application the most.

This repository contains what you need to trace Java applications. Two quick notes up front:

  • Datadog Java APM is currently in Beta
  • Datadog Java APM can only trace applications running Java 1.7 or later

The Components

These three things help you instrument Java applications:

Datadog Java Agent: a Java Agent that, when passed to your application:

  1. Automatically traces many Java frameworks, application servers, and databases using some of the libraries from opentracing-contrib, and
  2. Lets you add annotations to your methods to easily trace their execution times.

Note: dd-java-agent is considered experimental. Some integrations may not activate in all cases. Additional manual instrumentation using the Opentracing API is strongly encouraged.

Datadog Tracer: an OpenTracing-compatible library that lets you trace any piece of your Java code, not just whole methods.

Datadog APM Agent: a (non-Java) service that runs on your application servers, accepting trace data from the Datadog Java Agent and/or Datadog Tracer and sending it to Datadog. (The APM Agent is not part of this repo; it's the same Agent to which all Datadog tracers—Go, Python, etc—send data)

Getting Started

Before instrumenting your code, install the Datadog Agent on your application servers (or locally, if you're just trying out Java APM) and enable the APM Agent. See the special instructions for macOS and Docker if you're using either.

Automatic Tracing

Java Agent Setup

Download the latest Datadog Java Agent:

wget -O dd-java-agent.jar 'https://search.maven.org/remote_content?g=com.datadoghq&a=dd-java-agent&v=LATEST'

Then create a file dd-trace.yaml anywhere in your application's classpath (or provide the file's path via -Ddd.trace.configurationFile when starting the application):

# Main service name for the app
defaultServiceName: my-java-app

writer:
  type: DDAgentWriter # send traces to Datadog Trace Agent; only other option is LoggingWriter
  host: localhost     # host/IP address where Datadog Trace Agent listens
  port: 8126          # port where Datadog Trace Agent listens

sampler:
  type: AllSampler # Collect 100% of traces; only other option is RateSample
# rate: 0.5        # if using type: RateSample, uncomment to collect only 50% of traces

# Skip traces whose root span tag values matches some these regexps; useful if you want to skip health checks traces from your service stats
# skipTagsPatterns: {"http.url": ".*/demo/add.*"}

Note: this configuration file is also required for Manual Instrumentation with the Datadog Tracer.

Finally, add the following JVM argument when starting your application—in your IDE, your Maven or gradle application script, or your java -jar command:

-javaagent:/path/to/the/dd-java-agent.jar

The Java Agent—once passed to your application—automatically traces requests to the frameworks, application servers, and databases shown below. It does this by using various libraries from opentracing-contrib. In most cases you don't need to install or configure anything; traces will automatically show up in your Datadog dashboards.

Application Servers

Server Versions Comments
Java Servlet Compatible 2.3+, 3.0+ HTTP client calls with cross-process headers are linked

Note: Many application servers are Servlet compatible such as Tomcat, Jetty, Websphere, Weblogic, etc. Also, frameworks like Spring Boot and Dropwizard inherently work because they use a Servlet compatible embedded application server.

Frameworks

Framework Versions Comments
OkHTTP 3.x HTTP client calls with cross-process headers are linked
Apache HTTP Client 4.3 + HTTP client calls with cross-process headers are linked
AWS SDK 1.11.0+ Trace all client calls to any AWS service
Web Servlet Filters Depends on web server See Application Servers
JMS 2 2.x Trace calls to message brokers; distributed trace propagation not yet supported

Databases

Database Versions Comments
JDBC 4.x Intercepts calls to JDBC compatible clients
MongoDB 3.x Intercepts all the calls from the MongoDB client
Cassandra 3.2.x Intercepts all the calls from the Cassandra client

To disable tracing for any of these libraries, list them in disabledInstrumentations within dd-trace.yaml:

...

# Disable tracing on these
disabledInstrumentations: ["opentracing-apache-httpclient", "opentracing-mongo-driver", "opentracing-web-servlet-filter"]

See this YAML file for the proper names of all supported libraries (i.e. the names as you must list them in disabledInstrumentations).

The @Trace Annotation

The Java Agent lets you add a @Trace annotation to any method to measure its execution time. Setup the Java Agent first if you haven't done so.

Setup

Add the dd-trace-annotations dependency to your project. For Maven, add this to pom.xml:

<dependency>
	<groupId>com.datadoghq</groupId>
	<artifactId>dd-trace-annotations</artifactId>
	<version>{version}</version>
</dependency>

For gradle, add:

compile group: 'com.datadoghq', name: 'dd-trace-annotations', version: {version}

Then, in dd-trace.yaml, list any applications where you want to use @Trace:

enableCustomAnnotationTracingOver: ["com.example.myproject"]`.

The Java Agent lets you use @Trace not just for com.example.myproject, but also for any application whose name begins like that, e.g. com.example.myproject.foobar. If you're tempted to list something like ["com", "io"] to avoid having to fuss with this configuration as you add new projects, be careful; providing @Trace-ability to too many applications could hurt your package's build time.

Example

Add an annotation to some method in your code:

@Trace
public void myMethod() throws InterruptedException{
		...
}

You can pass an operationName to name the trace data as you want:

@Trace(operationName="database.before")
public void myMethod() throws InterruptedException{
	....
}

When you don't pass an operationName, the Java Agent sets it to the method name.

Manual Instrumentation

You can use the Datadog Tracer (dd-trace) library to measure execution times for specific pieces of code. This lets you trace your application more precisely than you can with the Java Agent alone.

Setup

For Maven, add this to pom.xml:

<!-- OpenTracing API -->
<dependency>
    <groupId>io.opentracing</groupId>
    <artifactId>opentracing-api</artifactId>
    <version>0.30.0</version>
</dependency>

<!-- OpenTracing Util -->
<dependency>
    <groupId>io.opentracing</groupId>
    <artifactId>opentracing-util</artifactId>
    <version>0.30.0</version>
</dependency>

<!-- Datadog Tracer (only needed if you do not use dd-java-agent) -->
<dependency>
    <groupId>com.datadoghq</groupId>
    <artifactId>dd-trace</artifactId>
    <version>${dd-trace-java.version}</version>
</dependency>

For gradle, add:

compile group: 'io.opentracing', name: 'opentracing-api', version: "0.30.0"
compile group: 'io.opentracing', name: 'opentracing-util', version: "0.30.0"
compile group: 'com.datadoghq', name: 'dd-trace', version: "${dd-trace-java.version}"

Examples

Rather than referencing classes directly from dd-trace (other than registering DDTracer), we strongly suggest using the OpenTracing API. Additional documentation on the api is also available.

Let's look at a simple example.

class InstrumentedClass {

    void method0() {
        // 1. Make sure dd-trace.yaml is in your resources directory
        // 2. If using the Java Agent (-javaagent;/path/to/agent.jar), do not instantiate the GlobalTracer; the Agent instantiates it for you
        Tracer tracer = io.opentracing.util.GlobalTracer.get();

        Span span = tracer.buildSpan("operation-name").startActive();
        span.setTag(DDTags.SERVICE_NAME, "my-new-service");

        // The code you're tracing
        Thread.sleep(1000);

        // If you don't call finish(), the span data will NOT make it to Datadog!
        span.finish();
    }
}

Alternatively, you can wrap the code you want to trace in a try-with-resources statement:

class InstrumentedClass {

    void method0() {
        Tracer tracer = io.opentracing.util.GlobalTracer.get();

        try (ActiveSpan span = tracer.buildSpan("operation-name").startActive()) {
            span.setTag(DDTags.SERVICE_NAME, "my-new-service");
            Thread.sleep(1000);
        }
    }
}

In this case, you don't need to call span.finish().

Finally, you must provide a configured tracer. This can be easily done by using the TracerFactory or manually in the bootstrap method (i.e. main).

public class Application {

    public static void main(String[] args) {

        // Initialize the tracer from the configuration file
        Tracer tracer = DDTracerFactory.createFromConfigurationFile();
        io.opentracing.util.GlobalTracer.register(tracer);

        // OR from the API
        Writer writer = new com.datadoghq.trace.writer.DDAgentWriter();
        Sampler sampler = new com.datadoghq.trace.sampling.AllSampler();
        Tracer tracer = new com.datadoghq.trace.DDTracer(writer, sampler);
        io.opentracing.util.GlobalTracer.register(tracer);

        // ...
    }
}

DDTracerFactory looks for dd-trace.yaml in the classpath.

Further Reading

Get in touch

If you have questions or feedback, email us at tracehelp@datadoghq.com or chat with us in the datadoghq slack channel #apm-java.