/** * Register a metric with this sensor * * @param metricName The name of the metric * @param stat The statistic to keep * @param config A special configuration for this metric. If null use the sensor default configuration. * @return true if metric is added to sensor, false if sensor is expired */ public synchronized boolean add(final MetricName metricName, final MeasurableStat stat, final MetricConfig config) { if (hasExpired()) { return false; } else if (metrics.containsKey(metricName)) { return true; } else { final KafkaMetric metric = new KafkaMetric( metricLock(), Utils.notNull(metricName), Utils.notNull(stat), config == null ? this.config : config, time ); registry.registerMetric(metric); metrics.put(metric.metricName(), metric); stats.add(stat); return true; } }
public void record(double value, long timeMs, boolean checkQuotas) { if (shouldRecord()) { this.lastRecordTime = timeMs; synchronized (this) { synchronized (metricLock()) { // increment all the stats for (Stat stat : this.stats) stat.record(config, value, timeMs); } if (checkQuotas) checkQuotas(timeMs); } for (Sensor parent : parents) parent.record(value, timeMs, checkQuotas); } }
/** * Register a compound statistic with this sensor which yields multiple measurable quantities (like a histogram) * @param stat The stat to register * @param config The configuration for this stat. If null then the stat will use the default configuration for this * sensor. * @return true if stat is added to sensor, false if sensor is expired */ public synchronized boolean add(CompoundStat stat, MetricConfig config) { if (hasExpired()) return false; this.stats.add(Utils.notNull(stat)); Object lock = metricLock(); for (NamedMeasurable m : stat.stats()) { final KafkaMetric metric = new KafkaMetric(lock, m.name(), m.stat(), config == null ? this.config : config, time); if (!metrics.containsKey(metric.metricName())) { registry.registerMetric(metric); metrics.put(metric.metricName(), metric); } } return true; }