From 8db618a3f4cba2db1ed99569e61d669962f60a20 Mon Sep 17 00:00:00 2001 From: michelescara <79908782+michelescara@users.noreply.github.com> Date: Tue, 23 Apr 2024 15:26:59 +0200 Subject: [PATCH] PN-10715: Removed redundant log. (#481) * PN-10715: Removed redundant log. Moved cloudwatch log level from debug to info Aligned MetricCollector with indications from the documentation. * PN-10715: set imap cache to true * PN-10715: Updated pn-ec-namirial-pec version to 1.2.0-SNAPSHOT * PN-10715-bis: added cache related properties to PnPecProvidersConf * PN-10715-bis: added properties to namirial.properties * PN-10715: added jedis 5.1.2 dependency * PN-10715: Fixed JUnit test failure issue. --------- Co-authored-by: mottone-dgs <119047198+mottone-dgs@users.noreply.github.com> Co-authored-by: damiano.lozzi Co-authored-by: Mario Ottone --- pom.xml | 9 ++- scripts/aws/cfn/microservice-dev-cfg.json | 2 +- ...loudWatchMetricPublisherConfiguration.java | 25 ------ .../CloudWatchMetricsPublisherWrapper.java | 1 - .../utils/CloudWatchPecMetrics.java | 2 +- .../pec/configuration/PnPecProvidersConf.java | 4 +- src/main/resources/application.properties | 4 +- .../annotation/SpringBootTestWebEnv.java | 3 + .../CloudWatchTestConfiguration.java | 24 ------ .../configuration/PnEcTestWatcher.java | 79 +++++++++++++++++++ .../localstack/LocalStackTestConfig.java | 3 +- .../resources/namirial/namirial.properties | 4 +- 12 files changed, 101 insertions(+), 59 deletions(-) delete mode 100644 src/test/java/it/pagopa/pn/ec/testutils/configuration/CloudWatchTestConfiguration.java create mode 100644 src/test/java/it/pagopa/pn/ec/testutils/configuration/PnEcTestWatcher.java diff --git a/pom.xml b/pom.xml index 5898b1b5c..735f442fe 100644 --- a/pom.xml +++ b/pom.xml @@ -82,7 +82,7 @@ com.namirial.pec.library pn-ec-namirial-pec - 1.1.0 + 1.2.0-SNAPSHOT @@ -288,6 +288,13 @@ + + + redis.clients + jedis + 5.1.2 + + org.springframework.boot diff --git a/scripts/aws/cfn/microservice-dev-cfg.json b/scripts/aws/cfn/microservice-dev-cfg.json index 199fbcb81..552c26726 100644 --- a/scripts/aws/cfn/microservice-dev-cfg.json +++ b/scripts/aws/cfn/microservice-dev-cfg.json @@ -37,7 +37,7 @@ "PnEcNamirialPoolSmtpMaxtotal": "40", "PnEcNamirialPoolSmtpMaxidle": "40", "PnEcNamirialPoolSmtpMinidle": "1", - "PnEcNamirialServerCache": "false", + "PnEcNamirialServerCache": "true", "PnEcNamirialServerCacheEndpoint": "18.102.130.190:6379", "CpuValue": "1024", "MemoryAmount": "4GB" 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 e99c696b6..acebc1577 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 @@ -39,7 +39,6 @@ public class CloudWatchMetricPublisherConfiguration { private final CloudWatchAsyncClient cloudWatchAsyncClient; private final Map cloudWatchMetricPublishers = new HashMap<>(); private final Map> cloudWatchSdkMetrics = new HashMap<>(); - private final Map cloudWatchMetricCollectors = new HashMap<>(); /** * Instantiates a new CloudWatchMetricPublisherConfiguration. @@ -59,7 +58,6 @@ private void init() { log.debug("Initializing CloudWatchMetricPublisher configurations."); initCloudWatchMetricPublishers(); initCloudWatchSdkMetrics(); - initCloudWatchMetricCollectors(); } /** @@ -80,21 +78,6 @@ public CloudWatchMetricsPublisherWrapper getMetricPublisherByNamespace(String na } } - /** - * Gets metric collector by metric name. - * - * @param metricName the metric name - * @return the metric collector by metric name - * @throws CloudWatchResourceNotFoundException.MetricCollectorNotFoundException if there is no metric collector for the given metric name - */ - public MetricCollector getMetricCollectorByMetricName(String metricName) { - try { - return cloudWatchMetricCollectors.get(metricName); - } catch (NullPointerException e) { - throw new CloudWatchResourceNotFoundException.MetricCollectorNotFoundException(metricName); - } - } - /** * Gets sdk metric by metric name. * @@ -127,12 +110,4 @@ private void initCloudWatchSdkMetrics() { cloudWatchSdkMetrics.put(deleteMessageResponseTimeMetric, SdkMetric.create(deleteMessageResponseTimeMetric, Long.class, MetricLevel.INFO, MetricCategory.HTTP_CLIENT)); } - /** - * Init method to initialize MetricCollectors - */ - private void initCloudWatchMetricCollectors() { - cloudWatchMetricCollectors.put(markMessageAsReadResponseTimeMetric, MetricCollector.create(markMessageAsReadResponseTimeMetric)); - cloudWatchMetricCollectors.put(deleteMessageResponseTimeMetric, MetricCollector.create(deleteMessageResponseTimeMetric)); - } - } \ No newline at end of file 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 index a1791a598..a4a73c486 100644 --- 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 @@ -50,7 +50,6 @@ public CloudWatchMetricsPublisherWrapper(String nameSpace, int maximumCallsPerUp * @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/scaricamentoesitipec/utils/CloudWatchPecMetrics.java b/src/main/java/it/pagopa/pn/ec/scaricamentoesitipec/utils/CloudWatchPecMetrics.java index c1275115f..2340f59dd 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 @@ -140,8 +140,8 @@ public Mono executeAndPublishResponseTime(Mono mono, String namespac public Mono publishResponseTime(String namespace, String metricName, long elapsedTime) { return Mono.fromRunnable(() -> { log.debug(CLIENT_METHOD_INVOCATION_WITH_ARGS, PUBLISH_RESPONSE_TIME, Stream.of(namespace, metricName, elapsedTime).toList()); - MetricCollector metricCollector = cloudWatchMetricPublisherConfiguration.getMetricCollectorByMetricName(metricName); SdkMetric responseTimeMetric = (SdkMetric) cloudWatchMetricPublisherConfiguration.getSdkMetricByMetricName(metricName); + MetricCollector metricCollector = MetricCollector.create(metricName); metricCollector.reportMetric(responseTimeMetric, elapsedTime); MetricCollection metricCollection = metricCollector.collect(); cloudWatchMetricPublisherConfiguration.getMetricPublisherByNamespace(namespace).publish(metricCollection); diff --git a/src/main/java/it/pagopa/pn/library/pec/configuration/PnPecProvidersConf.java b/src/main/java/it/pagopa/pn/library/pec/configuration/PnPecProvidersConf.java index a24a27309..442f657d0 100644 --- a/src/main/java/it/pagopa/pn/library/pec/configuration/PnPecProvidersConf.java +++ b/src/main/java/it/pagopa/pn/library/pec/configuration/PnPecProvidersConf.java @@ -25,7 +25,9 @@ public class PnPecProvidersConf { "namirial.pool.imap.minidle", "namirial.pool.smtp.maxtotal", "namirial.pool.smtp.maxidle", - "namirial.pool.smtp.minidle"); + "namirial.pool.smtp.minidle", + "namirial.server.cache", + "namirial.server.cache.endpoint"); @Bean public PnPecServiceImpl namirialService(@Autowired Environment env) { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 04c100a5f..af353dcb9 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -2,8 +2,8 @@ logging.config=classpath:logback-base.xml logging.level.it.pagopa.pn.ec=${PnEcLoggingLevelRoot:debug} logging.level.it.pagopa.pn.library.pec=${PnEcLoggingLevelRoot:debug} -#Setting CloudWatch logging level to debug, to make CloudWatchMetricPublisher logs visible. -logging.level.software.amazon.awssdk.metrics.publishers.cloudwatch=debug +#Setting CloudWatch logging level to info, to make CloudWatchMetricPublisher logs visible. +logging.level.software.amazon.awssdk.metrics.publishers.cloudwatch=info pn.env.runtime=PROD spring.application.name=PN-TEMPLATE-MS-BE diff --git a/src/test/java/it/pagopa/pn/ec/testutils/annotation/SpringBootTestWebEnv.java b/src/test/java/it/pagopa/pn/ec/testutils/annotation/SpringBootTestWebEnv.java index 8201eb085..049f8463f 100644 --- a/src/test/java/it/pagopa/pn/ec/testutils/annotation/SpringBootTestWebEnv.java +++ b/src/test/java/it/pagopa/pn/ec/testutils/annotation/SpringBootTestWebEnv.java @@ -1,7 +1,9 @@ package it.pagopa.pn.ec.testutils.annotation; +import it.pagopa.pn.ec.testutils.configuration.PnEcTestWatcher; import it.pagopa.pn.ec.testutils.configuration.MockMessageListenerConfiguration; import it.pagopa.pn.ec.testutils.localstack.LocalStackTestConfig; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Import; @@ -14,4 +16,5 @@ @Target(ElementType.TYPE) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @Import({LocalStackTestConfig.class, MockMessageListenerConfiguration.class}) +@ExtendWith(PnEcTestWatcher.class) public @interface SpringBootTestWebEnv {} diff --git a/src/test/java/it/pagopa/pn/ec/testutils/configuration/CloudWatchTestConfiguration.java b/src/test/java/it/pagopa/pn/ec/testutils/configuration/CloudWatchTestConfiguration.java deleted file mode 100644 index b767b7830..000000000 --- a/src/test/java/it/pagopa/pn/ec/testutils/configuration/CloudWatchTestConfiguration.java +++ /dev/null @@ -1,24 +0,0 @@ -package it.pagopa.pn.ec.testutils.configuration; - -import lombok.CustomLog; -import org.springframework.boot.test.context.TestConfiguration; -import software.amazon.awssdk.metrics.internal.DefaultSdkMetric; - -import javax.annotation.PostConstruct; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -@TestConfiguration -@CustomLog -public class CloudWatchTestConfiguration { - - @PostConstruct - public void initCloudWatchPublisher() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { - // Use reflection to get the clearDeclaredMetrics method and invoke it. - Class cls = DefaultSdkMetric.class; - Method method = cls.getDeclaredMethod("clearDeclaredMetrics"); - method.setAccessible(true); // Set the method access. - method.invoke(null); // Invoked the static method. - } - -} diff --git a/src/test/java/it/pagopa/pn/ec/testutils/configuration/PnEcTestWatcher.java b/src/test/java/it/pagopa/pn/ec/testutils/configuration/PnEcTestWatcher.java new file mode 100644 index 000000000..c95837282 --- /dev/null +++ b/src/test/java/it/pagopa/pn/ec/testutils/configuration/PnEcTestWatcher.java @@ -0,0 +1,79 @@ +package it.pagopa.pn.ec.testutils.configuration; + +import lombok.CustomLog; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.TestWatcher; +import software.amazon.awssdk.metrics.internal.DefaultSdkMetric; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Optional; + +/** + * PnEcTestWatcher is a custom implementation of the TestWatcher interface. + * It is used to take actions before and after a test is executed. + */ +@CustomLog +public class PnEcTestWatcher implements TestWatcher { + + /** + * This method is invoked after a test has been skipped due to being disabled. + * + * @param context provides information about the current test execution + * @param reason provides the reason why the test was disabled + */ + @Override + public void testDisabled(ExtensionContext context, Optional reason) { + clearDeclaredMetrics(); + } + + /** + * This method is invoked after a test has completed successfully. + * + * @param context provides information about the current test execution + */ + @Override + public void testSuccessful(ExtensionContext context) { + clearDeclaredMetrics(); + } + + /** + * This method is invoked after a test has been aborted. + * + * @param context provides information about the current test execution + * @param cause provides the exception that caused the test to be aborted + */ + @Override + public void testAborted(ExtensionContext context, Throwable cause) { + clearDeclaredMetrics(); + } + + /** + * This method is invoked after a test has failed. + * + * @param context provides information about the current test execution + * @param cause provides the exception that caused the test to fail + */ + @Override + public void testFailed(ExtensionContext context, Throwable cause) { + clearDeclaredMetrics(); + } + + /** + * This method clears the declared metrics. + * It uses reflection to access the clearDeclaredMetrics method of the DefaultSdkMetric class. + * If an error occurs during the process, it logs the error and throws a RuntimeException. + */ + public void clearDeclaredMetrics() { + try { + // Use reflection to get the clearDeclaredMetrics method and invoke it. + Class cls = DefaultSdkMetric.class; + Method method = cls.getDeclaredMethod("clearDeclaredMetrics"); + method.setAccessible(true); // Set the method access. + method.invoke(null); // Invoked the static method. + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + log.error("Error while clearing declared metrics", e); + throw new RuntimeException(e); + } + } +} \ No newline at end of file diff --git a/src/test/java/it/pagopa/pn/ec/testutils/localstack/LocalStackTestConfig.java b/src/test/java/it/pagopa/pn/ec/testutils/localstack/LocalStackTestConfig.java index 74279b6bd..1ba3e1f46 100644 --- a/src/test/java/it/pagopa/pn/ec/testutils/localstack/LocalStackTestConfig.java +++ b/src/test/java/it/pagopa/pn/ec/testutils/localstack/LocalStackTestConfig.java @@ -12,7 +12,6 @@ import it.pagopa.pn.ec.repositorymanager.model.entity.RequestPersonal; import it.pagopa.pn.ec.scaricamentoesitipec.configurationproperties.ScaricamentoEsitiPecProperties; import it.pagopa.pn.ec.sms.configurationproperties.SmsSqsQueueName; -import it.pagopa.pn.ec.testutils.configuration.CloudWatchTestConfiguration; import it.pagopa.pn.ec.testutils.configuration.DynamoTestConfiguration; import it.pagopa.pn.ec.testutils.configuration.SqsTestConfiguration; import it.pagopa.pn.ec.testutils.exception.DynamoDbInitTableCreationException; @@ -50,7 +49,7 @@ import static software.amazon.awssdk.services.dynamodb.model.TableStatus.ACTIVE; @TestConfiguration -@Import({SqsTestConfiguration.class, DynamoTestConfiguration.class, LocalStackClientConfig.class, CloudWatchTestConfiguration.class}) +@Import({SqsTestConfiguration.class, DynamoTestConfiguration.class, LocalStackClientConfig.class}) @CustomLog public class LocalStackTestConfig { diff --git a/src/test/resources/namirial/namirial.properties b/src/test/resources/namirial/namirial.properties index 3710f14b5..42630f233 100644 --- a/src/test/resources/namirial/namirial.properties +++ b/src/test/resources/namirial/namirial.properties @@ -7,4 +7,6 @@ namirial.pool.imap.maxidle=PnEcNamirialPoolImapMaxidle namirial.pool.imap.minidle=PnEcNamirialPoolImapMinidle namirial.pool.smtp.maxtotal=PnEcNamirialPoolSmtpMaxtotal namirial.pool.smtp.maxidle=PnEcNamirialPoolSmtpMaxidle -namirial.pool.smtp.minidle=PnEcNamirialPoolSmtpMinidle \ No newline at end of file +namirial.pool.smtp.minidle=PnEcNamirialPoolSmtpMinidle +namirial.server.cache=false +namirial.server.cache.endpoint="http://localhost:8080" \ No newline at end of file