Skip to content

Commit

Permalink
PN-10715: added CloudWatchMetricsPublisherWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
damiano.lozzi committed Apr 18, 2024
1 parent 58bcc05 commit 8011f87
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<String, MetricPublisher> cloudWatchMetricPublishers = new HashMap<>();
private final Map<String, CloudWatchMetricsPublisherWrapper> cloudWatchMetricPublishers = new HashMap<>();
private final Map<String, SdkMetric<?>> cloudWatchSdkMetrics = new HashMap<>();
private final Map<String, MetricCollector> cloudWatchMetricCollectors = new HashMap<>();

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}


}
5 changes: 5 additions & 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 @@ -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()";

}

0 comments on commit 8011f87

Please sign in to comment.