@platformatic/metrics 2.70.0 → 2.70.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -33,11 +33,17 @@ async function collectMetrics (serviceId, workerId, metricsConfig = {}, registry
33
33
  },
34
34
  histogram: {
35
35
  name: 'http_request_all_duration_seconds',
36
- help: 'request duration in seconds summary for all requests'
36
+ help: 'request duration in seconds summary for all requests',
37
+ collect: function () {
38
+ process.nextTick(() => this.reset())
39
+ }
37
40
  },
38
41
  summary: {
39
42
  name: 'http_request_all_summary_seconds',
40
- help: 'request duration in seconds histogram for all requests'
43
+ help: 'request duration in seconds histogram for all requests',
44
+ collect: function () {
45
+ process.nextTick(() => this.reset())
46
+ }
41
47
  }
42
48
  })
43
49
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/metrics",
3
- "version": "2.70.0",
3
+ "version": "2.70.1",
4
4
  "description": "Platformatic Stackable Metrics",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -4,6 +4,8 @@ const assert = require('node:assert')
4
4
  const { test } = require('node:test')
5
5
  const { collectMetrics, client } = require('..')
6
6
 
7
+ const nextTick = () => new Promise(resolve => process.nextTick(resolve))
8
+
7
9
  test('returns expected structure', async () => {
8
10
  const result = await collectMetrics('test-service', 1, {})
9
11
 
@@ -57,3 +59,70 @@ test('workerId is NOT included in labels when negative', async () => {
57
59
  const [{ values }] = await result.registry.getMetricsAsJSON()
58
60
  assert.strictEqual(values[0].labels.workerId, undefined)
59
61
  })
62
+
63
+ test('httpMetrics creates histogram and summary with collect functions', async () => {
64
+ const result = await collectMetrics('test-service', 1, { httpMetrics: true })
65
+ const metrics = await result.registry.getMetricsAsJSON()
66
+
67
+ const histogram = metrics.find(m => m.name === 'http_request_all_duration_seconds')
68
+ const summary = metrics.find(m => m.name === 'http_request_all_summary_seconds')
69
+
70
+ assert.ok(histogram, 'histogram metric should exist')
71
+ assert.ok(summary, 'summary metric should exist')
72
+ assert.strictEqual(histogram.help, 'request duration in seconds summary for all requests')
73
+ assert.strictEqual(summary.help, 'request duration in seconds histogram for all requests')
74
+ })
75
+
76
+ test('httpMetrics histogram resets after metric collection', async () => {
77
+ const result = await collectMetrics('test-service', 1, { httpMetrics: true })
78
+
79
+ const metricObjects = result.registry._metrics
80
+ const histogramMetric = metricObjects.http_request_all_duration_seconds
81
+
82
+ histogramMetric.observe({ method: 'GET', telemetry_id: 'test' }, 0.1)
83
+ histogramMetric.observe({ method: 'GET', telemetry_id: 'test' }, 0.2)
84
+ histogramMetric.observe({ method: 'GET', telemetry_id: 'test' }, 0.3)
85
+
86
+ const metricsBefore = await result.registry.getMetricsAsJSON()
87
+ const histogramBefore = metricsBefore.find(m => m.name === 'http_request_all_duration_seconds')
88
+ assert.ok(histogramBefore.values.length > 0, 'histogram should have values before collection')
89
+
90
+ await result.registry.metrics()
91
+
92
+ await nextTick()
93
+
94
+ const metricsAfter = await result.registry.getMetricsAsJSON()
95
+ const histogramAfter = metricsAfter.find(m => m.name === 'http_request_all_duration_seconds')
96
+
97
+ const sum = histogramAfter.values.find(v => v.metricName === 'http_request_all_duration_seconds_sum')
98
+ const count = histogramAfter.values.find(v => v.metricName === 'http_request_all_duration_seconds_count')
99
+ assert.strictEqual(sum?.value || 0, 0, 'histogram sum should be reset to 0')
100
+ assert.strictEqual(count?.value || 0, 0, 'histogram count should be reset to 0')
101
+ })
102
+
103
+ test('httpMetrics summary resets after metric collection', async () => {
104
+ const result = await collectMetrics('test-service', 1, { httpMetrics: true })
105
+
106
+ const metricObjects = result.registry._metrics
107
+ const summaryMetric = metricObjects.http_request_all_summary_seconds
108
+
109
+ summaryMetric.observe({ method: 'POST', telemetry_id: 'test' }, 0.15)
110
+ summaryMetric.observe({ method: 'POST', telemetry_id: 'test' }, 0.25)
111
+ summaryMetric.observe({ method: 'POST', telemetry_id: 'test' }, 0.35)
112
+
113
+ const metricsBefore = await result.registry.getMetricsAsJSON()
114
+ const summaryBefore = metricsBefore.find(m => m.name === 'http_request_all_summary_seconds')
115
+ assert.ok(summaryBefore.values.length > 0, 'summary should have values before collection')
116
+
117
+ await result.registry.metrics()
118
+
119
+ await nextTick()
120
+
121
+ const metricsAfter = await result.registry.getMetricsAsJSON()
122
+ const summaryAfter = metricsAfter.find(m => m.name === 'http_request_all_summary_seconds')
123
+
124
+ const sum = summaryAfter.values.find(v => v.metricName === 'http_request_all_summary_seconds_sum')
125
+ const count = summaryAfter.values.find(v => v.metricName === 'http_request_all_summary_seconds_count')
126
+ assert.strictEqual(sum?.value || 0, 0, 'summary sum should be reset to 0')
127
+ assert.strictEqual(count?.value || 0, 0, 'summary count should be reset to 0')
128
+ })