@platformatic/telemetry 2.30.0 → 2.30.1-alpha.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/lib/node-telemetry.js +13 -1
- package/lib/telemetry-config.js +10 -0
- package/lib/thread-interceptor-hooks.js +12 -2
- package/package.json +1 -1
package/lib/node-telemetry.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
const process = require('node:process')
|
|
3
|
+
const { AsyncLocalStorage } = require('node:async_hooks')
|
|
3
4
|
const opentelemetry = require('@opentelemetry/sdk-node')
|
|
4
5
|
const { Resource } = require('@opentelemetry/resources')
|
|
5
6
|
const FileSpanExporter = require('./file-span-exporter')
|
|
@@ -74,10 +75,21 @@ const setupNodeHTTPTelemetry = (opts) => {
|
|
|
74
75
|
spanProcessors.push(spanProcessor)
|
|
75
76
|
}
|
|
76
77
|
|
|
78
|
+
const clientSpansAls = new AsyncLocalStorage()
|
|
79
|
+
globalThis.platformatic = globalThis.platformatic || {}
|
|
80
|
+
globalThis.platformatic.clientSpansAls = clientSpansAls
|
|
81
|
+
|
|
77
82
|
const sdk = new opentelemetry.NodeSDK({
|
|
78
83
|
spanProcessors, // https://github.com/open-telemetry/opentelemetry-js/issues/4881#issuecomment-2358059714
|
|
79
84
|
instrumentations: [
|
|
80
|
-
new UndiciInstrumentation(
|
|
85
|
+
new UndiciInstrumentation({
|
|
86
|
+
responseHook: (span) => {
|
|
87
|
+
const store = clientSpansAls.getStore()
|
|
88
|
+
if (store) {
|
|
89
|
+
store.span = span
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}),
|
|
81
93
|
new HttpInstrumentation(),
|
|
82
94
|
new PgInstrumentation(),
|
|
83
95
|
],
|
package/lib/telemetry-config.js
CHANGED
|
@@ -282,6 +282,16 @@ function setupTelemetry (opts, logger) {
|
|
|
282
282
|
span.setAttributes({
|
|
283
283
|
'http.response.status_code': response.statusCode,
|
|
284
284
|
})
|
|
285
|
+
|
|
286
|
+
const httpCacheId = response.headers?.['x-plt-http-cache-id']
|
|
287
|
+
const isCacheHit = response.headers?.age !== undefined
|
|
288
|
+
if (httpCacheId) {
|
|
289
|
+
span.setAttributes({
|
|
290
|
+
'http.cache.id': httpCacheId,
|
|
291
|
+
'http.cache.hit': isCacheHit.toString()
|
|
292
|
+
})
|
|
293
|
+
}
|
|
294
|
+
|
|
285
295
|
span.setStatus(spanStatus)
|
|
286
296
|
} else {
|
|
287
297
|
span.setStatus({
|
|
@@ -80,7 +80,7 @@ const createTelemetryThreadInterceptorHooks = () => {
|
|
|
80
80
|
ctx.span = span
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
const
|
|
83
|
+
const onClientResponseEnd = (_req, res, ctx) => {
|
|
84
84
|
const span = ctx.span ?? null
|
|
85
85
|
if (!span) {
|
|
86
86
|
return
|
|
@@ -93,6 +93,16 @@ const createTelemetryThreadInterceptorHooks = () => {
|
|
|
93
93
|
span.setAttributes({
|
|
94
94
|
'http.response.status_code': res.statusCode,
|
|
95
95
|
})
|
|
96
|
+
|
|
97
|
+
const httpCacheId = res.headers?.['x-plt-http-cache-id']
|
|
98
|
+
const isCacheHit = res.headers?.age !== undefined
|
|
99
|
+
if (httpCacheId) {
|
|
100
|
+
span.setAttributes({
|
|
101
|
+
'http.cache.id': httpCacheId,
|
|
102
|
+
'http.cache.hit': isCacheHit.toString()
|
|
103
|
+
})
|
|
104
|
+
}
|
|
105
|
+
|
|
96
106
|
span.setStatus(spanStatus)
|
|
97
107
|
} else {
|
|
98
108
|
span.setStatus({
|
|
@@ -116,7 +126,7 @@ const createTelemetryThreadInterceptorHooks = () => {
|
|
|
116
126
|
onServerResponse,
|
|
117
127
|
onServerError,
|
|
118
128
|
onClientRequest,
|
|
119
|
-
|
|
129
|
+
onClientResponseEnd,
|
|
120
130
|
onClientError
|
|
121
131
|
}
|
|
122
132
|
}
|
package/package.json
CHANGED