@platformatic/telemetry 3.53.0-alpha.0 → 3.53.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.
@@ -0,0 +1,67 @@
1
+ import { diag, DiagLogLevel } from '@opentelemetry/api'
2
+ import util from 'node:util'
3
+
4
+ const pinoToDiagLogLevels = {
5
+ silent: DiagLogLevel.NONE,
6
+ fatal: DiagLogLevel.ERROR,
7
+ error: DiagLogLevel.ERROR,
8
+ warn: DiagLogLevel.WARN,
9
+ info: DiagLogLevel.INFO,
10
+ debug: DiagLogLevel.DEBUG,
11
+ trace: DiagLogLevel.VERBOSE
12
+ }
13
+
14
+ function getDiagLogLevel (logger) {
15
+ const logLevel = logger?.level
16
+
17
+ if (typeof logLevel === 'string') {
18
+ return pinoToDiagLogLevels[logLevel] ?? DiagLogLevel.INFO
19
+ }
20
+
21
+ return DiagLogLevel.INFO
22
+ }
23
+
24
+ function callLogger (logger, method, fallback, args) {
25
+ if (typeof logger?.[method] === 'function') {
26
+ if (typeof args[0] === 'string') {
27
+ logger[method](util.format(...args))
28
+ } else {
29
+ logger[method](...args)
30
+ }
31
+
32
+ return
33
+ }
34
+
35
+ fallback(...args)
36
+ }
37
+
38
+ export function createPlatformaticDiagLogger (logger = globalThis.platformatic?.logger) {
39
+ let target = logger
40
+
41
+ if (typeof target?.child === 'function') {
42
+ try {
43
+ target = target.child({ name: '@platformatic/telemetry/diag' })
44
+ } catch {
45
+ // Ignore child logger creation failures and use the parent logger.
46
+ }
47
+ }
48
+
49
+ return {
50
+ error: (...args) => callLogger(target, 'error', console.error.bind(console), args),
51
+ warn: (...args) => callLogger(target, 'warn', console.warn.bind(console), args),
52
+ info: (...args) => callLogger(target, 'info', console.info.bind(console), args),
53
+ debug: (...args) => callLogger(target, 'debug', console.debug.bind(console), args),
54
+ verbose: (...args) => callLogger(target, 'trace', console.debug.bind(console), args)
55
+ }
56
+ }
57
+
58
+ export function setupDiagLogger (opts, logger = globalThis.platformatic?.logger) {
59
+ if (opts?.diagLogger !== true) {
60
+ return false
61
+ }
62
+
63
+ return diag.setLogger(createPlatformaticDiagLogger(logger), {
64
+ logLevel: getDiagLogLevel(logger),
65
+ suppressOverrideMessage: true
66
+ })
67
+ }
@@ -10,6 +10,7 @@ import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions'
10
10
  import { AsyncLocalStorage } from 'node:async_hooks'
11
11
  import { readFileSync, statSync } from 'node:fs'
12
12
  import { tmpdir } from 'node:os'
13
+ import { setupDiagLogger } from './diag-logger.js'
13
14
  import { resolve } from 'node:path'
14
15
  import process from 'node:process'
15
16
  import util from 'node:util'
@@ -35,6 +36,8 @@ contextManager.enable()
35
36
  context.setGlobalContextManager(contextManager)
36
37
 
37
38
  const setupNodeHTTPTelemetry = async (opts, applicationDir) => {
39
+ setupDiagLogger(opts)
40
+
38
41
  const { applicationName, instrumentations = [] } = opts
39
42
  const additionalInstrumentations = await getInstrumentations(instrumentations, applicationDir)
40
43
 
@@ -4,6 +4,7 @@ import { resourceFromAttributes } from '@opentelemetry/resources'
4
4
  import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions'
5
5
  import fastUri from 'fast-uri'
6
6
  import { fastifyTextMapGetter, fastifyTextMapSetter } from './fastify-text-map.js'
7
+ import { setupDiagLogger } from './diag-logger.js'
7
8
  import { PlatformaticContext } from './platformatic-context.js'
8
9
  import { PlatformaticTracerProvider } from './platformatic-trace-provider.js'
9
10
  import { getSpanProcessors } from './span-processors.js'
@@ -89,6 +90,8 @@ const initTelemetry = (opts, logger) => {
89
90
  }
90
91
 
91
92
  export function setupTelemetry (opts, logger) {
93
+ setupDiagLogger(opts, logger)
94
+
92
95
  const openTelemetryAPIs = initTelemetry(opts, logger)
93
96
  const { tracer, propagator, provider } = openTelemetryAPIs
94
97
  const skipOperations =
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/telemetry",
3
- "version": "3.53.0-alpha.0",
3
+ "version": "3.53.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.53.0-alpha.0"
39
+ "@platformatic/foundation": "3.53.0"
40
40
  },
41
41
  "engines": {
42
42
  "node": ">=22.19.0"