001/*
002 * (C) Copyright 2017 Nuxeo (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 <kleturc@nuxeo.com>
018 */
019package org.nuxeo.runtime.metrics;
020
021import java.util.HashMap;
022import java.util.Map;
023import java.util.Set;
024import java.util.function.Supplier;
025
026import com.codahale.metrics.Gauge;
027import com.codahale.metrics.Metric;
028import com.codahale.metrics.MetricRegistry;
029import com.codahale.metrics.MetricSet;
030
031/**
032 * Just a helper to easily declare metrics inside a {@link MetricSet} with th ease of Java 8 Lambda expression.
033 * 
034 * @since 8.10-HF08, 9.2
035 */
036public class NuxeoMetricSet implements MetricSet {
037
038    protected final Map<String, Metric> metrics;
039
040    protected final String prefixName;
041
042    public NuxeoMetricSet() {
043        // we can inject null as prefix because MetricRegistry#name(String, String...) doesn't print null value
044        this(null);
045    }
046
047    public NuxeoMetricSet(String name, String... names) {
048        this(HashMap::new, name, names);
049    }
050
051    public NuxeoMetricSet(Supplier<Map<String, Metric>> metricsSupplier, String name, String... names) {
052        this.metrics = metricsSupplier.get();
053        this.prefixName = MetricRegistry.name(name, names);
054    }
055
056    /**
057     * Put a gauge inside this {@link MetricSet} as name {@code prefixName.name.names[0].names[1]...};
058     */
059    public <T> void putGauge(Gauge<T> gauge, String name, String... names) {
060        metrics.put(buildNameWithPrefix(name, names), gauge);
061    }
062
063    /**
064     * @return the name built from {@link MetricRegistry#name(String, String...)} prefixed with this
065     *         {@link NuxeoMetricSet}'s prefix
066     */
067    protected String buildNameWithPrefix(String name, String[] names) {
068        return MetricRegistry.name(MetricRegistry.name(prefixName, name), names);
069    }
070
071    @Override
072    public Map<String, Metric> getMetrics() {
073        return metrics;
074    }
075
076    /**
077     * @return the prefix name used by this {@link MetricSet} to prefix all added metrics, the value could be empty
078     */
079    public String getPrefixName() {
080        return prefixName;
081    }
082
083    /**
084     * @return all metric names registered into this {@link MetricSet}
085     */
086    public Set<String> getMetricNames() {
087        return metrics.keySet();
088    }
089
090}