Observability - New Relic Java Agent - Custom Configuration

Observability - New Relic Java Agent - Custom Configuration

WHY CUSTOM CONFIGURATION?

There may be occasions where you need to bridge the New Relic Java agent to your custom needs. The agent is open source, and you can extend it with your own configuration properties.

This article shows you how to add a custom configuration option to the New Relic Java agent.

STEP 1: Create the Custom Config Interface

Ref: Newrelic Java Agent config ref

package com.newrelic.agent.config;

public interface CustomConfig {

    String CLEANUP_TEMP_FILES_ON_EXIT = "cleanup_temp_files_on_exit";
    boolean DEFAULT_CLEANUP_TEMP_FILES_ON_EXIT = true;

    /**
     @return configuration to determine whether to delete temp files created by NewRelic java agent.
     */
    boolean isDeleteTempFilesOnExit();
}        

STEP 2: Create the Implementation

Ref: Newrelic Java Agent config ref

package com.newrelic.agent.config;

import java.util.Map;

public class CustomConfigImpl extends BaseConfig implements CustomConfig {

    private final boolean cleanTempFilesOnExit;

    public CustomConfigImpl(Map<String, Object> props, String systemPropertyPrefix) {
        super(props, systemPropertyPrefix);
        cleanTempFilesOnExit = getProperty(CLEANUP_TEMP_FILES_ON_EXIT, 
                                           DEFAULT_CLEANUP_TEMP_FILES_ON_EXIT);
    }

    @Override
    public boolean isDeleteTempFilesOnExit() {
        return cleanTempFilesOnExit;
    }
}        

STEP 3: Modify Nerelic Agent's AgentConfigImpl

Ref: AgentConfigImpl.java

Change the class hierarchy:

FROM:

public class AgentConfigImpl extends BaseConfig implements AgentConfig {        

TO:

public class AgentConfigImpl extends CustomConfigImpl implements AgentConfig {        

STEP 4: Build Your Custom Agent

Follow newrelic java agent build instructions to create youe own custom newrelic java agaent jar to be used subsequently.

USING YOUR CUSTOM CONFIGURATION

Once built, your new configuration is available:

newrelic.config.cleanup_temp_files_on_exit: true        

Or via system property:

java -Dnewrelic.config.cleanup_temp_files_on_exit=false -javaagent:newrelic.jar -jar your-app.jar        

SUMMARY

✅ Create interface defining your config property

✅ Create implementation extending BaseConfig

✅ Modify AgentConfigImpl to extend your implementation

✅ Build and use your custom agent jar

This pattern lets you add any custom configuration your organization needs while maintaining compatibility with the core New Relic agent.

Ramesh Dara

Linkedin . GitHub . Blog

To view or add a comment, sign in

Explore content categories