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 io.dropwizard.metrics5.Gauge;
027import io.dropwizard.metrics5.Metric;
028import io.dropwizard.metrics5.MetricName;
029import io.dropwizard.metrics5.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<MetricName, Metric> metrics;
039
040    protected final MetricName 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(MetricName.build(name).append(MetricName.build(names)));
049    }
050
051    public NuxeoMetricSet(MetricName name) {
052        this(HashMap::new, name);
053    }
054
055    public NuxeoMetricSet(Supplier<Map<MetricName, Metric>> metricsSupplier, MetricName name) {
056        this.metrics = metricsSupplier.get();
057        this.prefixName = name;
058    }
059
060    /**
061     * Put a gauge inside this {@link MetricSet} as name {@code prefixName.name.names[0].names[1]...};
062     */
063    public <T> void putGauge(Gauge<T> gauge, MetricName name) {
064        metrics.put(prefixName.append(name), gauge);
065    }
066
067    public <T> void putGauge(Gauge<T> gauge, String name, String... names) {
068        metrics.put(prefixName.append(MetricName.build(name).append(MetricName.build(names))), gauge);
069    }
070
071    @Override
072    public Map<MetricName, 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 MetricName getPrefixName() {
080        return prefixName;
081    }
082
083    /**
084     * @return all metric names registered into this {@link MetricSet}
085     */
086    public Set<MetricName> getMetricNames() {
087        return metrics.keySet();
088    }
089
090}