-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
customizable logging and fixed circular dependency in bean injection
- Loading branch information
Showing
4 changed files
with
93 additions
and
33 deletions.
There are no files selected for viewing
38 changes: 13 additions & 25 deletions
38
...relic/src/main/java/com/agorapulse/micronaut/newrelic/CriticalNewRelicInsightsClient.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 |
---|---|---|
@@ -1,41 +1,29 @@ | ||
package com.agorapulse.micronaut.newrelic; | ||
|
||
import io.micronaut.core.annotation.Nullable; | ||
import io.micronaut.context.annotation.Requires; | ||
import io.micronaut.retry.annotation.Retryable; | ||
import jakarta.inject.Named; | ||
import jakarta.inject.Singleton; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* This client is used to send critical events to New Relic Insights. | ||
* | ||
* This class does not implement the {@link NewRelicInsightsClient} interface to avoid cyclic dependencies. | ||
*/ | ||
@Singleton | ||
@Named("critical") | ||
public class CriticalNewRelicInsightsClient implements NewRelicInsightsClient { | ||
@Requires(bean = NewRelicInsightsClient.class) | ||
public class CriticalNewRelicInsightsClient { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(CriticalNewRelicInsightsClient.class); | ||
private final NewRelicInsightsClient client; | ||
|
||
private final NewRelicInsightsServiceClient serviceClient; | ||
private final NewRelicInsightsUrlClient urlClient; | ||
|
||
public CriticalNewRelicInsightsClient( | ||
@Nullable NewRelicInsightsServiceClient serviceClient, | ||
@Nullable NewRelicInsightsUrlClient urlClient | ||
) { | ||
this.serviceClient = serviceClient; | ||
this.urlClient = urlClient; | ||
public CriticalNewRelicInsightsClient(NewRelicInsightsClient client) { | ||
this.client = client; | ||
} | ||
|
||
@Override | ||
@Retryable(attempts = "${newrelic.retry.counts:3}") | ||
@Retryable(attempts = "${newrelic.retry.count:3}", predicate = NewRelicRetryPredicate.class) | ||
public void createEvents(Iterable<Map<String, Object>> events) { | ||
if (serviceClient != null) { | ||
serviceClient.createEvents(events); | ||
} else if (urlClient != null) { | ||
urlClient.createEvents(events); | ||
} else { | ||
LOGGER.error("No New Relic client found to send events: {}", events); | ||
} | ||
client.createEvents(events); | ||
} | ||
|
||
} |
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
41 changes: 41 additions & 0 deletions
41
...naut-newrelic/src/main/java/com/agorapulse/micronaut/newrelic/NewRelicRetryPredicate.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,41 @@ | ||
package com.agorapulse.micronaut.newrelic; | ||
|
||
import io.micronaut.core.annotation.Introspected; | ||
import io.micronaut.http.client.exceptions.HttpClientException; | ||
import io.micronaut.http.client.exceptions.ReadTimeoutException; | ||
import io.micronaut.retry.annotation.RetryPredicate; | ||
|
||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
@Introspected | ||
public class NewRelicRetryPredicate implements RetryPredicate { | ||
|
||
public static Predicate<Throwable> INSTANCE = new NewRelicRetryPredicate(); | ||
|
||
private static final List<String> RETRYABLE_MESSAGES = List.of( | ||
"request failed", | ||
"connection refused", | ||
"connection reset", | ||
"connection timed out", | ||
"no route to host", | ||
"read timed out", | ||
"write timed out" | ||
); | ||
|
||
@Override | ||
public boolean test(Throwable throwable) { | ||
if (!(throwable instanceof HttpClientException)) { | ||
return false; | ||
} | ||
|
||
if (throwable instanceof ReadTimeoutException) { | ||
return true; | ||
} | ||
|
||
String message = throwable.getMessage() == null ? "" : throwable.getMessage().toLowerCase(); | ||
|
||
return RETRYABLE_MESSAGES.stream().anyMatch(message::contains); | ||
} | ||
|
||
} |