Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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 io.tehuti.metrics;
 
 import java.util.List;
 import java.util.Map;
 
A registry of sensors and metrics.

A metric is a named, numerical measurement. A sensor is a handle to record numerical measurements as they occur. Each Sensor has zero or more associated metrics. For example a Sensor might represent message sizes and we might associate with this sensor a metric for the average, maximum, or other statistics computed off the sequence of message sizes that are recorded by the sensor.

Usage looks something like this:

 // set up metrics:
 MetricsRepository metrics = new MetricsRepository(); // this is the global repository of metrics and sensors
 Sensor sensor = metrics.sensor("message-sizes");
 sensor.add("message-sizes.avg", new Avg());
 sensor.add("message-sizes.max", new Max());
 
 // as messages are sent we record the sizes
 sensor.record(messageSize);
 
 
 public class MetricsRepository {
 
     private final MetricConfig config;
     private final ConcurrentMap<StringTehutiMetricmetrics;
     private final ConcurrentMap<StringSensorsensors;
     private final List<MetricsReporterreporters;
     private final Time time;

    
Create a metrics repository with no metric reporters and default configuration.
 
     public MetricsRepository() {
         this(new MetricConfig());
     }

    
Create a metrics repository with no metric reporters and default configuration.
 
     public MetricsRepository(Time time) {
         this(new MetricConfig(), new ArrayList<MetricsReporter>(0), time);
     }

    
Create a metrics repository with no reporters and the given default config. This config will be used for any metric that doesn't override its own config.

Parameters:
defaultConfig The default config to use for all metrics that don't override their config
 
     public MetricsRepository(MetricConfig defaultConfig) {
         this(defaultConfignew ArrayList<MetricsReporter>(0), new SystemTime());
     }

    
Create a metrics repository with a default config and the given metric reporters

Parameters:
defaultConfig The default config
reporters The metrics reporters
time The time instance to use with the metrics
 
     public MetricsRepository(MetricConfig defaultConfigList<MetricsReporterreportersTime time) {
         this. = defaultConfig;
         this. = new CopyOnWriteMap<StringSensor>();
         this. = new CopyOnWriteMap<StringTehutiMetric>();
         this. = Utils.notNull(reporters);
         this. = time;
         for (MetricsReporter reporter : reporters)
             reporter.init(new ArrayList<TehutiMetric>());
     }

    
Get the sensor with the given name if it exists

Parameters:
name The name of the sensor
Returns:
Return the sensor or null if no such sensor exists
 
     public Sensor getSensor(String name) {
        return this..get(Utils.notNull(name));
    }

    
Get or create a sensor with the given unique name and no parent sensors.

Parameters:
name The sensor name
Returns:
The sensor
    public Sensor sensor(String name) {
        return sensor(namenull, (Sensor[]) null);
    }

    
Get or create a sensor with the given unique name and zero or more parent sensors. All parent sensors will receive every value recorded with this sensor.

Parameters:
name The name of the sensor
parents The parent sensors
Returns:
The sensor that is created
    public Sensor sensor(String nameSensor... parents) {
        return sensor(namenullparents);
    }

    
Get or create a sensor with the given unique name and zero or more parent sensors. All parent sensors will receive every value recorded with this sensor.

Parameters:
name The name of the sensor
config A default configuration to use for this sensor for metrics that don't have their own config
parents The parent sensors
Returns:
The sensor that is created
    public synchronized Sensor sensor(String nameMetricConfig configSensor... parents) {
        Sensor s = getSensor(name);
        if (s == null) {
            s = new Sensor(thisnameparentsconfig == null ? this. : config);
            this..put(names);
        }
        return s;
    }

    
Add a metric to monitor an object that implements measurable. This metric won't be associated with any sensor. This is a way to expose existing values as metrics.

Parameters:
name The name of the metric
measurable The measurable that will be measured by this metric
Returns:
a io.tehuti.Metric instance representing the registered metric
    public Metric addMetric(String nameMeasurable measurable) {
        return addMetric(name""measurable);
    }

    
Add a metric to monitor an object that implements measurable. This metric won't be associated with any sensor. This is a way to expose existing values as metrics.

Parameters:
name The name of the metric
description A human-readable description to include in the metric
measurable The measurable that will be measured by this metric
Returns:
a io.tehuti.Metric instance representing the registered metric
    public Metric addMetric(String nameString descriptionMeasurable measurable) {
        return addMetric(namedescriptionnullmeasurable);
    }

    
Add a metric to monitor an object that implements measurable. This metric won't be associated with any sensor. This is a way to expose existing values as metrics.

Parameters:
name The name of the metric
config The configuration to use when measuring this measurable
measurable The measurable that will be measured by this metric
Returns:
a io.tehuti.Metric instance representing the registered metric
    public Metric addMetric(String nameMetricConfig configMeasurable measurable) {
        return addMetric(name""configmeasurable);
    }

    
Add a metric to monitor an object that implements measurable. This metric won't be associated with any sensor. This is a way to expose existing values as metrics.

Parameters:
name The name of the metric
description A human-readable description to include in the metric
config The configuration to use when measuring this measurable
measurable The measurable that will be measured by this metric
Returns:
a io.tehuti.Metric instance representing the registered metric N.B.: If the registered measurable is a CompoundStat (such as io.tehuti.metrics.stats.Percentiles), then the returned io.tehuti.Metric will represent that CompoundStat. In order to get access to the io.tehuti.Metric instances representing the specific metrics contained in the CompoundStat, you'll need to call getMetric(java.lang.String) or metrics().
    public synchronized Metric addMetric(String nameString descriptionMetricConfig configMeasurable measurable) {
        TehutiMetric m = new TehutiMetric(new Object(),
                                          Utils.notNull(name),
                                          Utils.notNull(description),
                                          Utils.notNull(measurable),
                                          config == null ? this. : config,
                                          );
        registerMetric(m);
        return m;
    }

    
Add a MetricReporter
    public synchronized void addReporter(MetricsReporter reporter) {
        Utils.notNull(reporter).init(new ArrayList<TehutiMetric>(.values()));
        this..add(reporter);
    }
    synchronized void registerMetric(TehutiMetric metric) {
        if (this..containsKey(metric.name()))
            throw new IllegalArgumentException("A metric named '" + metric.name() + "' already exists, can't register another one.");
        this..put(metric.name(), metric);
        for (MetricsReporter reporter : )
            reporter.metricChange(metric);
    }

    
Get all the metrics currently maintained indexed by metric name
    public Map<String, ? extends Metricmetrics() {
        return this.;
    }
    public Metric getMetric(String name) {
        return this..get(name);
    }

    
Close this metrics repository.
    public void close() {
        for (MetricsReporter reporter : this.)
            reporter.close();
    }
New to GrepCode? Check out our FAQ X