Subversion Repositories SmartDukaan

Rev

Rev 34578 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
34554 tejus.loha 1
package com.smartdukaan.cron.monitored;
2
 
34583 tejus.loha 3
import java.lang.reflect.Method;
34554 tejus.loha 4
import java.util.Date;
5
import java.util.Map;
6
import java.util.concurrent.ConcurrentHashMap;
34567 tejus.loha 7
import java.util.concurrent.ConcurrentMap;
8
import java.util.concurrent.atomic.AtomicInteger;
34554 tejus.loha 9
 
10
import io.micrometer.core.instrument.Counter;
34567 tejus.loha 11
import io.micrometer.core.instrument.Gauge;
12
import io.micrometer.core.instrument.Timer;
34554 tejus.loha 13
import org.aspectj.lang.ProceedingJoinPoint;
14
import org.aspectj.lang.annotation.Around;
15
import org.aspectj.lang.annotation.Aspect;
34583 tejus.loha 16
import org.aspectj.lang.reflect.MethodSignature;
34554 tejus.loha 17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.stereotype.Component;
19
 
20
import io.micrometer.core.instrument.MeterRegistry;
21
import org.slf4j.Logger;
22
import org.slf4j.LoggerFactory;
23
 
24
@Aspect
25
@Component
26
public class CronJobMonitorAspect {
27
 
28
    private static final Logger log = LoggerFactory.getLogger(CronJobMonitorAspect.class);
29
 
30
    @Autowired
31
    private MeterRegistry meterRegistry;
32
 
34567 tejus.loha 33
    private final ConcurrentMap<String, AtomicInteger> lastStatus = new ConcurrentHashMap<>();
34554 tejus.loha 34
 
35
    @Around("@annotation(org.springframework.scheduling.annotation.Scheduled)")
36
    public Object monitorCronJob(ProceedingJoinPoint joinPoint) throws Throwable {
34567 tejus.loha 37
        String methodName = joinPoint.getSignature().getName();
38
        lastStatus.computeIfAbsent(methodName, m -> {
39
            AtomicInteger gauge = new AtomicInteger(0);
40
            Gauge.builder("cron_job_last_status", gauge, AtomicInteger::get)
41
                    .description("1=success, 0=failure")
42
                    .tag("method", m)
43
                    .register(meterRegistry);
44
            return gauge;
45
        });
46
 
47
        boolean success = false;
48
 
49
        // Start a timer sample
50
        Timer.Sample sample = Timer.start(meterRegistry);
34554 tejus.loha 51
        try {
52
            Object result = joinPoint.proceed();
34567 tejus.loha 53
            success = true;
54
            // Record a success count
34583 tejus.loha 55
            meterRegistry.counter(
56
                    "cron_job_count_total", "method", methodName, "status", "success").increment();
34554 tejus.loha 57
            return result;
34567 tejus.loha 58
        } catch (Throwable t) {
59
            // Record a failure count with exception type
60
            meterRegistry.counter(
34583 tejus.loha 61
                    "cron_job_count_total", "method", methodName, "status", "failure").increment();
34567 tejus.loha 62
            throw t;
63
        } finally {
64
            sample.stop(Timer.builder("cron_job_duration_seconds")
65
                    .tag("method", methodName)
66
                    .register(meterRegistry));
67
            lastStatus.get(methodName).set(success ? 1 : 0);
34554 tejus.loha 68
 
69
        }
70
    }
71
 
72
}