UNPKG

@azure/monitor-opentelemetry

Version:
129 lines 5.23 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. Object.defineProperty(exports, "__esModule", { value: true }); exports.EnvConfig = void 0; const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base"); const index_js_1 = require("./logging/index.js"); const TRACES_SAMPLER = "OTEL_TRACES_SAMPLER"; const TRACES_SAMPLER_ARG = "OTEL_TRACES_SAMPLER_ARG"; const RATE_LIMITED_SAMPLER = "microsoft.rate_limited"; const FIXED_PERCENTAGE_SAMPLER = "microsoft.fixed_percentage"; const ALWAYS_ON_SAMPLER = "always_on"; const ALWAYS_OFF_SAMPLER = "always_off"; const TRACE_ID_RATIO_SAMPLER = "trace_id_ratio"; const PARENT_BASED_ALWAYS_ON_SAMPLER = "parentbased_always_on"; const PARENT_BASED_ALWAYS_OFF_SAMPLER = "parentbased_always_off"; const PARENT_BASED_TRACE_ID_RATIO_SAMPLER = "parentbased_trace_id_ratio"; const SUPPORTED_OTEL_SAMPLERS = [ RATE_LIMITED_SAMPLER, FIXED_PERCENTAGE_SAMPLER, ALWAYS_ON_SAMPLER, ALWAYS_OFF_SAMPLER, TRACE_ID_RATIO_SAMPLER, PARENT_BASED_ALWAYS_ON_SAMPLER, PARENT_BASED_ALWAYS_OFF_SAMPLER, PARENT_BASED_TRACE_ID_RATIO_SAMPLER, ]; /** * Azure Monitor OpenTelemetry Client Configuration through Env variables * @internal */ class EnvConfig { /** The rate of telemetry items tracked that should be transmitted (Default 1.0) */ samplingRatio; /** The maximum number of spans to sample per second. (Default undefined)*/ tracesPerSecond; /** Custom OpenTelemetry sampler derived from env configuration */ sampler; static instance; /** Get Singleton instance */ static getInstance() { if (!EnvConfig.instance) { EnvConfig.instance = new EnvConfig(); } return EnvConfig.instance; } /** * Initializes a new instance of the EnvConfig class. */ constructor() { const envSampler = process.env[TRACES_SAMPLER]?.trim().toLowerCase(); const envSamplerArg = process.env[TRACES_SAMPLER_ARG]?.trim(); if (!envSampler) { return; } if (envSampler === RATE_LIMITED_SAMPLER || envSampler === FIXED_PERCENTAGE_SAMPLER) { this._applyMicrosoftSampler(envSampler, envSamplerArg); return; } this._applyOtelSampler(envSampler, envSamplerArg); } _applyMicrosoftSampler(envSampler, envSamplerArg) { if (envSamplerArg === undefined) { return; } const argValue = Number(envSamplerArg); if (isNaN(argValue)) { index_js_1.Logger.getInstance().warn("Invalid value for OTEL_TRACES_SAMPLER_ARG. It must be a number."); return; } if (envSampler === RATE_LIMITED_SAMPLER) { this.tracesPerSecond = argValue; } else { this.samplingRatio = argValue; } } _applyOtelSampler(envSampler, envSamplerArg) { switch (envSampler) { case ALWAYS_ON_SAMPLER: this.sampler = new sdk_trace_base_1.AlwaysOnSampler(); this.samplingRatio = 1; return; case ALWAYS_OFF_SAMPLER: this.sampler = new sdk_trace_base_1.AlwaysOffSampler(); this.samplingRatio = 0; return; case TRACE_ID_RATIO_SAMPLER: { const ratio = this._parseProbability(envSamplerArg); this.sampler = new sdk_trace_base_1.TraceIdRatioBasedSampler(ratio); this.samplingRatio = ratio; return; } case PARENT_BASED_ALWAYS_ON_SAMPLER: this.sampler = new sdk_trace_base_1.ParentBasedSampler({ root: new sdk_trace_base_1.AlwaysOnSampler() }); this.samplingRatio = 1; return; case PARENT_BASED_ALWAYS_OFF_SAMPLER: this.sampler = new sdk_trace_base_1.ParentBasedSampler({ root: new sdk_trace_base_1.AlwaysOffSampler() }); this.samplingRatio = 0; return; case PARENT_BASED_TRACE_ID_RATIO_SAMPLER: { const ratio = this._parseProbability(envSamplerArg); this.sampler = new sdk_trace_base_1.ParentBasedSampler({ root: new sdk_trace_base_1.TraceIdRatioBasedSampler(ratio) }); this.samplingRatio = ratio; return; } default: index_js_1.Logger.getInstance().warn(`Unsupported value for OTEL_TRACES_SAMPLER: ${envSampler}. Supported values are: ${SUPPORTED_OTEL_SAMPLERS.join(", ")}.`); } } _parseProbability(arg) { if (arg === undefined) { return 1; } if (arg === "") { index_js_1.Logger.getInstance().warn("Invalid value for OTEL_TRACES_SAMPLER_ARG. It should be a number in the range [0,1]."); return 1; } const parsed = Number(arg); if (isNaN(parsed) || parsed < 0 || parsed > 1) { index_js_1.Logger.getInstance().warn("Invalid value for OTEL_TRACES_SAMPLER_ARG. It should be a number in the range [0,1]."); return 1; } return parsed; } } exports.EnvConfig = EnvConfig; //# sourceMappingURL=envConfig.js.map