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

com.signalfx.metrics.errorhandler.CountingOnSendErrorHandler Maven / Gradle / Ivy

The newest version!
package com.signalfx.metrics.errorhandler;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Counts errors.
 */
public class CountingOnSendErrorHandler implements OnSendErrorHandler {
    private final AtomicInteger totalErrors = new AtomicInteger(0);
    private final Map vals = new ConcurrentHashMap();

    @Override
    public void handleError(MetricError metricError) {
        totalErrors.incrementAndGet();
        AtomicInteger existingValue = vals.get(metricError.getMetricErrorType());
        if (existingValue != null) {
            existingValue.incrementAndGet();
        } else {
            synchronized (this) {
                AtomicInteger previousValue = new AtomicInteger(1);
                AtomicInteger prevValue = vals.put(metricError.getMetricErrorType(), previousValue);
                if (prevValue != null) {
                    previousValue.addAndGet(prevValue.get());
                }
            }
        }
    }

    public synchronized Map getValues() {
        return new HashMap<>(vals);
    }

    public int getTotalErrorCount() {
        return totalErrors.get();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy