Skip to content

Newrelic add metrics #630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
a8ec45c8ea4ba559247b654d01b0d35b21a68865
33f3224cb40e3fa8c56ddb88962e3a4e9319685d
430a1a0a5dd4efe78e21526c37bec9dbce036401
d0129c1095216d5c830900c8a6223ef5d4274de1
d0129c1095216d5c830900c8a6223ef5d4274de1
4bc5c823b8ebf5a00491c7e63e1ea49d29bf5ee7
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

package com.commercetools.monitoring.newrelic;

import static com.commercetools.monitoring.newrelic.NewrelicInfo.*;

import java.time.Duration;
import java.time.Instant;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
Expand All @@ -19,6 +23,18 @@
*
* {@include.example example.NewRelicApiRootBuilderTest#addNewRelic()}
*
* The middleware adds the following metrics to Newrelic:
* <ul>
* <li>Custom/Commercetools/Client/Duration: The duration of the request in milliseconds</li>
* <li>Custom/Commercetools/Client/Request/Total: The total number of requests</li>
* <li>Custom/Commercetools/Client/Request/Error: The total number of requests with a status code greater or equal to 400</li>
* </ul>
*
* <p>The metrics are added as metric timeslice data, therefore an APM is expected in the application.</p>
*
* <br/>
* <h2>Implementation details</h2>
*
* <p>The middleware reads the {@link NewRelicContext} from the Request and restores the transaction using a {@link Token}
* The details of the request and response are then reported as {@link Segment} with {@link HttpParameters}</p>
*
Expand All @@ -39,7 +55,7 @@
@Override
public CompletableFuture<ApiHttpResponse<byte[]>> invoke(ApiHttpRequest request,
Function<ApiHttpRequest, CompletableFuture<ApiHttpResponse<byte[]>>> next) {

final Instant start = Instant.now();

Check warning on line 58 in commercetools/commercetools-monitoring-newrelic/src/main/java/com/commercetools/monitoring/newrelic/NewRelicTelemetryMiddleware.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-newrelic/src/main/java/com/commercetools/monitoring/newrelic/NewRelicTelemetryMiddleware.java#L58

Added line #L58 was not covered by tests
Optional<NewRelicContext> context = Optional.ofNullable(request.getContext(NewRelicContext.class));
context.map(NewRelicContext::getToken).ifPresent(Token::link);
Optional<Token> token = context.map(NewRelicContext::getTransaction).map(Transaction::getToken);
Expand All @@ -54,6 +70,14 @@
.status(response.getStatusCode(), response.getMessage())
.build()));
segment.ifPresent(Segment::end);

NewRelic.incrementCounter(PREFIX + CLIENT_REQUEST_TOTAL);
NewRelic.recordResponseTimeMetric(PREFIX + CLIENT_DURATION,
Duration.between(start, Instant.now()).toMillis());

Check warning on line 76 in commercetools/commercetools-monitoring-newrelic/src/main/java/com/commercetools/monitoring/newrelic/NewRelicTelemetryMiddleware.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-newrelic/src/main/java/com/commercetools/monitoring/newrelic/NewRelicTelemetryMiddleware.java#L74-L76

Added lines #L74 - L76 were not covered by tests

if (response.getStatusCode() >= 400) {
NewRelic.incrementCounter(PREFIX + CLIENT_REQUEST_ERROR);

Check warning on line 79 in commercetools/commercetools-monitoring-newrelic/src/main/java/com/commercetools/monitoring/newrelic/NewRelicTelemetryMiddleware.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-newrelic/src/main/java/com/commercetools/monitoring/newrelic/NewRelicTelemetryMiddleware.java#L79

Added line #L79 was not covered by tests
}
return response;
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

package com.commercetools.monitoring.newrelic;

public class NewrelicInfo {

Check warning on line 4 in commercetools/commercetools-monitoring-newrelic/src/main/java/com/commercetools/monitoring/newrelic/NewrelicInfo.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-newrelic/src/main/java/com/commercetools/monitoring/newrelic/NewrelicInfo.java#L4

Added line #L4 was not covered by tests
public static final String CLIENT_DURATION = "Client/Duration";
public static final String CLIENT_REQUEST_ERROR = "Client/Request/Error";
public static final String CLIENT_REQUEST_TOTAL = "Client/Request/Total";
public static final String JSON_SERIALIZATION = "Json/Serialization";
public static final String JSON_DESERIALIZATION = "Json/Deserialization";
public static final String PREFIX = "Custom/Commercetools/";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

package com.commercetools.monitoring.newrelic;

import static com.commercetools.monitoring.newrelic.NewrelicInfo.*;

import java.time.Duration;
import java.time.Instant;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.newrelic.api.agent.NewRelic;

import io.vrap.rmf.base.client.ApiHttpResponse;
import io.vrap.rmf.base.client.ResponseSerializer;

/**
* This serializer uses API to submit metrics to newrelic.
*/
public class NewrelicResponseSerializer implements ResponseSerializer {
private final ResponseSerializer serializer;

public NewrelicResponseSerializer(final ResponseSerializer serializer) {
this.serializer = serializer;
}

Check warning on line 25 in commercetools/commercetools-monitoring-newrelic/src/main/java/com/commercetools/monitoring/newrelic/NewrelicResponseSerializer.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-newrelic/src/main/java/com/commercetools/monitoring/newrelic/NewrelicResponseSerializer.java#L23-L25

Added lines #L23 - L25 were not covered by tests

@Override
public <O> ApiHttpResponse<O> convertResponse(ApiHttpResponse<byte[]> response, Class<O> outputType) {
Instant start = Instant.now();
ApiHttpResponse<O> result = serializer.convertResponse(response, outputType);
long durationInMillis = Duration.between(start, Instant.now()).toMillis();
NewRelic.recordResponseTimeMetric(PREFIX + JSON_SERIALIZATION, durationInMillis);
return result;

Check warning on line 33 in commercetools/commercetools-monitoring-newrelic/src/main/java/com/commercetools/monitoring/newrelic/NewrelicResponseSerializer.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-newrelic/src/main/java/com/commercetools/monitoring/newrelic/NewrelicResponseSerializer.java#L29-L33

Added lines #L29 - L33 were not covered by tests
}

@Override
public <O> ApiHttpResponse<O> convertResponse(ApiHttpResponse<byte[]> response, JavaType outputType) {
Instant start = Instant.now();
ApiHttpResponse<O> result = serializer.convertResponse(response, outputType);
long durationInMillis = Duration.between(start, Instant.now()).toMillis();
NewRelic.recordResponseTimeMetric(PREFIX + JSON_SERIALIZATION, durationInMillis);
return result;

Check warning on line 42 in commercetools/commercetools-monitoring-newrelic/src/main/java/com/commercetools/monitoring/newrelic/NewrelicResponseSerializer.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-newrelic/src/main/java/com/commercetools/monitoring/newrelic/NewrelicResponseSerializer.java#L38-L42

Added lines #L38 - L42 were not covered by tests
}

@Override
public <O> ApiHttpResponse<O> convertResponse(ApiHttpResponse<byte[]> response, TypeReference<O> outputType) {
Instant start = Instant.now();
ApiHttpResponse<O> result = serializer.convertResponse(response, outputType);
long durationInMillis = Duration.between(start, Instant.now()).toMillis();
NewRelic.recordResponseTimeMetric(PREFIX + JSON_SERIALIZATION, durationInMillis);
return result;

Check warning on line 51 in commercetools/commercetools-monitoring-newrelic/src/main/java/com/commercetools/monitoring/newrelic/NewrelicResponseSerializer.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-newrelic/src/main/java/com/commercetools/monitoring/newrelic/NewrelicResponseSerializer.java#L47-L51

Added lines #L47 - L51 were not covered by tests
}

@Override
public byte[] toJsonByteArray(Object value) throws JsonProcessingException {
Instant start = Instant.now();
byte[] result = serializer.toJsonByteArray(value);
long durationInMillis = Duration.between(start, Instant.now()).toMillis();
NewRelic.recordResponseTimeMetric(PREFIX + JSON_DESERIALIZATION, durationInMillis);
return result;

Check warning on line 60 in commercetools/commercetools-monitoring-newrelic/src/main/java/com/commercetools/monitoring/newrelic/NewrelicResponseSerializer.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-newrelic/src/main/java/com/commercetools/monitoring/newrelic/NewrelicResponseSerializer.java#L56-L60

Added lines #L56 - L60 were not covered by tests
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.commercetools.monitoring.newrelic.NewRelicContext;
import com.commercetools.monitoring.newrelic.NewRelicTelemetryMiddleware;
import com.commercetools.monitoring.newrelic.NewrelicResponseSerializer;
import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.Trace;
import io.vrap.rmf.base.client.*;
Expand Down Expand Up @@ -40,6 +41,7 @@ public ApiHttpClient client() {
return ApiRootBuilder.of()
.defaultClient(credentials())
.withTelemetryMiddleware(new NewRelicTelemetryMiddleware())
.withSerializer(new NewrelicResponseSerializer(ResponseSerializer.of()))
.buildClient();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.concurrent.ExecutionException;

@Controller
public class AppContoller {
public class AppController {
@GetMapping("/")
public String home() {
return "home/index";
Expand Down
Loading