@platformatic/metrics 2.55.0 → 2.56.0
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 +1 -1
- package/package.json +2 -2
- package/test/index.test.js +61 -0
package/index.js
CHANGED
|
@@ -16,7 +16,7 @@ async function collectMetrics (serviceId, workerId, metricsConfig = {}, registry
|
|
|
16
16
|
const httpResponseCallbacks = []
|
|
17
17
|
|
|
18
18
|
const labels = { ...metricsConfig.labels, serviceId }
|
|
19
|
-
if (workerId) {
|
|
19
|
+
if (workerId >= 0) {
|
|
20
20
|
labels.workerId = workerId
|
|
21
21
|
}
|
|
22
22
|
registry.setDefaultLabels(labels)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/metrics",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.56.0",
|
|
4
4
|
"description": "Platformatic Stackable Metrics",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "commonjs",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"neostandard": "^0.12.0"
|
|
26
26
|
},
|
|
27
27
|
"scripts": {
|
|
28
|
-
"test": "npm run lint",
|
|
28
|
+
"test": "npm run lint && borp",
|
|
29
29
|
"lint": "eslint"
|
|
30
30
|
}
|
|
31
31
|
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const assert = require('node:assert')
|
|
4
|
+
const { test } = require('node:test')
|
|
5
|
+
const { collectMetrics, client } = require('..')
|
|
6
|
+
|
|
7
|
+
test('returns expected structure', async () => {
|
|
8
|
+
const result = await collectMetrics('test-service', 1, {})
|
|
9
|
+
|
|
10
|
+
assert.ok(result.registry)
|
|
11
|
+
assert.equal(typeof result.startHttpTimer, 'function')
|
|
12
|
+
assert.equal(typeof result.endHttpTimer, 'function')
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
test('accepts custom registry', async () => {
|
|
16
|
+
const customRegistry = new client.Registry()
|
|
17
|
+
const result = await collectMetrics('test-service', 1, {}, customRegistry)
|
|
18
|
+
|
|
19
|
+
assert.strictEqual(result.registry, customRegistry)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
test('with defaultMetrics enabled', async () => {
|
|
23
|
+
const result = await collectMetrics('test-service', 1, {
|
|
24
|
+
defaultMetrics: true
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
const metrics = await result.registry.getMetricsAsJSON()
|
|
28
|
+
assert.ok(metrics.length > 0)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
test('thread cpu metrics are created when defaultMetrics is enabled', async () => {
|
|
32
|
+
const result = await collectMetrics('test-service', 1, {
|
|
33
|
+
defaultMetrics: true
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
const metrics = await result.registry.getMetricsAsJSON()
|
|
37
|
+
const metricNames = metrics.map(m => m.name)
|
|
38
|
+
|
|
39
|
+
assert.ok(metricNames.includes('thread_cpu_user_system_seconds_total'))
|
|
40
|
+
assert.ok(metricNames.includes('thread_cpu_system_seconds_total'))
|
|
41
|
+
assert.ok(metricNames.includes('thread_cpu_seconds_total'))
|
|
42
|
+
assert.ok(metricNames.includes('thread_cpu_percent_usage'))
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
test('workerId is properly included in labels when zero', async () => {
|
|
46
|
+
const result = await collectMetrics('test-service', 0, { defaultMetrics: true })
|
|
47
|
+
const [{ values }] = await result.registry.getMetricsAsJSON()
|
|
48
|
+
assert.strictEqual(values[0].labels.workerId, 0)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('workerId is properly included in labels when positive', async () => {
|
|
52
|
+
const result = await collectMetrics('test-service', 42, { defaultMetrics: true })
|
|
53
|
+
const [{ values }] = await result.registry.getMetricsAsJSON()
|
|
54
|
+
assert.strictEqual(values[0].labels.workerId, 42)
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test('workerId is NOT included in labels when negative', async () => {
|
|
58
|
+
const result = await collectMetrics('test-service', -42, { defaultMetrics: true })
|
|
59
|
+
const [{ values }] = await result.registry.getMetricsAsJSON()
|
|
60
|
+
assert.strictEqual(values[0].labels.workerId, undefined)
|
|
61
|
+
})
|