From a3e38621aa86f569de4d46d50483dcc95a4c625f Mon Sep 17 00:00:00 2001 From: Loganathan Sekar Date: Thu, 16 Nov 2023 17:41:14 +0530 Subject: [PATCH 1/6] Adding timed annotation for repo-wip Signed-off-by: Loganathan Sekar --- .../java/io/mosip/resident/constant/ResidentConstants.java | 3 +++ .../mosip/resident/repository/ResidentSessionRepository.java | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/resident/resident-service/src/main/java/io/mosip/resident/constant/ResidentConstants.java b/resident/resident-service/src/main/java/io/mosip/resident/constant/ResidentConstants.java index 79bf2019257..1b384f75777 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/constant/ResidentConstants.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/constant/ResidentConstants.java @@ -305,6 +305,9 @@ private ResidentConstants() { public static final String API_RESPONSE_TIME_DESCRIPTION = "API Response Time"; public static final String API_RESPONSE_TIME_ID = "api.response.time"; + + public static final String DB_QUERY_RESPONSE_TIME_DESCRIPTION = "DB Query Response Time"; + public static final String DB_QUERY_RESPONSE_TIME_ID = "db.query.response.time"; public static final String UNDER_SCORE = "_"; public static final String IDENTITY = "identity"; } diff --git a/resident/resident-service/src/main/java/io/mosip/resident/repository/ResidentSessionRepository.java b/resident/resident-service/src/main/java/io/mosip/resident/repository/ResidentSessionRepository.java index 55205050a0d..d9f1e77ee9e 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/repository/ResidentSessionRepository.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/repository/ResidentSessionRepository.java @@ -1,5 +1,8 @@ package io.mosip.resident.repository; +import static io.mosip.resident.constant.ResidentConstants.*; +import static io.mosip.resident.constant.ResidentConstants.API_RESPONSE_TIME_ID; + import java.util.List; import java.util.Optional; @@ -7,6 +10,7 @@ import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; +import io.micrometer.core.annotation.Timed; import io.mosip.resident.entity.ResidentSessionEntity; /** @@ -20,6 +24,7 @@ public interface ResidentSessionRepository extends JpaRepository { Optional findById(String id); + @Timed(value=DB_QUERY_RESPONSE_TIME_ID,description=DB_QUERY_RESPONSE_TIME_DESCRIPTION, extraTags = {"repo" , "ResidentSessionRepository" , "queryName", "io.mosip.resident.repository.ResidentSessionRepository.findFirst2ByIdaTokenOrderByLoginDtimesDesc(String)"}, percentiles = {0.5, 0.9, 0.95, 0.99} ) List findFirst2ByIdaTokenOrderByLoginDtimesDesc(String idaToken); Optional findFirstByIdaTokenOrderByLoginDtimesDesc(String idaToken); From 3fc94e9a92a7ba2d968cfd4e0c5fd5e7742e3e90 Mon Sep 17 00:00:00 2001 From: Loganathan Sekar Date: Mon, 20 Nov 2023 14:12:07 +0530 Subject: [PATCH 2/6] Correction to audit description Signed-off-by: Loganathan Sekar --- .../main/java/io/mosip/resident/config/Config.java | 12 +++++++++++- .../main/java/io/mosip/resident/util/EventEnum.java | 3 +-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/resident/resident-service/src/main/java/io/mosip/resident/config/Config.java b/resident/resident-service/src/main/java/io/mosip/resident/config/Config.java index f11320fd754..f63649c2566 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/config/Config.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/config/Config.java @@ -7,7 +7,6 @@ import javax.servlet.Filter; -import io.mosip.resident.constant.ResidentConstants; import org.apache.commons.collections.CollectionUtils; import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.runtime.RuntimeConstants; @@ -20,6 +19,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.actuate.autoconfigure.metrics.web.servlet.WebMvcMetricsAutoConfiguration; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -37,9 +38,12 @@ import com.fasterxml.jackson.module.afterburner.AfterburnerModule; +import io.micrometer.core.aop.TimedAspect; +import io.micrometer.core.instrument.MeterRegistry; import io.mosip.kernel.core.templatemanager.spi.TemplateManager; import io.mosip.kernel.keygenerator.bouncycastle.KeyGenerator; import io.mosip.kernel.templatemanager.velocity.impl.TemplateManagerImpl; +import io.mosip.resident.constant.ResidentConstants; import io.mosip.resident.util.ResidentServiceRestClient; import io.mosip.resident.util.Utility; @@ -47,6 +51,7 @@ @Configuration @EnableScheduling @EnableAsync +@EnableAutoConfiguration(exclude = {WebMvcMetricsAutoConfiguration.class}) public class Config { private String defaultEncoding = StandardCharsets.UTF_8.name(); /** The resource loader. */ @@ -162,5 +167,10 @@ public TaskExecutor AuditExecutor() { executor.initialize(); return new DelegatingSecurityContextAsyncTaskExecutor(executor); } + + @Bean + public TimedAspect timedAspect(MeterRegistry registry) { + return new TimedAspect(registry); + } } diff --git a/resident/resident-service/src/main/java/io/mosip/resident/util/EventEnum.java b/resident/resident-service/src/main/java/io/mosip/resident/util/EventEnum.java index 89963bfb11e..262b3a60410 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/util/EventEnum.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/util/EventEnum.java @@ -332,8 +332,7 @@ public enum EventEnum { "download service history failed", "RS-DOWN_SER", "Download service history", RESIDENT_APPLICATION_ID, RESIDENT_APPLICATION_NAME), INVALID_REQUEST_TYPE_CODE("RES-SER-267", FAILURE, "Invalid request type code", - "\"Invalid Request Type. Please input eventId only for VID_CARD_DOWNLOAD,\" +\n" - + "\t\t\t\"DOWNLOAD_PERSONALIZED_CARD, UPDATE_MY_UIN", + "Invalid Request Type. Please input eventId only for VID_CARD_DOWNLOAD and UPDATE_MY_UIN", "RS-VAL", "Validation", RESIDENT_APPLICATION_ID, RESIDENT_APPLICATION_NAME), DOWNLOAD_REGISTRATION_CENTER_SUCCESS("RES-SER-269", SUCCESS, "download registration center: Success", "download registration center success based on language code and hierarchy level", "RS-DOWN_CARD", From fbd144857c23f1da3c5654ef5d937ff82bbd35a4 Mon Sep 17 00:00:00 2001 From: Loganathan Sekar Date: Mon, 20 Nov 2023 14:13:43 +0530 Subject: [PATCH 3/6] Reverted config change for timed aspect Signed-off-by: Loganathan Sekar --- .../main/java/io/mosip/resident/config/Config.java | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/resident/resident-service/src/main/java/io/mosip/resident/config/Config.java b/resident/resident-service/src/main/java/io/mosip/resident/config/Config.java index f63649c2566..f11320fd754 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/config/Config.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/config/Config.java @@ -7,6 +7,7 @@ import javax.servlet.Filter; +import io.mosip.resident.constant.ResidentConstants; import org.apache.commons.collections.CollectionUtils; import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.runtime.RuntimeConstants; @@ -19,8 +20,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.actuate.autoconfigure.metrics.web.servlet.WebMvcMetricsAutoConfiguration; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -38,12 +37,9 @@ import com.fasterxml.jackson.module.afterburner.AfterburnerModule; -import io.micrometer.core.aop.TimedAspect; -import io.micrometer.core.instrument.MeterRegistry; import io.mosip.kernel.core.templatemanager.spi.TemplateManager; import io.mosip.kernel.keygenerator.bouncycastle.KeyGenerator; import io.mosip.kernel.templatemanager.velocity.impl.TemplateManagerImpl; -import io.mosip.resident.constant.ResidentConstants; import io.mosip.resident.util.ResidentServiceRestClient; import io.mosip.resident.util.Utility; @@ -51,7 +47,6 @@ @Configuration @EnableScheduling @EnableAsync -@EnableAutoConfiguration(exclude = {WebMvcMetricsAutoConfiguration.class}) public class Config { private String defaultEncoding = StandardCharsets.UTF_8.name(); /** The resource loader. */ @@ -167,10 +162,5 @@ public TaskExecutor AuditExecutor() { executor.initialize(); return new DelegatingSecurityContextAsyncTaskExecutor(executor); } - - @Bean - public TimedAspect timedAspect(MeterRegistry registry) { - return new TimedAspect(registry); - } } From 3cb3735d821b1bf1e0a7931dbacdac18daa91113 Mon Sep 17 00:00:00 2001 From: Loganathan Sekar Date: Mon, 20 Nov 2023 19:07:46 +0530 Subject: [PATCH 4/6] Reverted repo code Signed-off-by: Loganathan Sekar --- .../mosip/resident/repository/ResidentSessionRepository.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/resident/resident-service/src/main/java/io/mosip/resident/repository/ResidentSessionRepository.java b/resident/resident-service/src/main/java/io/mosip/resident/repository/ResidentSessionRepository.java index d9f1e77ee9e..55205050a0d 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/repository/ResidentSessionRepository.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/repository/ResidentSessionRepository.java @@ -1,8 +1,5 @@ package io.mosip.resident.repository; -import static io.mosip.resident.constant.ResidentConstants.*; -import static io.mosip.resident.constant.ResidentConstants.API_RESPONSE_TIME_ID; - import java.util.List; import java.util.Optional; @@ -10,7 +7,6 @@ import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; -import io.micrometer.core.annotation.Timed; import io.mosip.resident.entity.ResidentSessionEntity; /** @@ -24,7 +20,6 @@ public interface ResidentSessionRepository extends JpaRepository { Optional findById(String id); - @Timed(value=DB_QUERY_RESPONSE_TIME_ID,description=DB_QUERY_RESPONSE_TIME_DESCRIPTION, extraTags = {"repo" , "ResidentSessionRepository" , "queryName", "io.mosip.resident.repository.ResidentSessionRepository.findFirst2ByIdaTokenOrderByLoginDtimesDesc(String)"}, percentiles = {0.5, 0.9, 0.95, 0.99} ) List findFirst2ByIdaTokenOrderByLoginDtimesDesc(String idaToken); Optional findFirstByIdaTokenOrderByLoginDtimesDesc(String idaToken); From cf275dc93dd1b0436e8c1af2e98b7a7d91221470 Mon Sep 17 00:00:00 2001 From: Loganathan Sekar Date: Mon, 20 Nov 2023 19:08:50 +0530 Subject: [PATCH 5/6] Added aspect for db query time logging Signed-off-by: Loganathan Sekar --- .../aspect/DatabaseMetricsAspect.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 resident/resident-service/src/main/java/io/mosip/resident/aspect/DatabaseMetricsAspect.java diff --git a/resident/resident-service/src/main/java/io/mosip/resident/aspect/DatabaseMetricsAspect.java b/resident/resident-service/src/main/java/io/mosip/resident/aspect/DatabaseMetricsAspect.java new file mode 100644 index 00000000000..6de5c7a2835 --- /dev/null +++ b/resident/resident-service/src/main/java/io/mosip/resident/aspect/DatabaseMetricsAspect.java @@ -0,0 +1,49 @@ +package io.mosip.resident.aspect; + +import static io.mosip.resident.constant.ResidentConstants.DB_QUERY_RESPONSE_TIME_DESCRIPTION; +import static io.mosip.resident.constant.ResidentConstants.DB_QUERY_RESPONSE_TIME_ID; + +import java.util.concurrent.TimeUnit; + +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.stereotype.Component; + +import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.Timer; +/** + * Aspect class for database logging + * + * @author Ritik Jain + */ +@Component +@Aspect +@EnableAspectJAutoProxy +@ConditionalOnProperty(value = "resident.db.metrics.aspect.enabled", havingValue = "true", matchIfMissing = false) +public class DatabaseMetricsAspect { + + @Autowired + private MeterRegistry registry; + + @Pointcut("execution(* io.mosip.resident.repository.ResidentTransactionRepository.*(..))") + public void repositoryMethods() { + } + + @Around("repositoryMethods()") + public Object aroundAdvice(ProceedingJoinPoint jp) throws Throwable { + Timer timer = Timer.builder(DB_QUERY_RESPONSE_TIME_ID) + .tag("label", DB_QUERY_RESPONSE_TIME_DESCRIPTION) + .tag("method", jp.getSignature().toLongString()) + .tag("target", jp.getTarget().getClass().getCanonicalName()) + .register(registry); + long start = System.nanoTime(); + Object result = jp.proceed(); + timer.record(System.nanoTime() - start, TimeUnit.NANOSECONDS); + return result; + } +} From 47a5fbc7d22a740c2366c223e8e2f0d7692e6334 Mon Sep 17 00:00:00 2001 From: Loganathan Sekar Date: Mon, 20 Nov 2023 19:14:58 +0530 Subject: [PATCH 6/6] Updated comment Signed-off-by: Loganathan Sekar --- .../java/io/mosip/resident/aspect/DatabaseMetricsAspect.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resident/resident-service/src/main/java/io/mosip/resident/aspect/DatabaseMetricsAspect.java b/resident/resident-service/src/main/java/io/mosip/resident/aspect/DatabaseMetricsAspect.java index 6de5c7a2835..0991f7f2763 100644 --- a/resident/resident-service/src/main/java/io/mosip/resident/aspect/DatabaseMetricsAspect.java +++ b/resident/resident-service/src/main/java/io/mosip/resident/aspect/DatabaseMetricsAspect.java @@ -17,7 +17,7 @@ import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.Timer; /** - * Aspect class for database logging + * Aspect class for database metrics to record the query time metrics * * @author Ritik Jain */