UNPKG

3.91 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright The OpenTelemetry Authors
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.buildSamplerFromEnv = exports.DEFAULT_CONFIG = void 0;
19const api_1 = require("@opentelemetry/api");
20const core_1 = require("@opentelemetry/core");
21const env = core_1.getEnv();
22const FALLBACK_OTEL_TRACES_SAMPLER = core_1.TracesSamplerValues.AlwaysOn;
23/**
24 * Default configuration. For fields with primitive values, any user-provided
25 * value will override the corresponding default value. For fields with
26 * non-primitive values (like `spanLimits`), the user-provided value will be
27 * used to extend the default value.
28 */
29exports.DEFAULT_CONFIG = {
30 sampler: buildSamplerFromEnv(env),
31 forceFlushTimeoutMillis: 30000,
32 spanLimits: {
33 attributeCountLimit: core_1.getEnv().OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT,
34 linkCountLimit: core_1.getEnv().OTEL_SPAN_LINK_COUNT_LIMIT,
35 eventCountLimit: core_1.getEnv().OTEL_SPAN_EVENT_COUNT_LIMIT,
36 },
37};
38/**
39 * Based on environment, builds a sampler, complies with specification.
40 * @param env optional, by default uses getEnv(), but allows passing a value to reuse parsed environment
41 */
42function buildSamplerFromEnv(env = core_1.getEnv()) {
43 switch (env.OTEL_TRACES_SAMPLER) {
44 case core_1.TracesSamplerValues.AlwaysOn:
45 return new core_1.AlwaysOnSampler();
46 case core_1.TracesSamplerValues.AlwaysOff:
47 return new core_1.AlwaysOffSampler();
48 case core_1.TracesSamplerValues.ParentBasedAlwaysOn:
49 return new core_1.ParentBasedSampler({
50 root: new core_1.AlwaysOnSampler(),
51 });
52 case core_1.TracesSamplerValues.ParentBasedAlwaysOff:
53 return new core_1.ParentBasedSampler({
54 root: new core_1.AlwaysOffSampler(),
55 });
56 case core_1.TracesSamplerValues.TraceIdRatio:
57 return new core_1.TraceIdRatioBasedSampler(getSamplerProbabilityFromEnv(env));
58 case core_1.TracesSamplerValues.ParentBasedTraceIdRatio:
59 return new core_1.ParentBasedSampler({
60 root: new core_1.TraceIdRatioBasedSampler(getSamplerProbabilityFromEnv(env)),
61 });
62 default:
63 api_1.diag.error(`OTEL_TRACES_SAMPLER value "${env.OTEL_TRACES_SAMPLER} invalid, defaulting to ${FALLBACK_OTEL_TRACES_SAMPLER}".`);
64 return new core_1.AlwaysOnSampler();
65 }
66}
67exports.buildSamplerFromEnv = buildSamplerFromEnv;
68const DEFAULT_RATIO = 1;
69function getSamplerProbabilityFromEnv(env) {
70 if (env.OTEL_TRACES_SAMPLER_ARG === undefined ||
71 env.OTEL_TRACES_SAMPLER_ARG === '') {
72 api_1.diag.error(`OTEL_TRACES_SAMPLER_ARG is blank, defaulting to ${DEFAULT_RATIO}.`);
73 return DEFAULT_RATIO;
74 }
75 const probability = Number(env.OTEL_TRACES_SAMPLER_ARG);
76 if (isNaN(probability)) {
77 api_1.diag.error(`OTEL_TRACES_SAMPLER_ARG=${env.OTEL_TRACES_SAMPLER_ARG} was given, but it is invalid, defaulting to ${DEFAULT_RATIO}.`);
78 return DEFAULT_RATIO;
79 }
80 if (probability < 0 || probability > 1) {
81 api_1.diag.error(`OTEL_TRACES_SAMPLER_ARG=${env.OTEL_TRACES_SAMPLER_ARG} was given, but it is out of range ([0..1]), defaulting to ${DEFAULT_RATIO}.`);
82 return DEFAULT_RATIO;
83 }
84 return probability;
85}
86//# sourceMappingURL=config.js.map
\No newline at end of file