UNPKG

autotel

Version:
170 lines (168 loc) 5.64 kB
import { requireModule } from './chunk-33WTKH7X.js'; import { AdaptiveSampler, RandomSampler, NeverSampler, AlwaysSampler } from './chunk-DPSA4QLA.js'; import { readFileSync, existsSync } from 'fs'; import path from 'path'; function loadYamlParser() { try { const mod = requireModule("yaml"); return mod.parse; } catch { throw new Error("YAML parser not found. Install with: pnpm add yaml"); } } var ENV_VAR_PATTERN = /\$\{env:([A-Za-z_][A-Za-z0-9_]*)(?::-([^}]*))?\}/g; function substituteEnvVars(value) { return value.replaceAll( ENV_VAR_PATTERN, (_match, varName, defaultValue) => { const envValue = process.env[varName]; if (envValue !== void 0) return envValue; if (defaultValue !== void 0) return defaultValue; console.warn( `[autotel] Environment variable ${varName} not set and no default provided` ); return ""; } ); } function substituteEnvVarsDeep(obj) { if (typeof obj === "string") { return substituteEnvVars(obj); } if (Array.isArray(obj)) { return obj.map((item) => substituteEnvVarsDeep(item)); } if (obj && typeof obj === "object") { const result = {}; for (const [key, value] of Object.entries(obj)) { result[key] = substituteEnvVarsDeep(value); } return result; } return obj; } function findConfigFile() { const envPath = process.env.AUTOTEL_CONFIG_FILE; if (envPath) { const resolved = path.resolve(envPath); if (existsSync(resolved)) return resolved; console.warn(`[autotel] Config file not found: ${envPath}`); return null; } const conventionPath = path.resolve(process.cwd(), "autotel.yaml"); if (existsSync(conventionPath)) return conventionPath; const altPath = path.resolve(process.cwd(), "autotel.yml"); if (existsSync(altPath)) return altPath; return null; } function yamlToAutotelConfig(yaml) { const config = {}; if (yaml.service?.name) config.service = yaml.service.name; if (yaml.service?.version) config.version = yaml.service.version; if (yaml.service?.environment) config.environment = yaml.service.environment; if (yaml.exporter?.endpoint) config.endpoint = yaml.exporter.endpoint; if (yaml.exporter?.protocol) config.protocol = yaml.exporter.protocol; if (yaml.exporter?.headers) config.headers = yaml.exporter.headers; if (yaml.resource) config.resourceAttributes = yaml.resource; if (yaml.autoInstrumentations) config.autoInstrumentations = yaml.autoInstrumentations; if (yaml.debug !== void 0) config.debug = yaml.debug; if (yaml.sampling?.preset) { warnOnIgnoredPresetOverrides(yaml.sampling); config.sampling = yaml.sampling.preset; } else { const sampler = createSamplerFromYaml(yaml.sampling); if (sampler) config.sampler = sampler; } return config; } function createSamplerFromYaml(sampling) { if (!sampling) return void 0; if (sampling.preset) return void 0; const type = sampling.type ?? "adaptive"; try { switch (type) { case "adaptive": { return new AdaptiveSampler({ baselineSampleRate: sampling.baseline_rate, alwaysSampleErrors: sampling.always_sample_errors, alwaysSampleSlow: sampling.always_sample_slow, slowThresholdMs: sampling.slow_threshold_ms }); } case "always_on": { return new AlwaysSampler(); } case "always_off": { return new NeverSampler(); } case "ratio": { if (sampling.ratio === void 0) { console.warn( "[autotel] sampling.ratio missing in YAML sampling config. Falling back to adaptive sampler." ); return new AdaptiveSampler(); } return new RandomSampler(sampling.ratio); } default: { console.warn( `[autotel] Unknown sampling type "${type}" in YAML config. Falling back to defaults.` ); return void 0; } } } catch (error) { console.warn( `[autotel] Failed to configure sampling from YAML: ${error instanceof Error ? error.message : String(error)}` ); return void 0; } } function warnOnIgnoredPresetOverrides(sampling) { const ignoredFields = [ "type", "ratio", "baseline_rate", "always_sample_errors", "always_sample_slow", "slow_threshold_ms" ].filter((field) => sampling[field] !== void 0); if (ignoredFields.length === 0) { return; } console.warn( `[autotel] sampling.preset="${sampling.preset}" ignores these YAML fields: ${ignoredFields.join(", ")}. Use the programmatic API with sampler or samplingPresets.*(...) for tuned presets.` ); } function loadYamlConfig() { const filePath = findConfigFile(); if (!filePath) return null; try { const content = readFileSync(filePath, "utf8"); const parseYaml = loadYamlParser(); const rawYaml = parseYaml(content); const substituted = substituteEnvVarsDeep(rawYaml); return yamlToAutotelConfig(substituted); } catch (error) { console.error( `[autotel] Failed to load YAML config from ${filePath}:`, error ); return null; } } function loadYamlConfigFromFile(filePath) { const resolved = path.resolve(filePath); const content = readFileSync(resolved, "utf8"); const parseYaml = loadYamlParser(); const rawYaml = parseYaml(content); const substituted = substituteEnvVarsDeep(rawYaml); return yamlToAutotelConfig(substituted); } function hasYamlConfig() { return findConfigFile() !== null; } export { hasYamlConfig, loadYamlConfig, loadYamlConfigFromFile }; //# sourceMappingURL=chunk-3SDILILG.js.map //# sourceMappingURL=chunk-3SDILILG.js.map