autotel 2.1.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.
Files changed (272) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1946 -0
  3. package/dist/chunk-2LNRY4QK.js +273 -0
  4. package/dist/chunk-2LNRY4QK.js.map +1 -0
  5. package/dist/chunk-3HENGDW2.js +587 -0
  6. package/dist/chunk-3HENGDW2.js.map +1 -0
  7. package/dist/chunk-4OAT42CA.cjs +73 -0
  8. package/dist/chunk-4OAT42CA.cjs.map +1 -0
  9. package/dist/chunk-5GWX5LFW.js +70 -0
  10. package/dist/chunk-5GWX5LFW.js.map +1 -0
  11. package/dist/chunk-5R2M36QB.js +195 -0
  12. package/dist/chunk-5R2M36QB.js.map +1 -0
  13. package/dist/chunk-5ZN622AO.js +73 -0
  14. package/dist/chunk-5ZN622AO.js.map +1 -0
  15. package/dist/chunk-77MSMAUQ.cjs +498 -0
  16. package/dist/chunk-77MSMAUQ.cjs.map +1 -0
  17. package/dist/chunk-ABPEQ6RK.cjs +596 -0
  18. package/dist/chunk-ABPEQ6RK.cjs.map +1 -0
  19. package/dist/chunk-BWYGJKRB.js +95 -0
  20. package/dist/chunk-BWYGJKRB.js.map +1 -0
  21. package/dist/chunk-BZHG5IZ4.js +73 -0
  22. package/dist/chunk-BZHG5IZ4.js.map +1 -0
  23. package/dist/chunk-G7VZBCD6.cjs +35 -0
  24. package/dist/chunk-G7VZBCD6.cjs.map +1 -0
  25. package/dist/chunk-GVLK7YUU.cjs +30 -0
  26. package/dist/chunk-GVLK7YUU.cjs.map +1 -0
  27. package/dist/chunk-HCCXC7XG.js +205 -0
  28. package/dist/chunk-HCCXC7XG.js.map +1 -0
  29. package/dist/chunk-HE6T6FIX.cjs +203 -0
  30. package/dist/chunk-HE6T6FIX.cjs.map +1 -0
  31. package/dist/chunk-KIXWPOCO.cjs +100 -0
  32. package/dist/chunk-KIXWPOCO.cjs.map +1 -0
  33. package/dist/chunk-KVGNW3FC.js +87 -0
  34. package/dist/chunk-KVGNW3FC.js.map +1 -0
  35. package/dist/chunk-LITNXTTT.js +3 -0
  36. package/dist/chunk-LITNXTTT.js.map +1 -0
  37. package/dist/chunk-M4ANN7RL.js +114 -0
  38. package/dist/chunk-M4ANN7RL.js.map +1 -0
  39. package/dist/chunk-NC52UBR2.cjs +32 -0
  40. package/dist/chunk-NC52UBR2.cjs.map +1 -0
  41. package/dist/chunk-NHCNRQD3.cjs +212 -0
  42. package/dist/chunk-NHCNRQD3.cjs.map +1 -0
  43. package/dist/chunk-NZ72VDNY.cjs +4 -0
  44. package/dist/chunk-NZ72VDNY.cjs.map +1 -0
  45. package/dist/chunk-P6JUDYNO.js +57 -0
  46. package/dist/chunk-P6JUDYNO.js.map +1 -0
  47. package/dist/chunk-RJYY7BWX.js +1349 -0
  48. package/dist/chunk-RJYY7BWX.js.map +1 -0
  49. package/dist/chunk-TRI4V5BF.cjs +126 -0
  50. package/dist/chunk-TRI4V5BF.cjs.map +1 -0
  51. package/dist/chunk-UL33I6IS.js +139 -0
  52. package/dist/chunk-UL33I6IS.js.map +1 -0
  53. package/dist/chunk-URRW6M2C.cjs +61 -0
  54. package/dist/chunk-URRW6M2C.cjs.map +1 -0
  55. package/dist/chunk-UY3UYPBZ.cjs +77 -0
  56. package/dist/chunk-UY3UYPBZ.cjs.map +1 -0
  57. package/dist/chunk-W3253FGB.cjs +277 -0
  58. package/dist/chunk-W3253FGB.cjs.map +1 -0
  59. package/dist/chunk-W7LHZVQF.js +26 -0
  60. package/dist/chunk-W7LHZVQF.js.map +1 -0
  61. package/dist/chunk-WBWNM6LB.cjs +1360 -0
  62. package/dist/chunk-WBWNM6LB.cjs.map +1 -0
  63. package/dist/chunk-WFJ7L2RV.js +494 -0
  64. package/dist/chunk-WFJ7L2RV.js.map +1 -0
  65. package/dist/chunk-X4RMFFMR.js +28 -0
  66. package/dist/chunk-X4RMFFMR.js.map +1 -0
  67. package/dist/chunk-Y4Y2S7BM.cjs +92 -0
  68. package/dist/chunk-Y4Y2S7BM.cjs.map +1 -0
  69. package/dist/chunk-YLPNXZFI.cjs +143 -0
  70. package/dist/chunk-YLPNXZFI.cjs.map +1 -0
  71. package/dist/chunk-YTXEZ4SD.cjs +77 -0
  72. package/dist/chunk-YTXEZ4SD.cjs.map +1 -0
  73. package/dist/chunk-Z6ZWNWWR.js +30 -0
  74. package/dist/chunk-Z6ZWNWWR.js.map +1 -0
  75. package/dist/config.cjs +26 -0
  76. package/dist/config.cjs.map +1 -0
  77. package/dist/config.d.cts +75 -0
  78. package/dist/config.d.ts +75 -0
  79. package/dist/config.js +5 -0
  80. package/dist/config.js.map +1 -0
  81. package/dist/db.cjs +233 -0
  82. package/dist/db.cjs.map +1 -0
  83. package/dist/db.d.cts +123 -0
  84. package/dist/db.d.ts +123 -0
  85. package/dist/db.js +228 -0
  86. package/dist/db.js.map +1 -0
  87. package/dist/decorators.cjs +67 -0
  88. package/dist/decorators.cjs.map +1 -0
  89. package/dist/decorators.d.cts +91 -0
  90. package/dist/decorators.d.ts +91 -0
  91. package/dist/decorators.js +65 -0
  92. package/dist/decorators.js.map +1 -0
  93. package/dist/event-subscriber.cjs +6 -0
  94. package/dist/event-subscriber.cjs.map +1 -0
  95. package/dist/event-subscriber.d.cts +116 -0
  96. package/dist/event-subscriber.d.ts +116 -0
  97. package/dist/event-subscriber.js +3 -0
  98. package/dist/event-subscriber.js.map +1 -0
  99. package/dist/event-testing.cjs +21 -0
  100. package/dist/event-testing.cjs.map +1 -0
  101. package/dist/event-testing.d.cts +110 -0
  102. package/dist/event-testing.d.ts +110 -0
  103. package/dist/event-testing.js +4 -0
  104. package/dist/event-testing.js.map +1 -0
  105. package/dist/event.cjs +30 -0
  106. package/dist/event.cjs.map +1 -0
  107. package/dist/event.d.cts +282 -0
  108. package/dist/event.d.ts +282 -0
  109. package/dist/event.js +13 -0
  110. package/dist/event.js.map +1 -0
  111. package/dist/exporters.cjs +17 -0
  112. package/dist/exporters.cjs.map +1 -0
  113. package/dist/exporters.d.cts +1 -0
  114. package/dist/exporters.d.ts +1 -0
  115. package/dist/exporters.js +4 -0
  116. package/dist/exporters.js.map +1 -0
  117. package/dist/functional.cjs +46 -0
  118. package/dist/functional.cjs.map +1 -0
  119. package/dist/functional.d.cts +478 -0
  120. package/dist/functional.d.ts +478 -0
  121. package/dist/functional.js +13 -0
  122. package/dist/functional.js.map +1 -0
  123. package/dist/http.cjs +189 -0
  124. package/dist/http.cjs.map +1 -0
  125. package/dist/http.d.cts +169 -0
  126. package/dist/http.d.ts +169 -0
  127. package/dist/http.js +184 -0
  128. package/dist/http.js.map +1 -0
  129. package/dist/index.cjs +333 -0
  130. package/dist/index.cjs.map +1 -0
  131. package/dist/index.d.cts +758 -0
  132. package/dist/index.d.ts +758 -0
  133. package/dist/index.js +143 -0
  134. package/dist/index.js.map +1 -0
  135. package/dist/instrumentation.cjs +182 -0
  136. package/dist/instrumentation.cjs.map +1 -0
  137. package/dist/instrumentation.d.cts +49 -0
  138. package/dist/instrumentation.d.ts +49 -0
  139. package/dist/instrumentation.js +179 -0
  140. package/dist/instrumentation.js.map +1 -0
  141. package/dist/logger.cjs +19 -0
  142. package/dist/logger.cjs.map +1 -0
  143. package/dist/logger.d.cts +146 -0
  144. package/dist/logger.d.ts +146 -0
  145. package/dist/logger.js +6 -0
  146. package/dist/logger.js.map +1 -0
  147. package/dist/metric-helpers.cjs +31 -0
  148. package/dist/metric-helpers.cjs.map +1 -0
  149. package/dist/metric-helpers.d.cts +13 -0
  150. package/dist/metric-helpers.d.ts +13 -0
  151. package/dist/metric-helpers.js +6 -0
  152. package/dist/metric-helpers.js.map +1 -0
  153. package/dist/metric-testing.cjs +21 -0
  154. package/dist/metric-testing.cjs.map +1 -0
  155. package/dist/metric-testing.d.cts +110 -0
  156. package/dist/metric-testing.d.ts +110 -0
  157. package/dist/metric-testing.js +4 -0
  158. package/dist/metric-testing.js.map +1 -0
  159. package/dist/metric.cjs +26 -0
  160. package/dist/metric.cjs.map +1 -0
  161. package/dist/metric.d.cts +240 -0
  162. package/dist/metric.d.ts +240 -0
  163. package/dist/metric.js +9 -0
  164. package/dist/metric.js.map +1 -0
  165. package/dist/processors.cjs +17 -0
  166. package/dist/processors.cjs.map +1 -0
  167. package/dist/processors.d.cts +1 -0
  168. package/dist/processors.d.ts +1 -0
  169. package/dist/processors.js +4 -0
  170. package/dist/processors.js.map +1 -0
  171. package/dist/sampling.cjs +40 -0
  172. package/dist/sampling.cjs.map +1 -0
  173. package/dist/sampling.d.cts +260 -0
  174. package/dist/sampling.d.ts +260 -0
  175. package/dist/sampling.js +7 -0
  176. package/dist/sampling.js.map +1 -0
  177. package/dist/semantic-helpers.cjs +35 -0
  178. package/dist/semantic-helpers.cjs.map +1 -0
  179. package/dist/semantic-helpers.d.cts +442 -0
  180. package/dist/semantic-helpers.d.ts +442 -0
  181. package/dist/semantic-helpers.js +14 -0
  182. package/dist/semantic-helpers.js.map +1 -0
  183. package/dist/tail-sampling-processor.cjs +13 -0
  184. package/dist/tail-sampling-processor.cjs.map +1 -0
  185. package/dist/tail-sampling-processor.d.cts +27 -0
  186. package/dist/tail-sampling-processor.d.ts +27 -0
  187. package/dist/tail-sampling-processor.js +4 -0
  188. package/dist/tail-sampling-processor.js.map +1 -0
  189. package/dist/testing.cjs +286 -0
  190. package/dist/testing.cjs.map +1 -0
  191. package/dist/testing.d.cts +291 -0
  192. package/dist/testing.d.ts +291 -0
  193. package/dist/testing.js +263 -0
  194. package/dist/testing.js.map +1 -0
  195. package/dist/trace-context-DRZdUvVY.d.cts +181 -0
  196. package/dist/trace-context-DRZdUvVY.d.ts +181 -0
  197. package/dist/trace-helpers.cjs +54 -0
  198. package/dist/trace-helpers.cjs.map +1 -0
  199. package/dist/trace-helpers.d.cts +524 -0
  200. package/dist/trace-helpers.d.ts +524 -0
  201. package/dist/trace-helpers.js +5 -0
  202. package/dist/trace-helpers.js.map +1 -0
  203. package/dist/tracer-provider.cjs +21 -0
  204. package/dist/tracer-provider.cjs.map +1 -0
  205. package/dist/tracer-provider.d.cts +169 -0
  206. package/dist/tracer-provider.d.ts +169 -0
  207. package/dist/tracer-provider.js +4 -0
  208. package/dist/tracer-provider.js.map +1 -0
  209. package/package.json +280 -0
  210. package/src/baggage-span-processor.test.ts +202 -0
  211. package/src/baggage-span-processor.ts +98 -0
  212. package/src/circuit-breaker.test.ts +341 -0
  213. package/src/circuit-breaker.ts +184 -0
  214. package/src/config.test.ts +94 -0
  215. package/src/config.ts +169 -0
  216. package/src/db.test.ts +252 -0
  217. package/src/db.ts +447 -0
  218. package/src/decorators.test.ts +203 -0
  219. package/src/decorators.ts +188 -0
  220. package/src/env-config.test.ts +246 -0
  221. package/src/env-config.ts +158 -0
  222. package/src/event-queue.test.ts +222 -0
  223. package/src/event-queue.ts +203 -0
  224. package/src/event-subscriber.ts +136 -0
  225. package/src/event-testing.ts +197 -0
  226. package/src/event.test.ts +718 -0
  227. package/src/event.ts +556 -0
  228. package/src/exporters.ts +96 -0
  229. package/src/functional.test.ts +1059 -0
  230. package/src/functional.ts +2295 -0
  231. package/src/http.test.ts +487 -0
  232. package/src/http.ts +424 -0
  233. package/src/index.ts +158 -0
  234. package/src/init.customization.test.ts +210 -0
  235. package/src/init.integrations.test.ts +366 -0
  236. package/src/init.openllmetry.test.ts +282 -0
  237. package/src/init.protocol.test.ts +215 -0
  238. package/src/init.ts +1426 -0
  239. package/src/instrumentation.test.ts +108 -0
  240. package/src/instrumentation.ts +308 -0
  241. package/src/logger.test.ts +117 -0
  242. package/src/logger.ts +246 -0
  243. package/src/metric-helpers.ts +47 -0
  244. package/src/metric-testing.ts +197 -0
  245. package/src/metric.ts +434 -0
  246. package/src/metrics.test.ts +205 -0
  247. package/src/operation-context.ts +93 -0
  248. package/src/processors.ts +106 -0
  249. package/src/rate-limiter.test.ts +199 -0
  250. package/src/rate-limiter.ts +98 -0
  251. package/src/sampling.test.ts +513 -0
  252. package/src/sampling.ts +428 -0
  253. package/src/semantic-helpers.test.ts +311 -0
  254. package/src/semantic-helpers.ts +584 -0
  255. package/src/shutdown.test.ts +311 -0
  256. package/src/shutdown.ts +222 -0
  257. package/src/stub.integration.test.ts +361 -0
  258. package/src/tail-sampling-processor.test.ts +226 -0
  259. package/src/tail-sampling-processor.ts +51 -0
  260. package/src/testing.ts +670 -0
  261. package/src/trace-context.ts +470 -0
  262. package/src/trace-helpers.new.test.ts +278 -0
  263. package/src/trace-helpers.test.ts +242 -0
  264. package/src/trace-helpers.ts +690 -0
  265. package/src/tracer-provider.test.ts +183 -0
  266. package/src/tracer-provider.ts +266 -0
  267. package/src/track.test.ts +153 -0
  268. package/src/track.ts +120 -0
  269. package/src/validation.test.ts +306 -0
  270. package/src/validation.ts +239 -0
  271. package/src/variable-name-inference.test.ts +178 -0
  272. package/src/variable-name-inference.ts +242 -0
@@ -0,0 +1,596 @@
1
+ 'use strict';
2
+
3
+ var chunkGVLK7YUU_cjs = require('./chunk-GVLK7YUU.cjs');
4
+ var chunkG7VZBCD6_cjs = require('./chunk-G7VZBCD6.cjs');
5
+ var api = require('@opentelemetry/api');
6
+ var sdkNode = require('@opentelemetry/sdk-node');
7
+ var sdkTraceBase = require('@opentelemetry/sdk-trace-base');
8
+ var resources = require('@opentelemetry/resources');
9
+ var semanticConventions = require('@opentelemetry/semantic-conventions');
10
+ var sdkMetrics = require('@opentelemetry/sdk-metrics');
11
+ var exporterMetricsOtlpHttp = require('@opentelemetry/exporter-metrics-otlp-http');
12
+ var exporterTraceOtlpHttp = require('@opentelemetry/exporter-trace-otlp-http');
13
+ var nodeEnvResolver = require('node-env-resolver');
14
+ var validators = require('node-env-resolver/validators');
15
+ var instrumentationPino = require('@opentelemetry/instrumentation-pino');
16
+ var instrumentationWinston = require('@opentelemetry/instrumentation-winston');
17
+ var autoInstrumentationsNode = require('@opentelemetry/auto-instrumentations-node');
18
+
19
+ var BaggageSpanProcessor = class {
20
+ prefix;
21
+ constructor(options = {}) {
22
+ this.prefix = options.prefix ?? "baggage.";
23
+ }
24
+ onStart(span, parentContext) {
25
+ let baggage = api.propagation.getBaggage(parentContext);
26
+ if (!baggage) {
27
+ baggage = api.propagation.getBaggage(api.context.active());
28
+ }
29
+ if (!baggage) {
30
+ try {
31
+ const { getActiveContextWithBaggage } = chunkG7VZBCD6_cjs.__require("../trace-context");
32
+ const storedContext = getActiveContextWithBaggage();
33
+ baggage = api.propagation.getBaggage(storedContext);
34
+ } catch {
35
+ }
36
+ }
37
+ if (!baggage) return;
38
+ for (const [key, entry] of baggage.getAllEntries()) {
39
+ span.setAttribute(`${this.prefix}${key}`, entry.value);
40
+ }
41
+ }
42
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
43
+ onEnd(_span) {
44
+ }
45
+ async shutdown() {
46
+ }
47
+ async forceFlush() {
48
+ }
49
+ };
50
+ function resolveOtelEnv() {
51
+ return nodeEnvResolver.resolve({
52
+ OTEL_SERVICE_NAME: validators.string({ optional: true }),
53
+ OTEL_EXPORTER_OTLP_ENDPOINT: validators.url({ optional: true }),
54
+ OTEL_EXPORTER_OTLP_HEADERS: validators.string({ optional: true }),
55
+ OTEL_RESOURCE_ATTRIBUTES: validators.string({ optional: true }),
56
+ OTEL_EXPORTER_OTLP_PROTOCOL: validators.optional(["http", "grpc"])
57
+ });
58
+ }
59
+ function parseResourceAttributes(input) {
60
+ if (!input || input.trim() === "") {
61
+ return {};
62
+ }
63
+ const attributes = {};
64
+ const pairs = input.split(",");
65
+ for (const pair of pairs) {
66
+ const trimmedPair = pair.trim();
67
+ if (!trimmedPair) continue;
68
+ const equalIndex = trimmedPair.indexOf("=");
69
+ if (equalIndex === -1) {
70
+ continue;
71
+ }
72
+ const key = trimmedPair.slice(0, equalIndex).trim();
73
+ const value = trimmedPair.slice(equalIndex + 1).trim();
74
+ if (key && value) {
75
+ attributes[key] = value;
76
+ }
77
+ }
78
+ return attributes;
79
+ }
80
+ function parseOtlpHeaders(input) {
81
+ if (!input || input.trim() === "") {
82
+ return {};
83
+ }
84
+ const headers = {};
85
+ const pairs = input.split(",");
86
+ for (const pair of pairs) {
87
+ const trimmedPair = pair.trim();
88
+ if (!trimmedPair) continue;
89
+ const equalIndex = trimmedPair.indexOf("=");
90
+ if (equalIndex === -1) {
91
+ continue;
92
+ }
93
+ const key = trimmedPair.slice(0, equalIndex).trim();
94
+ const value = trimmedPair.slice(equalIndex + 1).trim();
95
+ if (key && value) {
96
+ headers[key] = value;
97
+ }
98
+ }
99
+ return headers;
100
+ }
101
+ function envToConfig(env) {
102
+ const config2 = {};
103
+ if (env.OTEL_SERVICE_NAME) {
104
+ config2.service = env.OTEL_SERVICE_NAME;
105
+ }
106
+ if (env.OTEL_EXPORTER_OTLP_ENDPOINT) {
107
+ config2.endpoint = env.OTEL_EXPORTER_OTLP_ENDPOINT;
108
+ }
109
+ if (env.OTEL_EXPORTER_OTLP_PROTOCOL) {
110
+ config2.protocol = env.OTEL_EXPORTER_OTLP_PROTOCOL;
111
+ }
112
+ if (env.OTEL_EXPORTER_OTLP_HEADERS) {
113
+ config2.otlpHeaders = parseOtlpHeaders(env.OTEL_EXPORTER_OTLP_HEADERS);
114
+ }
115
+ const resourceAttrs = parseResourceAttributes(env.OTEL_RESOURCE_ATTRIBUTES);
116
+ if (Object.keys(resourceAttrs).length > 0) {
117
+ config2.resourceAttributes = resourceAttrs;
118
+ }
119
+ return config2;
120
+ }
121
+ function resolveConfigFromEnv() {
122
+ const env = resolveOtelEnv();
123
+ return envToConfig(env);
124
+ }
125
+ var OTLPTraceExporterGRPC;
126
+ var OTLPMetricExporterGRPC;
127
+ function loadGRPCTraceExporter() {
128
+ if (OTLPTraceExporterGRPC) return OTLPTraceExporterGRPC;
129
+ try {
130
+ const grpcModule = chunkG7VZBCD6_cjs.__require("@opentelemetry/exporter-trace-otlp-grpc");
131
+ OTLPTraceExporterGRPC = grpcModule.OTLPTraceExporter;
132
+ return OTLPTraceExporterGRPC;
133
+ } catch {
134
+ throw new Error(
135
+ "gRPC trace exporter not found. Install with: pnpm add @opentelemetry/exporter-trace-otlp-grpc"
136
+ );
137
+ }
138
+ }
139
+ function loadGRPCMetricExporter() {
140
+ if (OTLPMetricExporterGRPC) return OTLPMetricExporterGRPC;
141
+ try {
142
+ const grpcModule = chunkG7VZBCD6_cjs.__require("@opentelemetry/exporter-metrics-otlp-grpc");
143
+ OTLPMetricExporterGRPC = grpcModule.OTLPMetricExporter;
144
+ return OTLPMetricExporterGRPC;
145
+ } catch {
146
+ throw new Error(
147
+ "gRPC metric exporter not found. Install with: pnpm add @opentelemetry/exporter-metrics-otlp-grpc"
148
+ );
149
+ }
150
+ }
151
+ function createTraceExporter(protocol, config2) {
152
+ if (protocol === "grpc") {
153
+ const Exporter = loadGRPCTraceExporter();
154
+ return new Exporter(config2);
155
+ }
156
+ return new exporterTraceOtlpHttp.OTLPTraceExporter(config2);
157
+ }
158
+ function createMetricExporter(protocol, config2) {
159
+ if (protocol === "grpc") {
160
+ const Exporter = loadGRPCMetricExporter();
161
+ return new Exporter(config2);
162
+ }
163
+ return new exporterMetricsOtlpHttp.OTLPMetricExporter(config2);
164
+ }
165
+ function resolveProtocol(configProtocol) {
166
+ if (configProtocol === "grpc" || configProtocol === "http") {
167
+ return configProtocol;
168
+ }
169
+ const envProtocol = process.env.OTEL_EXPORTER_OTLP_PROTOCOL;
170
+ if (envProtocol === "grpc") return "grpc";
171
+ if (envProtocol === "http/protobuf" || envProtocol === "http") return "http";
172
+ return "http";
173
+ }
174
+ function formatEndpointUrl(endpoint, signal, protocol) {
175
+ if (protocol === "grpc") {
176
+ return endpoint.replace(/\/(v1\/)?(traces|metrics|logs)$/, "");
177
+ }
178
+ if (!endpoint.endsWith(`/v1/${signal}`)) {
179
+ return `${endpoint}/v1/${signal}`;
180
+ }
181
+ return endpoint;
182
+ }
183
+ var silentLogger = {
184
+ info: () => {
185
+ },
186
+ warn: () => {
187
+ },
188
+ error: () => {
189
+ },
190
+ debug: () => {
191
+ }
192
+ };
193
+ var initialized = false;
194
+ var config = null;
195
+ var sdk = null;
196
+ var warnedOnce = false;
197
+ var logger = silentLogger;
198
+ var validationConfig = null;
199
+ function resolveMetricsFlag(configFlag = "auto") {
200
+ const envFlag = process.env.AUTOTELEMETRY_METRICS;
201
+ if (envFlag === "on" || envFlag === "true") return true;
202
+ if (envFlag === "off" || envFlag === "false") return false;
203
+ if (configFlag === true) return true;
204
+ if (configFlag === false) return false;
205
+ return true;
206
+ }
207
+ function resolveDebugFlag(configFlag) {
208
+ const envFlag = process.env.AUTOLEMETRY_DEBUG;
209
+ if (envFlag === "true" || envFlag === "1") return true;
210
+ if (envFlag === "false" || envFlag === "0") return false;
211
+ return configFlag ?? false;
212
+ }
213
+ function normalizeOtlpHeaders(headers) {
214
+ if (!headers) return void 0;
215
+ if (typeof headers !== "string") return headers;
216
+ const parsed = {};
217
+ for (const pair of headers.split(",")) {
218
+ const [key, ...valueParts] = pair.split("=");
219
+ if (!key || valueParts.length === 0) continue;
220
+ parsed[key.trim()] = valueParts.join("=").trim();
221
+ }
222
+ return parsed;
223
+ }
224
+ function createLoggerInstrumentation(logger2) {
225
+ if ("child" in logger2 && "bindings" in logger2 && typeof logger2.child === "function") {
226
+ return new instrumentationPino.PinoInstrumentation();
227
+ }
228
+ if ("transports" in logger2 || "defaultMeta" in logger2) {
229
+ return new instrumentationWinston.WinstonInstrumentation();
230
+ }
231
+ return null;
232
+ }
233
+ function init(cfg) {
234
+ const envConfig = resolveConfigFromEnv();
235
+ const mergedConfig = {
236
+ ...envConfig,
237
+ ...cfg,
238
+ // Deep merge for resourceAttributes
239
+ resourceAttributes: {
240
+ ...envConfig.resourceAttributes,
241
+ ...cfg.resourceAttributes
242
+ },
243
+ // Handle otlpHeaders merge (can be string or object)
244
+ otlpHeaders: cfg.otlpHeaders === void 0 ? envConfig.otlpHeaders === void 0 ? void 0 : envConfig.otlpHeaders : cfg.otlpHeaders
245
+ };
246
+ logger = mergedConfig.logger || silentLogger;
247
+ if (initialized) {
248
+ logger.warn(
249
+ "[autotel] init() called again - last config wins. This may cause unexpected behavior."
250
+ );
251
+ }
252
+ config = mergedConfig;
253
+ validationConfig = mergedConfig.validation || null;
254
+ const endpoint = mergedConfig.endpoint;
255
+ const otlpHeaders = normalizeOtlpHeaders(mergedConfig.otlpHeaders);
256
+ const version = mergedConfig.version || detectVersion();
257
+ const environment = mergedConfig.environment || process.env.NODE_ENV || "development";
258
+ const metricsEnabled = resolveMetricsFlag(mergedConfig.metrics);
259
+ const hostname = detectHostname();
260
+ let resource = resources.resourceFromAttributes({
261
+ [semanticConventions.ATTR_SERVICE_NAME]: cfg.service,
262
+ [semanticConventions.ATTR_SERVICE_VERSION]: version,
263
+ // Support both old and new OpenTelemetry semantic conventions for environment
264
+ "deployment.environment": environment,
265
+ // Deprecated but widely supported
266
+ "deployment.environment.name": environment
267
+ // OTel v1.27.0+ standard
268
+ });
269
+ if (hostname) {
270
+ resource = resource.merge(
271
+ resources.resourceFromAttributes({
272
+ "host.name": hostname,
273
+ // OpenTelemetry standard
274
+ "datadog.host.name": hostname
275
+ // Datadog-specific, highest priority for Datadog
276
+ })
277
+ );
278
+ }
279
+ if (cfg.resource) {
280
+ resource = resource.merge(cfg.resource);
281
+ }
282
+ if (cfg.resourceAttributes) {
283
+ resource = resource.merge(resources.resourceFromAttributes(cfg.resourceAttributes));
284
+ }
285
+ const protocol = resolveProtocol(cfg.protocol);
286
+ const spanProcessors = [];
287
+ if (cfg.spanProcessors && cfg.spanProcessors.length > 0) {
288
+ spanProcessors.push(...cfg.spanProcessors);
289
+ } else if (cfg.spanExporters && cfg.spanExporters.length > 0) {
290
+ for (const exporter of cfg.spanExporters) {
291
+ spanProcessors.push(
292
+ new chunkGVLK7YUU_cjs.TailSamplingSpanProcessor(new sdkTraceBase.BatchSpanProcessor(exporter))
293
+ );
294
+ }
295
+ } else if (endpoint) {
296
+ const traceExporter = createTraceExporter(protocol, {
297
+ url: formatEndpointUrl(endpoint, "traces", protocol),
298
+ headers: otlpHeaders
299
+ });
300
+ spanProcessors.push(
301
+ new chunkGVLK7YUU_cjs.TailSamplingSpanProcessor(new sdkTraceBase.BatchSpanProcessor(traceExporter))
302
+ );
303
+ }
304
+ if (cfg.baggage) {
305
+ const prefix = typeof cfg.baggage === "string" ? cfg.baggage ? `${cfg.baggage}.` : "" : "baggage.";
306
+ spanProcessors.push(new BaggageSpanProcessor({ prefix }));
307
+ }
308
+ const debugMode = resolveDebugFlag(cfg.debug);
309
+ if (debugMode) {
310
+ spanProcessors.push(new sdkTraceBase.SimpleSpanProcessor(new sdkTraceBase.ConsoleSpanExporter()));
311
+ }
312
+ const metricReaders = [];
313
+ if (cfg.metricReaders && cfg.metricReaders.length > 0) {
314
+ metricReaders.push(...cfg.metricReaders);
315
+ } else if (metricsEnabled && endpoint) {
316
+ const metricExporter = createMetricExporter(protocol, {
317
+ url: formatEndpointUrl(endpoint, "metrics", protocol),
318
+ headers: otlpHeaders
319
+ });
320
+ metricReaders.push(
321
+ new sdkMetrics.PeriodicExportingMetricReader({
322
+ exporter: metricExporter
323
+ })
324
+ );
325
+ }
326
+ let logRecordProcessors;
327
+ if (cfg.logRecordProcessors && cfg.logRecordProcessors.length > 0) {
328
+ logRecordProcessors = [...cfg.logRecordProcessors];
329
+ }
330
+ let finalInstrumentations = cfg.instrumentations ? [...cfg.instrumentations] : [];
331
+ if (cfg.logger) {
332
+ const loggerInstrumentation = createLoggerInstrumentation(cfg.logger);
333
+ if (loggerInstrumentation) {
334
+ finalInstrumentations = [...finalInstrumentations, loggerInstrumentation];
335
+ logger.debug(
336
+ `[autotel] Auto-enabled ${loggerInstrumentation.constructor.name} for logger`
337
+ );
338
+ }
339
+ }
340
+ if (cfg.integrations !== void 0) {
341
+ try {
342
+ const manualInstrumentationNames = getInstrumentationNames(
343
+ cfg.instrumentations ?? []
344
+ );
345
+ if (manualInstrumentationNames.size > 0 && cfg.integrations !== false && cfg.integrations !== void 0) {
346
+ const manualNames = [...manualInstrumentationNames].join(", ");
347
+ logger.info(
348
+ `[autotel] Detected manual instrumentations (${manualNames}). These will take precedence over auto-instrumentations. Tip: Set integrations:false if you want full manual control, or remove manual configs to use auto-instrumentations.`
349
+ );
350
+ }
351
+ const autoInstrumentations = getAutoInstrumentations(
352
+ cfg.integrations,
353
+ manualInstrumentationNames
354
+ );
355
+ if (autoInstrumentations && autoInstrumentations.length > 0) {
356
+ finalInstrumentations = [
357
+ ...finalInstrumentations,
358
+ ...autoInstrumentations
359
+ ];
360
+ }
361
+ } catch (error) {
362
+ logger.warn(
363
+ `[autotel] Failed to configure auto-instrumentations: ${error instanceof Error ? error.message : String(error)}`
364
+ );
365
+ }
366
+ }
367
+ const sdkOptions = {
368
+ resource,
369
+ instrumentations: finalInstrumentations
370
+ };
371
+ if (spanProcessors.length > 0) {
372
+ sdkOptions.spanProcessors = spanProcessors;
373
+ }
374
+ if (metricReaders.length > 0) {
375
+ sdkOptions.metricReaders = metricReaders;
376
+ }
377
+ if (logRecordProcessors && logRecordProcessors.length > 0) {
378
+ sdkOptions.logRecordProcessors = logRecordProcessors;
379
+ }
380
+ sdk = cfg.sdkFactory ? cfg.sdkFactory(sdkOptions) : new sdkNode.NodeSDK(sdkOptions);
381
+ if (!sdk) {
382
+ throw new Error("[autotel] sdkFactory must return a NodeSDK instance");
383
+ }
384
+ sdk.start();
385
+ if (cfg.openllmetry?.enabled) {
386
+ let initializedSync = false;
387
+ try {
388
+ const traceloop = chunkG7VZBCD6_cjs.__require("@traceloop/node-server-sdk");
389
+ const initOptions = {
390
+ ...cfg.openllmetry.options
391
+ };
392
+ try {
393
+ const tracerProvider = sdk.getTracerProvider();
394
+ initOptions.tracerProvider = tracerProvider;
395
+ } catch {
396
+ }
397
+ if (typeof traceloop.initialize === "function") {
398
+ traceloop.initialize(initOptions);
399
+ logger.info("[autotel] OpenLLMetry initialized successfully");
400
+ initializedSync = true;
401
+ }
402
+ } catch (error) {
403
+ if (error instanceof Error && (error.message.includes("Cannot find module") || error.message.includes("Module not found") || error.message.includes("Cannot resolve module") || error.message.includes("Dynamic require"))) {
404
+ initializeOpenLLMetry(
405
+ cfg.openllmetry.options,
406
+ sdk,
407
+ cfg.spanExporters?.[0]
408
+ // Pass first exporter if available
409
+ ).catch((error_) => {
410
+ logger.warn(
411
+ `[autotel] OpenLLMetry initialization error: ${error_ instanceof Error ? error_.message : String(error_)}`
412
+ );
413
+ });
414
+ } else if (!initializedSync) {
415
+ logger.warn(
416
+ `[autotel] Failed to initialize OpenLLMetry: ${error instanceof Error ? error.message : String(error)}`
417
+ );
418
+ }
419
+ }
420
+ }
421
+ initialized = true;
422
+ }
423
+ async function initializeOpenLLMetry(options, sdkInstance, spanExporter) {
424
+ try {
425
+ let traceloop;
426
+ try {
427
+ traceloop = chunkG7VZBCD6_cjs.__require("@traceloop/node-server-sdk");
428
+ } catch {
429
+ traceloop = await import('@traceloop/node-server-sdk');
430
+ }
431
+ const initOptions = {
432
+ ...options
433
+ };
434
+ if (spanExporter) {
435
+ initOptions.exporter = spanExporter;
436
+ }
437
+ if (sdkInstance) {
438
+ try {
439
+ const tracerProvider = sdkInstance.getTracerProvider();
440
+ initOptions.tracerProvider = tracerProvider;
441
+ } catch (error) {
442
+ logger.debug(
443
+ `[autotel] Could not get tracer provider for OpenLLMetry: ${error instanceof Error ? error.message : String(error)}`
444
+ );
445
+ }
446
+ }
447
+ if (typeof traceloop.initialize === "function") {
448
+ traceloop.initialize(initOptions);
449
+ logger.info("[autotel] OpenLLMetry initialized successfully");
450
+ } else {
451
+ logger.warn(
452
+ "[autotel] OpenLLMetry initialize function not found. Check @traceloop/node-server-sdk version."
453
+ );
454
+ }
455
+ } catch (error) {
456
+ if (error instanceof Error && (error.message.includes("Cannot find module") || error.message.includes("Module not found") || error.message.includes("Cannot resolve module"))) {
457
+ logger.warn(
458
+ "[autotel] OpenLLMetry enabled but @traceloop/node-server-sdk is not installed. Install it as a peer dependency to use OpenLLMetry integration."
459
+ );
460
+ } else {
461
+ logger.warn(
462
+ `[autotel] Failed to initialize OpenLLMetry: ${error instanceof Error ? error.message : String(error)}`
463
+ );
464
+ }
465
+ }
466
+ }
467
+ function getInstrumentationNames(instrumentations) {
468
+ const names = /* @__PURE__ */ new Set();
469
+ if (!instrumentations) return names;
470
+ for (const instrumentation of instrumentations) {
471
+ if (instrumentation && typeof instrumentation === "object") {
472
+ names.add(instrumentation.constructor.name);
473
+ }
474
+ }
475
+ return names;
476
+ }
477
+ var INSTRUMENTATION_CLASS_TO_PACKAGE = {
478
+ HttpInstrumentation: "@opentelemetry/instrumentation-http",
479
+ HttpsInstrumentation: "@opentelemetry/instrumentation-http",
480
+ ExpressInstrumentation: "@opentelemetry/instrumentation-express",
481
+ FastifyInstrumentation: "@opentelemetry/instrumentation-fastify",
482
+ MongoDBInstrumentation: "@opentelemetry/instrumentation-mongodb",
483
+ MongooseInstrumentation: "@opentelemetry/instrumentation-mongoose",
484
+ PrismaInstrumentation: "@opentelemetry/instrumentation-prisma",
485
+ PinoInstrumentation: "@opentelemetry/instrumentation-pino",
486
+ WinstonInstrumentation: "@opentelemetry/instrumentation-winston",
487
+ RedisInstrumentation: "@opentelemetry/instrumentation-redis",
488
+ GraphQLInstrumentation: "@opentelemetry/instrumentation-graphql",
489
+ GrpcInstrumentation: "@opentelemetry/instrumentation-grpc",
490
+ IORedisInstrumentation: "@opentelemetry/instrumentation-ioredis",
491
+ KnexInstrumentation: "@opentelemetry/instrumentation-knex",
492
+ NestJsInstrumentation: "@opentelemetry/instrumentation-nestjs-core",
493
+ PgInstrumentation: "@opentelemetry/instrumentation-pg",
494
+ MySQLInstrumentation: "@opentelemetry/instrumentation-mysql",
495
+ MySQL2Instrumentation: "@opentelemetry/instrumentation-mysql2"
496
+ };
497
+ function getAutoInstrumentations(integrations, manualInstrumentationNames = /* @__PURE__ */ new Set()) {
498
+ if (integrations === false) {
499
+ return [];
500
+ }
501
+ const exclusionConfig = {};
502
+ for (const className of manualInstrumentationNames) {
503
+ const packageName = INSTRUMENTATION_CLASS_TO_PACKAGE[className];
504
+ if (packageName) {
505
+ exclusionConfig[packageName] = { enabled: false };
506
+ }
507
+ }
508
+ if (integrations === true) {
509
+ if (Object.keys(exclusionConfig).length > 0) {
510
+ return autoInstrumentationsNode.getNodeAutoInstrumentations(exclusionConfig);
511
+ }
512
+ return autoInstrumentationsNode.getNodeAutoInstrumentations();
513
+ }
514
+ if (Array.isArray(integrations)) {
515
+ const config3 = { ...exclusionConfig };
516
+ for (const name of integrations) {
517
+ const packageName = `@opentelemetry/instrumentation-${name}`;
518
+ if (!exclusionConfig[packageName]) {
519
+ config3[packageName] = { enabled: true };
520
+ }
521
+ }
522
+ return autoInstrumentationsNode.getNodeAutoInstrumentations(config3);
523
+ }
524
+ const config2 = {
525
+ ...exclusionConfig,
526
+ ...integrations
527
+ };
528
+ for (const packageName of Object.keys(exclusionConfig)) {
529
+ const integrationsKey = Object.keys(integrations).find(
530
+ (key) => packageName.includes(key)
531
+ );
532
+ if (integrationsKey) {
533
+ config2[packageName] = { enabled: false };
534
+ }
535
+ }
536
+ return autoInstrumentationsNode.getNodeAutoInstrumentations(config2);
537
+ }
538
+ function isInitialized() {
539
+ return initialized;
540
+ }
541
+ function getConfig() {
542
+ return config;
543
+ }
544
+ function getLogger() {
545
+ return logger;
546
+ }
547
+ function getValidationConfig() {
548
+ return validationConfig;
549
+ }
550
+ function warnIfNotInitialized(context) {
551
+ if (!initialized && !warnedOnce) {
552
+ logger.warn(
553
+ `[autotel] ${context} used before init() called. Call init({ service: "..." }) first. See: https://docs.autotel.dev/quickstart`
554
+ );
555
+ warnedOnce = true;
556
+ }
557
+ }
558
+ function detectVersion() {
559
+ try {
560
+ const fs = chunkG7VZBCD6_cjs.__require("fs");
561
+ const pkg = JSON.parse(
562
+ fs.readFileSync(`${process.cwd()}/package.json`, "utf8")
563
+ );
564
+ return pkg.version || "1.0.0";
565
+ } catch {
566
+ return "1.0.0";
567
+ }
568
+ }
569
+ function detectHostname() {
570
+ if (process.env.DD_HOSTNAME) {
571
+ return process.env.DD_HOSTNAME;
572
+ }
573
+ if (process.env.HOSTNAME) {
574
+ return process.env.HOSTNAME;
575
+ }
576
+ try {
577
+ const os = chunkG7VZBCD6_cjs.__require("os");
578
+ return os.hostname();
579
+ } catch {
580
+ return void 0;
581
+ }
582
+ }
583
+ function getSdk() {
584
+ return sdk;
585
+ }
586
+
587
+ exports.BaggageSpanProcessor = BaggageSpanProcessor;
588
+ exports.getConfig = getConfig;
589
+ exports.getLogger = getLogger;
590
+ exports.getSdk = getSdk;
591
+ exports.getValidationConfig = getValidationConfig;
592
+ exports.init = init;
593
+ exports.isInitialized = isInitialized;
594
+ exports.warnIfNotInitialized = warnIfNotInitialized;
595
+ //# sourceMappingURL=chunk-ABPEQ6RK.cjs.map
596
+ //# sourceMappingURL=chunk-ABPEQ6RK.cjs.map