UNPKG

spinal-obs-node

Version:

WithSpinal cost-aware OpenTelemetry SDK for Node.js

88 lines (87 loc) 3.27 kB
// src/runtime/config.ts import { diag, DiagLogLevel } from "@opentelemetry/api"; import path from "path"; var DefaultScrubber = class { sensitive = [ /password/i, /passwd/i, /secret/i, /api[._-]?key/i, /auth[._-]?token/i, /access[._-]?token/i, /private[._-]?key/i, /encryption[._-]?key/i, /bearer/i, /credential/i, /user[._-]?name/i, /first[._-]?name/i, /last[._-]?name/i, /email/i, /email[._-]?address/i, /phone[._-]?number/i, /ip[._-]?address/i ]; protected = [/^attributes$/i, /^spinal\./i]; scrubAttributes(attributes) { const out = {}; for (const [k, v] of Object.entries(attributes ?? {})) { if (this.sensitive.some((r) => r.test(k))) { out[k] = "[Scrubbed]"; } else if (Array.isArray(v)) { out[k] = v.map((x) => typeof x === "object" && x !== null ? this.scrubAttributes(x) : x); } else if (typeof v === "object" && v !== null) { out[k] = this.scrubAttributes(v); } else { out[k] = v; } } return out; } }; var globalConfig; function configure(opts = {}) { const endpoint = opts.endpoint ?? process.env.SPINAL_TRACING_ENDPOINT ?? "https://cloud.withspinal.com"; const apiKey = opts.apiKey ?? process.env.SPINAL_API_KEY ?? ""; const disableLocalMode = opts.disableLocalMode ?? process.env.SPINAL_DISABLE_LOCAL_MODE === "true"; const inferredMode = (opts.mode ?? process.env.SPINAL_MODE) || (apiKey ? "cloud" : "local"); const mode = disableLocalMode && !apiKey ? (() => { throw new Error("Cannot disable local mode without providing an API key for cloud mode"); })() : inferredMode; const headers = mode === "cloud" ? { ...opts.headers ?? {}, "X-SPINAL-API-KEY": apiKey } : { ...opts.headers ?? {} }; const timeoutMs = opts.timeoutMs ?? 5e3; const maxQueueSize = opts.maxQueueSize ?? parseInt(process.env.SPINAL_PROCESS_MAX_QUEUE_SIZE ?? "2048", 10); const maxExportBatchSize = opts.maxExportBatchSize ?? parseInt(process.env.SPINAL_PROCESS_MAX_EXPORT_BATCH_SIZE ?? "512", 10); const scheduleDelayMs = opts.scheduleDelayMs ?? parseInt(process.env.SPINAL_PROCESS_SCHEDULE_DELAY ?? "5000", 10); const exportTimeoutMs = opts.exportTimeoutMs ?? parseInt(process.env.SPINAL_PROCESS_EXPORT_TIMEOUT ?? "30000", 10); const scrubber = opts.scrubber ?? new DefaultScrubber(); const opentelemetryLogLevel = opts.opentelemetryLogLevel ?? DiagLogLevel.ERROR; const localStorePath = opts.localStorePath ?? process.env.SPINAL_LOCAL_STORE_PATH ?? path.join(process.cwd(), ".spinal", "spans.jsonl"); if (!endpoint) throw new Error("Spinal endpoint must be provided"); if (mode === "cloud" && !apiKey) throw new Error("No API key provided. Set SPINAL_API_KEY or pass to configure()."); diag.setLogger(console, opentelemetryLogLevel); globalConfig = { endpoint, apiKey, headers, timeoutMs, maxQueueSize, maxExportBatchSize, scheduleDelayMs, exportTimeoutMs, scrubber, opentelemetryLogLevel, mode, localStorePath, disableLocalMode }; return globalConfig; } function getConfig() { if (!globalConfig) return configure(); return globalConfig; } export { configure, getConfig }; //# sourceMappingURL=chunk-7N3YU2IZ.js.map