Subversion Repositories SmartDukaan

Rev

Rev 34578 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 34578 Rev 34583
Line 1... Line 1...
1
package com.smartdukaan.cron.monitored;
1
package com.smartdukaan.cron.monitored;
2
 
2
 
-
 
3
import java.lang.reflect.Method;
3
import java.util.Date;
4
import java.util.Date;
4
import java.util.Map;
5
import java.util.Map;
5
import java.util.concurrent.ConcurrentHashMap;
6
import java.util.concurrent.ConcurrentHashMap;
6
import java.util.concurrent.ConcurrentMap;
7
import java.util.concurrent.ConcurrentMap;
7
import java.util.concurrent.atomic.AtomicInteger;
8
import java.util.concurrent.atomic.AtomicInteger;
Line 10... Line 11...
10
import io.micrometer.core.instrument.Gauge;
11
import io.micrometer.core.instrument.Gauge;
11
import io.micrometer.core.instrument.Timer;
12
import io.micrometer.core.instrument.Timer;
12
import org.aspectj.lang.ProceedingJoinPoint;
13
import org.aspectj.lang.ProceedingJoinPoint;
13
import org.aspectj.lang.annotation.Around;
14
import org.aspectj.lang.annotation.Around;
14
import org.aspectj.lang.annotation.Aspect;
15
import org.aspectj.lang.annotation.Aspect;
-
 
16
import org.aspectj.lang.reflect.MethodSignature;
15
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.stereotype.Component;
18
import org.springframework.stereotype.Component;
17
 
19
 
18
import io.micrometer.core.instrument.MeterRegistry;
20
import io.micrometer.core.instrument.MeterRegistry;
19
import org.slf4j.Logger;
21
import org.slf4j.Logger;
Line 31... Line 33...
31
    private final ConcurrentMap<String, AtomicInteger> lastStatus = new ConcurrentHashMap<>();
33
    private final ConcurrentMap<String, AtomicInteger> lastStatus = new ConcurrentHashMap<>();
32
 
34
 
33
    @Around("@annotation(org.springframework.scheduling.annotation.Scheduled)")
35
    @Around("@annotation(org.springframework.scheduling.annotation.Scheduled)")
34
    public Object monitorCronJob(ProceedingJoinPoint joinPoint) throws Throwable {
36
    public Object monitorCronJob(ProceedingJoinPoint joinPoint) throws Throwable {
35
        String methodName = joinPoint.getSignature().getName();
37
        String methodName = joinPoint.getSignature().getName();
36
 
-
 
37
        lastStatus.computeIfAbsent(methodName, m -> {
38
        lastStatus.computeIfAbsent(methodName, m -> {
38
            AtomicInteger gauge = new AtomicInteger(0);
39
            AtomicInteger gauge = new AtomicInteger(0);
39
            Gauge.builder("cron_job_last_status", gauge, AtomicInteger::get)
40
            Gauge.builder("cron_job_last_status", gauge, AtomicInteger::get)
40
                    .description("1=success, 0=failure")
41
                    .description("1=success, 0=failure")
41
                    .tag("method", m)
42
                    .tag("method", m)
Line 49... Line 50...
49
        Timer.Sample sample = Timer.start(meterRegistry);
50
        Timer.Sample sample = Timer.start(meterRegistry);
50
        try {
51
        try {
51
            Object result = joinPoint.proceed();
52
            Object result = joinPoint.proceed();
52
            success = true;
53
            success = true;
53
            // Record a success count
54
            // Record a success count
-
 
55
            meterRegistry.counter(
54
            meterRegistry.counter("cron_job_count_total", "method", methodName, "status", "success", "exception", "No Exception").increment();
56
                    "cron_job_count_total", "method", methodName, "status", "success").increment();
55
            return result;
57
            return result;
56
        } catch (Throwable t) {
58
        } catch (Throwable t) {
57
            String exceptionName = t.getClass().getSimpleName();
-
 
58
            // Record a failure count with exception type
59
            // Record a failure count with exception type
59
            meterRegistry.counter(
60
            meterRegistry.counter(
60
                    "cron_job_count_total", "method", methodName, "status", "failure", "exception", exceptionName).increment();
61
                    "cron_job_count_total", "method", methodName, "status", "failure").increment();
61
            throw t;
62
            throw t;
62
        } finally {
63
        } finally {
63
            sample.stop(Timer.builder("cron_job_duration_seconds")
64
            sample.stop(Timer.builder("cron_job_duration_seconds")
64
                    .tag("method", methodName)
65
                    .tag("method", methodName)
65
                    .register(meterRegistry));
66
                    .register(meterRegistry));