diff --git a/src/main/java/it/pagopa/pn/ec/commons/utils/LogUtils.java b/src/main/java/it/pagopa/pn/ec/commons/utils/LogUtils.java index a98e04692..115d25b5b 100644 --- a/src/main/java/it/pagopa/pn/ec/commons/utils/LogUtils.java +++ b/src/main/java/it/pagopa/pn/ec/commons/utils/LogUtils.java @@ -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()"; diff --git a/src/main/java/it/pagopa/pn/ec/scaricamentoesitipec/utils/CloudWatchPecMetrics.java b/src/main/java/it/pagopa/pn/ec/scaricamentoesitipec/utils/CloudWatchPecMetrics.java index 09978e43e..49eb879f9 100644 --- a/src/main/java/it/pagopa/pn/ec/scaricamentoesitipec/utils/CloudWatchPecMetrics.java +++ b/src/main/java/it/pagopa/pn/ec/scaricamentoesitipec/utils/CloudWatchPecMetrics.java @@ -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.*; @@ -93,4 +94,30 @@ public Mono publishMessageCount(Long count, String namespace) { return Mono.empty(); }).then(); } + + //Method to execute a Mono and publish its response time to CloudWatch. + public Mono executeAndPublishResponseTime(Mono 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 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(); + } } diff --git a/src/main/java/it/pagopa/pn/library/pec/service/impl/PnEcPecServiceImpl.java b/src/main/java/it/pagopa/pn/library/pec/service/impl/PnEcPecServiceImpl.java index da2b2ed83..d7a9e24d9 100644 --- a/src/main/java/it/pagopa/pn/library/pec/service/impl/PnEcPecServiceImpl.java +++ b/src/main/java/it/pagopa/pn/library/pec/service/impl/PnEcPecServiceImpl.java @@ -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 @@ -125,7 +129,7 @@ public Mono getMessageCount() { public Mono 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)) @@ -136,7 +140,7 @@ public Mono markMessageAsRead(String messageID, String providerName) { public Mono 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)) diff --git a/src/main/resources/library/pec/providers.properties b/src/main/resources/library/pec/providers.properties index 9d8be60a2..5c950f03c 100644 --- a/src/main/resources/library/pec/providers.properties +++ b/src/main/resources/library/pec/providers.properties @@ -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} \ No newline at end of file diff --git a/src/test/java/it/pagopa/pn/ec/pec/service/impl/PnPecServiceTest.java b/src/test/java/it/pagopa/pn/ec/pec/service/impl/PnPecServiceTest.java index 39374119e..1e3a78555 100644 --- a/src/test/java/it/pagopa/pn/ec/pec/service/impl/PnPecServiceTest.java +++ b/src/test/java/it/pagopa/pn/ec/pec/service/impl/PnPecServiceTest.java @@ -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"; @@ -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 @@ -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 @@ -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 @@ -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