
com.google.gerrit.metrics.MetricMaker Maven / Gradle / Ivy
// Copyright (C) 2015 The Android Open Source Project
//
// Licensed 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 com.google.gerrit.metrics;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableSet;
import com.google.gerrit.extensions.registration.RegistrationHandle;
import java.util.Set;
/** Factory to create metrics for monitoring. */
public abstract class MetricMaker {
/**
* Metric whose value increments during the life of the process.
*
* @param name field name
* @param desc field description
* @return counter
*/
public abstract Counter0 newCounter(String name, Description desc);
public abstract Counter1 newCounter(String name, Description desc, Field field1);
public abstract Counter2 newCounter(
String name, Description desc, Field field1, Field field2);
public abstract Counter3 newCounter(
String name, Description desc, Field field1, Field field2, Field field3);
/**
* Metric recording time spent on an operation.
*
* @param name field name
* @param desc field description
* @return timer
*/
public abstract Timer0 newTimer(String name, Description desc);
public abstract Timer1 newTimer(String name, Description desc, Field field1);
public abstract Timer2 newTimer(
String name, Description desc, Field field1, Field field2);
public abstract Timer3 newTimer(
String name, Description desc, Field field1, Field field2, Field field3);
/**
* Metric recording statistical distribution of values.
*
* @param name field name
* @param desc field description
* @return histogram
*/
public abstract Histogram0 newHistogram(String name, Description desc);
public abstract Histogram1 newHistogram(String name, Description desc, Field field1);
public abstract Histogram2 newHistogram(
String name, Description desc, Field field1, Field field2);
public abstract Histogram3 newHistogram(
String name, Description desc, Field field1, Field field2, Field field3);
/**
* Constant value that does not change.
*
* @param name unique name of the metric.
* @param value only value of the metric.
* @param desc description of the metric.
*/
public void newConstantMetric(String name, final V value, Description desc) {
desc.setConstant();
@SuppressWarnings("unchecked")
Class type = (Class) value.getClass();
final CallbackMetric0 metric = newCallbackMetric(name, type, desc);
newTrigger(
metric,
new Runnable() {
@Override
public void run() {
metric.set(value);
}
});
}
/**
* Instantaneous reading of a value.
*
*
* metricMaker.newCallbackMetric("memory",
* new Description("Total bytes of memory used")
* .setGauge()
* .setUnit(Units.BYTES),
* new Supplier<Long>() {
* public Long get() {
* return Runtime.getRuntime().totalMemory();
* }
* });
*
*
* @param name unique name of the metric.
* @param valueClass type of value recorded by the metric.
* @param desc description of the metric.
* @param trigger function to compute the value of the metric.
*/
public void newCallbackMetric(
String name, Class valueClass, Description desc, final Supplier trigger) {
final CallbackMetric0 metric = newCallbackMetric(name, valueClass, desc);
newTrigger(
metric,
new Runnable() {
@Override
public void run() {
metric.set(trigger.get());
}
});
}
/**
* Instantaneous reading of a single value.
*
* @param name field name
* @param valueClass field type
* @param desc field description
* @return callback
*/
public abstract CallbackMetric0 newCallbackMetric(
String name, Class valueClass, Description desc);
public abstract CallbackMetric1 newCallbackMetric(
String name, Class valueClass, Description desc, Field field1);
/**
* Connect logic to populate a previously created {@link CallbackMetric}.
*
* @param metric1 previously created callback
* @param trigger trigger to connect
* @return registration handle
*/
public RegistrationHandle newTrigger(CallbackMetric> metric1, Runnable trigger) {
return newTrigger(ImmutableSet.>of(metric1), trigger);
}
public RegistrationHandle newTrigger(
CallbackMetric> metric1, CallbackMetric> metric2, Runnable trigger) {
return newTrigger(ImmutableSet.of(metric1, metric2), trigger);
}
public RegistrationHandle newTrigger(
CallbackMetric> metric1,
CallbackMetric> metric2,
CallbackMetric> metric3,
Runnable trigger) {
return newTrigger(ImmutableSet.of(metric1, metric2, metric3), trigger);
}
public abstract RegistrationHandle newTrigger(Set> metrics, Runnable trigger);
/**
* Sanitize the given metric name.
*
* @param name the name to sanitize.
* @return sanitized version of the name.
*/
public String sanitizeMetricName(String name) {
return name;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy