Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Aggregator.prototype.update = function (timestamp, name, value, calcDiff) {
this.lastUpdate = Date.now()
if (isNaN(value)) {
return
}
if (this.metrics[name] === undefined) {
this.metrics[name] = new Measured.Histogram()
}
if (!this.lastValues[name]) {
this.lastValues[name] = {value: value, ts: timestamp}
}
if (calcDiff === true) {
var diff = value - this.lastValues[name].value
if (diff < 0) {
// container stop might have caused a reset of metric value.
this.lastValues[name].value = 0
} else {
this.metrics[name].update(diff, timestamp)
this.lastValues[name] = {value: value, ts: timestamp}
}
} else {
this.metrics[name].update(value, timestamp)
getOrCreateMeter(name, dimensions, publishingIntervalInSeconds) {
// todo validate options
let meter;
if (this._registry.hasMetric(name, dimensions)) {
meter = this._registry.getMetric(name, dimensions);
} else {
meter = new Meter();
const key = this._registry.putMetric(name, meter, dimensions);
this._reporters.forEach(reporter => reporter.reportMetricOnInterval(key, publishingIntervalInSeconds));
}
return meter;
}
getOrCreateTimer(name, dimensions, publishingIntervalInSeconds) {
validateTimerOptions(name, dimensions, publishingIntervalInSeconds);
let timer;
if (this._registry.hasMetric(name, dimensions)) {
timer = this._registry.getMetric(name, dimensions);
} else {
timer = new Timer();
const key = this._registry.putMetric(name, timer, dimensions);
this._reporters.forEach(reporter => reporter.reportMetricOnInterval(key, publishingIntervalInSeconds));
}
return timer;
}
constructor(properties = {}) {
const { duration } = properties;
return new Timer(Object.assign({}, properties, {
meter: new Meter({ duration })
}));
}
getOrCreateTimer(name, dimensions, publishingIntervalInSeconds) {
validateTimerOptions(name, dimensions, publishingIntervalInSeconds);
let timer;
if (this._registry.hasMetric(name, dimensions)) {
timer = this._registry.getMetric(name, dimensions);
} else {
timer = new Timer({ meter: new NoOpMeter() });
const key = this._registry.putMetric(name, timer, dimensions);
this._reporters.forEach(reporter => reporter.reportMetricOnInterval(key, publishingIntervalInSeconds));
}
return timer;
}
'node.os.cpu.all-cores-avg': (updateIntervalInSeconds, sampleTimeInSeconds) => {
updateIntervalInSeconds = updateIntervalInSeconds || 30;
sampleTimeInSeconds = sampleTimeInSeconds || 5;
return new CachedGauge(() => {
return new Promise(resolve => {
//Grab first CPU Measure
const startMeasure = cpuAverage();
setTimeout(() => {
//Grab second Measure
const endMeasure = cpuAverage();
const percentageCPU = calculateCpuUsagePercent(startMeasure, endMeasure);
resolve(percentageCPU);
}, sampleTimeInSeconds);
});
}, updateIntervalInSeconds);
}
};
getOrCreateCachedGauge(
name,
valueProducingPromiseCallback,
cachedGaugeUpdateIntervalInSeconds,
dimensions,
publishingIntervalInSeconds
) {
validateCachedGaugeOptions(name, valueProducingPromiseCallback, dimensions, publishingIntervalInSeconds);
let cachedGauge;
if (this._registry.hasMetric(name, dimensions)) {
cachedGauge = this._registry.getMetric(name, dimensions);
} else {
cachedGauge = new CachedGauge(valueProducingPromiseCallback, cachedGaugeUpdateIntervalInSeconds);
const key = this._registry.putMetric(name, cachedGauge, dimensions);
this._reporters.forEach(reporter => reporter.reportMetricOnInterval(key, publishingIntervalInSeconds));
}
return cachedGauge;
}
getOrCreateHistogram(name, dimensions, publishingIntervalInSeconds) {
validateHistogramOptions(name, dimensions, publishingIntervalInSeconds);
let histogram;
if (this._registry.hasMetric(name, dimensions)) {
histogram = this._registry.getMetric(name, dimensions);
} else {
histogram = new Histogram();
const key = this._registry.putMetric(name, histogram, dimensions);
this._reporters.forEach(reporter => reporter.reportMetricOnInterval(key, publishingIntervalInSeconds));
}
return histogram;
}
getOrCreateMeter(name, dimensions, publishingIntervalInSeconds) {
this._log.error(
'Meters will not get reported using the SignalFx reporter as they waste DPM, please use a counter instead'
);
return new NoOpMeter();
}
getOrCreateCounter(name, dimensions, publishingIntervalInSeconds) {
validateCounterOptions(name, dimensions, publishingIntervalInSeconds);
let counter;
if (this._registry.hasMetric(name, dimensions)) {
counter = this._registry.getMetric(name, dimensions);
} else {
counter = new Counter();
const key = this._registry.putMetric(name, counter, dimensions);
this._reporters.forEach(reporter => reporter.reportMetricOnInterval(key, publishingIntervalInSeconds));
}
return counter;
}