001/*
002 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Kevin Leturc
018 */
019package org.nuxeo.ecm.core.storage.dbs;
020
021import org.nuxeo.runtime.metrics.NuxeoMetricSet;
022
023import com.google.common.cache.Cache;
024
025import io.dropwizard.metrics5.MetricName;
026import io.dropwizard.metrics5.MetricSet;
027
028/**
029 * Wrapper used to wrap the Guava cache's statistics into Gauges in order to report them via Codahale Metrics.
030 *
031 * @since 8.10
032 */
033public class GuavaCacheMetric extends NuxeoMetricSet {
034
035    private GuavaCacheMetric(MetricName name) {
036        super(name);
037    }
038
039    private GuavaCacheMetric(String name, String... names) {
040        super(name, names);
041    }
042
043    public static MetricSet of(Cache<?, ?> cache, MetricName name) {
044        GuavaCacheMetric metrics = new GuavaCacheMetric(name);
045        addCacheMetrics(cache, metrics);
046        return metrics;
047    }
048
049    public static MetricSet of(Cache<?, ?> cache, String name, String... names) {
050        GuavaCacheMetric metrics = new GuavaCacheMetric(name, names);
051        addCacheMetrics(cache, metrics);
052        return metrics;
053    }
054
055    protected static void addCacheMetrics(Cache<?, ?> cache, GuavaCacheMetric metrics) {
056        metrics.putGauge(() -> cache.size(), "size");
057        metrics.putGauge(() -> cache.stats().evictionCount(), "eviction", "count");
058        metrics.putGauge(() -> cache.stats().hitCount(), "hit", "count");
059        metrics.putGauge(() -> cache.stats().hitRate(), "hit", "ratio");
060        metrics.putGauge(() -> cache.stats().missCount(), "miss", "count");
061        metrics.putGauge(() -> cache.stats().requestCount(), "read", "count");
062        // metrics.putGauge(() -> cache.stats().averageLoadPenalty(), "average", "load", "penalty");
063        // metrics.putGauge(() -> cache.stats().loadCount(), "load", "count");
064        // metrics.putGauge(() -> cache.stats().loadExceptionCount(), "load", "exception", "count");
065        // metrics.putGauge(() -> cache.stats().loadExceptionRate(), "load", "exception", "rate");
066        // metrics.putGauge(() -> cache.stats().loadSuccessCount(), "load", "success", "count");
067        // metrics.putGauge(() -> cache.stats().missRate(), "miss", "rate");
068        // metrics.putGauge(() -> cache.stats().totalLoadTime(), "total", "load", "time");
069    }
070
071}