This repository has been archived by the owner on Mar 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: log route errors to config manager (#43)
* feat: add dead letter channel for sending error information to the ConfigManager * chore: log error if sending error logs to ConfigManager fails * refactor: rename ErrorDto to RouteError * fix: set HTTP method to POST for sending error logs to ConfigManager * docs: update CHANGELOG.md * docs: increase version in POM
- Loading branch information
1 parent
594b18e
commit 848e2b4
Showing
7 changed files
with
155 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ | |
|
||
<properties> | ||
<!-- General --> | ||
<revision>1.1.2</revision> | ||
<revision>1.2.0</revision> | ||
<email>[email protected]</email> | ||
|
||
<!-- Build info --> | ||
|
28 changes: 28 additions & 0 deletions
28
...n/java/de/fraunhofer/isst/dataspaceconnector/camel/config/context/ErrorHandlerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package de.fraunhofer.isst.dataspaceconnector.camel.config.context; | ||
|
||
import org.apache.camel.builder.DeadLetterChannelBuilder; | ||
import org.apache.camel.processor.errorhandler.RedeliveryPolicy; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* Defines beans required for error handling. | ||
*/ | ||
@Configuration | ||
public class ErrorHandlerConfig { | ||
|
||
/** | ||
* Returns a DeadLetterChannelBuilder instance that routes all failed exchanges to the | ||
* designated error handler route. | ||
* | ||
* @return the DeadLetterChannelBuilder | ||
*/ | ||
@Bean | ||
public DeadLetterChannelBuilder errorHandler() { | ||
final var dlcBuilder = new DeadLetterChannelBuilder(); | ||
dlcBuilder.setDeadLetterUri("direct:deadLetterChannel"); | ||
dlcBuilder.setRedeliveryPolicy(new RedeliveryPolicy().disableRedelivery()); | ||
return dlcBuilder; | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...ain/java/de/fraunhofer/isst/dataspaceconnector/camel/errorhandling/DeadLetterChannel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package de.fraunhofer.isst.dataspaceconnector.camel.errorhandling; | ||
|
||
import org.apache.camel.Exchange; | ||
import org.apache.camel.builder.RouteBuilder; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Defines the route used for handling errors, which will send an error description to the | ||
* Configuration Manager endpoint defined in application.properties. | ||
*/ | ||
@Component | ||
public class DeadLetterChannel extends RouteBuilder { | ||
|
||
/** | ||
* The Configuration Manager endpoint for logging errors. | ||
*/ | ||
@Value("${config-manager.error-api.url}") | ||
private String errorLogEndpoint; | ||
|
||
/** | ||
* Configures the error route. The error route uses a processor to create an {@link RouteError} | ||
* and then sends this to the Configuration Manager. | ||
* | ||
* @throws Exception if an error occurs writing the ErrorDto as JSON. | ||
*/ | ||
@Override | ||
public void configure() throws Exception { | ||
onException(Exception.class) | ||
.process(exchange -> { | ||
final var cause = exchange | ||
.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class); | ||
log.warn("Failed to send error logs to Configuration Manager: {}", | ||
cause.getMessage()); | ||
}) | ||
.handled(true); | ||
|
||
from("direct:deadLetterChannel") | ||
.process("dlcProcessor") | ||
.setHeader(Exchange.HTTP_METHOD, constant("POST")) | ||
.to(errorLogEndpoint); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
...de/fraunhofer/isst/dataspaceconnector/camel/errorhandling/DeadLetterChannelProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package de.fraunhofer.isst.dataspaceconnector.camel.errorhandling; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.apache.camel.Exchange; | ||
import org.apache.camel.Processor; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Prepares an {@link RouteError} for a failed exchange. | ||
*/ | ||
@Component("dlcProcessor") | ||
@Log4j2 | ||
public class DeadLetterChannelProcessor implements Processor { | ||
|
||
/** | ||
* Writes the ErrorDto as JSON. | ||
*/ | ||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
/** | ||
* Reads the route's ID, the endpoint where the exchange failed and the exception that caused | ||
* the failure from an Exchange object and creates an {@link RouteError}. The ErrorDto is set as | ||
* the body of the exchange's message. | ||
* | ||
* @param exchange the failed exchange. | ||
* @throws Exception if an error occurs writing the error DTO as JSON. | ||
*/ | ||
@Override | ||
public void process(final Exchange exchange) throws Exception { | ||
final var routeId = exchange.getProperty("CamelFailureRouteId", String.class); | ||
final var failureEndpoint = exchange.getProperty("CamelFailureEndpoint", String.class); | ||
final var cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class); | ||
|
||
final var errorDto = new RouteError(routeId, failureEndpoint, cause.getMessage()); | ||
|
||
log.warn("Caught an exception during route execution: {}", errorDto); | ||
|
||
exchange.getIn().setBody(objectMapper.writeValueAsString(errorDto)); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/de/fraunhofer/isst/dataspaceconnector/camel/errorhandling/RouteError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package de.fraunhofer.isst.dataspaceconnector.camel.errorhandling; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
|
||
/** | ||
* DTO for sending error information to the Configuration Manager. | ||
*/ | ||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
@ToString | ||
public class RouteError { | ||
|
||
/** | ||
* ID of the route where an error occurred. | ||
*/ | ||
private String routeId; | ||
|
||
/** | ||
* The endpoint where the error occurred. | ||
*/ | ||
private String endpoint; | ||
|
||
/** | ||
* The exception message. | ||
*/ | ||
private String message; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters