autotel
Version:
Write Once, Observe Anywhere
178 lines (173 loc) • 6 kB
JavaScript
;
var chunkYS6C2YJE_cjs = require('./chunk-YS6C2YJE.cjs');
var chunkVH77IPJN_cjs = require('./chunk-VH77IPJN.cjs');
var fs = require('fs');
var path = require('path');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
var path__default = /*#__PURE__*/_interopDefault(path);
function loadYamlParser() {
try {
const mod = chunkYS6C2YJE_cjs.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__default.default.resolve(envPath);
if (fs.existsSync(resolved)) return resolved;
console.warn(`[autotel] Config file not found: ${envPath}`);
return null;
}
const conventionPath = path__default.default.resolve(process.cwd(), "autotel.yaml");
if (fs.existsSync(conventionPath)) return conventionPath;
const altPath = path__default.default.resolve(process.cwd(), "autotel.yml");
if (fs.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 chunkVH77IPJN_cjs.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 chunkVH77IPJN_cjs.AlwaysSampler();
}
case "always_off": {
return new chunkVH77IPJN_cjs.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 chunkVH77IPJN_cjs.AdaptiveSampler();
}
return new chunkVH77IPJN_cjs.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 = fs.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__default.default.resolve(filePath);
const content = fs.readFileSync(resolved, "utf8");
const parseYaml = loadYamlParser();
const rawYaml = parseYaml(content);
const substituted = substituteEnvVarsDeep(rawYaml);
return yamlToAutotelConfig(substituted);
}
function hasYamlConfig() {
return findConfigFile() !== null;
}
exports.hasYamlConfig = hasYamlConfig;
exports.loadYamlConfig = loadYamlConfig;
exports.loadYamlConfigFromFile = loadYamlConfigFromFile;
//# sourceMappingURL=chunk-OC6X2VIN.cjs.map
//# sourceMappingURL=chunk-OC6X2VIN.cjs.map