From 71164faa140a333a076af7b9cb442ec6fb3f31e1 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Mon, 7 Dec 2020 17:15:25 +0100 Subject: [PATCH] Fix docs and usage of Collector callback Fix callback typo in Collector constructor jsdocs, add notice that the collector is passed as the first argument, and use this first arugument in the built-in collector implementations. --- packages/lib/prometheus.js | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/packages/lib/prometheus.js b/packages/lib/prometheus.js index bb163ea2..cc167625 100644 --- a/packages/lib/prometheus.js +++ b/packages/lib/prometheus.js @@ -337,9 +337,10 @@ class LabeledCollector extends Collector { * @param {boolean=} options.register - * If true registers this collector with the default registry. * Defaults to true. - * @param {function()=} options.callaback - + * @param {function()=} options.callback - * Possibly async function that is called before the metric is - * collected. + * collected. The collector being collected is passed as the + * argument. * @param { * function(module:lib/prometheus.LabeledCollector,string) * } childClass - @@ -479,9 +480,10 @@ class ValueCollector extends LabeledCollector { * @param {boolean=} options.register - * If true registers this collector with the default registry. * Defaults to true. - * @param {function()=} options.callaback - + * @param {function()=} options.callback - * Possibly async function that is called before the collector value - * is collected. + * is collected. The collector being collected is passed as the + * argument. * @param { * function( * new:module:lib/prometheus.ValueCollectorChild, @@ -666,9 +668,10 @@ class Counter extends ValueCollector { * @param {boolean=} options.register - * If true registers this collector with the default registry. * Defaults to true. - * @param {function()=} options.callaback - + * @param {function()=} options.callback - * Possibly async function that is called when the metric is - * collected. + * collected. The collector being collected is passed as the + * argument. */ constructor(name, help, options = {}) { super("counter", name, help, options, CounterChild); @@ -855,9 +858,10 @@ class Gauge extends ValueCollector { * @param {boolean=} options.register - * If true registers this collector with the default registry. * Defaults to true. - * @param {function()=} options.callaback - + * @param {function()=} options.callback - * Possibly async function that is called when the metric is - * collected. + * collected. The collector being collected is passed as the + * argument. */ constructor(name, help, options = {}) { super("gauge", name, help, options, GaugeChild); @@ -1102,9 +1106,10 @@ class Histogram extends LabeledCollector { * @param {boolean=} options.register - * If true registers this collector with the default registry. * Defaults to true. - * @param {function()=} options.callaback - + * @param {function()=} options.callback - * Possibly async function that is called when the metric is - * collected. + * collected. The collector being collected is passed as the + * argument. */ constructor(name, help, options = {}) { // These defaults are taken from the Python Prometheus client @@ -1525,9 +1530,9 @@ defaultCollectors.processCpuSecondsTotal = new Gauge( "process_cpu_seconds_total", "Total user and system CPU time spent in seconds.", { - callback: async function() { + callback: async function(collector) { let usage = process.cpuUsage(); - this.set((usage.user + usage.system) / 1e6); + collector.set((usage.user + usage.system) / 1e6); }, }, ); @@ -1536,8 +1541,8 @@ defaultCollectors.processResidentMemoryBytes = new Gauge( "process_resident_memory_bytes", "Resident memory size in bytes.", { - callback: function() { - this.set(process.memoryUsage().rss); + callback: function(collector) { + collector.set(process.memoryUsage().rss); }, }, ); @@ -1546,8 +1551,8 @@ defaultCollectors.processHeapBytes = new Gauge( "process_heap_bytes", "Process heap size in bytes.", { - callback: function() { - this.set(process.memoryUsage().heapUsed); + callback: function(collector) { + collector.set(process.memoryUsage().heapUsed); }, }, );