@platformatic/telemetry 3.38.0 → 3.39.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 CHANGED
@@ -1,3 +1,4 @@
1
+ export { addPinoInstrumentation } from './lib/logger.js'
1
2
  export * as schema from './lib/schema.js'
2
3
  export * as telemetry from './lib/telemetry.js'
3
4
  export { createTelemetryThreadInterceptorHooks } from './lib/thread-interceptor-hooks.js'
package/lib/logger.js ADDED
@@ -0,0 +1,42 @@
1
+ import { context, isSpanContextValid, trace } from '@opentelemetry/api'
2
+
3
+ const defaultLogKeys = {
4
+ traceId: 'trace_id',
5
+ spanId: 'span_id',
6
+ traceFlags: 'trace_flags'
7
+ }
8
+
9
+ function pinoInstrumentationCombinedMixin (logKeys, original, ...args) {
10
+ const result = original(...args)
11
+ const spanContext = trace.getSpan(context.active())?.spanContext()
12
+
13
+ if (spanContext && isSpanContextValid(spanContext)) {
14
+ result[logKeys.traceId] = spanContext.traceId
15
+ result[logKeys.spanId] = spanContext.spanId
16
+ result[logKeys.traceFlags] = `0${spanContext.traceFlags.toString(16)}`
17
+ }
18
+
19
+ return result
20
+ }
21
+
22
+ function pinoInstrumentationStandaloneMixin (logKeys) {
23
+ const spanContext = trace.getSpan(context.active())?.spanContext()
24
+
25
+ if (!spanContext || !isSpanContextValid(spanContext)) {
26
+ return {}
27
+ }
28
+
29
+ return {
30
+ [logKeys.traceId]: spanContext.traceId,
31
+ [logKeys.spanId]: spanContext.spanId,
32
+ [logKeys.traceFlags]: `0${spanContext.traceFlags.toString(16)}`
33
+ }
34
+ }
35
+
36
+ export function addPinoInstrumentation (options, overrides = {}) {
37
+ const logKeys = Object.assign({}, defaultLogKeys, overrides)
38
+
39
+ options.mixin = options.mixin
40
+ ? pinoInstrumentationCombinedMixin.bind(null, logKeys, options.mixin)
41
+ : pinoInstrumentationStandaloneMixin.bind(null, logKeys)
42
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/telemetry",
3
- "version": "3.38.0",
3
+ "version": "3.39.0",
4
4
  "description": "OpenTelemetry integration for Platformatic",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -36,7 +36,7 @@
36
36
  "@opentelemetry/semantic-conventions": "1.36.0",
37
37
  "fast-uri": "^3.0.6",
38
38
  "fastify-plugin": "^5.0.1",
39
- "@platformatic/foundation": "3.38.0"
39
+ "@platformatic/foundation": "3.39.0"
40
40
  },
41
41
  "engines": {
42
42
  "node": ">=22.19.0"