From c94043e2fe199ca2b597c0adb5bf33e5e5f870b2 Mon Sep 17 00:00:00 2001 From: ilkin-overleaf <100852799+ilkin-overleaf@users.noreply.github.com> Date: Thu, 27 Oct 2022 16:53:58 +0300 Subject: [PATCH] Merge pull request #10144 from overleaf/ii-metrics-api-patch-opts-arg Handle opts value in Metrics API GitOrigin-RevId: dae0de02f513bf65beb7b665259424a07490dc41 --- libraries/metrics/CHANGELOG.md | 4 ++++ libraries/metrics/index.js | 31 +++++++++++++++++++++++++++++++ libraries/metrics/package.json | 2 +- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/libraries/metrics/CHANGELOG.md b/libraries/metrics/CHANGELOG.md index 9af5022720..30f24ea358 100644 --- a/libraries/metrics/CHANGELOG.md +++ b/libraries/metrics/CHANGELOG.md @@ -1,3 +1,7 @@ +## v4.1.0 + +* Allows skipping the `sampleRate` argument. + ## v4.0.0 * Send unmodified request and response to logger. diff --git a/libraries/metrics/index.js b/libraries/metrics/index.js index d78d51f24f..58e22285ae 100644 --- a/libraries/metrics/index.js +++ b/libraries/metrics/index.js @@ -100,6 +100,10 @@ function set(key, value, sampleRate = 1) { } function inc(key, sampleRate = 1, opts = {}) { + if (arguments.length === 2 && typeof sampleRate === 'object') { + opts = sampleRate + } + key = buildPromKey(key) promWrapper.metric('counter', key).inc(opts) if (process.env.DEBUG_METRICS) { @@ -108,6 +112,10 @@ function inc(key, sampleRate = 1, opts = {}) { } function count(key, count, sampleRate = 1, opts = {}) { + if (arguments.length === 3 && typeof sampleRate === 'object') { + opts = sampleRate + } + key = buildPromKey(key) promWrapper.metric('counter', key).inc(opts, count) if (process.env.DEBUG_METRICS) { @@ -124,6 +132,10 @@ function summary(key, value, opts = {}) { } function timing(key, timeSpan, sampleRate = 1, opts = {}) { + if (arguments.length === 3 && typeof sampleRate === 'object') { + opts = sampleRate + } + key = buildPromKey('timer_' + key) promWrapper.metric('summary', key).observe(opts, timeSpan) if (process.env.DEBUG_METRICS) { @@ -141,6 +153,21 @@ function histogram(key, value, buckets, opts = {}) { class Timer { constructor(key, sampleRate = 1, opts = {}, buckets) { + if (typeof sampleRate === 'object') { + // called with (key, opts, buckets) + if (arguments.length === 3) { + buckets = opts + opts = sampleRate + } + + // called with (key, opts) + if (arguments.length === 2) { + opts = sampleRate + } + + sampleRate = 1 // default value to pass to timing function + } + this.start = new Date() key = buildPromKey(key) this.key = key @@ -161,6 +188,10 @@ class Timer { } function gauge(key, value, sampleRate = 1, opts = {}) { + if (arguments.length === 3 && typeof sampleRate === 'object') { + opts = sampleRate + } + key = buildPromKey(key) promWrapper .metric('gauge', key) diff --git a/libraries/metrics/package.json b/libraries/metrics/package.json index 314b68c4ba..5e13662f97 100644 --- a/libraries/metrics/package.json +++ b/libraries/metrics/package.json @@ -1,6 +1,6 @@ { "name": "@overleaf/metrics", - "version": "4.0.0", + "version": "4.1.0", "description": "A drop-in metrics and monitoring module for node.js apps", "repository": { "type": "git",