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;
 import java.util.Set;

A sensor applies a continuous sequence of numerical values to a set of associated metrics. For example a sensor on message size would record a sequence of message sizes using the record(double) api and would maintain a set of metrics about request sizes such as the average or max.
 
 public final class Sensor {
 
     private final MetricsRepository registry;
     private final String name;
     private final Sensor[] parents;
     private final List<Statstats;
     private final List<TehutiMetricmetrics;
     private final MetricConfig config;
     private final Time time;
 
     Sensor(MetricsRepository registryString nameSensor[] parentsMetricConfig configTime time) {
         super();
         this. = registry;
         this. = Utils.notNull(name);
         this. = parents == null ? new Sensor[0] : parents;
         this. = new ArrayList<TehutiMetric>();
         this. = new ArrayList<Stat>();
         this. = config;
         this. = time;
         checkForest(new HashSet<Sensor>());
     }

    
Validate that this sensor doesn't end up referencing itself
 
     private void checkForest(Set<Sensorsensors) {
         if (!sensors.add(this))
             throw new IllegalArgumentException("Circular dependency in sensors: " + name() + " is its own parent.");
         for (int i = 0; i < .i++)
             [i].checkForest(sensors);
     }

    
The name this sensor is registered with. This name will be unique among all registered sensors.
 
     public String name() {
         return this.;
     }

    
Record an occurrence, this is just short-hand for record(1.0)
 
     public void record() {
         record(1.0);
     }

    
Record a value with this sensor

Parameters:
value The value to record
Throws:
QuotaViolationException if recording this value moves a metric beyond its configured maximum or minimum bound
 
     public void record(double value) {
         record(value.milliseconds());
     }

    
Record a value at a known time. This method is slightly faster than record(double) since it will reuse the time stamp.

Parameters:
value The value we are recording
timeMs The current POSIX time in milliseconds
Throws:
QuotaViolationException if recording this value moves a metric beyond its configured maximum or minimum bound
 
     public void record(double valuelong timeMs) {
         synchronized (this) {
             // increment all the stats
             for (int i = 0; i < this..size(); i++)
                 this..get(i).record(valuetimeMs);
            checkQuotas(timeMs);
        }
        for (int i = 0; i < .i++)
            [i].record(valuetimeMs);
    }

    
Check if we have violated our quota for any metric that has a configured quota

Parameters:
timeMs The current POSIX time in milliseconds
    private void checkQuotas(long timeMs) {
        for (int i = 0; i < this..size(); i++) {
            TehutiMetric metric = this..get(i);
            MetricConfig config = metric.config();
            if (config != null) {
                Quota quota = config.quota();
                if (quota != null) {
                    double value = metric.value(timeMs);
                    if (!quota.acceptable(value))
                        throw new QuotaViolationException("Metric " + metric.name() + " is in violation of its " + quota.toString(), value);
                }
            }
        }
    }

    
Register a compound statistic with this sensor with no config override. Equivalent to add(stat, null)
    public Map<StringMetricadd(CompoundStat stat) {
        return add(statnull);
    }

    
Register a compound statistic with this sensor which yields multiple measurable quantities (like a histogram)

Parameters:
stat The stat to register
config The configuration for this stat. If null then the stat will use the default configuration for this sensor.
Returns:
a map of io.tehuti.Metric indexed by their name, each of which were contained in the CompoundStat and added to this sensor.
    public synchronized Map<StringMetricadd(CompoundStat statMetricConfig config) {
        this..add(Utils.notNull(stat));
        Map<StringMetricaddedMetrics = new HashMap<StringMetric>();
        for (NamedMeasurable m : stat.stats()) {
            TehutiMetric metric = new TehutiMetric(thism.name(), m.description(), m.stat(), config == null ? this. : config);
            this..registerMetric(metric);
            this..add(metric);
            addedMetrics.put(metric.name(), metric);
        }
        return addedMetrics;
    }

    
Add a metric with default configuration and no description. Equivalent to add(name, "", stat, null)
    public Metric add(String nameMeasurableStat stat) {
        return add(namestatnull);
    }

    
Add a metric with default configuration. Equivalent to add(name, description, stat, null)
    public Metric add(String nameString descriptionMeasurableStat stat) {
        return add(namedescriptionstatnull);
    }

    
Add a metric to this sensor with no description. Equivalent to add(name, "", stat, config)
    public Metric add(String nameMeasurableStat statMetricConfig config) {
        return add(name""statconfig);
    }

    
Register a metric with this sensor

Parameters:
name The name of the metric
description A description used when reporting the value
stat The statistic to keep
config A special configuration for this metric. If null use the sensor default configuration.
Returns:
a io.tehuti.Metric instance representing the registered metric
    public synchronized Metric add(String nameString descriptionMeasurableStat statMetricConfig config) {
        TehutiMetric metric = new TehutiMetric(this,
                                             Utils.notNull(name),
                                             Utils.notNull(description),
                                             Utils.notNull(stat),
                                             config == null ? this. : config,
                                             );
        this..registerMetric(metric);
        this..add(metric);
        this..add(stat);
        return metric;
    }
    synchronized List<? extends Metricmetrics() {
        return Collections.unmodifiableList(this.);
    }
New to GrepCode? Check out our FAQ X