Java instrumentation by XML

For your New Relic-monitored Java application, one custom instrumentation method is to use an XML file to specify your app methods that should be instrumented. The Java agent will read the XML file and instrument the relevant classes on startup; XML files added to the extensions directory after startup do not require a JVM restart to be detected.

See Java agent custom instrumentation for a description of the custom instrumentation options and the reasons to use them.

Accessing your XML file

From the New Relic menu bar, go to rpm.newrelic.com/apm > (select an app) > Settings > Live Instrumentation. From here you can:

  • Download a sample XML file.
  • Select an edit existing XML file.
  • Search the instrumentation history.

XML file structure

Your New Relic agent download package (newrelic_agent.zip) contains two templates for using XML instrumentation:

  • The extension.xsd template is the XML schema definition which all custom extensions must follow.
  • The extension-example.xml template is an example. This file instruments some of the basic JDK methods. If you edit and rename this file, ensure you modify the name="extension-example" attribute to match the new filename.

For more information about the properties in the extension.xsd template, see the XML file format overview.

XML file validation

Before starting your application, validate your XML file using New Relic's command line tool. (The command line tool can only be used when your classes are in a jar.) Run the following command:

java -Djava.ext.dirs=/path/to/jarred/classes -jar newrelic.jar instrument -file /path/to/file.xml -debug true

The argument -Djava.ext.dirs points to the directory containing the jar or jars with your source code files.

The application is run with the newrelic.jar. The call must contain the keyword instrument followed by the -file flag with the path to your XML file. The property -debug is optional and may be set to true to provide more information during the validation.

The tool will:

  • Validate the XML syntax.
  • Check that each class to be instrumented is present on the class path.
  • Check that each method is contained in the appropriate class.

If the XML file is valid, you will see a pass statement printed to the terminal:

PASS: The extension at file.txt was successfully validated.

If the XML fails validation, you will see a failure message printed to the terminal:

FAIL: reason

XML file location

The Java Agent reads any XML files in the extensions directory on process start up. The extensions directory is also read every harvest cycle (typically once per minute). XML files added to this directory during runtime will be read within a few minutes and so do not require a JVM restart.

There are two ways to specify the XML file location:

XML file location options Procedure
Create an extensions directory
  1. In the directory where the newrelic.jar and newrelic.yml is located, create a directory named extensions.
  2. Put the XML file in this directory with a .xml extension.
  3. In newrelic.yml, check that the property extensions.dir is not set.
Specify an existing extensions directory
  1. In the newrelic.yml configuration file under the common section, use the property extensions.dir to specify the directory where the XML file is located.
  2. Make sure the file is in the specified directory and that the file has a .xml extension.

Verify the file was read

To verify the agent read the XML file, first set the New Relic logging to finer:

  1. Edit the newrelic.yml configuration file and change the log_level property:

    log_level: finer
  2. Start or restart the application.

If the read was successful, logs/newrelic_agent.log will indicate this in a statement similar to:

Reading custom extension file /path/to/file.xml

If no statement appears in logs/newrelic_agent.log, then the XML file was not found. Check the location of the XML file, and ensure the New Relic process has read access to the file.

Multiple XML files

While using a single XML file is preferred, you can use multiple custom XML files. These files will all be instrumented as long as the extension name in the files is unique.

  • If two custom XML files contain the same extension name, then the one with the highest version will be implemented and the other will be ignored.
  • If the two files have the same name and version, then the first file read by the agent will be implemented and the other will be ignored.

XML and YAML

Prior to 2.10.0, YAML files could be used for custom instrumentation. These legacy YAML files are still supported. However, new users should use XML files for custom instrumentation.

Legacy users: If both a YAML and XML file exist with the same extension name, only the XML file will be instrumented. The YAML file will be ignored. If you want both the YAML and XML files to be instrumented, give each file a different extension name.

Example XML file

For an example of a custom instrumentation XML file, see Java XML examples.

For more help

Join the discussion about Java monitoring in the New Relic Online Technical Community! The Technical Community is a public platform to discuss and troubleshoot your New Relic toolset.

If you need additional help, get support at support.newrelic.com.