com.netflix.spectator.metrics5.MetricsGaugeAggr Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2014-2022 Netflix, Inc.
*
* 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.netflix.spectator.metrics5;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* Aggregate gauge used to compute the sum of gauges with the same Id.
* This aggregate gauge is only used to register with Metrics, the register with spectator is for each one of
* the registered gauges in this Aggregate gauge.
*
* @author Kennedy Oliveira
*/
class MetricsGaugeAggr implements io.dropwizard.metrics5.Gauge {
private ConcurrentLinkedQueue gauges;
/**
* Creates a new Aggregate Gauge.
*/
MetricsGaugeAggr() {
this.gauges = new ConcurrentLinkedQueue<>();
}
@Override
public Double getValue() {
double aggregatedValue = Double.NaN;
final Iterator iterator = gauges.iterator();
while (iterator.hasNext()) {
final MetricsGauge gauge = iterator.next();
if (gauge.hasExpired()) {
iterator.remove();
} else {
final double gaugeVal = gauge.value();
// When it's NaN means the gauge can be unregistered due to it's reference to the value to extract value
// was garbage collected or simple the gauge returned a NaN so i don't count it
if (!Double.isNaN(gaugeVal))
aggregatedValue = (Double.isNaN(aggregatedValue) ? 0 : aggregatedValue) + gaugeVal;
}
}
return aggregatedValue;
}
/**
* Add a gauge to be aggragate to the others.
*
* @param gauge Gauge to be added
*/
void addGauge(MetricsGauge gauge) {
this.gauges.add(gauge);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy