Merge pull request #10144 from overleaf/ii-metrics-api-patch-opts-arg

Handle opts value in Metrics API

GitOrigin-RevId: dae0de02f513bf65beb7b665259424a07490dc41
This commit is contained in:
ilkin-overleaf 2022-10-27 16:53:58 +03:00 committed by Copybot
parent ba68db6ba7
commit c94043e2fe
3 changed files with 36 additions and 1 deletions

View file

@ -1,3 +1,7 @@
## v4.1.0
* Allows skipping the `sampleRate` argument.
## v4.0.0
* Send unmodified request and response to logger.

View file

@ -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)

View file

@ -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",