| 34554 |
tejus.loha |
1 |
package com.smartdukaan.cron.monitored;
|
|
|
2 |
|
|
|
3 |
import java.util.Date;
|
|
|
4 |
import java.util.Map;
|
|
|
5 |
import java.util.concurrent.ConcurrentHashMap;
|
|
|
6 |
import java.util.concurrent.TimeUnit;
|
|
|
7 |
|
|
|
8 |
import io.micrometer.core.instrument.Counter;
|
|
|
9 |
import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
10 |
import org.aspectj.lang.annotation.Around;
|
|
|
11 |
import org.aspectj.lang.annotation.Aspect;
|
|
|
12 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
13 |
import org.springframework.stereotype.Component;
|
|
|
14 |
|
|
|
15 |
import io.micrometer.core.instrument.MeterRegistry;
|
|
|
16 |
import org.slf4j.Logger;
|
|
|
17 |
import org.slf4j.LoggerFactory;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Aspect to monitor execution of cron jobs via metrics and logging.
|
|
|
21 |
*/
|
|
|
22 |
@Aspect
|
|
|
23 |
@Component
|
|
|
24 |
public class CronJobMonitorAspect {
|
|
|
25 |
|
|
|
26 |
private static final Logger log = LoggerFactory.getLogger(CronJobMonitorAspect.class);
|
|
|
27 |
|
|
|
28 |
@Autowired
|
|
|
29 |
private MeterRegistry meterRegistry;
|
|
|
30 |
|
|
|
31 |
private final Map<String, Counter> failureCounters = new ConcurrentHashMap<>();
|
|
|
32 |
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Intercept any method annotated with @Scheduled (i.e. cron jobs).
|
|
|
36 |
*/
|
|
|
37 |
@Around("@annotation(org.springframework.scheduling.annotation.Scheduled)")
|
|
|
38 |
public Object monitorCronJob(ProceedingJoinPoint joinPoint) throws Throwable {
|
|
|
39 |
String methodName = joinPoint.getSignature().toShortString();
|
|
|
40 |
log.info("Cron job {} started", methodName);
|
|
|
41 |
long startTime = System.currentTimeMillis();
|
|
|
42 |
|
|
|
43 |
try {
|
|
|
44 |
// Proceed with the scheduled method
|
|
|
45 |
Object result = joinPoint.proceed();
|
|
|
46 |
long elapsed = System.currentTimeMillis() - startTime;
|
|
|
47 |
|
|
|
48 |
log.info("Cron job {} completed successfully in {} ms", methodName, elapsed);
|
|
|
49 |
|
|
|
50 |
try {
|
|
|
51 |
meterRegistry.counter("custom.cron.success.count", "method", methodName, "status", "success")
|
|
|
52 |
.increment();
|
|
|
53 |
meterRegistry.timer("custom.cron.execution.time", "method", methodName, "status", "success")
|
|
|
54 |
.record(elapsed, TimeUnit.MILLISECONDS);
|
|
|
55 |
} catch (Exception e) {
|
|
|
56 |
log.warn("Failed to update success metrics for {}: {}", methodName, e.toString());
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
return result;
|
|
|
60 |
} catch (Throwable ex) {
|
|
|
61 |
long elapsed = System.currentTimeMillis() - startTime;
|
|
|
62 |
log.error("Cron job {} failed in {} ms: {}", methodName, elapsed, ex.toString());
|
|
|
63 |
|
|
|
64 |
try {
|
|
|
65 |
meterRegistry.counter("custom.cron.failure.count", "method", methodName, "status", "failure")
|
|
|
66 |
.increment();
|
|
|
67 |
meterRegistry.timer("custom.cron.execution.time", "method", methodName, "status", "failure")
|
|
|
68 |
.record(elapsed, TimeUnit.MILLISECONDS);
|
|
|
69 |
} catch (Exception e) {
|
|
|
70 |
log.warn("Failed to update failure metrics for {}: {}", methodName, e.toString());
|
|
|
71 |
}
|
|
|
72 |
getFailureCounter(methodName).increment();
|
|
|
73 |
throw ex;
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
private Counter getFailureCounter(String methodName) {
|
|
|
78 |
return failureCounters.computeIfAbsent(methodName, name ->
|
|
|
79 |
Counter.builder("cron_execution_failure_count_total")
|
|
|
80 |
.description("Number of failed executions of scheduled method")
|
|
|
81 |
.tag("method", name)
|
|
|
82 |
.register(meterRegistry)
|
|
|
83 |
);
|
|
|
84 |
}
|
|
|
85 |
}
|