Class NewRelicTelemetryMiddleware

java.lang.Object
com.commercetools.monitoring.newrelic.NewRelicTelemetryMiddleware
All Implemented Interfaces:
Middleware, TelemetryMiddleware

public class NewRelicTelemetryMiddleware extends Object implements TelemetryMiddleware

The NewRelicTelemetry middleware can be used to report outgoing request to commercetools to NewRelic. It can be registered as TelemetryMiddleware to the ClientBuilder or the ApiRootBuilder.

ApiHttpClient client = ApiRootBuilder.of()
        .defaultClient(ServiceRegion.GCP_EUROPE_WEST1.getApiUrl())
        .withTelemetryMiddleware(new NewRelicTelemetryMiddleware())
        .buildClient();

See the test code.

The middleware adds the following metrics to Newrelic:
  • Custom/Commercetools/Client/Duration: The duration of the request in milliseconds
  • Custom/Commercetools/Client/Request/Total: The total number of requests
  • Custom/Commercetools/Client/Request/Error: The total number of requests with a status code greater or equal to 400

The metrics are added as metric timeslice data, therefore an APM is expected in the application.


Implementation details

The middleware reads the NewRelicContext from the Request and restores the transaction using a Token The details of the request and response are then reported as Segment with HttpParameters

The Context has to be attached in your application either to a ContextApiHttpClient or the Request itself.

ContextApiHttpClient contextClient = ContextApiHttpClient.of(apiHttpClient,
    NewRelicContext.of(NewRelic.getAgent().getTransaction()), false // don't close the ApiHttpClient
);
ProjectApiRoot apiRoot = ProjectApiRoot.fromClient(projectKey, contextClient);

See the test code.

ProjectApiRoot apiRoot = ProjectApiRoot.withContext(projectKey, apiHttpClient,
    NewRelicContext.of(NewRelic.getAgent().getTransaction()), false // don't close the apiHttpClient
);

See the test code.

The NewRelicContextImpl will expire the Token when it's closed. Closing a Context is ensured by ContextApiHttpClient if it's implementing Closeable See also the Spring MVC example application in the examples folder for further details.