diff --git a/src/main/java/it/pagopa/pn/ec/commons/configuration/aws/cloudwatch/CloudWatchMetricPublisherConfiguration.java b/src/main/java/it/pagopa/pn/ec/commons/configuration/aws/cloudwatch/CloudWatchMetricPublisherConfiguration.java index f66e00ec..e99c696b 100644 --- a/src/main/java/it/pagopa/pn/ec/commons/configuration/aws/cloudwatch/CloudWatchMetricPublisherConfiguration.java +++ b/src/main/java/it/pagopa/pn/ec/commons/configuration/aws/cloudwatch/CloudWatchMetricPublisherConfiguration.java @@ -1,6 +1,7 @@ package it.pagopa.pn.ec.commons.configuration.aws.cloudwatch; import it.pagopa.pn.ec.commons.exception.cloudwatch.CloudWatchResourceNotFoundException; +import it.pagopa.pn.ec.commons.model.pojo.cloudwatch.CloudWatchMetricsPublisherWrapper; import lombok.CustomLog; import org.slf4j.event.Level; import org.springframework.beans.factory.annotation.Autowired; @@ -36,7 +37,7 @@ public class CloudWatchMetricPublisherConfiguration { @Value("${library.pec.cloudwatch.metric.response-time.delete-message}") private String deleteMessageResponseTimeMetric; private final CloudWatchAsyncClient cloudWatchAsyncClient; - private final Map cloudWatchMetricPublishers = new HashMap<>(); + private final Map cloudWatchMetricPublishers = new HashMap<>(); private final Map> cloudWatchSdkMetrics = new HashMap<>(); private final Map cloudWatchMetricCollectors = new HashMap<>(); @@ -71,7 +72,7 @@ private void init() { /* */ - public MetricPublisher getMetricPublisherByNamespace(String namespace) { + public CloudWatchMetricsPublisherWrapper getMetricPublisherByNamespace(String namespace) { try { return cloudWatchMetricPublishers.get(namespace); } catch (NullPointerException e) { @@ -114,8 +115,8 @@ public SdkMetric getSdkMetricByMetricName(String metricName) { * If maximumCallsPerUpload and uploadFrequencyMillis fields are null, CloudWatchMetricPublisher class will use its default values. */ private void initCloudWatchMetricPublishers() { - cloudWatchMetricPublishers.put(arubaPecNamespace, CloudWatchMetricPublisher.builder().detailedMetrics().cloudWatchClient(cloudWatchAsyncClient).namespace(arubaPecNamespace).maximumCallsPerUpload(maximumCallsPerUpload).uploadFrequency(Duration.ofMillis(uploadFrequencyMillis)).build()); - cloudWatchMetricPublishers.put(namirialPecNamespace, CloudWatchMetricPublisher.builder().cloudWatchClient(cloudWatchAsyncClient).namespace(namirialPecNamespace).maximumCallsPerUpload(maximumCallsPerUpload).uploadFrequency(Duration.ofMillis(uploadFrequencyMillis)).build()); + cloudWatchMetricPublishers.put(arubaPecNamespace, new CloudWatchMetricsPublisherWrapper(arubaPecNamespace, maximumCallsPerUpload, Duration.ofMillis(uploadFrequencyMillis), cloudWatchAsyncClient)); + cloudWatchMetricPublishers.put(namirialPecNamespace, new CloudWatchMetricsPublisherWrapper(namirialPecNamespace, maximumCallsPerUpload, Duration.ofMillis(uploadFrequencyMillis), cloudWatchAsyncClient)); } /** diff --git a/src/main/java/it/pagopa/pn/ec/commons/model/pojo/cloudwatch/CloudWatchMetricsPublisherWrapper.java b/src/main/java/it/pagopa/pn/ec/commons/model/pojo/cloudwatch/CloudWatchMetricsPublisherWrapper.java new file mode 100644 index 00000000..a1791a59 --- /dev/null +++ b/src/main/java/it/pagopa/pn/ec/commons/model/pojo/cloudwatch/CloudWatchMetricsPublisherWrapper.java @@ -0,0 +1,58 @@ +package it.pagopa.pn.ec.commons.model.pojo.cloudwatch; + +import lombok.CustomLog; +import lombok.Getter; +import lombok.experimental.FieldDefaults; +import software.amazon.awssdk.metrics.MetricCollection; +import software.amazon.awssdk.metrics.publishers.cloudwatch.CloudWatchMetricPublisher; +import software.amazon.awssdk.services.cloudwatch.CloudWatchAsyncClient; + +import java.time.Duration; + + +@Getter +@CustomLog +@FieldDefaults(level = lombok.AccessLevel.PRIVATE, makeFinal = true) +public class CloudWatchMetricsPublisherWrapper { + + CloudWatchMetricPublisher cloudWatchMetricPublisher; + String namespace; + int maximumCallsPerUpload; + Duration uploadFrequency; + CloudWatchAsyncClient cloudWatchClient; + + + /** + * Instantiates a new CloudWatchMetricsPublisherWrapper. + * + * @param nameSpace the name space + * @param maximumCallsPerUpload the maximum calls per upload + * @param uploadFrequency the upload frequency + * @param cloudWatchClient the cloud watch client + */ + public CloudWatchMetricsPublisherWrapper(String nameSpace, int maximumCallsPerUpload, Duration uploadFrequency, CloudWatchAsyncClient cloudWatchClient) { + log.debug("Initializing CloudWatchMetricPublisher wrapper with args: nameSpace={}, maximumCallsPerUpload={}, uploadFrequencyMillis={}", nameSpace, maximumCallsPerUpload, uploadFrequency.toSeconds()+"s"); + this.namespace = nameSpace; + this.maximumCallsPerUpload = maximumCallsPerUpload; + this.uploadFrequency = uploadFrequency; + this.cloudWatchClient = cloudWatchClient; + this.cloudWatchMetricPublisher = CloudWatchMetricPublisher.builder() + .cloudWatchClient(cloudWatchClient) + .namespace(nameSpace) + .maximumCallsPerUpload(maximumCallsPerUpload) + .uploadFrequency(uploadFrequency) + .build(); + } + + /** + * Method for calling the publish method of the CloudWatchMetricPublisher. + * + * @param metricCollection the metric collection to publish + */ + public void publish(MetricCollection metricCollection) { + log.debug("Publishing metric collection: {} ,namespace= {}", metricCollection, namespace); + cloudWatchMetricPublisher.publish(metricCollection); + } + + +} 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 bcbeb91d..7a9020d7 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 @@ -230,4 +230,9 @@ public static String concatIds(String id1, String id2) { public static final String PN_EC_PEC_DELETE_MESSAGE = "PnEcPecService.deleteMessage()"; public static final String NOT_VALID_FOR_DELETE = "Event with requestId '{}' is not valid for delete."; + //CloudWatch + + public static final String CLOUD_WATCH_METRICS_PUBLISHER_WRAPPER = "CloudWatchMetricsPublisherWrapper"; + public static final String CLOUD_WATCH_METRICS_PUBLISH= CLOUD_WATCH_METRICS_PUBLISHER_WRAPPER +".publish()"; + }