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

org.graylog2.metrics.CacheStatsSet Maven / Gradle / Ivy

There is a newer version: 6.0.2
Show newest version
/**
 * This file is part of Graylog.
 *
 * Graylog is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Graylog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Graylog.  If not, see .
 */
package org.graylog2.metrics;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricSet;
import com.google.common.cache.Cache;
import com.google.common.collect.ImmutableMap;

import java.util.Map;

import static com.codahale.metrics.MetricRegistry.name;

public class CacheStatsSet implements MetricSet {
    private final Map metrics;

    public CacheStatsSet(final String prefix, final Cache cache) {
        this.metrics = ImmutableMap.builder()
                .put(name(prefix, "requests"), new Gauge() {
                    @Override
                    public Long getValue() {
                        return cache.stats().requestCount();
                    }
                })
                .put(name(prefix, "hits"), new Gauge() {
                    @Override
                    public Long getValue() {
                        return cache.stats().hitCount();
                    }
                })
                .put(name(prefix, "misses"), new Gauge() {
                    @Override
                    public Long getValue() {
                        return cache.stats().missCount();
                    }
                })
                .put(name(prefix, "evictions"), new Gauge() {
                    @Override
                    public Long getValue() {
                        return cache.stats().evictionCount();
                    }
                })
                .put(name(prefix, "total-load-time-ns"), new Gauge() {
                    @Override
                    public Long getValue() {
                        return cache.stats().totalLoadTime();
                    }
                })
                .put(name(prefix, "load-successes"), new Gauge() {
                    @Override
                    public Long getValue() {
                        return cache.stats().loadSuccessCount();
                    }
                })
                .put(name(prefix, "load-exceptions"), new Gauge() {
                    @Override
                    public Long getValue() {
                        return cache.stats().loadExceptionCount();
                    }
                })
                .put(name(prefix, "hit-rate"), new Gauge() {
                    @Override
                    public Double getValue() {
                        return cache.stats().hitRate();
                    }
                })
                .put(name(prefix, "miss-rate"), new Gauge() {
                    @Override
                    public Double getValue() {
                        return cache.stats().missRate();
                    }
                })
                .build();
    }

    @Override
    public Map getMetrics() {
        return metrics;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy