-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #251 from buerokratt/250-ruuter-error-handling
#250 Ruuter Error Handling (REHA)
- Loading branch information
Showing
8 changed files
with
194 additions
and
11 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
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
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
82 changes: 82 additions & 0 deletions
82
src/main/java/ee/buerokratt/ruuter/service/OpenSearchSender.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,82 @@ | ||
package ee.buerokratt.ruuter.service; | ||
|
||
import ee.buerokratt.ruuter.configuration.ApplicationProperties; | ||
import lombok.Data; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.time.Instant; | ||
import java.util.Map; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
@Slf4j | ||
@Service | ||
public class OpenSearchSender { | ||
|
||
@RequiredArgsConstructor | ||
@Data | ||
public static class RuuterEvent { | ||
Long timestamp = Instant.now().toEpochMilli(); | ||
final String level; | ||
|
||
final String dslName; | ||
final String dslMethod; | ||
|
||
final String stepName; | ||
|
||
final Integer statusCode; | ||
final Integer errorCode; | ||
|
||
final Map<String, Object> requestParams; | ||
final Map<String, String> requestHeaders; | ||
final Map<String, Object> requestBody; | ||
|
||
final String message; | ||
|
||
final StackTraceElement[] stackTrace; | ||
} | ||
|
||
private ApplicationProperties properties; | ||
|
||
private WebClient webClient; | ||
private String indexName; | ||
|
||
private boolean loggingEnabled ; | ||
|
||
public OpenSearchSender(ApplicationProperties properties) { | ||
this.properties = properties; | ||
loggingEnabled = properties.getOpenSearchConfiguration() != null; | ||
if (!loggingEnabled) { | ||
log.warn("OpenSearch logging disabled"); | ||
} | ||
} | ||
|
||
private void createClient() { | ||
webClient = WebClient.create(properties.getOpenSearchConfiguration().getUrl()); | ||
indexName = properties.getOpenSearchConfiguration().getIndex(); | ||
} | ||
|
||
public void log(RuuterEvent ruuterEvent) { | ||
if (properties.getOpenSearchConfiguration() == null) { | ||
return; | ||
} | ||
|
||
if (webClient == null) | ||
createClient(); | ||
|
||
webClient.post() | ||
.uri("/{logIndex}/_doc", indexName) | ||
.bodyValue(ruuterEvent) | ||
.retrieve() | ||
.bodyToMono(Void.class) | ||
.onErrorResume(exception -> { | ||
log.error("Unable to send log to OpenSearch", exception); | ||
return Mono.empty(); | ||
}) | ||
.subscribe(); | ||
} | ||
} |
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