
org.apache.omid.metrics.MetricsRegistryMap Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.omid.metrics;
import com.google.common.base.Optional;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class MetricsRegistryMap {
private final ConcurrentMap metrics = new ConcurrentHashMap<>();
interface MetricBuilder {
MetricBuilder> GAUGES = new MetricBuilder>() {
@Override
public boolean isInstance(Metric metric) {
return Gauge.class.isInstance(metric);
}
};
MetricBuilder COUNTERS = new MetricBuilder() {
@Override
public boolean isInstance(Metric metric) {
return Counter.class.isInstance(metric);
}
};
MetricBuilder TIMERS = new MetricBuilder() {
@Override
public boolean isInstance(Metric metric) {
return Timer.class.isInstance(metric);
}
};
MetricBuilder METERS = new MetricBuilder() {
@Override
public boolean isInstance(Metric metric) {
return Meter.class.isInstance(metric);
}
};
MetricBuilder HISTOGRAMS = new MetricBuilder() {
@Override
public boolean isInstance(Metric metric) {
return Histogram.class.isInstance(metric);
}
};
boolean isInstance(Metric metric);
}
public Optional extends Metric> get(final String name,
MetricBuilder extends Metric> builder,
Class extends Metric> type) {
final Metric metric = metrics.get(name);
if (builder.isInstance(metric)) {
return Optional.of(type.cast(metric));
}
return Optional.absent();
}
@SuppressWarnings("unchecked")
public List> getGauges() {
List> gaugesList = new ArrayList<>();
for (Metric metric : metrics.values()) {
if (metric instanceof Gauge) {
gaugesList.add((Gauge) metric);
}
}
return gaugesList;
}
public void register(String name, Metric metric) throws IllegalArgumentException {
final Metric existing = metrics.putIfAbsent(name, metric);
if (existing != null) {
throw new IllegalArgumentException("A metric named " +
name +
" of class " +
metric.getClass().getCanonicalName() +
" already exists");
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy