All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.icthh.xm.commons.scheduler.metric.SchedulerMetricsSet Maven / Gradle / Ivy

The newest version!
package com.icthh.xm.commons.scheduler.metric;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricSet;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

@Component
public class SchedulerMetricsSet implements MetricSet {

    private final AtomicLong countSuccessMessages = new AtomicLong(0L);
    private final AtomicLong countFailedMessages = new AtomicLong(0L);
    private final AtomicReference lastSuccess = new AtomicReference<>(Instant.MIN);
    private final AtomicReference lastError = new AtomicReference<>(Instant.MIN);

    @Override
    public Map getMetrics() {
        Map metrics = new HashMap<>();
        metrics.put("success.messages.count", (Gauge) countSuccessMessages::get);
        metrics.put("success.last.time", (Gauge) () -> lastSuccess.get().toString());
        metrics.put("failed.messages.count", (Gauge) countFailedMessages::get);
        metrics.put("failed.last.time", (Gauge) () -> lastError.get().toString());
        return metrics;
    }

    public void onSuccess() {
        countSuccessMessages.incrementAndGet();
        lastSuccess.set(Instant.now());
    }

    public void onError() {
        countFailedMessages.incrementAndGet();
        lastError.set(Instant.now());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy