• /
  • Log in

Java agent API: Instrument using annotation

New Relic's Java agent provides several options for custom instrumentation. One of those options is adding the Java agent API's @Trace, @TraceLambda or @TraceByReturnType annotations to your application code. This document describes how to use annotations.

Important

To use annotations, you must modify the source code. If you can't or don't want to modify your source code, see Custom instrumentation for other instrumentation options.

Configure your agent for annotations

By default, the configuration setting enable_custom_tracing is set to true in the Java agent, which is the setting required for @Trace annotations to function. This setting is not included in the newrelic.yml by default.

The only time you need to incorporate this setting into your configuration file is if you want to disable @Trace annotations altogether. To do this, set enable_custom_tracing: false (prefaced with two spaces) in the common stanza of your newrelic.yml.

Make sure that newrelic-api.jar appears in your classpath as it contains all these annotations.

@Trace

Annotating a method with @Trace tells the Java agent that measurements should be taken for that method.

To add a method call as a custom trace add @Trace annotations to your method.

import com.newrelic.api.agent.Trace;
...
@Trace
public void run() {
// background task
}

Create a new transaction

If transactions do not appear and you want to start a new transaction, include dispatcher=true with the @Trace annotation:

@Trace (dispatcher=true)
public void run() {
// background task
}

Add detail to your transactions

If your transaction traces show large blocks of uninstrumented time and you want to include some more methods within the trace, you can use the @Trace annotation without parameters:

@Trace
protected void methodWithinTransaction() {
// work
}

Convert a transaction to a web request

To make a background task report as a web browser transaction with a Java agent API call: In the method annotated with @Trace(dispatcher=true), call:

NewRelic.setRequestAndResponse(Request request, Response response)

The arguments are implementations of the Request and Response interfaces in newrelic-api.jar.

Important

Even if your Request and Response objects already are present, you still need to add this API call.

Define your own @Trace annotation class

If you define your own @Trace annotation class, there is no dependency on the newrelic-api.jar. To define the class:

package com.test;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Trace {
public static final String NULL = "";
String metricName() default NULL;
boolean dispatcher() default false;
String tracerFactoryName() default NULL;
}

Then, configure the agent to use this annotation in the common section of the newrelic.yml:

class_transformer:
trace_annotation_class_name: com.test.Trace

Properties for @Trace

The @Trace annotation supports the following properties.

@TraceLambda

If your transaction traces show large blocks of uninstrumented time and you want to include lambda expressions within the trace, you can use the @TraceLambda annotation without parameters:

import com.newrelic.api.agent.TraceLambda;
@TraceLambda
class ClassContainingLambdaExpressions() {
// work
}

Lambda expressions become static methods of the containing class after compilation. By default, static methods within classes marked with the @TraceLambda annotation matching the annotations pattern will be marked with the @Trace annotation.

Properties for @TraceLambda

The @TraceLambda annotation supports the following properties.

@TraceByReturnType

To include methods with a particular return type within the trace, you can use the @TraceByReturnType annotation to mark a class passing the return types as a property. Methods in annotated classes that match one of the specified return types will be marked with the @Trace annotation.

@TraceByReturnType(traceReturnTypes={Integer.class, String.class})
class ClassContainingMethods() {
// ...
}

Properties for @TraceByReturnType

The @TraceByReturnType annotation supports the following properties.

Performance considerations

When the Java agent is present in the JVM, it will inject code on the annotated methods. The performance hit is negligible in heavyweight operations, such as database or webservice calls, but is noticeable in methods that are called frequently, such as an accessor called thousands of times a second.

Caution

Do not instrument all of your methods, as this can lead to decreased performance and to a metric grouping issue.

More API functions

For more about the Java agent API and its functionality, see the Java agent API introduction.

For more help

If you need more help, check out these support and learning resources:

Create issueEdit page
Copyright © 2021 New Relic Inc.