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 java.util.HashMap;
022import java.util.Map;
023
024import com.codahale.metrics.Gauge;
025import com.codahale.metrics.Metric;
026import com.codahale.metrics.MetricRegistry;
027import com.codahale.metrics.MetricSet;
028import com.google.common.cache.Cache;
029
030/**
031 * Wrapper used to wrap the Guava cache's statistics into Gauges in order to report them via Codahale Metrics.
032 *
033 * @since 8.10
034 */
035public class GuavaCacheMetric implements MetricSet {
036
037    private Map<String, Metric> metrics = new HashMap<>();
038
039    private GuavaCacheMetric() {
040    }
041
042    @Override
043    public Map<String, Metric> getMetrics() {
044        return metrics;
045    }
046
047    private <T> void putMetrics(Gauge<T> gauge, String name, String... names) {
048        metrics.put(MetricRegistry.name(name, names), gauge);
049    }
050
051    public static MetricSet of(Cache cache, String name, String... names) {
052        String basicName = MetricRegistry.name(name, names);
053
054        GuavaCacheMetric metrics = new GuavaCacheMetric();
055        metrics.putMetrics(() -> cache.stats().averageLoadPenalty(), basicName, "average", "load", "penalty");
056        metrics.putMetrics(() -> cache.stats().evictionCount(), basicName, "eviction", "count");
057        metrics.putMetrics(() -> cache.stats().hitCount(), basicName, "hit", "count");
058        metrics.putMetrics(() -> cache.stats().hitRate(), basicName, "hit", "rate");
059        metrics.putMetrics(() -> cache.stats().loadCount(), basicName, "load", "count");
060        metrics.putMetrics(() -> cache.stats().loadExceptionCount(), basicName, "load", "exception", "count");
061        metrics.putMetrics(() -> cache.stats().loadExceptionRate(), basicName, "load", "exception", "rate");
062        metrics.putMetrics(() -> cache.stats().loadSuccessCount(), basicName, "load", "success", "count");
063        metrics.putMetrics(() -> cache.stats().missCount(), basicName, "miss", "count");
064        metrics.putMetrics(() -> cache.stats().missRate(), basicName, "miss", "rate");
065        metrics.putMetrics(() -> cache.stats().requestCount(), basicName, "request", "count");
066        metrics.putMetrics(() -> cache.stats().totalLoadTime(), basicName, "total", "load", "time");
067        return metrics;
068    }
069
070}