The New Relic Java agent API lets you control, customize, and extend the functionality of the Java agent. This API consists of:
- Static methods on the
com.newrelic.api.agent.NewRelic
class - A @Trace annotation for implementing custom instrumentation
- A hierarchy of API objects providing additional functionality
Use this API to set up custom instrumentation of your Java app and collect more in-depth data. For detailed information about this API, see the complete Javadoc on GitHub.
Another way to set up custom instrumentation is to use XML instrumentation. The XML option is simpler and does not require modification of your app code, but it lacks the complete functionality of the Java agent API.
Importante
For best results when using the API, ensure that you have the latest Java agent release. Several APIs used in the examples require Java agent 6.4.0 or higher.
For all available New Relic APIs, see Intro to APIs.
Use the API
To access the API, download it from maven central via a build tool.
Here's an example for gradle where you can replace ${agent.version}
with version of your agent:
implementation 'com.newrelic.agent.java:newrelic-api:${agent.version}'
You can call the API even without the agent running, but the methods will be a no-op until the agent loads your application.
Transactions
To instrument Transactions
in your application, use the following APIs.
If you want to... | Use this |
---|---|
Create a |
|
Capture the duration of a method that New Relic does not automatically trace |
|
Set the name of the current | |
Start the timer for the response time of the current | |
Add custom attributes to | |
Adds user tracking to | |
Prevent a | |
Exclude a |
See related logs
To see logs directly within the context of your application's errors and traces, use these API calls to annotate your logs:
For more information about correlating log data with other telemetry data, see our logs in context documentation.
Instrument asynchronous work
For detailed information, see Java agent API for asynchronous applications.
If you want to... | Use this |
---|---|
Trace an asynchronous method if it is linked to an existing | |
Link the | |
Expire a | |
Stop timing a | |
Stop timing a |
Distributed tracing API usage
These APIs require distributed tracing to be enabled. See Java agent configuration for all distributed tracing config options.
Distributed tracing lets you see the path that a request takes as it travels through a distributed system. For general instructions on how to use the calls below to implement distributed tracing, see Use distributed tracing APIs. To see these APIs in action, see Using Java agent distributing tracing API with Kafka.
Importante
With agent version 6.4.0, the following distributed tracing APIs were introduced, with the exception of addCustomAttribute()
, which was introduced in 6.1.0. We highly recommended using these APIs instead of the deprecated ones.
If you want to... | Use this |
---|---|
Learn about the type-specific headers of an inbound or outbound message. | For a provided implementation of See an example of W3C trace context headers implementation in DT API usage with Kafka. |
Create and insert distributed tracing headers into a | For more on obtaining references to the current transaction and other API classes, see Obtain references. |
Accept the distributed tracing headers sent from the calling service and link these services together in a distributed trace. | For more on obtaining references to the current transaction and other API classes, see Obtain references. |
Understand a utility class that provides enum constants for defining the transport type when accepting distributed tracing headers. | |
Add custom attributes to |
|
Cuidado
With agent version 6.4.0, the following distributed tracing APIs have been deprecated and replaced by the APIs in the above table. It's highly recommended to upgrade the agent and use the new APIs instead of these deprecated ones.
If you want to... | Use this |
---|---|
Create a payload to be sent to a called service. | For more on obtaining references to the current transaction and other API classes, see Obtain references. CuidadoAPI deprecated as of agent 6.4.0 |
Accept a payload sent from the first service; this will link these services together in a trace. | For more on obtaining references to the current transaction and other API classes, see Obtain references. CuidadoAPI deprecated as of agent 6.4.0 |
Payload used to connect services. The |
CuidadoAPI deprecated as of agent 6.4.0 |
Payload used to connect services. The |
CuidadoAPI deprecated as of agent 6.4.0 |
Cross application tracing (CAT) API usage
Importante
Cross application tracing has been deprecated as of agent version 7.4.0 and will be removed in a future agent version.
Instead of using cross application tracing, we recommend our distributed tracing features. Distributed tracing is an improvement on the cross application tracing feature and is recommended for large, distributed systems.
To track external calls and add cross application tracing, use the following APIs:
If you want to... | Use this |
---|---|
Trace across a custom transport channel that New Relic does not support by default, such as a proprietary RPC transport | Also refer to the information in this document about using |
View or change the metric name or a rollup metric name of a (A rollup metric name, such as | Also refer to the information in this document about using |
Report a call to an external HTTP service, database server, message queue, or other external resource that is being traced using the Java agent API's |
Also refer to the information in this document about using |
Enable and add cross application tracing when communicating with an external HTTP or JMS service that is instrumented by New Relic |
Also refer to the information in this document about using |
Add timing for an application server or dispatcher that is not supported automatically |
Also refer to the information in this document about using |
Obtain references to New Relic API classes
Other tasks require the New Relic Agent
object. The Agent
object exposes multiple objects that give you the following functionality:
If you want to... | Use this |
---|---|
Get a reference to the current | |
Get a | |
Start and get a reference to a | |
Get a reference to the method currently being traced | |
Get a reference to the | |
Get a reference to the | |
Get a reference to an aggregator for custom metrics | |
Get a reference to |
Additional API functionality
The following APIs provide additional functionality, such as setting app server info, reporting errors, adding page load timing information, recording custom metrics, and sending custom events.
If you want to... | Use this |
---|---|
Explicitly set port, name, and version information for an application server or dispatcher and the instance name for a JVM | |
Report an error that New Relic does not report automatically | When inside a transaction, the last call to |
Group errors with your own custom defined fingerprint, implement the |
|
Set your own error fingerprint via a callback function.
To register an | |
Add browser page load timing for | |
Create and accumulate custom metrics |
|
Record custom events |
Or, use Also refer to the information in this document about using |
Provide general cloud provider account information to the agent |
With this method, you can provide the type of account information and its value. The agent uses this information to populate the attribute AWS DynamoDB and Kinesis are services that require this value to populate the Calling this method overrides the respective cloud configuration. The call shown above overrides |
Provide SDK client specific cloud provider account information to the agent |
This call provides the same information as the preceding method, but it is specific to the SDK client provided. When the specified SDK client is used, this data will be used instead of the general one. |
Additional API usage examples
For detailed code examples about using the APIs, see New Relic's documentation about custom instrumentation for:
- External calls, cross application traces, messaging, datastores, and web frameworks
- Cross application tracing and external datastore calls
- Apps using custom instrumentation with annotation
- Custom framework instrumentation API
- Preventing unwanted instrumentation
- Inserting custom attributes
- Inserting custom events
- Collecting custom metrics