Skip to content

Commit

Permalink
PN-10633: Added CloudWatch metric publish for "markMessageAsRead" and…
Browse files Browse the repository at this point in the history
… "deleteMessage" response time.
  • Loading branch information
michelescara committed Apr 9, 2024
1 parent 7752caf commit 9a5435c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main/java/it/pagopa/pn/ec/commons/utils/LogUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ private LogUtils() {
//CLOUD WATCH
public static final String PUBLISH_CUSTOM_PEC_METRICS = "CloudWatchPecMetrics.publishCustomPecMetrics()";
public static final String PUBLISH_PEC_MESSAGE_COUNT= "CloudWatchPecMetrics.publishMessageCount()";
public static final String PUBLISH_RESPONSE_TIME = "CloudWatchPecMetrics.publishResponseTime()";

//EVENT BRIDGE
public static final String EVENT_BRIDGE_PUT_EVENT_EXTERNAL = "EventBridge - PutEvents.putEventExternal()";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.stream.Stream;

import static it.pagopa.pn.ec.commons.constant.Status.*;
import static it.pagopa.pn.ec.commons.utils.LogUtils.*;
Expand Down Expand Up @@ -93,4 +94,30 @@ public Mono<Void> publishMessageCount(Long count, String namespace) {
return Mono.empty();
}).then();
}

//Method to execute a Mono<Void> and publish its response time to CloudWatch.
public Mono<Void> executeAndPublishResponseTime(Mono<Void> mono, String namespace, String metricName) {
return mono.thenReturn(true)
.elapsed()
.flatMap(tuple -> publishResponseTime(namespace, metricName, tuple.getT1()));
}

//Method to publish a response time related CloudWatch metric.
public Mono<Void> publishResponseTime(String namespace, String metricName, long elapsedTime) {
return Mono.fromCompletionStage(() -> {
log.debug(CLIENT_METHOD_INVOCATION_WITH_ARGS, PUBLISH_RESPONSE_TIME, Stream.of(namespace, metricName, elapsedTime).toList());
return cloudWatchAsyncClient.putMetricData(PutMetricDataRequest.builder()
.namespace(namespace)
.metricData(MetricDatum.builder()
.unit(StandardUnit.MILLISECONDS)
.metricName(metricName)
.value((double) elapsedTime)
.timestamp(Instant.now())
.build()).build());
})
.onErrorResume(throwable -> {
log.error(EXCEPTION_IN_PROCESS, PUBLISH_RESPONSE_TIME, throwable, throwable.getMessage());
return Mono.empty();
}).then();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public class PnEcPecServiceImpl implements PnEcPecService {
private String arubaProviderNamespace;
@Value("${library.pec.cloudwatch.namespace.namirial}")
private String namirialProviderNamespace;
@Value("${library.pec.cloudwatch.metric.response-time.mark-message-as-read}")
private String markMessageAsReadResponseTimeMetric;
@Value("${library.pec.cloudwatch.metric.response-time.delete-message}")
private String deleteMessageResponseTimeMetric;


@Autowired
Expand Down Expand Up @@ -125,7 +129,7 @@ public Mono<Integer> getMessageCount() {
public Mono<Void> markMessageAsRead(String messageID, String providerName) {
log.logStartingProcess(PN_EC_PEC_MARK_MESSAGE_AS_READ);
PnPecService provider = getProviderByName(providerName);
return provider.markMessageAsRead(messageID)
return cloudWatchPecMetrics.executeAndPublishResponseTime(provider.markMessageAsRead(messageID), getMetricNamespace(provider), markMessageAsReadResponseTimeMetric)
.retryWhen(getPnPecRetryStrategy(PN_EC_PEC_MARK_MESSAGE_AS_READ, provider))
.then()
.doOnSuccess(result -> log.logEndingProcess(PN_EC_PEC_MARK_MESSAGE_AS_READ))
Expand All @@ -136,7 +140,7 @@ public Mono<Void> markMessageAsRead(String messageID, String providerName) {
public Mono<Void> deleteMessage(String messageID, String senderMessageID) {
log.logStartingProcess(PN_EC_PEC_DELETE_MESSAGE);
PnPecService provider = getProviderByMessageId(senderMessageID);
return provider.deleteMessage(messageID)
return cloudWatchPecMetrics.executeAndPublishResponseTime(provider.deleteMessage(messageID), getMetricNamespace(provider), deleteMessageResponseTimeMetric)
.retryWhen(getPnPecRetryStrategy(PN_EC_PEC_DELETE_MESSAGE, provider))
.then()
.doOnSuccess(result -> log.logEndingProcess(PN_EC_PEC_DELETE_MESSAGE))
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/library/pec/providers.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
library.pec.postacert.path=it.pagopa.pn.library.pec.model.pojo.ArubaPostacert
library.pec.cloudwatch.namespace.aruba=PEC/Aruba
library.pec.cloudwatch.namespace.namirial=PEC/Namirial
library.pec.cloudwatch.metric.response-time.mark-message-as-read=MarkMessageAsReadResponseTime
library.pec.cloudwatch.metric.response-time.delete-message=DeleteMessageResponseTime

aruba.server.address=${PnEcArubaServerAddress:https://bridgews.pec.it/PecImapBridge}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ class PnPecServiceTest {
private String arubaProviderNamespace;
@Value("${library.pec.cloudwatch.namespace.namirial}")
private String namirialProviderNamespace;
@Value("${library.pec.cloudwatch.metric.response-time.mark-message-as-read}")
private String markMessageAsReadResponseTimeMetric;
@Value("${library.pec.cloudwatch.metric.response-time.delete-message}")
private String deleteMessageResponseTimeMetric;

private String PROVIDER_SWITCH_READ_DEFAULT = "1970-01-01T00:00:00Z;aruba";
private String PROVIDER_SWITCH_WRITE_DEFAULT = "1970-01-01T00:00:00Z;aruba";
Expand Down Expand Up @@ -515,6 +519,7 @@ void markMessageAsReadFromArubaOk() {

verify(arubaService, times(1)).markMessageAsRead(ARUBA_MESSAGE_ID);
verify(namirialService, never()).markMessageAsRead(anyString());
verify(cloudWatchPecMetrics, times(1)).publishResponseTime(eq(arubaProviderNamespace), eq(markMessageAsReadResponseTimeMetric), anyLong());
}

@Test
Expand All @@ -528,6 +533,7 @@ void markMessageAsReadFromNamirialOk() {

verify(arubaService, never()).markMessageAsRead(NAMIRIAL_MESSAGE_ID);
verify(namirialService, times(1)).markMessageAsRead(anyString());
verify(cloudWatchPecMetrics, times(1)).publishResponseTime(eq(namirialProviderNamespace), eq(markMessageAsReadResponseTimeMetric), anyLong());
}

@Test
Expand Down Expand Up @@ -596,6 +602,7 @@ void deleteMessageFromArubaOk() {

verify(arubaService, times(1)).deleteMessage(ARUBA_MESSAGE_ID);
verify(namirialService, never()).deleteMessage(anyString());
verify(cloudWatchPecMetrics, times(1)).publishResponseTime(eq(arubaProviderNamespace), eq(deleteMessageResponseTimeMetric), anyLong());
}

@Test
Expand All @@ -610,6 +617,7 @@ void deleteMessageFromNamirialOk() {

verify(arubaService, never()).deleteMessage(NAMIRIAL_MESSAGE_ID);
verify(namirialService, times(1)).deleteMessage(anyString());
verify(cloudWatchPecMetrics, times(1)).publishResponseTime(eq(namirialProviderNamespace), eq(deleteMessageResponseTimeMetric), anyLong());
}

@Test
Expand Down

0 comments on commit 9a5435c

Please sign in to comment.