Subversion Repositories SmartDukaan

Rev

Rev 34567 | Go to most recent revision | 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
 
3
import java.util.Date;
4
import java.util.Map;
5
import java.util.concurrent.ConcurrentHashMap;
34567 tejus.loha 6
import java.util.concurrent.ConcurrentMap;
7
import java.util.concurrent.atomic.AtomicInteger;
34554 tejus.loha 8
 
9
import io.micrometer.core.instrument.Counter;
34567 tejus.loha 10
import io.micrometer.core.instrument.Gauge;
11
import io.micrometer.core.instrument.Timer;
34554 tejus.loha 12
import org.aspectj.lang.ProceedingJoinPoint;
13
import org.aspectj.lang.annotation.Around;
14
import org.aspectj.lang.annotation.Aspect;
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.stereotype.Component;
17
 
18
import io.micrometer.core.instrument.MeterRegistry;
19
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
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
 
34567 tejus.loha 31
    private final ConcurrentMap<String, AtomicInteger> lastStatus = new ConcurrentHashMap<>();
34554 tejus.loha 32
 
33
    @Around("@annotation(org.springframework.scheduling.annotation.Scheduled)")
34
    public Object monitorCronJob(ProceedingJoinPoint joinPoint) throws Throwable {
34567 tejus.loha 35
        String methodName = joinPoint.getSignature().getName();
34554 tejus.loha 36
 
34567 tejus.loha 37
        lastStatus.computeIfAbsent(methodName, m -> {
38
            AtomicInteger gauge = new AtomicInteger(0);
39
            Gauge.builder("cron_job_last_status", gauge, AtomicInteger::get)
40
                    .description("1=success, 0=failure")
41
                    .tag("method", m)
42
                    .register(meterRegistry);
43
            return gauge;
44
        });
45
 
46
        boolean success = false;
47
 
48
        // Start a timer sample
49
        Timer.Sample sample = Timer.start(meterRegistry);
34554 tejus.loha 50
        try {
51
            Object result = joinPoint.proceed();
34567 tejus.loha 52
            success = true;
53
            // Record a success count
34578 vikas.jang 54
            meterRegistry.counter("cron_job_count_total", "method", methodName, "status", "success", "exception", "No Exception").increment();
34554 tejus.loha 55
            return result;
34567 tejus.loha 56
        } catch (Throwable t) {
57
            String exceptionName = t.getClass().getSimpleName();
58
            // Record a failure count with exception type
59
            meterRegistry.counter(
60
                    "cron_job_count_total", "method", methodName, "status", "failure", "exception", exceptionName).increment();
61
            throw t;
62
        } finally {
63
            sample.stop(Timer.builder("cron_job_duration_seconds")
64
                    .tag("method", methodName)
65
                    .register(meterRegistry));
66
            lastStatus.get(methodName).set(success ? 1 : 0);
34554 tejus.loha 67
 
68
        }
69
    }
70
 
71
}