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

io.soffa.foundation.core.metrics.MetricsRegistry Maven / Gradle / Ivy

The newest version!
package io.soffa.foundation.core.metrics;

import com.google.common.collect.ImmutableMap;
import io.soffa.foundation.commons.Logger;
import io.soffa.foundation.errors.ManagedException;
import io.soffa.foundation.errors.TechnicalException;

import java.time.Duration;
import java.util.Map;
import java.util.function.Supplier;

public interface MetricsRegistry {

    String FAILED_SUFFIX = "_failed";
    String DURATION_SUFFIX = "_duration";
    Logger LOG = Logger.get(MetricsRegistry.class);

    default void increment(String counter) {
        increment(counter, 1, ImmutableMap.of());
    }

    default void increment(String counter, Map tags) {
        increment(counter, 1, tags);
    }

    default  T track(String prefix, Map tags, Supplier supplier) {
        try {
            T result = timed(prefix + DURATION_SUFFIX, tags, supplier);
            increment(prefix, tags);
            return result;
        } catch (Exception e) {
            increment(prefix + FAILED_SUFFIX, tags);
            if (e instanceof ManagedException) {
                throw e;
            } else {
                throw new TechnicalException(e.getMessage(), e);
            }
        }
    }

    default void track(String prefix, Map tags, Runnable runnable) {
        try {
            timed(prefix + DURATION_SUFFIX, tags, runnable);
            increment(prefix, tags);
        } catch (Exception e) {
            increment(prefix + FAILED_SUFFIX, tags);
            if (e instanceof ManagedException) {
                throw e;
            } else {
                throw new TechnicalException(e.getMessage(), e);
            }
        }
    }

    void increment(String counter, double amount, Map tags);

    double counter(String name);

    double globalCounter(String name);

    void timed(String name, Duration duration, Map tags);

    void timed(String name, Map tags, Runnable runnable);

     F timed(String name, Map tags, Supplier supplier);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy