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.codahale.metrics.MetricSet;
024import com.google.common.cache.Cache;
025
026/**
027 * Wrapper used to wrap the Guava cache's statistics into Gauges in order to report them via Codahale Metrics.
028 *
029 * @since 8.10
030 */
031public class GuavaCacheMetric extends NuxeoMetricSet {
032
033    private GuavaCacheMetric(String name, String... names) {
034        super(name, names);
035    }
036
037    public static MetricSet of(Cache cache, String name, String... names) {
038        GuavaCacheMetric metrics = new GuavaCacheMetric(name, names);
039        metrics.putGauge(() -> cache.size(), "size");
040        metrics.putGauge(() -> cache.stats().averageLoadPenalty(), "average", "load", "penalty");
041        metrics.putGauge(() -> cache.stats().evictionCount(), "eviction", "count");
042        metrics.putGauge(() -> cache.stats().hitCount(), "hit", "count");
043        metrics.putGauge(() -> cache.stats().hitRate(), "hit", "rate");
044        metrics.putGauge(() -> cache.stats().loadCount(), "load", "count");
045        metrics.putGauge(() -> cache.stats().loadExceptionCount(), "load", "exception", "count");
046        metrics.putGauge(() -> cache.stats().loadExceptionRate(), "load", "exception", "rate");
047        metrics.putGauge(() -> cache.stats().loadSuccessCount(), "load", "success", "count");
048        metrics.putGauge(() -> cache.stats().missCount(), "miss", "count");
049        metrics.putGauge(() -> cache.stats().missRate(), "miss", "rate");
050        metrics.putGauge(() -> cache.stats().requestCount(), "request", "count");
051        metrics.putGauge(() -> cache.stats().totalLoadTime(), "total", "load", "time");
052        return metrics;
053    }
054
055}