Skip to content

Commit

Permalink
PN-10715: Removed redundant log. (#481)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: damiano.lozzi <[email protected]>
Co-authored-by: Mario Ottone <[email protected]>
  • Loading branch information
4 people authored Apr 23, 2024
1 parent bf15147 commit 8db618a
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 59 deletions.
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<dependency>
<groupId>com.namirial.pec.library</groupId>
<artifactId>pn-ec-namirial-pec</artifactId>
<version>1.1.0</version>
<version>1.2.0-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -288,6 +288,13 @@
</dependency>
<!-- End SOAP dependencies -->

<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>5.1.2</version>
</dependency>

<!-- TEST DEPENDENCIES-->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
2 changes: 1 addition & 1 deletion scripts/aws/cfn/microservice-dev-cfg.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"PnEcNamirialPoolSmtpMaxtotal": "40",
"PnEcNamirialPoolSmtpMaxidle": "40",
"PnEcNamirialPoolSmtpMinidle": "1",
"PnEcNamirialServerCache": "false",
"PnEcNamirialServerCache": "true",
"PnEcNamirialServerCacheEndpoint": "18.102.130.190:6379",
"CpuValue": "1024",
"MemoryAmount": "4GB"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public class CloudWatchMetricPublisherConfiguration {
private final CloudWatchAsyncClient cloudWatchAsyncClient;
private final Map<String, CloudWatchMetricsPublisherWrapper> cloudWatchMetricPublishers = new HashMap<>();
private final Map<String, SdkMetric<?>> cloudWatchSdkMetrics = new HashMap<>();
private final Map<String, MetricCollector> cloudWatchMetricCollectors = new HashMap<>();

/**
* Instantiates a new CloudWatchMetricPublisherConfiguration.
Expand All @@ -59,7 +58,6 @@ private void init() {
log.debug("Initializing CloudWatchMetricPublisher configurations.");
initCloudWatchMetricPublishers();
initCloudWatchSdkMetrics();
initCloudWatchMetricCollectors();
}

/**
Expand All @@ -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.
*
Expand Down Expand Up @@ -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));
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ public Mono<Void> executeAndPublishResponseTime(Mono<Void> mono, String namespac
public Mono<Void> 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<Long> responseTimeMetric = (SdkMetric<Long>) cloudWatchMetricPublisherConfiguration.getSdkMetricByMetricName(metricName);
MetricCollector metricCollector = MetricCollector.create(metricName);
metricCollector.reportMetric(responseTimeMetric, elapsedTime);
MetricCollection metricCollection = metricCollector.collect();
cloudWatchMetricPublisherConfiguration.getMetricPublisherByNamespace(namespace).publish(metricCollection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -14,4 +16,5 @@
@Target(ElementType.TYPE)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Import({LocalStackTestConfig.class, MockMessageListenerConfiguration.class})
@ExtendWith(PnEcTestWatcher.class)
public @interface SpringBootTestWebEnv {}

This file was deleted.

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

Expand Down
4 changes: 3 additions & 1 deletion src/test/resources/namirial/namirial.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
namirial.pool.smtp.minidle=PnEcNamirialPoolSmtpMinidle
namirial.server.cache=false
namirial.server.cache.endpoint="http://localhost:8080"

0 comments on commit 8db618a

Please sign in to comment.