1. Introduction

Micronaut NewRelic helps send custom events to NewRelic.

2. Installation

Micronaut NewRelic helps send custom events to NewRelic.

There are two implementations of the client, one using Micronaut HTTP client with the NewRelic Java library, and the default implementation which logs the events' JSON to the com.agorapulse.micronaut.newrelic.NewRelicInsightsService log.

2.1. Default Implementation

If you are developing a library, you can let the developers choose the implementation later. You only need to depend on this library:

repositories {
    mavenCentral()
}

dependencies {
    // standard Micronaut dependencies
    implementation 'com.agorapulse:micronaut-newrelic:2.0.8'
}

2.2. HTTP Client

To enable the HTTP client implementation, add a dependency on this library and micronaut-http-client:

repositories {
    mavenCentral()
}

dependencies {
    // standard Micronaut dependencies
    implementation 'com.agorapulse:micronaut-newrelic:2.0.8'
    implementation 'io.micronaut:micronaut-http-client'

    // for reporting Micronaut URL from controllers
    implementation 'com.agorapulse:micronaut-newrelic-http:2.0.8'
}

You must also specify values for the newrelic.url and newrelic.token properties:

newrelic:
  url: https://insights-collector.newrelic.com/v1/accounts/123456
  token: abcdef1234567890

If you need more control about the HTTP client settings then you can use configure the HTTP client with Micronaut HTTP Services newrelic-insights client.

newrelic:
  token: abcdef1234567890
micronaut:
  http:
    services:
      newrelic-insights:
        url: https://insights-collector.newrelic.com/v1/accounts/123456
        event-loop-group: newrelic-insights
  netty:
    event-loops:
      newrelic-insights:
        executor: io

2.3. NewRelic Library

To enable the NewRelic client implementation, add a dependency on this library and the NewRelic Java library. The Java agent must be running alongside.

repositories {
    mavenCentral()
}

dependencies {
    // standard Micronaut dependencies
    implementation 'com.agorapulse:micronaut-newrelic:2.0.8'
    implementation 'com.newrelic.agent.java:newrelic-api:4.9.0'

    // for reporting Micronaut URL from controllers
    implementation 'com.agorapulse:micronaut-newrelic-http:2.0.8'
}

3. Usage

Micronaut NewRelic library provides the NewRelicInsightsService bean with two methods, createEvent and createEvents:

newRelicInsightsService.createEvent(NewRelicInsightsEvent.create("TestEvent", "key", 10));
newRelicInsightsService.createEvents(Arrays.asList(
    new MyEvent().withKey("key").withValue(20),
    NewRelicInsightsEvent.create("TestEvent", "key", 10)
));

Events can be instances of any class annotated with @Introspected. In that case the event type will be same as the name of the event class, e.g., MyEvent.

If you use the library in a non-Micronaut environment (e.g., Grails), ensure your @Introspected classes are processed by micronaut-inject-java or micronaut-inject-groovy.

The events can optionally implement the NewRelicInsightsEvent interface, which let you fine-tune the eventType and timestamp properties.

All event values are truncated to comply to the NewRelic Insights API limits:

You can have getters annotated with @Flatten to include them in the event. Those getters should return a Map<String, Object>. Then all the entries in the map will be included in the event. If you have multiple getters annotated with @Flatten, the entries will be merged with no guarantee on the order. The recommendation is to only have one getter annotated with @Flatten.

There might be some HTTP communication issues when communicating with the NewRelic Insights API when using the HTTP client. By default, the error is only logged as a warning. You can fine tune the logging level by setting newrelic.log-level Micronaut property to one of the possible values: TRACE, DEBUG, INFO, WARN, ERROR, OFF.

You can mark your events with @Critical annotation to try to retry the event delivery when there is any communication issue such as connection timeout or reset. The default number of retries is 3, but you can change it by setting the newrelic.retry-count Micronaut property. If you are using NewRelicInsightsEvent interface, you can also mark your event critical by returning true from the isCritical method. If you are not implementing the NewRelicInsightsEvent interface then you can still add critical property to your event and set it to true.