Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.datadog.api.client.ApiException;
import com.datadog.api.client.v2.api.MetricsApi;

import io.vrap.rmf.base.client.ApiHttpException;
import io.vrap.rmf.base.client.ApiHttpRequest;
import io.vrap.rmf.base.client.ApiHttpResponse;
import io.vrap.rmf.base.client.http.TelemetryMiddleware;
Expand Down Expand Up @@ -51,14 +52,23 @@
public CompletableFuture<ApiHttpResponse<byte[]>> invoke(ApiHttpRequest request,
Function<ApiHttpRequest, CompletableFuture<ApiHttpResponse<byte[]>>> next) {
final Instant start = Instant.now();
return next.apply(request).thenApply(response -> {
return next.apply(request).handle((response, throwable) -> {
final int statusCode;
if (response != null) {
statusCode = response.getStatusCode();
}
else if (throwable instanceof ApiHttpException && ((ApiHttpException) throwable).getResponse() != null) {
statusCode = ((ApiHttpException) throwable).getResponse().getStatusCode();

Check warning on line 61 in commercetools/commercetools-monitoring-datadog/src/main/java/com/commercetools/monitoring/datadog/DatadogMiddleware.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-datadog/src/main/java/com/commercetools/monitoring/datadog/DatadogMiddleware.java#L61

Added line #L61 was not covered by tests
}
else {
statusCode = 0;

Check warning on line 64 in commercetools/commercetools-monitoring-datadog/src/main/java/com/commercetools/monitoring/datadog/DatadogMiddleware.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-datadog/src/main/java/com/commercetools/monitoring/datadog/DatadogMiddleware.java#L64

Added line #L64 was not covered by tests
}
try {
submitClientDurationMetric(request, apiInstance, Duration.between(start, Instant.now()).toMillis(),
response);
submitTotalRequestsMetric(request, apiInstance, response);

if (response.getStatusCode() >= 400) {
submitErrorRequestsMetric(request, apiInstance, response);
statusCode);
submitTotalRequestsMetric(request, apiInstance, statusCode);
if (statusCode >= 400 || throwable != null) {
submitErrorRequestsMetric(request, apiInstance, statusCode);
}
}
catch (ApiException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,50 @@

protected static void submitClientDurationMetric(final ApiHttpRequest request, final MetricsApi apiInstance,
final double durationInMillis, final ApiHttpResponse<byte[]> response) throws ApiException {
submitClientDurationMetric(request, apiInstance, durationInMillis, response.getStatusCode());
}

Check warning on line 32 in commercetools/commercetools-monitoring-datadog/src/main/java/com/commercetools/monitoring/datadog/DatadogUtils.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-datadog/src/main/java/com/commercetools/monitoring/datadog/DatadogUtils.java#L31-L32

Added lines #L31 - L32 were not covered by tests

protected static void submitClientDurationMetric(final ApiHttpRequest request, final MetricsApi apiInstance,
final double durationInMillis, final int statusCode) throws ApiException {
final String name = PREFIX + "." + CLIENT_DURATION;
final MetricIntakeType type = MetricIntakeType.UNSPECIFIED;
submitMetricWithHttpTags(name, durationInMillis, type, "ms", request, apiInstance, response);
submitMetricWithHttpTags(name, durationInMillis, type, "ms", request, apiInstance, statusCode);
}

protected static void submitErrorRequestsMetric(final ApiHttpRequest request, final MetricsApi apiInstance,
final ApiHttpResponse<byte[]> response) throws ApiException {
submitErrorRequestsMetric(request, apiInstance, response.getStatusCode());
}

Check warning on line 44 in commercetools/commercetools-monitoring-datadog/src/main/java/com/commercetools/monitoring/datadog/DatadogUtils.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-datadog/src/main/java/com/commercetools/monitoring/datadog/DatadogUtils.java#L43-L44

Added lines #L43 - L44 were not covered by tests

protected static void submitErrorRequestsMetric(final ApiHttpRequest request, final MetricsApi apiInstance,
final int statusCode) throws ApiException {
final String name = PREFIX + "." + CLIENT_REQUEST_ERROR;
final MetricIntakeType count = MetricIntakeType.COUNT;
submitMetricWithHttpTags(name, 1.0, count, "count", request, apiInstance, response);
submitMetricWithHttpTags(name, 1.0, count, "count", request, apiInstance, statusCode);
}

protected static void submitTotalRequestsMetric(final ApiHttpRequest request, final MetricsApi apiInstance,
final ApiHttpResponse<byte[]> response) throws ApiException {
submitTotalRequestsMetric(request, apiInstance, response.getStatusCode());
}

Check warning on line 56 in commercetools/commercetools-monitoring-datadog/src/main/java/com/commercetools/monitoring/datadog/DatadogUtils.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-datadog/src/main/java/com/commercetools/monitoring/datadog/DatadogUtils.java#L55-L56

Added lines #L55 - L56 were not covered by tests

protected static void submitTotalRequestsMetric(final ApiHttpRequest request, final MetricsApi apiInstance,
final int statusCode) throws ApiException {
final String name = PREFIX + "." + CLIENT_REQUEST_TOTAL;
final MetricIntakeType count = MetricIntakeType.COUNT;
submitMetricWithHttpTags(name, 1.0, count, "count", request, apiInstance, response);
submitMetricWithHttpTags(name, 1.0, count, "count", request, apiInstance, statusCode);
}

private static void submitMetricWithHttpTags(final String name, final double value, final MetricIntakeType type,
final String unit, final ApiHttpRequest request, final MetricsApi apiInstance,
final ApiHttpResponse<byte[]> response) throws ApiException {
final List<String> tags = Arrays.asList(format("%s:%s", HTTP_RESPONSE_STATUS_CODE, response.getStatusCode()),
submitMetricWithHttpTags(name, value, type, unit, request, apiInstance, response.getStatusCode());
}

Check warning on line 69 in commercetools/commercetools-monitoring-datadog/src/main/java/com/commercetools/monitoring/datadog/DatadogUtils.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-datadog/src/main/java/com/commercetools/monitoring/datadog/DatadogUtils.java#L68-L69

Added lines #L68 - L69 were not covered by tests

private static void submitMetricWithHttpTags(final String name, final double value, final MetricIntakeType type,
final String unit, final ApiHttpRequest request, final MetricsApi apiInstance, final int statusCode)
throws ApiException {
final List<String> tags = Arrays.asList(format("%s:%s", HTTP_RESPONSE_STATUS_CODE, statusCode),
format("%s:%s", HTTP_REQUEST_METHOD, request.getMethod().name()),
format("%s:%s", SERVER_ADDRESS, request.getUri().getHost()));
if (request.getUri().getPort() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import com.timgroup.statsd.StatsDClient;

import io.vrap.rmf.base.client.ApiHttpException;
import io.vrap.rmf.base.client.ApiHttpRequest;
import io.vrap.rmf.base.client.ApiHttpResponse;
import io.vrap.rmf.base.client.http.TelemetryMiddleware;
Expand Down Expand Up @@ -49,9 +50,19 @@
Function<ApiHttpRequest, CompletableFuture<ApiHttpResponse<byte[]>>> next) {
final Instant start = Instant.now();

return next.apply(request).thenApply(response -> {
return next.apply(request).handle((response, throwable) -> {

Check warning on line 53 in commercetools/commercetools-monitoring-datadog/src/main/java/com/commercetools/monitoring/datadog/statsd/DatadogMiddleware.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-datadog/src/main/java/com/commercetools/monitoring/datadog/statsd/DatadogMiddleware.java#L53

Added line #L53 was not covered by tests
final List<String> tags = new ArrayList<>(4);
tags.add(format("%s:%s", HTTP_RESPONSE_STATUS_CODE, response.getStatusCode()));
final int statusCode;
if (response != null) {
statusCode = response.getStatusCode();

Check warning on line 57 in commercetools/commercetools-monitoring-datadog/src/main/java/com/commercetools/monitoring/datadog/statsd/DatadogMiddleware.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-datadog/src/main/java/com/commercetools/monitoring/datadog/statsd/DatadogMiddleware.java#L57

Added line #L57 was not covered by tests
}
else if (throwable instanceof ApiHttpException && ((ApiHttpException) throwable).getResponse() != null) {
statusCode = ((ApiHttpException) throwable).getResponse().getStatusCode();

Check warning on line 60 in commercetools/commercetools-monitoring-datadog/src/main/java/com/commercetools/monitoring/datadog/statsd/DatadogMiddleware.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-datadog/src/main/java/com/commercetools/monitoring/datadog/statsd/DatadogMiddleware.java#L60

Added line #L60 was not covered by tests
}
else {
statusCode = 0;

Check warning on line 63 in commercetools/commercetools-monitoring-datadog/src/main/java/com/commercetools/monitoring/datadog/statsd/DatadogMiddleware.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-datadog/src/main/java/com/commercetools/monitoring/datadog/statsd/DatadogMiddleware.java#L63

Added line #L63 was not covered by tests
}
tags.add(format("%s:%s", HTTP_RESPONSE_STATUS_CODE, statusCode));

Check warning on line 65 in commercetools/commercetools-monitoring-datadog/src/main/java/com/commercetools/monitoring/datadog/statsd/DatadogMiddleware.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-datadog/src/main/java/com/commercetools/monitoring/datadog/statsd/DatadogMiddleware.java#L65

Added line #L65 was not covered by tests
tags.add(format("%s:%s", HTTP_REQUEST_METHOD, request.getMethod().name()));
tags.add(format("%s:%s", SERVER_ADDRESS, request.getUri().getHost()));
if (request.getUri().getPort() > 0) {
Expand All @@ -62,7 +73,7 @@
Duration.between(start, Instant.now()).toMillis(), tags.toArray(new String[0]));

this.statsDClient.incrementCounter(PREFIX + "." + CLIENT_REQUEST_TOTAL, tags.toArray(new String[0]));
if (response.getStatusCode() >= 400) {
if (statusCode >= 400 || throwable != null) {
this.statsDClient.incrementCounter(PREFIX + "." + CLIENT_REQUEST_ERROR, tags.toArray(new String[0]));
}
return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import com.newrelic.api.agent.*;

import io.vrap.rmf.base.client.ApiHttpException;
import io.vrap.rmf.base.client.ApiHttpRequest;
import io.vrap.rmf.base.client.ApiHttpResponse;
import io.vrap.rmf.base.client.ContextApiHttpClientImpl;
Expand Down Expand Up @@ -61,21 +62,38 @@
Optional<Token> token = context.map(NewRelicContext::getTransaction).map(Transaction::getToken);
Optional<Segment> segment = context.map(c -> c.getTransaction()
.startSegment("commercetools", request.getMethod() + " " + request.getUri().getPath()));
return next.apply(request).thenApply(response -> {
return next.apply(request).handle((response, throwable) -> {

Check warning on line 65 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#L65

Added line #L65 was not covered by tests
token.ifPresent(Token::linkAndExpire);

final int statusCode;
final String message;
if (response != null) {
statusCode = response.getStatusCode();
message = response.getMessage();

Check warning on line 72 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#L71-L72

Added lines #L71 - L72 were not covered by tests
}
else if (throwable instanceof ApiHttpException && ((ApiHttpException) throwable).getResponse() != null) {
ApiHttpResponse<byte[]> errorResponse = ((ApiHttpException) throwable).getResponse();
statusCode = errorResponse.getStatusCode();
message = errorResponse.getMessage();
}

Check warning on line 78 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#L75-L78

Added lines #L75 - L78 were not covered by tests
else {
statusCode = 0;
message = throwable.getMessage();

Check warning on line 81 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#L80-L81

Added lines #L80 - L81 were not covered by tests
}

segment.ifPresent(s -> s.reportAsExternal(HttpParameters.library("commercetools-sdk-java-v2")
.uri(request.getUri())
.procedure(request.getMethod().name())
.noInboundHeaders()
.status(response.getStatusCode(), response.getMessage())
.status(statusCode, message)

Check warning on line 88 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#L88

Added line #L88 was not covered by tests
.build()));
segment.ifPresent(Segment::end);

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

if (response.getStatusCode() >= 400) {
if (statusCode >= 400 || throwable != null) {
NewRelic.incrementCounter(PREFIX + CLIENT_REQUEST_ERROR);
}
return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.opentelemetry.api.metrics.LongCounter;
import io.opentelemetry.api.metrics.LongHistogram;
import io.opentelemetry.api.metrics.Meter;
import io.vrap.rmf.base.client.ApiHttpException;
import io.vrap.rmf.base.client.ApiHttpRequest;
import io.vrap.rmf.base.client.ApiHttpResponse;
import io.vrap.rmf.base.client.http.TelemetryMiddleware;
Expand Down Expand Up @@ -67,9 +68,19 @@
public CompletableFuture<ApiHttpResponse<byte[]>> invoke(ApiHttpRequest request,
Function<ApiHttpRequest, CompletableFuture<ApiHttpResponse<byte[]>>> next) {
Instant start = Instant.now();
return next.apply(request).thenApply(response -> {
return next.apply(request).handle((response, throwable) -> {

Check warning on line 71 in commercetools/commercetools-monitoring-opentelemetry/src/main/java/com/commercetools/monitoring/opentelemetry/OpenTelemetryMiddleware.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-opentelemetry/src/main/java/com/commercetools/monitoring/opentelemetry/OpenTelemetryMiddleware.java#L71

Added line #L71 was not covered by tests
final int statusCode;
if (response != null) {
statusCode = response.getStatusCode();

Check warning on line 74 in commercetools/commercetools-monitoring-opentelemetry/src/main/java/com/commercetools/monitoring/opentelemetry/OpenTelemetryMiddleware.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-opentelemetry/src/main/java/com/commercetools/monitoring/opentelemetry/OpenTelemetryMiddleware.java#L74

Added line #L74 was not covered by tests
}
else if (throwable instanceof ApiHttpException && ((ApiHttpException) throwable).getResponse() != null) {
statusCode = ((ApiHttpException) throwable).getResponse().getStatusCode();

Check warning on line 77 in commercetools/commercetools-monitoring-opentelemetry/src/main/java/com/commercetools/monitoring/opentelemetry/OpenTelemetryMiddleware.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-opentelemetry/src/main/java/com/commercetools/monitoring/opentelemetry/OpenTelemetryMiddleware.java#L77

Added line #L77 was not covered by tests
}
else {
statusCode = 0;

Check warning on line 80 in commercetools/commercetools-monitoring-opentelemetry/src/main/java/com/commercetools/monitoring/opentelemetry/OpenTelemetryMiddleware.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-opentelemetry/src/main/java/com/commercetools/monitoring/opentelemetry/OpenTelemetryMiddleware.java#L80

Added line #L80 was not covered by tests
}
AttributesBuilder builder = Attributes.builder()
.put(OpenTelemetryInfo.HTTP_RESPONSE_STATUS_CODE, response.getStatusCode())
.put(OpenTelemetryInfo.HTTP_RESPONSE_STATUS_CODE, statusCode)

Check warning on line 83 in commercetools/commercetools-monitoring-opentelemetry/src/main/java/com/commercetools/monitoring/opentelemetry/OpenTelemetryMiddleware.java

View check run for this annotation

Codecov / codecov/patch

commercetools/commercetools-monitoring-opentelemetry/src/main/java/com/commercetools/monitoring/opentelemetry/OpenTelemetryMiddleware.java#L83

Added line #L83 was not covered by tests
.put(OpenTelemetryInfo.HTTP_REQUEST_METHOD, request.getMethod().name())
.put(OpenTelemetryInfo.SERVER_ADDRESS, request.getUri().getHost());
if (request.getUri().getPort() > 0) {
Expand All @@ -79,7 +90,7 @@
Optional.ofNullable(histogram)
.ifPresent(h -> h.record(Duration.between(start, Instant.now()).toMillis(), attributes));
requestCounter.add(1, attributes);
if (response.getStatusCode() >= 400) {
if (statusCode >= 400 || throwable != null) {
errorCounter.add(1, attributes);
}
return response;
Expand Down