@ciq-dev/neoiq-foundation-node 1.1.2-beta.8 → 1.1.2-beta.9

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.
@@ -5,6 +5,7 @@ import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-grpc";
5
5
  import { resourceFromAttributes } from "@opentelemetry/resources";
6
6
  import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from "@opentelemetry/semantic-conventions";
7
7
  import { SpanStatusCode as SpanStatusCode$1, context as context$1, propagation as propagation$1, trace as trace$1 } from "@opentelemetry/api";
8
+ import { FastifyOtelInstrumentation } from "@fastify/otel";
8
9
  import { z } from "zod";
9
10
 
10
11
  //#region rolldown:runtime
@@ -105,6 +106,12 @@ function getDefaultOtelEndpoint() {
105
106
 
106
107
  //#endregion
107
108
  //#region src/features/tracing.ts
109
+ /** User-agents of infrastructure health-checkers whose requests we don't trace. */
110
+ const HEALTHCHECK_USER_AGENT_PREFIXES = [
111
+ "ELB-HealthChecker",
112
+ "kube-probe",
113
+ "GoogleHC"
114
+ ];
108
115
  let sdk = null;
109
116
  let isInitialized = false;
110
117
  const MONITORED_MODULES = [
@@ -136,18 +143,59 @@ function setupTracing(options) {
136
143
  "deployment.environment": environment
137
144
  });
138
145
  const traceExporter = new OTLPTraceExporter({ url: endpoint });
139
- const instrumentationConfig = buildInstrumentationConfig(autoInstrumentation);
140
146
  if (!metricReader && !process.env.OTEL_METRICS_EXPORTER) process.env.OTEL_METRICS_EXPORTER = "none";
141
147
  sdk = new NodeSDK({
142
148
  resource,
143
149
  traceExporter,
144
150
  ...metricReader ? { metricReaders: [metricReader] } : {},
145
151
  logRecordProcessors: [],
146
- instrumentations: [getNodeAutoInstrumentations(instrumentationConfig)]
152
+ instrumentations: buildInstrumentations(autoInstrumentation)
147
153
  });
148
154
  sdk.start();
149
155
  isInitialized = true;
150
156
  }
157
+ /**
158
+ * Produce a clean name for a Fastify lifecycle (hook/handler) span.
159
+ *
160
+ * @fastify/otel names these "<hook> - <fn name>", but anonymous hook functions fall
161
+ * back to the full plugin-encapsulation chain ("fastify -> @fastify/otel -> ... ->
162
+ * @fastify/x"), which is unreadable. When that chain is detected, collapse it to a
163
+ * clean, route-scoped name (e.g. "onRequest /api/v1/foo"). Returns `null` when the
164
+ * span is already cleanly named (a named hook like "onRequest - handleCors"), so the
165
+ * caller leaves it untouched.
166
+ */
167
+ function cleanLifecycleSpanName(hookName, handlerName, routeUrl) {
168
+ if (!handlerName.includes(" -> ")) return null;
169
+ return routeUrl ? `${hookName} ${routeUrl}` : hookName;
170
+ }
171
+ /**
172
+ * Build the full instrumentation list for the NodeSDK.
173
+ *
174
+ * auto-instrumentations-node covers http, mongodb, redis, etc., but it no longer
175
+ * bundles Fastify, so Fastify is added explicitly via @fastify/otel (the Fastify
176
+ * core team's official OpenTelemetry plugin, v4–v5). With registerOnInitialization it
177
+ * self-registers on every Fastify instance via the fastify.initialization diagnostics
178
+ * channel, and — riding on instrumentation-http — names the server span "GET /route"
179
+ * and emits the request → hook → handler waterfall (the Fastify equivalent of the
180
+ * Express middleware spans on sibling services), with DB spans nesting underneath.
181
+ */
182
+ function buildInstrumentations(autoInstrumentation = {}) {
183
+ const instrumentations = [...getNodeAutoInstrumentations(buildInstrumentationConfig(autoInstrumentation))];
184
+ if (autoInstrumentation.fastify !== false) instrumentations.push(new FastifyOtelInstrumentation({
185
+ registerOnInitialization: true,
186
+ requestHook: (span, request) => {
187
+ const routeUrl = request.routeOptions?.url;
188
+ const method = request.method;
189
+ if (routeUrl && method) span.updateName(`${method} ${routeUrl}`);
190
+ },
191
+ lifecycleHook: (span, info) => {
192
+ const i = info;
193
+ const cleaned = cleanLifecycleSpanName(i.hookName ?? "", typeof i.handler === "string" ? i.handler : "", i.request?.routeOptions?.url);
194
+ if (cleaned) span.updateName(cleaned);
195
+ }
196
+ }));
197
+ return instrumentations;
198
+ }
151
199
  function buildInstrumentationConfig(config) {
152
200
  const mapping = {
153
201
  http: "@opentelemetry/instrumentation-http",
@@ -167,6 +215,14 @@ function buildInstrumentationConfig(config) {
167
215
  const defaultValue = key !== "fs" && key !== "dns";
168
216
  result[instrumentationName] = { enabled: userSetting ?? defaultValue };
169
217
  }
218
+ result["@opentelemetry/instrumentation-http"] = {
219
+ ...result["@opentelemetry/instrumentation-http"],
220
+ ignoreIncomingRequestHook: (request) => {
221
+ const ua = request.headers?.["user-agent"];
222
+ const uaStr = Array.isArray(ua) ? ua[0] ?? "" : ua ?? "";
223
+ return HEALTHCHECK_USER_AGENT_PREFIXES.some((prefix) => uaStr.startsWith(prefix));
224
+ }
225
+ };
170
226
  return result;
171
227
  }
172
228
  /** Shutdown tracing gracefully */
@@ -202,4 +258,4 @@ function isTracingEnabled() {
202
258
 
203
259
  //#endregion
204
260
  export { AutoInstrumentationConfigSchema, FeaturesConfigSchema, FoundationConfigSchema, LoggingConfigSchema, OtelConfigSchema, RedactionConfigSchema, RequestLoggingConfigSchema, ShutdownConfigSchema, SpanStatusCode$1 as SpanStatusCode, VaultConfigSchema, context$1 as context, getActiveSpan, getDefaultOtelEndpoint, getTraceContext, getTracer, isTracingEnabled, parseConfig, propagation$1 as propagation, setupTracing, shutdownTracing, trace$1 as trace };
205
- //# sourceMappingURL=tracing-CXEMuJKs.mjs.map
261
+ //# sourceMappingURL=tracing-LOP7TT9u.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracing-LOP7TT9u.mjs","names":["input: FoundationConfigInput","sdk: NodeSDK | null","options: TracingOptions","hookName: string","handlerName: string","routeUrl?: string","autoInstrumentation: AutoInstrumentationConfig","instrumentations: Instrumentation[]","config: AutoInstrumentationConfig","mapping: Record<string, string>","result: Record<string, Record<string, unknown>>","request: {\n headers?: Record<string, string | string[] | undefined>;\n }","name: string"],"sources":["../src/config.ts","../src/features/tracing.ts"],"sourcesContent":["/**\n * Foundation Configuration with Zod Validation\n */\n\nimport { z } from 'zod';\n\n// Auto-Instrumentation Config\nexport const AutoInstrumentationConfigSchema = z\n .object({\n http: z.boolean().default(true),\n fastify: z.boolean().default(true),\n express: z.boolean().default(true),\n mongodb: z.boolean().default(true),\n pg: z.boolean().default(true),\n mysql: z.boolean().default(true),\n redis: z.boolean().default(true),\n ioredis: z.boolean().default(true),\n grpc: z.boolean().default(true),\n fs: z.boolean().default(false),\n dns: z.boolean().default(false),\n })\n .partial();\n\nexport type AutoInstrumentationConfig = z.infer<typeof AutoInstrumentationConfigSchema>;\n\n// Features Config\nexport const FeaturesConfigSchema = z\n .object({\n tracing: z.boolean().default(true),\n metrics: z.boolean().default(true),\n logging: z.boolean().default(true),\n autoInstrumentation: AutoInstrumentationConfigSchema.default({}),\n })\n .partial();\n\nexport type FeaturesConfig = z.infer<typeof FeaturesConfigSchema>;\n\n// OTEL Config\nconst DEFAULT_OTEL_ENDPOINT =\n 'http://otel-stack-deployment-collector.observability.svc.cluster.local:4317';\n\nexport const OtelConfigSchema = z.object({\n endpoint: z\n .string()\n .default(() => process.env.OTEL_EXPORTER_OTLP_ENDPOINT || DEFAULT_OTEL_ENDPOINT),\n metricsIntervalMs: z.number().min(1000).default(5000),\n});\n\nexport type OtelConfig = z.infer<typeof OtelConfigSchema>;\n\n// Logging Config\nexport const LoggingConfigSchema = z\n .object({\n level: z.enum(['debug', 'info', 'warn', 'error']).default('info'),\n prettyPrint: z.boolean().optional(),\n })\n .partial();\n\nexport type LoggingConfig = z.infer<typeof LoggingConfigSchema>;\n\n// Request Logging Config\nexport const RequestLoggingConfigSchema = z\n .object({\n logHeaders: z.boolean().default(true),\n logBody: z.boolean().default(false),\n logResponseBody: z.boolean().default(false),\n maxBodySize: z.number().default(10 * 1024),\n redactHeaders: z.array(z.string()).optional(),\n })\n .partial();\n\nexport type RequestLoggingConfig = z.infer<typeof RequestLoggingConfigSchema>;\n\n// Redaction Config\nexport const RedactionConfigSchema = z\n .object({\n additionalPaths: z.array(z.string()).default([]),\n })\n .partial();\n\nexport type RedactionConfig = z.infer<typeof RedactionConfigSchema>;\n\n// Shutdown Config\nexport const ShutdownConfigSchema = z\n .object({\n /** Flush telemetry on uncaughtException / unhandledRejection before process exits */\n flushOnCrash: z.boolean().default(false),\n /** Grace period (ms) for flushing before forcing exit */\n flushTimeoutMs: z.number().min(100).default(5000),\n })\n .partial();\n\nexport type ShutdownConfig = z.infer<typeof ShutdownConfigSchema>;\n\n// Vault Config\nexport const VaultConfigSchema = z.object({\n enabled: z.boolean().default(false),\n address: z.string().default('https://vault.beta.commerceiq.ai'),\n authMethod: z.enum(['kubernetes', 'token']).default('kubernetes'),\n role: z.string().optional(),\n mountPoint: z.string().default('secret'),\n tokenPath: z.string().default('/var/run/secrets/kubernetes.io/serviceaccount/token'),\n k8sAuthMount: z.string().default('kubernetes'),\n namespace: z.string().optional(),\n});\n\nexport type VaultConfig = z.infer<typeof VaultConfigSchema>;\n\n// Main Foundation Config\nexport const FoundationConfigSchema = z.object({\n serviceName: z.string().min(1, 'serviceName is required'),\n serviceVersion: z.string().default(process.env.SERVICE_VERSION || '1.0.0'),\n environment: z\n .enum(['development', 'staging', 'qa', 'production'])\n .default(\n ['development', 'staging', 'qa', 'production'].includes(process.env.NODE_ENV ?? '')\n ? (process.env.NODE_ENV as 'development' | 'staging' | 'qa' | 'production')\n : 'development'\n ),\n features: FeaturesConfigSchema.default({}),\n otel: OtelConfigSchema.default({}),\n logging: LoggingConfigSchema.default({}),\n requestLogging: RequestLoggingConfigSchema.default({}),\n redaction: RedactionConfigSchema.default({}),\n shutdown: ShutdownConfigSchema.default({}),\n vault: VaultConfigSchema.default({}),\n});\n\nexport type FoundationConfig = z.infer<typeof FoundationConfigSchema>;\n\nexport type FoundationConfigInput = z.input<typeof FoundationConfigSchema> & {\n serviceName: string;\n};\n\n/** Parse and validate configuration */\nexport function parseConfig(input: FoundationConfigInput): FoundationConfig {\n const result = FoundationConfigSchema.safeParse(input);\n\n if (!result.success) {\n const errors = result.error.errors\n .map((e) => ` - ${e.path.join('.')}: ${e.message}`)\n .join('\\n');\n throw new Error(`Invalid foundation configuration:\\n${errors}`);\n }\n\n return result.data;\n}\n\n/** Get default OTEL endpoint */\nexport function getDefaultOtelEndpoint(): string {\n return process.env.OTEL_EXPORTER_OTLP_ENDPOINT || DEFAULT_OTEL_ENDPOINT;\n}\n","/**\n * OpenTelemetry Tracing Setup\n */\n\nimport { NodeSDK } from '@opentelemetry/sdk-node';\nimport { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';\nimport { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';\nimport { resourceFromAttributes } from '@opentelemetry/resources';\nimport { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';\nimport { trace, context, propagation, SpanStatusCode, type Tracer } from '@opentelemetry/api';\nimport { type IMetricReader } from '@opentelemetry/sdk-metrics';\nimport { FastifyOtelInstrumentation } from '@fastify/otel';\nimport { type AutoInstrumentationConfig, getDefaultOtelEndpoint } from '../config';\n\n/**\n * Element type of the NodeSDK instrumentations list. Derived from\n * getNodeAutoInstrumentations so we don't take a direct import on\n * @opentelemetry/instrumentation (only a transitive dependency here).\n */\ntype Instrumentation = ReturnType<typeof getNodeAutoInstrumentations>[number];\ntype InstrumentationConfigMap = Parameters<typeof getNodeAutoInstrumentations>[0];\n\n/** User-agents of infrastructure health-checkers whose requests we don't trace. */\nconst HEALTHCHECK_USER_AGENT_PREFIXES = ['ELB-HealthChecker', 'kube-probe', 'GoogleHC'];\n\nexport interface TracingOptions {\n serviceName: string;\n serviceVersion: string;\n environment: string;\n endpoint?: string;\n autoInstrumentation?: AutoInstrumentationConfig;\n /**\n * Metric reader to attach to the NodeSDK so a single SDK owns both traces and\n * metrics (one global MeterProvider, same gRPC collector). When provided, NodeSDK\n * is the sole owner of metrics; when omitted, NodeSDK is opted out of metrics\n * entirely (see below) so it never registers a stray default MeterProvider.\n */\n metricReader?: IMetricReader;\n}\n\nlet sdk: NodeSDK | null = null;\nlet isInitialized = false;\n\nconst MONITORED_MODULES = ['pg', 'mongodb', 'ioredis', 'mysql2', 'express', '@grpc/grpc-js'];\n\nfunction warnPreloadedModules(): void {\n // require.resolve / require.cache are only available in CJS contexts\n if (typeof require === 'undefined' || typeof require.cache === 'undefined') return;\n\n for (const mod of MONITORED_MODULES) {\n try {\n const resolved = require.resolve(mod);\n if (require.cache[resolved]) {\n console.warn(\n `[neoiq-foundation] \"${mod}\" was imported before tracing init. ` +\n 'Auto-instrumentation may not work for this module. ' +\n 'Use node -r @ciq-dev/neoiq-foundation-node/bootstrap or call createFoundation() first.'\n );\n }\n } catch {\n // Module not installed — no warning needed\n }\n }\n}\n\n/** Initialize OpenTelemetry tracing */\nexport function setupTracing(options: TracingOptions): void {\n if (isInitialized) {\n console.warn('[neoiq-foundation] Tracing already initialized');\n return;\n }\n\n warnPreloadedModules();\n\n const {\n serviceName,\n serviceVersion,\n environment,\n endpoint = getDefaultOtelEndpoint(),\n autoInstrumentation = {},\n metricReader,\n } = options;\n\n const resource = resourceFromAttributes({\n [ATTR_SERVICE_NAME]: serviceName,\n [ATTR_SERVICE_VERSION]: serviceVersion,\n 'deployment.environment': environment,\n });\n\n const traceExporter = new OTLPTraceExporter({ url: endpoint });\n\n // Foundation owns every OpenTelemetry signal explicitly. Left to its own devices,\n // NodeSDK auto-configures metrics AND logs exporters from env defaults — both OTLP\n // over HTTP to localhost:4318 — which nothing listens on, producing ECONNREFUSED\n // spam every export interval. We therefore configure each signal on NodeSDK directly:\n // - traces: the explicit gRPC exporter above.\n // - metrics: the explicit gRPC reader when provided (single SDK, single global\n // MeterProvider, same collector as traces). When metrics are disabled\n // and the deployer hasn't chosen an exporter, opt NodeSDK out via\n // OTEL_METRICS_EXPORTER=none (the only switch NodeSDK exposes for that).\n // - logs: foundation logs via pino, never OTLP. Passing an empty processor list\n // stops NodeSDK from falling back to its env-default localhost:4318 log\n // exporter (`logRecordProcessors: []` takes the explicit branch and skips\n // configureLoggerProviderFromEnv()).\n if (!metricReader && !process.env.OTEL_METRICS_EXPORTER) {\n process.env.OTEL_METRICS_EXPORTER = 'none';\n }\n\n sdk = new NodeSDK({\n resource,\n traceExporter,\n ...(metricReader ? { metricReaders: [metricReader] } : {}),\n logRecordProcessors: [],\n instrumentations: buildInstrumentations(autoInstrumentation),\n });\n\n sdk.start();\n isInitialized = true;\n}\n\n/**\n * Produce a clean name for a Fastify lifecycle (hook/handler) span.\n *\n * @fastify/otel names these \"<hook> - <fn name>\", but anonymous hook functions fall\n * back to the full plugin-encapsulation chain (\"fastify -> @fastify/otel -> ... ->\n * @fastify/x\"), which is unreadable. When that chain is detected, collapse it to a\n * clean, route-scoped name (e.g. \"onRequest /api/v1/foo\"). Returns `null` when the\n * span is already cleanly named (a named hook like \"onRequest - handleCors\"), so the\n * caller leaves it untouched.\n */\nexport function cleanLifecycleSpanName(\n hookName: string,\n handlerName: string,\n routeUrl?: string\n): string | null {\n if (!handlerName.includes(' -> ')) return null;\n return routeUrl ? `${hookName} ${routeUrl}` : hookName;\n}\n\n/**\n * Build the full instrumentation list for the NodeSDK.\n *\n * auto-instrumentations-node covers http, mongodb, redis, etc., but it no longer\n * bundles Fastify, so Fastify is added explicitly via @fastify/otel (the Fastify\n * core team's official OpenTelemetry plugin, v4–v5). With registerOnInitialization it\n * self-registers on every Fastify instance via the fastify.initialization diagnostics\n * channel, and — riding on instrumentation-http — names the server span \"GET /route\"\n * and emits the request → hook → handler waterfall (the Fastify equivalent of the\n * Express middleware spans on sibling services), with DB spans nesting underneath.\n */\nexport function buildInstrumentations(\n autoInstrumentation: AutoInstrumentationConfig = {}\n): Instrumentation[] {\n const instrumentations: Instrumentation[] = [\n ...getNodeAutoInstrumentations(buildInstrumentationConfig(autoInstrumentation)),\n ];\n\n if (autoInstrumentation.fastify !== false) {\n instrumentations.push(\n new FastifyOtelInstrumentation({\n registerOnInitialization: true,\n // Name the Fastify request span \"METHOD /route\" (route template, low cardinality).\n requestHook: (span, request) => {\n const routeUrl = (request as { routeOptions?: { url?: string } }).routeOptions?.url;\n const method = (request as { method?: string }).method;\n if (routeUrl && method) span.updateName(`${method} ${routeUrl}`);\n },\n // Collapse the verbose anonymous-hook plugin chains into readable names.\n lifecycleHook: (span, info) => {\n const i = info as {\n hookName?: string;\n handler?: unknown;\n request?: { routeOptions?: { url?: string } };\n };\n const cleaned = cleanLifecycleSpanName(\n i.hookName ?? '',\n typeof i.handler === 'string' ? i.handler : '',\n i.request?.routeOptions?.url\n );\n if (cleaned) span.updateName(cleaned);\n },\n })\n );\n }\n\n return instrumentations;\n}\n\nfunction buildInstrumentationConfig(config: AutoInstrumentationConfig): InstrumentationConfigMap {\n const mapping: Record<string, string> = {\n http: '@opentelemetry/instrumentation-http',\n // Fastify is intentionally absent here — it's not in the auto-instrumentations\n // bundle and is added separately in buildInstrumentations() via\n // @opentelemetry/instrumentation-fastify.\n express: '@opentelemetry/instrumentation-express',\n mongodb: '@opentelemetry/instrumentation-mongodb',\n pg: '@opentelemetry/instrumentation-pg',\n mysql: '@opentelemetry/instrumentation-mysql',\n redis: '@opentelemetry/instrumentation-redis',\n ioredis: '@opentelemetry/instrumentation-ioredis',\n grpc: '@opentelemetry/instrumentation-grpc',\n fs: '@opentelemetry/instrumentation-fs',\n dns: '@opentelemetry/instrumentation-dns',\n };\n\n const result: Record<string, Record<string, unknown>> = {};\n for (const [key, instrumentationName] of Object.entries(mapping)) {\n const userSetting = config[key as keyof AutoInstrumentationConfig];\n const defaultValue = key !== 'fs' && key !== 'dns';\n result[instrumentationName] = { enabled: userSetting ?? defaultValue };\n }\n\n // Don't create spans for infrastructure health-checks (ELB / kube-probe / GoogleHC).\n // They're high-volume, low-value, and otherwise dominate (and skew sampling of) traces.\n result['@opentelemetry/instrumentation-http'] = {\n ...result['@opentelemetry/instrumentation-http'],\n ignoreIncomingRequestHook: (request: {\n headers?: Record<string, string | string[] | undefined>;\n }) => {\n const ua = request.headers?.['user-agent'];\n const uaStr = Array.isArray(ua) ? (ua[0] ?? '') : (ua ?? '');\n return HEALTHCHECK_USER_AGENT_PREFIXES.some((prefix) => uaStr.startsWith(prefix));\n },\n };\n\n return result as InstrumentationConfigMap;\n}\n\n/** Shutdown tracing gracefully */\nexport async function shutdownTracing(): Promise<void> {\n if (!sdk) return;\n try {\n await sdk.shutdown();\n isInitialized = false;\n sdk = null;\n } catch (error) {\n console.error('[neoiq-foundation] Error shutting down tracing:', error);\n throw error;\n }\n}\n\nexport function getTracer(name: string): Tracer {\n return trace.getTracer(name);\n}\n\nexport function getActiveSpan(): ReturnType<typeof trace.getActiveSpan> {\n return trace.getActiveSpan();\n}\n\nexport function getTraceContext(): { traceId?: string; spanId?: string } {\n const span = trace.getActiveSpan();\n if (!span) return {};\n const ctx = span.spanContext();\n return { traceId: ctx.traceId, spanId: ctx.spanId };\n}\n\nexport function isTracingEnabled(): boolean {\n return isInitialized;\n}\n\nexport { trace, context, propagation, SpanStatusCode };\nexport type { Tracer };\n"],"mappings":";;;;;;;;;;;;;;;AAOA,MAAa,kCAAkC,EAC5C,OAAO;CACN,MAAM,EAAE,SAAS,CAAC,QAAQ,KAAK;CAC/B,SAAS,EAAE,SAAS,CAAC,QAAQ,KAAK;CAClC,SAAS,EAAE,SAAS,CAAC,QAAQ,KAAK;CAClC,SAAS,EAAE,SAAS,CAAC,QAAQ,KAAK;CAClC,IAAI,EAAE,SAAS,CAAC,QAAQ,KAAK;CAC7B,OAAO,EAAE,SAAS,CAAC,QAAQ,KAAK;CAChC,OAAO,EAAE,SAAS,CAAC,QAAQ,KAAK;CAChC,SAAS,EAAE,SAAS,CAAC,QAAQ,KAAK;CAClC,MAAM,EAAE,SAAS,CAAC,QAAQ,KAAK;CAC/B,IAAI,EAAE,SAAS,CAAC,QAAQ,MAAM;CAC9B,KAAK,EAAE,SAAS,CAAC,QAAQ,MAAM;AAChC,EAAC,CACD,SAAS;AAKZ,MAAa,uBAAuB,EACjC,OAAO;CACN,SAAS,EAAE,SAAS,CAAC,QAAQ,KAAK;CAClC,SAAS,EAAE,SAAS,CAAC,Q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AAQ,MAAM,mDAAmD,MAAM;AACvE,QAAM;CACP;AACF;AAED,SAAgB,UAAUC,MAAsB;AAC9C,QAAO,QAAM,UAAU,KAAK;AAC7B;AAED,SAAgB,gBAAwD;AACtE,QAAO,QAAM,eAAe;AAC7B;AAED,SAAgB,kBAAyD;CACvE,MAAM,OAAO,QAAM,eAAe;AAClC,MAAK,KAAM,QAAO,CAAE;CACpB,MAAM,MAAM,KAAK,aAAa;AAC9B,QAAO;EAAE,SAAS,IAAI;EAAS,QAAQ,IAAI;CAAQ;AACpD;AAED,SAAgB,mBAA4B;AAC1C,QAAO;AACR"}
@@ -28,6 +28,7 @@ const __opentelemetry_exporter_trace_otlp_grpc = __toESM(require("@opentelemetry
28
28
  const __opentelemetry_resources = __toESM(require("@opentelemetry/resources"));
29
29
  const __opentelemetry_semantic_conventions = __toESM(require("@opentelemetry/semantic-conventions"));
30
30
  const __opentelemetry_api = __toESM(require("@opentelemetry/api"));
31
+ const __fastify_otel = __toESM(require("@fastify/otel"));
31
32
  const zod = __toESM(require("zod"));
32
33
 
33
34
  //#region src/config.ts
@@ -124,6 +125,12 @@ function getDefaultOtelEndpoint() {
124
125
 
125
126
  //#endregion
126
127
  //#region src/features/tracing.ts
128
+ /** User-agents of infrastructure health-checkers whose requests we don't trace. */
129
+ const HEALTHCHECK_USER_AGENT_PREFIXES = [
130
+ "ELB-HealthChecker",
131
+ "kube-probe",
132
+ "GoogleHC"
133
+ ];
127
134
  let sdk = null;
128
135
  let isInitialized = false;
129
136
  const MONITORED_MODULES = [
@@ -155,18 +162,59 @@ function setupTracing(options) {
155
162
  "deployment.environment": environment
156
163
  });
157
164
  const traceExporter = new __opentelemetry_exporter_trace_otlp_grpc.OTLPTraceExporter({ url: endpoint });
158
- const instrumentationConfig = buildInstrumentationConfig(autoInstrumentation);
159
165
  if (!metricReader && !process.env.OTEL_METRICS_EXPORTER) process.env.OTEL_METRICS_EXPORTER = "none";
160
166
  sdk = new __opentelemetry_sdk_node.NodeSDK({
161
167
  resource,
162
168
  traceExporter,
163
169
  ...metricReader ? { metricReaders: [metricReader] } : {},
164
170
  logRecordProcessors: [],
165
- instrumentations: [(0, __opentelemetry_auto_instrumentations_node.getNodeAutoInstrumentations)(instrumentationConfig)]
171
+ instrumentations: buildInstrumentations(autoInstrumentation)
166
172
  });
167
173
  sdk.start();
168
174
  isInitialized = true;
169
175
  }
176
+ /**
177
+ * Produce a clean name for a Fastify lifecycle (hook/handler) span.
178
+ *
179
+ * @fastify/otel names these "<hook> - <fn name>", but anonymous hook functions fall
180
+ * back to the full plugin-encapsulation chain ("fastify -> @fastify/otel -> ... ->
181
+ * @fastify/x"), which is unreadable. When that chain is detected, collapse it to a
182
+ * clean, route-scoped name (e.g. "onRequest /api/v1/foo"). Returns `null` when the
183
+ * span is already cleanly named (a named hook like "onRequest - handleCors"), so the
184
+ * caller leaves it untouched.
185
+ */
186
+ function cleanLifecycleSpanName(hookName, handlerName, routeUrl) {
187
+ if (!handlerName.includes(" -> ")) return null;
188
+ return routeUrl ? `${hookName} ${routeUrl}` : hookName;
189
+ }
190
+ /**
191
+ * Build the full instrumentation list for the NodeSDK.
192
+ *
193
+ * auto-instrumentations-node covers http, mongodb, redis, etc., but it no longer
194
+ * bundles Fastify, so Fastify is added explicitly via @fastify/otel (the Fastify
195
+ * core team's official OpenTelemetry plugin, v4–v5). With registerOnInitialization it
196
+ * self-registers on every Fastify instance via the fastify.initialization diagnostics
197
+ * channel, and — riding on instrumentation-http — names the server span "GET /route"
198
+ * and emits the request → hook → handler waterfall (the Fastify equivalent of the
199
+ * Express middleware spans on sibling services), with DB spans nesting underneath.
200
+ */
201
+ function buildInstrumentations(autoInstrumentation = {}) {
202
+ const instrumentations = [...(0, __opentelemetry_auto_instrumentations_node.getNodeAutoInstrumentations)(buildInstrumentationConfig(autoInstrumentation))];
203
+ if (autoInstrumentation.fastify !== false) instrumentations.push(new __fastify_otel.FastifyOtelInstrumentation({
204
+ registerOnInitialization: true,
205
+ requestHook: (span, request) => {
206
+ const routeUrl = request.routeOptions?.url;
207
+ const method = request.method;
208
+ if (routeUrl && method) span.updateName(`${method} ${routeUrl}`);
209
+ },
210
+ lifecycleHook: (span, info) => {
211
+ const i = info;
212
+ const cleaned = cleanLifecycleSpanName(i.hookName ?? "", typeof i.handler === "string" ? i.handler : "", i.request?.routeOptions?.url);
213
+ if (cleaned) span.updateName(cleaned);
214
+ }
215
+ }));
216
+ return instrumentations;
217
+ }
170
218
  function buildInstrumentationConfig(config) {
171
219
  const mapping = {
172
220
  http: "@opentelemetry/instrumentation-http",
@@ -186,6 +234,14 @@ function buildInstrumentationConfig(config) {
186
234
  const defaultValue = key !== "fs" && key !== "dns";
187
235
  result[instrumentationName] = { enabled: userSetting ?? defaultValue };
188
236
  }
237
+ result["@opentelemetry/instrumentation-http"] = {
238
+ ...result["@opentelemetry/instrumentation-http"],
239
+ ignoreIncomingRequestHook: (request) => {
240
+ const ua = request.headers?.["user-agent"];
241
+ const uaStr = Array.isArray(ua) ? ua[0] ?? "" : ua ?? "";
242
+ return HEALTHCHECK_USER_AGENT_PREFIXES.some((prefix) => uaStr.startsWith(prefix));
243
+ }
244
+ };
189
245
  return result;
190
246
  }
191
247
  /** Shutdown tracing gracefully */
@@ -328,4 +384,4 @@ Object.defineProperty(exports, 'shutdownTracing', {
328
384
  return shutdownTracing;
329
385
  }
330
386
  });
331
- //# sourceMappingURL=tracing-BfIbmZ87.js.map
387
+ //# sourceMappingURL=tracing-Yrfx2JK6.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracing-Yrfx2JK6.js","names":["input: FoundationConfigInput","sdk: NodeSDK | null","options: TracingOptions","hookName: string","handlerName: string","routeUrl?: string","autoInstrumentation: AutoInstrumentationConfig","instrumentations: Instrumentation[]","config: AutoInstrumentationConfig","mapping: Record<string, string>","result: Record<string, Record<string, unknown>>","request: {\n headers?: Record<string, string | string[] | undefined>;\n }","name: string"],"sources":["../src/config.ts","../src/features/tracing.ts"],"sourcesContent":["/**\n * Foundation Configuration with Zod Validation\n */\n\nimport { z } from 'zod';\n\n// Auto-Instrumentation Config\nexport const AutoInstrumentationConfigSchema = z\n .object({\n http: z.boolean().default(true),\n fastify: z.boolean().default(true),\n express: z.boolean().default(true),\n mongodb: z.boolean().default(true),\n pg: z.boolean().default(true),\n mysql: z.boolean().default(true),\n redis: z.boolean().default(true),\n ioredis: z.boolean().default(true),\n grpc: z.boolean().default(true),\n fs: z.boolean().default(false),\n dns: z.boolean().default(false),\n })\n .partial();\n\nexport type AutoInstrumentationConfig = z.infer<typeof AutoInstrumentationConfigSchema>;\n\n// Features Config\nexport const FeaturesConfigSchema = z\n .object({\n tracing: z.boolean().default(true),\n metrics: z.boolean().default(true),\n logging: z.boolean().default(true),\n autoInstrumentation: AutoInstrumentationConfigSchema.default({}),\n })\n .partial();\n\nexport type FeaturesConfig = z.infer<typeof FeaturesConfigSchema>;\n\n// OTEL Config\nconst DEFAULT_OTEL_ENDPOINT =\n 'http://otel-stack-deployment-collector.observability.svc.cluster.local:4317';\n\nexport const OtelConfigSchema = z.object({\n endpoint: z\n .string()\n .default(() => process.env.OTEL_EXPORTER_OTLP_ENDPOINT || DEFAULT_OTEL_ENDPOINT),\n metricsIntervalMs: z.number().min(1000).default(5000),\n});\n\nexport type OtelConfig = z.infer<typeof OtelConfigSchema>;\n\n// Logging Config\nexport const LoggingConfigSchema = z\n .object({\n level: z.enum(['debug', 'info', 'warn', 'error']).default('info'),\n prettyPrint: z.boolean().optional(),\n })\n .partial();\n\nexport type LoggingConfig = z.infer<typeof LoggingConfigSchema>;\n\n// Request Logging Config\nexport const RequestLoggingConfigSchema = z\n .object({\n logHeaders: z.boolean().default(true),\n logBody: z.boolean().default(false),\n logResponseBody: z.boolean().default(false),\n maxBodySize: z.number().default(10 * 1024),\n redactHeaders: z.array(z.string()).optional(),\n })\n .partial();\n\nexport type RequestLoggingConfig = z.infer<typeof RequestLoggingConfigSchema>;\n\n// Redaction Config\nexport const RedactionConfigSchema = z\n .object({\n additionalPaths: z.array(z.string()).default([]),\n })\n .partial();\n\nexport type RedactionConfig = z.infer<typeof RedactionConfigSchema>;\n\n// Shutdown Config\nexport const ShutdownConfigSchema = z\n .object({\n /** Flush telemetry on uncaughtException / unhandledRejection before process exits */\n flushOnCrash: z.boolean().default(false),\n /** Grace period (ms) for flushing before forcing exit */\n flushTimeoutMs: z.number().min(100).default(5000),\n })\n .partial();\n\nexport type ShutdownConfig = z.infer<typeof ShutdownConfigSchema>;\n\n// Vault Config\nexport const VaultConfigSchema = z.object({\n enabled: z.boolean().default(false),\n address: z.string().default('https://vault.beta.commerceiq.ai'),\n authMethod: z.enum(['kubernetes', 'token']).default('kubernetes'),\n role: z.string().optional(),\n mountPoint: z.string().default('secret'),\n tokenPath: z.string().default('/var/run/secrets/kubernetes.io/serviceaccount/token'),\n k8sAuthMount: z.string().default('kubernetes'),\n namespace: z.string().optional(),\n});\n\nexport type VaultConfig = z.infer<typeof VaultConfigSchema>;\n\n// Main Foundation Config\nexport const FoundationConfigSchema = z.object({\n serviceName: z.string().min(1, 'serviceName is required'),\n serviceVersion: z.string().default(process.env.SERVICE_VERSION || '1.0.0'),\n environment: z\n .enum(['development', 'staging', 'qa', 'production'])\n .default(\n ['development', 'staging', 'qa', 'production'].includes(process.env.NODE_ENV ?? '')\n ? (process.env.NODE_ENV as 'development' | 'staging' | 'qa' | 'production')\n : 'development'\n ),\n features: FeaturesConfigSchema.default({}),\n otel: OtelConfigSchema.default({}),\n logging: LoggingConfigSchema.default({}),\n requestLogging: RequestLoggingConfigSchema.default({}),\n redaction: RedactionConfigSchema.default({}),\n shutdown: ShutdownConfigSchema.default({}),\n vault: VaultConfigSchema.default({}),\n});\n\nexport type FoundationConfig = z.infer<typeof FoundationConfigSchema>;\n\nexport type FoundationConfigInput = z.input<typeof FoundationConfigSchema> & {\n serviceName: string;\n};\n\n/** Parse and validate configuration */\nexport function parseConfig(input: FoundationConfigInput): FoundationConfig {\n const result = FoundationConfigSchema.safeParse(input);\n\n if (!result.success) {\n const errors = result.error.errors\n .map((e) => ` - ${e.path.join('.')}: ${e.message}`)\n .join('\\n');\n throw new Error(`Invalid foundation configuration:\\n${errors}`);\n }\n\n return result.data;\n}\n\n/** Get default OTEL endpoint */\nexport function getDefaultOtelEndpoint(): string {\n return process.env.OTEL_EXPORTER_OTLP_ENDPOINT || DEFAULT_OTEL_ENDPOINT;\n}\n","/**\n * OpenTelemetry Tracing Setup\n */\n\nimport { NodeSDK } from '@opentelemetry/sdk-node';\nimport { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';\nimport { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';\nimport { resourceFromAttributes } from '@opentelemetry/resources';\nimport { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';\nimport { trace, context, propagation, SpanStatusCode, type Tracer } from '@opentelemetry/api';\nimport { type IMetricReader } from '@opentelemetry/sdk-metrics';\nimport { FastifyOtelInstrumentation } from '@fastify/otel';\nimport { type AutoInstrumentationConfig, getDefaultOtelEndpoint } from '../config';\n\n/**\n * Element type of the NodeSDK instrumentations list. Derived from\n * getNodeAutoInstrumentations so we don't take a direct import on\n * @opentelemetry/instrumentation (only a transitive dependency here).\n */\ntype Instrumentation = ReturnType<typeof getNodeAutoInstrumentations>[number];\ntype InstrumentationConfigMap = Parameters<typeof getNodeAutoInstrumentations>[0];\n\n/** User-agents of infrastructure health-checkers whose requests we don't trace. */\nconst HEALTHCHECK_USER_AGENT_PREFIXES = ['ELB-HealthChecker', 'kube-probe', 'GoogleHC'];\n\nexport interface TracingOptions {\n serviceName: string;\n serviceVersion: string;\n environment: string;\n endpoint?: string;\n autoInstrumentation?: AutoInstrumentationConfig;\n /**\n * Metric reader to attach to the NodeSDK so a single SDK owns both traces and\n * metrics (one global MeterProvider, same gRPC collector). When provided, NodeSDK\n * is the sole owner of metrics; when omitted, NodeSDK is opted out of metrics\n * entirely (see below) so it never registers a stray default MeterProvider.\n */\n metricReader?: IMetricReader;\n}\n\nlet sdk: NodeSDK | null = null;\nlet isInitialized = false;\n\nconst MONITORED_MODULES = ['pg', 'mongodb', 'ioredis', 'mysql2', 'express', '@grpc/grpc-js'];\n\nfunction warnPreloadedModules(): void {\n // require.resolve / require.cache are only available in CJS contexts\n if (typeof require === 'undefined' || typeof require.cache === 'undefined') return;\n\n for (const mod of MONITORED_MODULES) {\n try {\n const resolved = require.resolve(mod);\n if (require.cache[resolved]) {\n console.warn(\n `[neoiq-foundation] \"${mod}\" was imported before tracing init. ` +\n 'Auto-instrumentation may not work for this module. ' +\n 'Use node -r @ciq-dev/neoiq-foundation-node/bootstrap or call createFoundation() first.'\n );\n }\n } catch {\n // Module not installed — no warning needed\n }\n }\n}\n\n/** Initialize OpenTelemetry tracing */\nexport function setupTracing(options: TracingOptions): void {\n if (isInitialized) {\n console.warn('[neoiq-foundation] Tracing already initialized');\n return;\n }\n\n warnPreloadedModules();\n\n const {\n serviceName,\n serviceVersion,\n environment,\n endpoint = getDefaultOtelEndpoint(),\n autoInstrumentation = {},\n metricReader,\n } = options;\n\n const resource = resourceFromAttributes({\n [ATTR_SERVICE_NAME]: serviceName,\n [ATTR_SERVICE_VERSION]: serviceVersion,\n 'deployment.environment': environment,\n });\n\n const traceExporter = new OTLPTraceExporter({ url: endpoint });\n\n // Foundation owns every OpenTelemetry signal explicitly. Left to its own devices,\n // NodeSDK auto-configures metrics AND logs exporters from env defaults — both OTLP\n // over HTTP to localhost:4318 — which nothing listens on, producing ECONNREFUSED\n // spam every export interval. We therefore configure each signal on NodeSDK directly:\n // - traces: the explicit gRPC exporter above.\n // - metrics: the explicit gRPC reader when provided (single SDK, single global\n // MeterProvider, same collector as traces). When metrics are disabled\n // and the deployer hasn't chosen an exporter, opt NodeSDK out via\n // OTEL_METRICS_EXPORTER=none (the only switch NodeSDK exposes for that).\n // - logs: foundation logs via pino, never OTLP. Passing an empty processor list\n // stops NodeSDK from falling back to its env-default localhost:4318 log\n // exporter (`logRecordProcessors: []` takes the explicit branch and skips\n // configureLoggerProviderFromEnv()).\n if (!metricReader && !process.env.OTEL_METRICS_EXPORTER) {\n process.env.OTEL_METRICS_EXPORTER = 'none';\n }\n\n sdk = new NodeSDK({\n resource,\n traceExporter,\n ...(metricReader ? { metricReaders: [metricReader] } : {}),\n logRecordProcessors: [],\n instrumentations: buildInstrumentations(autoInstrumentation),\n });\n\n sdk.start();\n isInitialized = true;\n}\n\n/**\n * Produce a clean name for a Fastify lifecycle (hook/handler) span.\n *\n * @fastify/otel names these \"<hook> - <fn name>\", but anonymous hook functions fall\n * back to the full plugin-encapsulation chain (\"fastify -> @fastify/otel -> ... ->\n * @fastify/x\"), which is unreadable. When that chain is detected, collapse it to a\n * clean, route-scoped name (e.g. \"onRequest /api/v1/foo\"). Returns `null` when the\n * span is already cleanly named (a named hook like \"onRequest - handleCors\"), so the\n * caller leaves it untouched.\n */\nexport function cleanLifecycleSpanName(\n hookName: string,\n handlerName: string,\n routeUrl?: string\n): string | null {\n if (!handlerName.includes(' -> ')) return null;\n return routeUrl ? `${hookName} ${routeUrl}` : hookName;\n}\n\n/**\n * Build the full instrumentation list for the NodeSDK.\n *\n * auto-instrumentations-node covers http, mongodb, redis, etc., but it no longer\n * bundles Fastify, so Fastify is added explicitly via @fastify/otel (the Fastify\n * core team's official OpenTelemetry plugin, v4–v5). With registerOnInitialization it\n * self-registers on every Fastify instance via the fastify.initialization diagnostics\n * channel, and — riding on instrumentation-http — names the server span \"GET /route\"\n * and emits the request → hook → handler waterfall (the Fastify equivalent of the\n * Express middleware spans on sibling services), with DB spans nesting underneath.\n */\nexport function buildInstrumentations(\n autoInstrumentation: AutoInstrumentationConfig = {}\n): Instrumentation[] {\n const instrumentations: Instrumentation[] = [\n ...getNodeAutoInstrumentations(buildInstrumentationConfig(autoInstrumentation)),\n ];\n\n if (autoInstrumentation.fastify !== false) {\n instrumentations.push(\n new FastifyOtelInstrumentation({\n registerOnInitialization: true,\n // Name the Fastify request span \"METHOD /route\" (route template, low cardinality).\n requestHook: (span, request) => {\n const routeUrl = (request as { routeOptions?: { url?: string } }).routeOptions?.url;\n const method = (request as { method?: string }).method;\n if (routeUrl && method) span.updateName(`${method} ${routeUrl}`);\n },\n // Collapse the verbose anonymous-hook plugin chains into readable names.\n lifecycleHook: (span, info) => {\n const i = info as {\n hookName?: string;\n handler?: unknown;\n request?: { routeOptions?: { url?: string } };\n };\n const cleaned = cleanLifecycleSpanName(\n i.hookName ?? '',\n typeof i.handler === 'string' ? i.handler : '',\n i.request?.routeOptions?.url\n );\n if (cleaned) span.updateName(cleaned);\n },\n })\n );\n }\n\n return instrumentations;\n}\n\nfunction buildInstrumentationConfig(config: AutoInstrumentationConfig): InstrumentationConfigMap {\n const mapping: Record<string, string> = {\n http: '@opentelemetry/instrumentation-http',\n // Fastify is intentionally absent here — it's not in the auto-instrumentations\n // bundle and is added separately in buildInstrumentations() via\n // @opentelemetry/instrumentation-fastify.\n express: '@opentelemetry/instrumentation-express',\n mongodb: '@opentelemetry/instrumentation-mongodb',\n pg: '@opentelemetry/instrumentation-pg',\n mysql: '@opentelemetry/instrumentation-mysql',\n redis: '@opentelemetry/instrumentation-redis',\n ioredis: '@opentelemetry/instrumentation-ioredis',\n grpc: '@opentelemetry/instrumentation-grpc',\n fs: '@opentelemetry/instrumentation-fs',\n dns: '@opentelemetry/instrumentation-dns',\n };\n\n const result: Record<string, Record<string, unknown>> = {};\n for (const [key, instrumentationName] of Object.entries(mapping)) {\n const userSetting = config[key as keyof AutoInstrumentationConfig];\n const defaultValue = key !== 'fs' && key !== 'dns';\n result[instrumentationName] = { enabled: userSetting ?? defaultValue };\n }\n\n // Don't create spans for infrastructure health-checks (ELB / kube-probe / GoogleHC).\n // They're high-volume, low-value, and otherwise dominate (and skew sampling of) traces.\n result['@opentelemetry/instrumentation-http'] = {\n ...result['@opentelemetry/instrumentation-http'],\n ignoreIncomingRequestHook: (request: {\n headers?: Record<string, string | string[] | undefined>;\n }) => {\n const ua = request.headers?.['user-agent'];\n const uaStr = Array.isArray(ua) ? (ua[0] ?? '') : (ua ?? '');\n return HEALTHCHECK_USER_AGENT_PREFIXES.some((prefix) => uaStr.startsWith(prefix));\n },\n };\n\n return result as InstrumentationConfigMap;\n}\n\n/** Shutdown tracing gracefully */\nexport async function shutdownTracing(): Promise<void> {\n if (!sdk) return;\n try {\n await sdk.shutdown();\n isInitialized = false;\n sdk = null;\n } catch (error) {\n console.error('[neoiq-foundation] Error shutting down tracing:', error);\n throw error;\n }\n}\n\nexport function getTracer(name: string): Tracer {\n return trace.getTracer(name);\n}\n\nexport function getActiveSpan(): ReturnType<typeof trace.getActiveSpan> {\n return trace.getActiveSpan();\n}\n\nexport function getTraceContext(): { traceId?: string; spanId?: string } {\n const span = trace.getActiveSpan();\n if (!span) return {};\n const ctx = span.spanContext();\n return { traceId: ctx.traceId, spanId: ctx.spanId };\n}\n\nexport function isTracingEnabled(): boolean {\n return isInitialized;\n}\n\nexport { trace, context, propagation, SpanStatusCode };\nexport type { Tracer };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAOA,MAAa,kCAAkC,MAC5C,OAAO;CACN,MAAM,MAAE,SAAS,CAAC,QAAQ,KAAK;CAC/B,SAAS,MAAE,SAAS,CAAC,QAAQ,KAAK;CAClC,SAAS,MAAE,SAAS,CAAC,QAAQ,KAAK;CAClC,SAAS,MAAE,SAAS,CAAC,QAAQ,KAAK;CAClC,IAAI,MAAE,SAAS,CAAC,QAAQ,KAAK;CAC7B,OAAO,MAAE,SAAS,CAAC,QAAQ,KAAK;CAChC,OAAO,MAAE,SAAS,CAAC,QAAQ,KAAK;CAChC,SAAS,MAAE,SAAS,CAAC,QAAQ,KAAK;CAClC,MAAM,MAAE,SAAS,CAAC,QAAQ,KAAK;CAC/B,IAAI,MAAE,SAAS,CAAC,QAAQ,MAAM;CAC9B,KAAK,MAAE,SAAS,CAAC,QAAQ,MAAM;AAChC,EAAC,CACD,SAAS;AAKZ,MAAa,u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kBAAiC;AACrD,MAAK,IAAK;AACV,KAAI;AACF,QAAM,IAAI,UAAU;AACpB,kBAAgB;AAChB,QAAM;CACP,SAAQ,OAAO;AACd,UAAQ,MAAM,mDAAmD,MAAM;AACvE,QAAM;CACP;AACF;AAED,SAAgB,UAAUC,MAAsB;AAC9C,QAAO,0BAAM,UAAU,KAAK;AAC7B;AAED,SAAgB,gBAAwD;AACtE,QAAO,0BAAM,eAAe;AAC7B;AAED,SAAgB,kBAAyD;CACvE,MAAM,OAAO,0BAAM,eAAe;AAClC,MAAK,KAAM,QAAO,CAAE;CACpB,MAAM,MAAM,KAAK,aAAa;AAC9B,QAAO;EAAE,SAAS,IAAI;EAAS,QAAQ,IAAI;CAAQ;AACpD;AAED,SAAgB,mBAA4B;AAC1C,QAAO;AACR"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ciq-dev/neoiq-foundation-node",
3
- "version": "1.1.2-beta.8",
3
+ "version": "1.1.2-beta.9",
4
4
  "description": "Node.js observability foundation for CommerceIQ services. Integrates with Groundcover via OpenTelemetry.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -48,14 +48,14 @@
48
48
  "peerDependencies": {
49
49
  "@aws-sdk/client-s3": "^3.0.0",
50
50
  "@aws-sdk/s3-request-presigner": "^3.0.0",
51
- "@opentelemetry/api": "^1.7.0",
52
- "@opentelemetry/auto-instrumentations-node": ">=0.41.0",
53
- "@opentelemetry/exporter-metrics-otlp-grpc": ">=0.48.0",
54
- "@opentelemetry/exporter-trace-otlp-grpc": ">=0.48.0",
55
- "@opentelemetry/resources": ">=1.21.0",
56
- "@opentelemetry/sdk-metrics": ">=1.21.0",
57
- "@opentelemetry/sdk-node": ">=0.48.0",
58
- "@opentelemetry/semantic-conventions": "^1.21.0",
51
+ "@opentelemetry/api": "^1.9.0",
52
+ "@opentelemetry/auto-instrumentations-node": ">=0.76.0",
53
+ "@opentelemetry/exporter-metrics-otlp-grpc": ">=0.218.0",
54
+ "@opentelemetry/exporter-trace-otlp-grpc": ">=0.218.0",
55
+ "@opentelemetry/resources": ">=2.0.0",
56
+ "@opentelemetry/sdk-metrics": ">=2.0.0",
57
+ "@opentelemetry/sdk-node": ">=0.218.0",
58
+ "@opentelemetry/semantic-conventions": "^1.28.0",
59
59
  "axios": "^1.6.0",
60
60
  "axios-retry": "^4.0.0",
61
61
  "fastify": ">=4.25.0",
@@ -78,6 +78,7 @@
78
78
  "url": "git+https://github.com/commerceiq/neoiq-foundation-node.git"
79
79
  },
80
80
  "dependencies": {
81
+ "@fastify/otel": "^0.18.1",
81
82
  "node-vault": "^0.12.0"
82
83
  }
83
84
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"tracing-BfIbmZ87.js","names":["input: FoundationConfigInput","sdk: NodeSDK | null","options: TracingOptions","config: AutoInstrumentationConfig","mapping: Record<string, string>","result: Record<string, { enabled: boolean }>","name: string"],"sources":["../src/config.ts","../src/features/tracing.ts"],"sourcesContent":["/**\n * Foundation Configuration with Zod Validation\n */\n\nimport { z } from 'zod';\n\n// Auto-Instrumentation Config\nexport const AutoInstrumentationConfigSchema = z\n .object({\n http: z.boolean().default(true),\n fastify: z.boolean().default(true),\n express: z.boolean().default(true),\n mongodb: z.boolean().default(true),\n pg: z.boolean().default(true),\n mysql: z.boolean().default(true),\n redis: z.boolean().default(true),\n ioredis: z.boolean().default(true),\n grpc: z.boolean().default(true),\n fs: z.boolean().default(false),\n dns: z.boolean().default(false),\n })\n .partial();\n\nexport type AutoInstrumentationConfig = z.infer<typeof AutoInstrumentationConfigSchema>;\n\n// Features Config\nexport const FeaturesConfigSchema = z\n .object({\n tracing: z.boolean().default(true),\n metrics: z.boolean().default(true),\n logging: z.boolean().default(true),\n autoInstrumentation: AutoInstrumentationConfigSchema.default({}),\n })\n .partial();\n\nexport type FeaturesConfig = z.infer<typeof FeaturesConfigSchema>;\n\n// OTEL Config\nconst DEFAULT_OTEL_ENDPOINT =\n 'http://otel-stack-deployment-collector.observability.svc.cluster.local:4317';\n\nexport const OtelConfigSchema = z.object({\n endpoint: z\n .string()\n .default(() => process.env.OTEL_EXPORTER_OTLP_ENDPOINT || DEFAULT_OTEL_ENDPOINT),\n metricsIntervalMs: z.number().min(1000).default(5000),\n});\n\nexport type OtelConfig = z.infer<typeof OtelConfigSchema>;\n\n// Logging Config\nexport const LoggingConfigSchema = z\n .object({\n level: z.enum(['debug', 'info', 'warn', 'error']).default('info'),\n prettyPrint: z.boolean().optional(),\n })\n .partial();\n\nexport type LoggingConfig = z.infer<typeof LoggingConfigSchema>;\n\n// Request Logging Config\nexport const RequestLoggingConfigSchema = z\n .object({\n logHeaders: z.boolean().default(true),\n logBody: z.boolean().default(false),\n logResponseBody: z.boolean().default(false),\n maxBodySize: z.number().default(10 * 1024),\n redactHeaders: z.array(z.string()).optional(),\n })\n .partial();\n\nexport type RequestLoggingConfig = z.infer<typeof RequestLoggingConfigSchema>;\n\n// Redaction Config\nexport const RedactionConfigSchema = z\n .object({\n additionalPaths: z.array(z.string()).default([]),\n })\n .partial();\n\nexport type RedactionConfig = z.infer<typeof RedactionConfigSchema>;\n\n// Shutdown Config\nexport const ShutdownConfigSchema = z\n .object({\n /** Flush telemetry on uncaughtException / unhandledRejection before process exits */\n flushOnCrash: z.boolean().default(false),\n /** Grace period (ms) for flushing before forcing exit */\n flushTimeoutMs: z.number().min(100).default(5000),\n })\n .partial();\n\nexport type ShutdownConfig = z.infer<typeof ShutdownConfigSchema>;\n\n// Vault Config\nexport const VaultConfigSchema = z.object({\n enabled: z.boolean().default(false),\n address: z.string().default('https://vault.beta.commerceiq.ai'),\n authMethod: z.enum(['kubernetes', 'token']).default('kubernetes'),\n role: z.string().optional(),\n mountPoint: z.string().default('secret'),\n tokenPath: z.string().default('/var/run/secrets/kubernetes.io/serviceaccount/token'),\n k8sAuthMount: z.string().default('kubernetes'),\n namespace: z.string().optional(),\n});\n\nexport type VaultConfig = z.infer<typeof VaultConfigSchema>;\n\n// Main Foundation Config\nexport const FoundationConfigSchema = z.object({\n serviceName: z.string().min(1, 'serviceName is required'),\n serviceVersion: z.string().default(process.env.SERVICE_VERSION || '1.0.0'),\n environment: z\n .enum(['development', 'staging', 'qa', 'production'])\n .default(\n ['development', 'staging', 'qa', 'production'].includes(process.env.NODE_ENV ?? '')\n ? (process.env.NODE_ENV as 'development' | 'staging' | 'qa' | 'production')\n : 'development'\n ),\n features: FeaturesConfigSchema.default({}),\n otel: OtelConfigSchema.default({}),\n logging: LoggingConfigSchema.default({}),\n requestLogging: RequestLoggingConfigSchema.default({}),\n redaction: RedactionConfigSchema.default({}),\n shutdown: ShutdownConfigSchema.default({}),\n vault: VaultConfigSchema.default({}),\n});\n\nexport type FoundationConfig = z.infer<typeof FoundationConfigSchema>;\n\nexport type FoundationConfigInput = z.input<typeof FoundationConfigSchema> & {\n serviceName: string;\n};\n\n/** Parse and validate configuration */\nexport function parseConfig(input: FoundationConfigInput): FoundationConfig {\n const result = FoundationConfigSchema.safeParse(input);\n\n if (!result.success) {\n const errors = result.error.errors\n .map((e) => ` - ${e.path.join('.')}: ${e.message}`)\n .join('\\n');\n throw new Error(`Invalid foundation configuration:\\n${errors}`);\n }\n\n return result.data;\n}\n\n/** Get default OTEL endpoint */\nexport function getDefaultOtelEndpoint(): string {\n return process.env.OTEL_EXPORTER_OTLP_ENDPOINT || DEFAULT_OTEL_ENDPOINT;\n}\n","/**\n * OpenTelemetry Tracing Setup\n */\n\nimport { NodeSDK } from '@opentelemetry/sdk-node';\nimport { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';\nimport { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';\nimport { resourceFromAttributes } from '@opentelemetry/resources';\nimport { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';\nimport { trace, context, propagation, SpanStatusCode, type Tracer } from '@opentelemetry/api';\nimport { type IMetricReader } from '@opentelemetry/sdk-metrics';\nimport { type AutoInstrumentationConfig, getDefaultOtelEndpoint } from '../config';\n\nexport interface TracingOptions {\n serviceName: string;\n serviceVersion: string;\n environment: string;\n endpoint?: string;\n autoInstrumentation?: AutoInstrumentationConfig;\n /**\n * Metric reader to attach to the NodeSDK so a single SDK owns both traces and\n * metrics (one global MeterProvider, same gRPC collector). When provided, NodeSDK\n * is the sole owner of metrics; when omitted, NodeSDK is opted out of metrics\n * entirely (see below) so it never registers a stray default MeterProvider.\n */\n metricReader?: IMetricReader;\n}\n\nlet sdk: NodeSDK | null = null;\nlet isInitialized = false;\n\nconst MONITORED_MODULES = ['pg', 'mongodb', 'ioredis', 'mysql2', 'express', '@grpc/grpc-js'];\n\nfunction warnPreloadedModules(): void {\n // require.resolve / require.cache are only available in CJS contexts\n if (typeof require === 'undefined' || typeof require.cache === 'undefined') return;\n\n for (const mod of MONITORED_MODULES) {\n try {\n const resolved = require.resolve(mod);\n if (require.cache[resolved]) {\n console.warn(\n `[neoiq-foundation] \"${mod}\" was imported before tracing init. ` +\n 'Auto-instrumentation may not work for this module. ' +\n 'Use node -r @ciq-dev/neoiq-foundation-node/bootstrap or call createFoundation() first.'\n );\n }\n } catch {\n // Module not installed — no warning needed\n }\n }\n}\n\n/** Initialize OpenTelemetry tracing */\nexport function setupTracing(options: TracingOptions): void {\n if (isInitialized) {\n console.warn('[neoiq-foundation] Tracing already initialized');\n return;\n }\n\n warnPreloadedModules();\n\n const {\n serviceName,\n serviceVersion,\n environment,\n endpoint = getDefaultOtelEndpoint(),\n autoInstrumentation = {},\n metricReader,\n } = options;\n\n const resource = resourceFromAttributes({\n [ATTR_SERVICE_NAME]: serviceName,\n [ATTR_SERVICE_VERSION]: serviceVersion,\n 'deployment.environment': environment,\n });\n\n const traceExporter = new OTLPTraceExporter({ url: endpoint });\n const instrumentationConfig = buildInstrumentationConfig(autoInstrumentation);\n\n // Foundation owns every OpenTelemetry signal explicitly. Left to its own devices,\n // NodeSDK auto-configures metrics AND logs exporters from env defaults — both OTLP\n // over HTTP to localhost:4318 — which nothing listens on, producing ECONNREFUSED\n // spam every export interval. We therefore configure each signal on NodeSDK directly:\n // - traces: the explicit gRPC exporter above.\n // - metrics: the explicit gRPC reader when provided (single SDK, single global\n // MeterProvider, same collector as traces). When metrics are disabled\n // and the deployer hasn't chosen an exporter, opt NodeSDK out via\n // OTEL_METRICS_EXPORTER=none (the only switch NodeSDK exposes for that).\n // - logs: foundation logs via pino, never OTLP. Passing an empty processor list\n // stops NodeSDK from falling back to its env-default localhost:4318 log\n // exporter (`logRecordProcessors: []` takes the explicit branch and skips\n // configureLoggerProviderFromEnv()).\n if (!metricReader && !process.env.OTEL_METRICS_EXPORTER) {\n process.env.OTEL_METRICS_EXPORTER = 'none';\n }\n\n sdk = new NodeSDK({\n resource,\n traceExporter,\n ...(metricReader ? { metricReaders: [metricReader] } : {}),\n logRecordProcessors: [],\n instrumentations: [getNodeAutoInstrumentations(instrumentationConfig)],\n });\n\n sdk.start();\n isInitialized = true;\n}\n\nfunction buildInstrumentationConfig(\n config: AutoInstrumentationConfig\n): Record<string, { enabled: boolean }> {\n const mapping: Record<string, string> = {\n http: '@opentelemetry/instrumentation-http',\n // No Fastify entry: auto-instrumentations-node dropped instrumentation-fastify\n // (it never supported Fastify v5). Fastify's HTTP server span comes from\n // instrumentation-http and is named with its route template in the foundation\n // Fastify plugin's onRequest hook — no framework instrumentation needed.\n express: '@opentelemetry/instrumentation-express',\n mongodb: '@opentelemetry/instrumentation-mongodb',\n pg: '@opentelemetry/instrumentation-pg',\n mysql: '@opentelemetry/instrumentation-mysql',\n redis: '@opentelemetry/instrumentation-redis',\n ioredis: '@opentelemetry/instrumentation-ioredis',\n grpc: '@opentelemetry/instrumentation-grpc',\n fs: '@opentelemetry/instrumentation-fs',\n dns: '@opentelemetry/instrumentation-dns',\n };\n\n const result: Record<string, { enabled: boolean }> = {};\n for (const [key, instrumentationName] of Object.entries(mapping)) {\n const userSetting = config[key as keyof AutoInstrumentationConfig];\n const defaultValue = key !== 'fs' && key !== 'dns';\n result[instrumentationName] = { enabled: userSetting ?? defaultValue };\n }\n return result;\n}\n\n/** Shutdown tracing gracefully */\nexport async function shutdownTracing(): Promise<void> {\n if (!sdk) return;\n try {\n await sdk.shutdown();\n isInitialized = false;\n sdk = null;\n } catch (error) {\n console.error('[neoiq-foundation] Error shutting down tracing:', error);\n throw error;\n }\n}\n\nexport function getTracer(name: string): Tracer {\n return trace.getTracer(name);\n}\n\nexport function getActiveSpan(): ReturnType<typeof trace.getActiveSpan> {\n return trace.getActiveSpan();\n}\n\nexport function getTraceContext(): { traceId?: string; spanId?: string } {\n const span = trace.getActiveSpan();\n if (!span) return {};\n const ctx = span.spanContext();\n return { traceId: ctx.traceId, spanId: ctx.spanId };\n}\n\nexport function isTracingEnabled(): boolean {\n return isInitialized;\n}\n\nexport { trace, context, propagation, SpanStatusCode };\nexport type { Tracer };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAOA,MAAa,kCAAkC,MAC5C,OAAO;CACN,MAAM,MAAE,SAAS,CAAC,QAAQ,KAAK;CAC/B,SAAS,MAAE,SAAS,CAAC,QAAQ,KAAK;CAClC,SAAS,MAAE,SAAS,CAAC,QAAQ,KAAK;CAClC,SAAS,MAAE,SAAS,CAAC,QAAQ,KAAK;CAClC,IAAI,MAAE,SAAS,CAAC,QAAQ,KAAK;CAC7B,OAAO,MAAE,SAAS,CAAC,QAAQ,KAAK;CAChC,OAAO,MAAE,SAAS,CAAC,QAAQ,KAAK;CAChC,SAAS,MAAE,SAAS,CAAC,QAAQ,KAAK;CAClC,MAAM,MAAE,SAAS,CAAC,QAAQ,KAAK;CAC/B,IAAI,MAAE,SAAS,CAAC,QAAQ,MAAM;CAC9B,KAAK,MAAE,SAAS,CAAC,QAAQ,MAAM;AAChC,EAAC,CACD,SAAS;AAKZ,MAAa,uBAAuB,MACjC,OAAO;CACN,SAAS,MAAE,SAAS,CAAC,QAAQ,KAAK;CAClC,SAAS,MAAE,SAAS,CAAC,QAAQ,K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AAQ,EAAE;EAChE,MAAM,cAAc,OAAO;EAC3B,MAAM,eAAe,QAAQ,QAAQ,QAAQ;AAC7C,SAAO,uBAAuB,EAAE,SAAS,eAAe,aAAc;CACvE;AACD,QAAO;AACR;;AAGD,eAAsB,kBAAiC;AACrD,MAAK,IAAK;AACV,KAAI;AACF,QAAM,IAAI,UAAU;AACpB,kBAAgB;AAChB,QAAM;CACP,SAAQ,OAAO;AACd,UAAQ,MAAM,mDAAmD,MAAM;AACvE,QAAM;CACP;AACF;AAED,SAAgB,UAAUC,MAAsB;AAC9C,QAAO,0BAAM,UAAU,KAAK;AAC7B;AAED,SAAgB,gBAAwD;AACtE,QAAO,0BAAM,eAAe;AAC7B;AAED,SAAgB,kBAAyD;CACvE,MAAM,OAAO,0BAAM,eAAe;AAClC,MAAK,KAAM,QAAO,CAAE;CACpB,MAAM,MAAM,KAAK,aAAa;AAC9B,QAAO;EAAE,SAAS,IAAI;EAAS,QAAQ,IAAI;CAAQ;AACpD;AAED,SAAgB,mBAA4B;AAC1C,QAAO;AACR"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"tracing-CXEMuJKs.mjs","names":["input: FoundationConfigInput","sdk: NodeSDK | null","options: TracingOptions","config: AutoInstrumentationConfig","mapping: Record<string, string>","result: Record<string, { enabled: boolean }>","name: string"],"sources":["../src/config.ts","../src/features/tracing.ts"],"sourcesContent":["/**\n * Foundation Configuration with Zod Validation\n */\n\nimport { z } from 'zod';\n\n// Auto-Instrumentation Config\nexport const AutoInstrumentationConfigSchema = z\n .object({\n http: z.boolean().default(true),\n fastify: z.boolean().default(true),\n express: z.boolean().default(true),\n mongodb: z.boolean().default(true),\n pg: z.boolean().default(true),\n mysql: z.boolean().default(true),\n redis: z.boolean().default(true),\n ioredis: z.boolean().default(true),\n grpc: z.boolean().default(true),\n fs: z.boolean().default(false),\n dns: z.boolean().default(false),\n })\n .partial();\n\nexport type AutoInstrumentationConfig = z.infer<typeof AutoInstrumentationConfigSchema>;\n\n// Features Config\nexport const FeaturesConfigSchema = z\n .object({\n tracing: z.boolean().default(true),\n metrics: z.boolean().default(true),\n logging: z.boolean().default(true),\n autoInstrumentation: AutoInstrumentationConfigSchema.default({}),\n })\n .partial();\n\nexport type FeaturesConfig = z.infer<typeof FeaturesConfigSchema>;\n\n// OTEL Config\nconst DEFAULT_OTEL_ENDPOINT =\n 'http://otel-stack-deployment-collector.observability.svc.cluster.local:4317';\n\nexport const OtelConfigSchema = z.object({\n endpoint: z\n .string()\n .default(() => process.env.OTEL_EXPORTER_OTLP_ENDPOINT || DEFAULT_OTEL_ENDPOINT),\n metricsIntervalMs: z.number().min(1000).default(5000),\n});\n\nexport type OtelConfig = z.infer<typeof OtelConfigSchema>;\n\n// Logging Config\nexport const LoggingConfigSchema = z\n .object({\n level: z.enum(['debug', 'info', 'warn', 'error']).default('info'),\n prettyPrint: z.boolean().optional(),\n })\n .partial();\n\nexport type LoggingConfig = z.infer<typeof LoggingConfigSchema>;\n\n// Request Logging Config\nexport const RequestLoggingConfigSchema = z\n .object({\n logHeaders: z.boolean().default(true),\n logBody: z.boolean().default(false),\n logResponseBody: z.boolean().default(false),\n maxBodySize: z.number().default(10 * 1024),\n redactHeaders: z.array(z.string()).optional(),\n })\n .partial();\n\nexport type RequestLoggingConfig = z.infer<typeof RequestLoggingConfigSchema>;\n\n// Redaction Config\nexport const RedactionConfigSchema = z\n .object({\n additionalPaths: z.array(z.string()).default([]),\n })\n .partial();\n\nexport type RedactionConfig = z.infer<typeof RedactionConfigSchema>;\n\n// Shutdown Config\nexport const ShutdownConfigSchema = z\n .object({\n /** Flush telemetry on uncaughtException / unhandledRejection before process exits */\n flushOnCrash: z.boolean().default(false),\n /** Grace period (ms) for flushing before forcing exit */\n flushTimeoutMs: z.number().min(100).default(5000),\n })\n .partial();\n\nexport type ShutdownConfig = z.infer<typeof ShutdownConfigSchema>;\n\n// Vault Config\nexport const VaultConfigSchema = z.object({\n enabled: z.boolean().default(false),\n address: z.string().default('https://vault.beta.commerceiq.ai'),\n authMethod: z.enum(['kubernetes', 'token']).default('kubernetes'),\n role: z.string().optional(),\n mountPoint: z.string().default('secret'),\n tokenPath: z.string().default('/var/run/secrets/kubernetes.io/serviceaccount/token'),\n k8sAuthMount: z.string().default('kubernetes'),\n namespace: z.string().optional(),\n});\n\nexport type VaultConfig = z.infer<typeof VaultConfigSchema>;\n\n// Main Foundation Config\nexport const FoundationConfigSchema = z.object({\n serviceName: z.string().min(1, 'serviceName is required'),\n serviceVersion: z.string().default(process.env.SERVICE_VERSION || '1.0.0'),\n environment: z\n .enum(['development', 'staging', 'qa', 'production'])\n .default(\n ['development', 'staging', 'qa', 'production'].includes(process.env.NODE_ENV ?? '')\n ? (process.env.NODE_ENV as 'development' | 'staging' | 'qa' | 'production')\n : 'development'\n ),\n features: FeaturesConfigSchema.default({}),\n otel: OtelConfigSchema.default({}),\n logging: LoggingConfigSchema.default({}),\n requestLogging: RequestLoggingConfigSchema.default({}),\n redaction: RedactionConfigSchema.default({}),\n shutdown: ShutdownConfigSchema.default({}),\n vault: VaultConfigSchema.default({}),\n});\n\nexport type FoundationConfig = z.infer<typeof FoundationConfigSchema>;\n\nexport type FoundationConfigInput = z.input<typeof FoundationConfigSchema> & {\n serviceName: string;\n};\n\n/** Parse and validate configuration */\nexport function parseConfig(input: FoundationConfigInput): FoundationConfig {\n const result = FoundationConfigSchema.safeParse(input);\n\n if (!result.success) {\n const errors = result.error.errors\n .map((e) => ` - ${e.path.join('.')}: ${e.message}`)\n .join('\\n');\n throw new Error(`Invalid foundation configuration:\\n${errors}`);\n }\n\n return result.data;\n}\n\n/** Get default OTEL endpoint */\nexport function getDefaultOtelEndpoint(): string {\n return process.env.OTEL_EXPORTER_OTLP_ENDPOINT || DEFAULT_OTEL_ENDPOINT;\n}\n","/**\n * OpenTelemetry Tracing Setup\n */\n\nimport { NodeSDK } from '@opentelemetry/sdk-node';\nimport { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';\nimport { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';\nimport { resourceFromAttributes } from '@opentelemetry/resources';\nimport { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';\nimport { trace, context, propagation, SpanStatusCode, type Tracer } from '@opentelemetry/api';\nimport { type IMetricReader } from '@opentelemetry/sdk-metrics';\nimport { type AutoInstrumentationConfig, getDefaultOtelEndpoint } from '../config';\n\nexport interface TracingOptions {\n serviceName: string;\n serviceVersion: string;\n environment: string;\n endpoint?: string;\n autoInstrumentation?: AutoInstrumentationConfig;\n /**\n * Metric reader to attach to the NodeSDK so a single SDK owns both traces and\n * metrics (one global MeterProvider, same gRPC collector). When provided, NodeSDK\n * is the sole owner of metrics; when omitted, NodeSDK is opted out of metrics\n * entirely (see below) so it never registers a stray default MeterProvider.\n */\n metricReader?: IMetricReader;\n}\n\nlet sdk: NodeSDK | null = null;\nlet isInitialized = false;\n\nconst MONITORED_MODULES = ['pg', 'mongodb', 'ioredis', 'mysql2', 'express', '@grpc/grpc-js'];\n\nfunction warnPreloadedModules(): void {\n // require.resolve / require.cache are only available in CJS contexts\n if (typeof require === 'undefined' || typeof require.cache === 'undefined') return;\n\n for (const mod of MONITORED_MODULES) {\n try {\n const resolved = require.resolve(mod);\n if (require.cache[resolved]) {\n console.warn(\n `[neoiq-foundation] \"${mod}\" was imported before tracing init. ` +\n 'Auto-instrumentation may not work for this module. ' +\n 'Use node -r @ciq-dev/neoiq-foundation-node/bootstrap or call createFoundation() first.'\n );\n }\n } catch {\n // Module not installed — no warning needed\n }\n }\n}\n\n/** Initialize OpenTelemetry tracing */\nexport function setupTracing(options: TracingOptions): void {\n if (isInitialized) {\n console.warn('[neoiq-foundation] Tracing already initialized');\n return;\n }\n\n warnPreloadedModules();\n\n const {\n serviceName,\n serviceVersion,\n environment,\n endpoint = getDefaultOtelEndpoint(),\n autoInstrumentation = {},\n metricReader,\n } = options;\n\n const resource = resourceFromAttributes({\n [ATTR_SERVICE_NAME]: serviceName,\n [ATTR_SERVICE_VERSION]: serviceVersion,\n 'deployment.environment': environment,\n });\n\n const traceExporter = new OTLPTraceExporter({ url: endpoint });\n const instrumentationConfig = buildInstrumentationConfig(autoInstrumentation);\n\n // Foundation owns every OpenTelemetry signal explicitly. Left to its own devices,\n // NodeSDK auto-configures metrics AND logs exporters from env defaults — both OTLP\n // over HTTP to localhost:4318 — which nothing listens on, producing ECONNREFUSED\n // spam every export interval. We therefore configure each signal on NodeSDK directly:\n // - traces: the explicit gRPC exporter above.\n // - metrics: the explicit gRPC reader when provided (single SDK, single global\n // MeterProvider, same collector as traces). When metrics are disabled\n // and the deployer hasn't chosen an exporter, opt NodeSDK out via\n // OTEL_METRICS_EXPORTER=none (the only switch NodeSDK exposes for that).\n // - logs: foundation logs via pino, never OTLP. Passing an empty processor list\n // stops NodeSDK from falling back to its env-default localhost:4318 log\n // exporter (`logRecordProcessors: []` takes the explicit branch and skips\n // configureLoggerProviderFromEnv()).\n if (!metricReader && !process.env.OTEL_METRICS_EXPORTER) {\n process.env.OTEL_METRICS_EXPORTER = 'none';\n }\n\n sdk = new NodeSDK({\n resource,\n traceExporter,\n ...(metricReader ? { metricReaders: [metricReader] } : {}),\n logRecordProcessors: [],\n instrumentations: [getNodeAutoInstrumentations(instrumentationConfig)],\n });\n\n sdk.start();\n isInitialized = true;\n}\n\nfunction buildInstrumentationConfig(\n config: AutoInstrumentationConfig\n): Record<string, { enabled: boolean }> {\n const mapping: Record<string, string> = {\n http: '@opentelemetry/instrumentation-http',\n // No Fastify entry: auto-instrumentations-node dropped instrumentation-fastify\n // (it never supported Fastify v5). Fastify's HTTP server span comes from\n // instrumentation-http and is named with its route template in the foundation\n // Fastify plugin's onRequest hook — no framework instrumentation needed.\n express: '@opentelemetry/instrumentation-express',\n mongodb: '@opentelemetry/instrumentation-mongodb',\n pg: '@opentelemetry/instrumentation-pg',\n mysql: '@opentelemetry/instrumentation-mysql',\n redis: '@opentelemetry/instrumentation-redis',\n ioredis: '@opentelemetry/instrumentation-ioredis',\n grpc: '@opentelemetry/instrumentation-grpc',\n fs: '@opentelemetry/instrumentation-fs',\n dns: '@opentelemetry/instrumentation-dns',\n };\n\n const result: Record<string, { enabled: boolean }> = {};\n for (const [key, instrumentationName] of Object.entries(mapping)) {\n const userSetting = config[key as keyof AutoInstrumentationConfig];\n const defaultValue = key !== 'fs' && key !== 'dns';\n result[instrumentationName] = { enabled: userSetting ?? defaultValue };\n }\n return result;\n}\n\n/** Shutdown tracing gracefully */\nexport async function shutdownTracing(): Promise<void> {\n if (!sdk) return;\n try {\n await sdk.shutdown();\n isInitialized = false;\n sdk = null;\n } catch (error) {\n console.error('[neoiq-foundation] Error shutting down tracing:', error);\n throw error;\n }\n}\n\nexport function getTracer(name: string): Tracer {\n return trace.getTracer(name);\n}\n\nexport function getActiveSpan(): ReturnType<typeof trace.getActiveSpan> {\n return trace.getActiveSpan();\n}\n\nexport function getTraceContext(): { traceId?: string; spanId?: string } {\n const span = trace.getActiveSpan();\n if (!span) return {};\n const ctx = span.spanContext();\n return { traceId: ctx.traceId, spanId: ctx.spanId };\n}\n\nexport function isTracingEnabled(): boolean {\n return isInitialized;\n}\n\nexport { trace, context, propagation, SpanStatusCode };\nexport type { Tracer };\n"],"mappings":";;;;;;;;;;;;;;AAOA,MAAa,kCAAkC,EAC5C,OAAO;CACN,MAAM,EAAE,SAAS,CAAC,QAAQ,KAAK;CAC/B,SAAS,EAAE,SAAS,CAAC,QAAQ,KAAK;CAClC,SAAS,EAAE,SAAS,CAAC,QAAQ,KAAK;CAClC,SAAS,EAAE,SAAS,CAAC,QAAQ,KAAK;CAClC,IAAI,EAAE,SAAS,CAAC,QAAQ,KAAK;CAC7B,OAAO,EAAE,SAAS,CAAC,QAAQ,KAAK;CAChC,OAAO,EAAE,SAAS,CAAC,QAAQ,KAAK;CAChC,SAAS,EAAE,SAAS,CAAC,QAAQ,KAAK;CAClC,MAAM,EAAE,SAAS,CAAC,QAAQ,KAAK;CAC/B,IAAI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