@platformatic/metrics 3.36.0 → 3.38.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 +2 -6
- package/package.json +1 -1
- package/test/index.test.js +28 -21
package/index.js
CHANGED
|
@@ -316,14 +316,10 @@ export async function collectThreadMetrics (applicationId, workerId, metricsConf
|
|
|
316
316
|
// Build custom labels configuration from metrics config
|
|
317
317
|
// Returns { customLabels: string[], getCustomLabels: (req) => object }
|
|
318
318
|
export function buildCustomLabelsConfig (customLabelsConfig) {
|
|
319
|
-
// Default: use telemetry_id from x-plt-telemetry-id header
|
|
320
319
|
if (!customLabelsConfig || customLabelsConfig.length === 0) {
|
|
321
320
|
return {
|
|
322
|
-
customLabels: [
|
|
323
|
-
getCustomLabels:
|
|
324
|
-
const telemetryId = req.headers?.['x-plt-telemetry-id'] ?? 'unknown'
|
|
325
|
-
return { telemetry_id: telemetryId }
|
|
326
|
-
}
|
|
321
|
+
customLabels: [],
|
|
322
|
+
getCustomLabels: () => ({})
|
|
327
323
|
}
|
|
328
324
|
}
|
|
329
325
|
|
package/package.json
CHANGED
package/test/index.test.js
CHANGED
|
@@ -78,9 +78,9 @@ test('httpMetrics histogram resets after metric collection', async () => {
|
|
|
78
78
|
const histogramMetric = result.registry.getSingleMetric('http_request_all_duration_seconds')
|
|
79
79
|
assert.ok(histogramMetric, 'histogram metric should exist')
|
|
80
80
|
|
|
81
|
-
histogramMetric.observe({ method: 'GET'
|
|
82
|
-
histogramMetric.observe({ method: 'GET'
|
|
83
|
-
histogramMetric.observe({ method: 'GET'
|
|
81
|
+
histogramMetric.observe({ method: 'GET' }, 0.1)
|
|
82
|
+
histogramMetric.observe({ method: 'GET' }, 0.2)
|
|
83
|
+
histogramMetric.observe({ method: 'GET' }, 0.3)
|
|
84
84
|
|
|
85
85
|
const metricsBefore = await result.registry.getMetricsAsJSON()
|
|
86
86
|
const histogramBefore = metricsBefore.find(m => m.name === 'http_request_all_duration_seconds')
|
|
@@ -106,9 +106,9 @@ test('httpMetrics summary resets after metric collection', async () => {
|
|
|
106
106
|
const summaryMetric = result.registry.getSingleMetric('http_request_all_summary_seconds')
|
|
107
107
|
assert.ok(summaryMetric, 'summary metric should exist')
|
|
108
108
|
|
|
109
|
-
summaryMetric.observe({ method: 'POST'
|
|
110
|
-
summaryMetric.observe({ method: 'POST'
|
|
111
|
-
summaryMetric.observe({ method: 'POST'
|
|
109
|
+
summaryMetric.observe({ method: 'POST' }, 0.15)
|
|
110
|
+
summaryMetric.observe({ method: 'POST' }, 0.25)
|
|
111
|
+
summaryMetric.observe({ method: 'POST' }, 0.35)
|
|
112
112
|
|
|
113
113
|
const metricsBefore = await result.registry.getMetricsAsJSON()
|
|
114
114
|
const summaryBefore = metricsBefore.find(m => m.name === 'http_request_all_summary_seconds')
|
|
@@ -128,28 +128,19 @@ test('httpMetrics summary resets after metric collection', async () => {
|
|
|
128
128
|
})
|
|
129
129
|
|
|
130
130
|
// Tests for buildCustomLabelsConfig
|
|
131
|
-
test('buildCustomLabelsConfig returns
|
|
131
|
+
test('buildCustomLabelsConfig returns empty custom labels when no config provided', () => {
|
|
132
132
|
const result = buildCustomLabelsConfig(undefined)
|
|
133
133
|
|
|
134
|
-
assert.deepStrictEqual(result.customLabels, [
|
|
134
|
+
assert.deepStrictEqual(result.customLabels, [])
|
|
135
135
|
assert.strictEqual(typeof result.getCustomLabels, 'function')
|
|
136
|
-
|
|
137
|
-
// Test default getCustomLabels function
|
|
138
|
-
const labels = result.getCustomLabels({ headers: { 'x-plt-telemetry-id': 'test-id' } })
|
|
139
|
-
assert.deepStrictEqual(labels, { telemetry_id: 'test-id' })
|
|
136
|
+
assert.deepStrictEqual(result.getCustomLabels({}), {})
|
|
140
137
|
})
|
|
141
138
|
|
|
142
|
-
test('buildCustomLabelsConfig returns
|
|
139
|
+
test('buildCustomLabelsConfig returns empty custom labels when empty array provided', () => {
|
|
143
140
|
const result = buildCustomLabelsConfig([])
|
|
144
141
|
|
|
145
|
-
assert.deepStrictEqual(result.customLabels, [
|
|
146
|
-
})
|
|
147
|
-
|
|
148
|
-
test('buildCustomLabelsConfig uses unknown as default when header is missing', () => {
|
|
149
|
-
const result = buildCustomLabelsConfig(undefined)
|
|
150
|
-
|
|
151
|
-
const labels = result.getCustomLabels({ headers: {} })
|
|
152
|
-
assert.deepStrictEqual(labels, { telemetry_id: 'unknown' })
|
|
142
|
+
assert.deepStrictEqual(result.customLabels, [])
|
|
143
|
+
assert.deepStrictEqual(result.getCustomLabels({}), {})
|
|
153
144
|
})
|
|
154
145
|
|
|
155
146
|
test('buildCustomLabelsConfig builds custom labels from configuration', () => {
|
|
@@ -238,3 +229,19 @@ test('httpMetrics with custom labels configuration', async () => {
|
|
|
238
229
|
const hasCustomLabel = histogramAfterObserve.values.some(v => v.labels?.domain === 'example.com')
|
|
239
230
|
assert.ok(hasCustomLabel, 'custom domain label should be present in histogram values')
|
|
240
231
|
})
|
|
232
|
+
|
|
233
|
+
test('httpMetrics does not include telemetry_id label by default', async () => {
|
|
234
|
+
const result = await collectMetrics('test-service', 1, { httpMetrics: true })
|
|
235
|
+
|
|
236
|
+
const histogramMetric = result.registry.getSingleMetric('http_request_all_duration_seconds')
|
|
237
|
+
assert.ok(histogramMetric, 'histogram metric should exist')
|
|
238
|
+
|
|
239
|
+
histogramMetric.observe({ method: 'GET' }, 0.1)
|
|
240
|
+
|
|
241
|
+
const metrics = await result.registry.getMetricsAsJSON()
|
|
242
|
+
const histogram = metrics.find(m => m.name === 'http_request_all_duration_seconds')
|
|
243
|
+
|
|
244
|
+
const value = histogram.values.find(v => v.labels.method === 'GET')
|
|
245
|
+
assert.ok(value, 'should have a value with method=GET')
|
|
246
|
+
assert.strictEqual(value.labels.telemetry_id, undefined)
|
|
247
|
+
})
|