@platformatic/metrics 2.69.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
@@ -12,9 +12,6 @@ async function collectMetrics (serviceId, workerId, metricsConfig = {}, registry
12
12
  registry = new Registry()
13
13
  }
14
14
 
15
- const httpRequestCallbacks = []
16
- const httpResponseCallbacks = []
17
-
18
15
  const labels = { ...metricsConfig.labels, serviceId }
19
16
  if (workerId >= 0) {
20
17
  labels.workerId = workerId
@@ -28,51 +25,31 @@ async function collectMetrics (serviceId, workerId, metricsConfig = {}, registry
28
25
  }
29
26
 
30
27
  if (metricsConfig.httpMetrics) {
31
- {
32
- const { startTimer, endTimer } = collectHttpMetrics(registry, {
33
- customLabels: ['telemetry_id'],
34
- getCustomLabels: req => {
35
- const telemetryId = req.headers?.['x-plt-telemetry-id'] ?? 'unknown'
36
- return { telemetry_id: telemetryId }
28
+ collectHttpMetrics(registry, {
29
+ customLabels: ['telemetry_id'],
30
+ getCustomLabels: req => {
31
+ const telemetryId = req.headers?.['x-plt-telemetry-id'] ?? 'unknown'
32
+ return { telemetry_id: telemetryId }
33
+ },
34
+ histogram: {
35
+ name: 'http_request_all_duration_seconds',
36
+ help: 'request duration in seconds summary for all requests',
37
+ collect: function () {
38
+ process.nextTick(() => this.reset())
37
39
  }
38
- })
39
- httpRequestCallbacks.push(startTimer)
40
- httpResponseCallbacks.push(endTimer)
41
- }
42
-
43
- {
44
- // TODO: check if it's a nodejs environment
45
- // Needed for the Meraki metrics
46
- const { startTimer, endTimer } = collectHttpMetrics(registry, {
47
- customLabels: ['telemetry_id'],
48
- getCustomLabels: req => {
49
- const telemetryId = req.headers?.['x-plt-telemetry-id'] ?? 'unknown'
50
- return { telemetry_id: telemetryId }
51
- },
52
- histogram: {
53
- name: 'http_request_all_duration_seconds',
54
- help: 'request duration in seconds summary for all requests',
55
- collect: function () {
56
- process.nextTick(() => this.reset())
57
- }
58
- },
59
- summary: {
60
- name: 'http_request_all_summary_seconds',
61
- help: 'request duration in seconds histogram for all requests',
62
- collect: function () {
63
- process.nextTick(() => this.reset())
64
- }
40
+ },
41
+ summary: {
42
+ name: 'http_request_all_summary_seconds',
43
+ help: 'request duration in seconds histogram for all requests',
44
+ collect: function () {
45
+ process.nextTick(() => this.reset())
65
46
  }
66
- })
67
- httpRequestCallbacks.push(startTimer)
68
- httpResponseCallbacks.push(endTimer)
69
- }
47
+ }
48
+ })
70
49
  }
71
50
 
72
51
  return {
73
52
  registry,
74
- startHttpTimer: options => httpRequestCallbacks.forEach(cb => cb(options)),
75
- endHttpTimer: options => httpResponseCallbacks.forEach(cb => cb(options))
76
53
  }
77
54
  }
78
55
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/metrics",
3
- "version": "2.69.0",
3
+ "version": "2.70.1",
4
4
  "description": "Platformatic Stackable Metrics",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
package/test/helper.js CHANGED
@@ -124,15 +124,7 @@ const expectedMetrics = [
124
124
  {
125
125
  name: 'http_request_all_duration_seconds',
126
126
  type: 'histogram'
127
- },
128
- {
129
- name: 'http_request_duration_seconds',
130
- type: 'histogram'
131
- },
132
- {
133
- name: 'http_request_summary_seconds',
134
- type: 'summary'
135
- },
127
+ }
136
128
  ]
137
129
 
138
130
  function assertMetric (metrics, metric) {
@@ -148,31 +140,31 @@ function assertMetric (metrics, metric) {
148
140
 
149
141
  function assertSummary (metrics, metric) {
150
142
  assert.ok(metrics.includes(`${metric.name}{quantile="0.01"`))
151
- assert.ok(metrics.includes('http_request_summary_seconds{quantile="0.01"'))
152
- assert.ok(metrics.includes('http_request_summary_seconds{quantile="0.05"'))
153
- assert.ok(metrics.includes('http_request_summary_seconds{quantile="0.5"'))
154
- assert.ok(metrics.includes('http_request_summary_seconds{quantile="0.9"'))
155
- assert.ok(metrics.includes('http_request_summary_seconds{quantile="0.95"'))
156
- assert.ok(metrics.includes('http_request_summary_seconds{quantile="0.99"'))
157
- assert.ok(metrics.includes('http_request_summary_seconds{quantile="0.999"'))
158
- assert.ok(metrics.includes('http_request_summary_seconds_sum{'))
159
- assert.ok(metrics.includes('http_request_summary_seconds_count{'))
143
+ assert.ok(metrics.includes('http_request_all_summary_seconds{quantile="0.01"'))
144
+ assert.ok(metrics.includes('http_request_all_summary_seconds{quantile="0.05"'))
145
+ assert.ok(metrics.includes('http_request_all_summary_seconds{quantile="0.5"'))
146
+ assert.ok(metrics.includes('http_request_all_summary_seconds{quantile="0.9"'))
147
+ assert.ok(metrics.includes('http_request_all_summary_seconds{quantile="0.95"'))
148
+ assert.ok(metrics.includes('http_request_all_summary_seconds{quantile="0.99"'))
149
+ assert.ok(metrics.includes('http_request_all_summary_seconds{quantile="0.999"'))
150
+ assert.ok(metrics.includes('http_request_all_summary_seconds_sum{'))
151
+ assert.ok(metrics.includes('http_request_all_summary_seconds_count{'))
160
152
  }
161
153
 
162
154
  function assertHistogram (metrics, metric) {
163
- assert.ok(metrics.includes('http_request_duration_seconds_bucket{le="0.005"'))
164
- assert.ok(metrics.includes('http_request_duration_seconds_bucket{le="0.01"'))
165
- assert.ok(metrics.includes('http_request_duration_seconds_bucket{le="0.025"'))
166
- assert.ok(metrics.includes('http_request_duration_seconds_bucket{le="0.05"'))
167
- assert.ok(metrics.includes('http_request_duration_seconds_bucket{le="0.1"'))
168
- assert.ok(metrics.includes('http_request_duration_seconds_bucket{le="0.25"'))
169
- assert.ok(metrics.includes('http_request_duration_seconds_bucket{le="0.5"'))
170
- assert.ok(metrics.includes('http_request_duration_seconds_bucket{le="1"'))
171
- assert.ok(metrics.includes('http_request_duration_seconds_bucket{le="2.5"'))
172
- assert.ok(metrics.includes('http_request_duration_seconds_bucket{le="5"'))
173
- assert.ok(metrics.includes('http_request_duration_seconds_bucket{le="10"'))
174
- assert.ok(metrics.includes('http_request_duration_seconds_sum{'))
175
- assert.ok(metrics.includes('http_request_duration_seconds_count{'))
155
+ assert.ok(metrics.includes('http_request_all_duration_seconds_bucket{le="0.005"'))
156
+ assert.ok(metrics.includes('http_request_all_duration_seconds_bucket{le="0.01"'))
157
+ assert.ok(metrics.includes('http_request_all_duration_seconds_bucket{le="0.025"'))
158
+ assert.ok(metrics.includes('http_request_all_duration_seconds_bucket{le="0.05"'))
159
+ assert.ok(metrics.includes('http_request_all_duration_seconds_bucket{le="0.1"'))
160
+ assert.ok(metrics.includes('http_request_all_duration_seconds_bucket{le="0.25"'))
161
+ assert.ok(metrics.includes('http_request_all_duration_seconds_bucket{le="0.5"'))
162
+ assert.ok(metrics.includes('http_request_all_duration_seconds_bucket{le="1"'))
163
+ assert.ok(metrics.includes('http_request_all_duration_seconds_bucket{le="2.5"'))
164
+ assert.ok(metrics.includes('http_request_all_duration_seconds_bucket{le="5"'))
165
+ assert.ok(metrics.includes('http_request_all_duration_seconds_bucket{le="10"'))
166
+ assert.ok(metrics.includes('http_request_all_duration_seconds_sum{'))
167
+ assert.ok(metrics.includes('http_request_all_duration_seconds_count{'))
176
168
  }
177
169
 
178
170
  module.exports = {
@@ -4,12 +4,12 @@ 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
 
10
12
  assert.ok(result.registry)
11
- assert.equal(typeof result.startHttpTimer, 'function')
12
- assert.equal(typeof result.endHttpTimer, 'function')
13
13
  })
14
14
 
15
15
  test('accepts custom registry', async () => {
@@ -59,3 +59,70 @@ test('workerId is NOT included in labels when negative', async () => {
59
59
  const [{ values }] = await result.registry.getMetricsAsJSON()
60
60
  assert.strictEqual(values[0].labels.workerId, undefined)
61
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
+ })