Jelajahi Sumber

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

Handle opts value in Metrics API

GitOrigin-RevId: dae0de02f513bf65beb7b665259424a07490dc41
ilkin-overleaf 3 tahun lalu
induk
melakukan
c94043e2fe

+ 4 - 0
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.

+ 31 - 0
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)

+ 1 - 1
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",