Skip to content

Commit

Permalink
Add reporting of errors sent through Error Processor
Browse files Browse the repository at this point in the history
  • Loading branch information
dhilpipre committed Feb 7, 2024
1 parent f634ac9 commit 0a82a86
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
4 changes: 2 additions & 2 deletions micronaut-http-server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
apply plugin: 'java'

dependencies {
implementation 'io.micronaut:micronaut-http:2.0.0'
implementation 'io.micronaut:micronaut-http-server:2.4.0'

// New Relic Java Agent dependencies
implementation 'com.newrelic.agent.java:newrelic-agent:7.4.0'
Expand All @@ -23,5 +23,5 @@ jar {
}

verifyInstrumentation {
passes 'io.micronaut:micronaut-http:[2.0.0,)'
passes 'io.micronaut:micronaut-http-server:[2.4.0,)'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.micronaut.http.server.exceptions.response;

import java.util.HashMap;
import java.util.List;
import java.util.Optional;

import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;

import io.micronaut.http.HttpRequest;
import io.micronaut.http.MutableHttpResponse;

@Weave(type = MatchType.Interface)
public abstract class ErrorResponseProcessor<T> {

public MutableHttpResponse<T> processResponse(ErrorContext errorContext, MutableHttpResponse<?> baseResponse) {
if(errorContext.hasErrors()) {
HttpRequest<?> request = errorContext.getRequest();
HashMap<String, Object> attributes = new HashMap<>();
List<Error> errorList = errorContext.getErrors();
attributes.put("NumberOfErrors", errorList.size());
attributes.put("Request-Path", request.getPath());
attributes.put("Request-URI", request.getUri());
attributes.put("Request-Method", request.getMethod());
attributes.put("Request-ServerName", request.getServerName());
Optional<Throwable> rootCause = errorContext.getRootCause();
if(rootCause.isPresent()) NewRelic.noticeError(rootCause.get(), attributes);
}
return Weaver.callOriginal();
}
}

0 comments on commit 0a82a86

Please sign in to comment.