-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from tmfg/feature/DPO-2313-road
- Loading branch information
Showing
9 changed files
with
213 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,15 @@ | ||
target/ | ||
* | ||
|
||
!/.gitignore | ||
!/pom.xml | ||
!/LICENSE | ||
!/README.md | ||
|
||
!src/ | ||
!src/**/ | ||
!src/**/*.java | ||
|
||
!.github/ | ||
!.github/**/ | ||
!.github/workflows/**/**.yml | ||
!.github/CODEOWNERS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/java/fi/livi/digitraffic/common/annotation/NoJobLogging.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package fi.livi.digitraffic.common.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Methods annotated with @NoJobLogging will not be monitored for execution time | ||
* by ScheduledJobLogger.monitorScheduledJob() | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.METHOD}) | ||
public @interface NoJobLogging { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/fi/livi/digitraffic/common/aop/ScheduledJobLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package fi.livi.digitraffic.common.aop; | ||
|
||
import static fi.livi.digitraffic.common.scheduler.JobLogger.JobType.Scheduled; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.commons.lang3.time.StopWatch; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.annotation.Order; | ||
|
||
import fi.livi.digitraffic.common.scheduler.JobLogger; | ||
import fi.livi.digitraffic.common.scheduler.JobLogger.JobType; | ||
|
||
@Aspect | ||
@Order(Ordered.HIGHEST_PRECEDENCE) | ||
public class ScheduledJobLogger { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(ScheduledJobLogger.class); | ||
private static final JobType jobType = Scheduled; | ||
|
||
/** | ||
* Execution start (debug level) and end (info or error level) times will be | ||
* logged for each method annotated with @Scheduled. | ||
* | ||
* Methods annotated with @NoJobLogging will not be monitored. | ||
*/ | ||
@Around("@annotation(org.springframework.scheduling.annotation.Scheduled) && !@annotation(fi.livi.digitraffic.common.annotation.NoJobLogging)") | ||
public Object monitorScheduledJob(final ProceedingJoinPoint pjp) throws Throwable { | ||
final String method = pjp.getSignature().getName(); | ||
// Strip away Configuration suffix and Spring proxy classes | ||
final String jobClass = StringUtils.substringBefore(StringUtils.substringBefore(pjp.getTarget().getClass().getSimpleName(),"Configuration"), "$"); | ||
|
||
final StopWatch stopWatch = StopWatch.createStarted(); | ||
final String jobName = jobClass + "." + method; | ||
|
||
JobLogger.logJobStart(log, jobType, jobName); | ||
|
||
Exception error = null; | ||
try { | ||
return pjp.proceed(); | ||
} catch (final Exception e) { | ||
error = e; | ||
throw e; | ||
} finally { | ||
stopWatch.stop(); | ||
if (error == null) { | ||
JobLogger.logJobEndStatusSuccess(log, jobType, jobName, stopWatch.getTime()); | ||
} else { | ||
JobLogger.logJobEndStatusFail(log, jobType, jobName, stopWatch.getTime(), error); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/fi/livi/digitraffic/common/config/metrics/HikariCPMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package fi.livi.digitraffic.common.config.metrics; | ||
|
||
public final class HikariCPMetrics { | ||
public static final String CONNECTIONS_MAX = "hikaricp.connections.max"; | ||
public static final String CONNECTIONS_PENDING = "hikaricp.connections.pending"; | ||
public static final String CONNECTIONS_TIMEOUT = "hikaricp.connections.timeout"; | ||
public static final String CONNECTIONS_ACTIVE = "hikaricp.connections.active"; | ||
|
||
public static final String TAG_POOL = "pool"; | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/fi/livi/digitraffic/common/config/metrics/PendingConnectionDebugger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package fi.livi.digitraffic.common.config.metrics; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
|
||
import fi.livi.digitraffic.common.annotation.NoJobLogging; | ||
import fi.livi.digitraffic.common.aop.TransactionLoggerAspect; | ||
import io.micrometer.core.instrument.Measurement; | ||
import io.micrometer.core.instrument.Meter; | ||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.search.RequiredSearch; | ||
|
||
@Service | ||
public class PendingConnectionDebugger { | ||
private final MeterRegistry meterRegistry; | ||
|
||
private static final int PENDING_CONNECTIONS_LOG_LIMIT = 5; | ||
|
||
private static final Logger log = LoggerFactory.getLogger("PendingConnectionDebugger"); | ||
|
||
public PendingConnectionDebugger(final MeterRegistry meterRegistry) { | ||
this.meterRegistry = meterRegistry; | ||
} | ||
|
||
@Scheduled(fixedRate = 500) | ||
@NoJobLogging | ||
void debugPendingConnections() { | ||
final RequiredSearch requiredSearch = meterRegistry.get(HikariCPMetrics.CONNECTIONS_PENDING); | ||
final Meter meter = requiredSearch.meter(); // should only have one meter | ||
final Measurement measurement = meter.measure().iterator().next(); // should only have one measurement | ||
|
||
// when too many connections are pending, print all active transactions | ||
if(measurement.getValue() > PENDING_CONNECTIONS_LOG_LIMIT) { | ||
log.error("Connections pending! count={}", measurement.getValue()); | ||
|
||
TransactionLoggerAspect.logActiveTransactions(log); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/fi/livi/digitraffic/common/scheduler/JobLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package fi.livi.digitraffic.common.scheduler; | ||
|
||
import org.slf4j.Logger; | ||
|
||
public class JobLogger { | ||
|
||
public enum JobType { | ||
Scheduled, | ||
Quartz; | ||
} | ||
|
||
public enum JobEndStatus { | ||
SUCCESS, | ||
FAIL; | ||
} | ||
|
||
public static void logJobStart(final Logger log, final JobType jobType, final String jobName) { | ||
log.debug("jobType={} jobName={} start", jobType.name(), jobName); | ||
} | ||
|
||
public static void logJobEndStatusFail(final Logger log, final JobType jobType, final String jobName, long timeMs, final Exception lastError) { | ||
log.error(formatMessage(jobType, jobName, JobEndStatus.FAIL, timeMs), lastError); | ||
} | ||
|
||
public static void logJobEndStatusSuccess(final Logger log, final JobType jobType, final String jobName, long timeMs) { | ||
log.info(formatMessage(jobType, jobName, JobEndStatus.SUCCESS, timeMs)); | ||
} | ||
|
||
private static final String messageFormat = "jobType=%s jobName=%s jobEndStatus=%s jobTimeMs=%d"; | ||
|
||
private static String formatMessage(final JobType jobType, final String jobName, final JobEndStatus status, final long timeMs) { | ||
return String.format(messageFormat, jobType, jobName, status, timeMs); | ||
} | ||
} |