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.1'
}

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.1'
    implementation 'io.micronaut:micronaut-http-client'

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

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.1'
    implementation 'com.newrelic.agent.java:newrelic-api:4.9.0'

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

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.