@platformatic/telemetry 2.30.0 → 2.30.1-alpha.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.
@@ -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
  ],
@@ -282,6 +282,12 @@ 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
+ if (httpCacheId) {
288
+ span.setAttributes({ 'http.cache.id': httpCacheId })
289
+ }
290
+
285
291
  span.setStatus(spanStatus)
286
292
  } else {
287
293
  span.setStatus({
@@ -80,7 +80,7 @@ const createTelemetryThreadInterceptorHooks = () => {
80
80
  ctx.span = span
81
81
  }
82
82
 
83
- const onClientResponse = (_req, res, ctx) => {
83
+ const onClientResponseEnd = (_req, res, ctx) => {
84
84
  const span = ctx.span ?? null
85
85
  if (!span) {
86
86
  return
@@ -93,6 +93,12 @@ 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
+ if (httpCacheId) {
99
+ span.setAttributes({ 'http.cache.id': httpCacheId })
100
+ }
101
+
96
102
  span.setStatus(spanStatus)
97
103
  } else {
98
104
  span.setStatus({
@@ -116,7 +122,7 @@ const createTelemetryThreadInterceptorHooks = () => {
116
122
  onServerResponse,
117
123
  onServerError,
118
124
  onClientRequest,
119
- onClientResponse,
125
+ onClientResponseEnd,
120
126
  onClientError
121
127
  }
122
128
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/telemetry",
3
- "version": "2.30.0",
3
+ "version": "2.30.1-alpha.0",
4
4
  "description": "OpenTelemetry integration for Platformatic",
5
5
  "main": "index.js",
6
6
  "author": "Platformatic Inc. <oss@platformatic.dev> (https://platformatic.dev)",