Automatic application naming

The names you give to your applications can help organize your New Relic performance metrics.

New Relic's Java agent reports all transactions and other metrics in a single JVM to the application name specified in newrelic.yml under the setting app_name (for more information about application naming, see Naming your application). By changing enable_auto_app_naming in newrelic.yml to true, applications will be named for their context, filter, servlet, or request attribute.

This setting allows you to run several web applications in a single JVM while reporting transactions, errors, and metrics to other appropriate applications in the New Relic user interface. Background tasks will still use the default application name from the newrelic.yml.

Note: These configuration changes require a JVM restart to take effect.

Application name sources

When you set enable_auto_app_naming to true, New Relic uses the following sources of information to name your applications:

App type Naming source
Background tasks The default application name from newrelic.yml
Web transactions

In order by priority:

  • Request attribute (highest priority)
  • Servlet init parameter
  • Filter init parameter
  • Web app context parameter
  • Web app context name (display-name)
  • Web app context path (lowest priority)

Request attribute

The APPLICATION_NAME request attribute takes precedence over any settings in the XML. Set this attribute as early as possible in the web transaction. If called multiple times, the last invocation determines the application name.

APPLICATION_NAME only works with ServletRequests.

To use the request attribute for fine-grained application naming based on the request URI:

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ...
   String requestUri = httpServletRequest.getRequestURI();

   if (requestUri.startsWith("/my-special-request/")) {
       request.setAttribute("com.newrelic.agent.APPLICATION_NAME", "MySpecialWebApp");
    }
    ...

Servlet init parameter

To set the application name for individual servlets in web.xml using init parameters:

<servlet>
 <servlet-name>SqlServlet</servlet-name>
 <servlet-class>test.SqlServlet</servlet-class>
 <init-param>
   <param-name>com.newrelic.agent.APPLICATION_NAME</param-name>
   <param-value>MyServletApp</param-value>
 </init-param>
</servlet>

New Relic obtains the value of the init-param by calling

javax.servlet.ServletConfig#getInitParameter(String)


with the argument com.newrelic.agent.APPLICATION_NAME.

If a web request calls multiple servlets, the init-param of the first servlet to finish has precedence. A servlet that does not have an init-param uses the default application name for the web app.

Besides declaring the application name in XML, you can also set it in your application code by storing an attribute in the javax.servlet.ServletRequest:

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ...
    request.setAttribute("com.newrelic.agent.APPLICATION_NAME", "MyWebApp");
    ...

Filter init parameter

If your web app does not have servlets, you can use an init parameter for a filter:

<filter>
  <filter-name>SqlFilter</filter-name>
  <filter-class>test.SqlFilter</filter-class>
  <init-param>
    <param-name>com.newrelic.agent.APPLICATION_NAME</param-name>
    <param-value>MyFilterApp</param-value>
   </init-param>
</filter>

New Relic obtains the value of the init-param by calling javax.servlet.FilterConfig#getInitParameter(String) with the argument com.newrelic.agent.APPLICATION_NAME. Servlets takes precedence over filters for application naming, so the init parameter for a filter is used only if no servlets were invoked. The init-param of the first filter to finish has precedence.

Context parameter

To use a context parameter to set the application name:

<context-param>
  <param-name>com.newrelic.agent.APPLICATION_NAME</param-name>
  <param-value>MyWebApp</param-value>
</context-param>

New Relic obtains the value of the context parameter is by calling javax.servlet.ServletContext#getInitParameter(String) with the argument com.newrelic.agent.APPLICATION_NAME. The context parameter takes precedence over the display-name element.

Display name

To determine the application name by using the display-name element in the web.xml:

<display-name>MyWebApp</display-name>

New Relic obtains the value of the display-name element by calling javax.servlet.ServletContext#getServletContextName().

Context path

If there is no display-name element, and none of the other higher hierarchy methods are used to set the app name, the application name comes from the context path of the web app. New Relic obtains the context path by calling javax.servlet.ServletContext#getContextPath().

The context path is the portion of the request URI that is used to select the context of the request. The context path always comes first in a request URI. For example, if the URL is http://example.com/newrelic-axis2-ws/getWeather:

  • The request URI is /newrelic-axis2-ws/getWeather.
  • The context path is /newrelic-axis2-ws.
  • The application is named newrelic-axis2-ws.

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.