UNPKG

9.5 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.parseEnvironment = exports.DEFAULT_ENVIRONMENT = exports.DEFAULT_SPAN_ATTRIBUTE_PER_LINK_COUNT_LIMIT = exports.DEFAULT_SPAN_ATTRIBUTE_PER_EVENT_COUNT_LIMIT = exports.DEFAULT_ATTRIBUTE_COUNT_LIMIT = exports.DEFAULT_ATTRIBUTE_VALUE_LENGTH_LIMIT = void 0;
19const api_1 = require("@opentelemetry/api");
20const sampling_1 = require("./sampling");
21const DEFAULT_LIST_SEPARATOR = ',';
22/**
23 * Environment interface to define all names
24 */
25const ENVIRONMENT_BOOLEAN_KEYS = ['OTEL_SDK_DISABLED'];
26function isEnvVarABoolean(key) {
27 return (ENVIRONMENT_BOOLEAN_KEYS.indexOf(key) > -1);
28}
29const ENVIRONMENT_NUMBERS_KEYS = [
30 'OTEL_BSP_EXPORT_TIMEOUT',
31 'OTEL_BSP_MAX_EXPORT_BATCH_SIZE',
32 'OTEL_BSP_MAX_QUEUE_SIZE',
33 'OTEL_BSP_SCHEDULE_DELAY',
34 'OTEL_BLRP_EXPORT_TIMEOUT',
35 'OTEL_BLRP_MAX_EXPORT_BATCH_SIZE',
36 'OTEL_BLRP_MAX_QUEUE_SIZE',
37 'OTEL_BLRP_SCHEDULE_DELAY',
38 'OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT',
39 'OTEL_ATTRIBUTE_COUNT_LIMIT',
40 'OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT',
41 'OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT',
42 'OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT',
43 'OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT',
44 'OTEL_SPAN_EVENT_COUNT_LIMIT',
45 'OTEL_SPAN_LINK_COUNT_LIMIT',
46 'OTEL_SPAN_ATTRIBUTE_PER_EVENT_COUNT_LIMIT',
47 'OTEL_SPAN_ATTRIBUTE_PER_LINK_COUNT_LIMIT',
48 'OTEL_EXPORTER_OTLP_TIMEOUT',
49 'OTEL_EXPORTER_OTLP_TRACES_TIMEOUT',
50 'OTEL_EXPORTER_OTLP_METRICS_TIMEOUT',
51 'OTEL_EXPORTER_OTLP_LOGS_TIMEOUT',
52 'OTEL_EXPORTER_JAEGER_AGENT_PORT',
53];
54function isEnvVarANumber(key) {
55 return (ENVIRONMENT_NUMBERS_KEYS.indexOf(key) > -1);
56}
57const ENVIRONMENT_LISTS_KEYS = [
58 'OTEL_NO_PATCH_MODULES',
59 'OTEL_PROPAGATORS',
60];
61function isEnvVarAList(key) {
62 return ENVIRONMENT_LISTS_KEYS.indexOf(key) > -1;
63}
64exports.DEFAULT_ATTRIBUTE_VALUE_LENGTH_LIMIT = Infinity;
65exports.DEFAULT_ATTRIBUTE_COUNT_LIMIT = 128;
66exports.DEFAULT_SPAN_ATTRIBUTE_PER_EVENT_COUNT_LIMIT = 128;
67exports.DEFAULT_SPAN_ATTRIBUTE_PER_LINK_COUNT_LIMIT = 128;
68/**
69 * Default environment variables
70 */
71exports.DEFAULT_ENVIRONMENT = {
72 OTEL_SDK_DISABLED: false,
73 CONTAINER_NAME: '',
74 ECS_CONTAINER_METADATA_URI_V4: '',
75 ECS_CONTAINER_METADATA_URI: '',
76 HOSTNAME: '',
77 KUBERNETES_SERVICE_HOST: '',
78 NAMESPACE: '',
79 OTEL_BSP_EXPORT_TIMEOUT: 30000,
80 OTEL_BSP_MAX_EXPORT_BATCH_SIZE: 512,
81 OTEL_BSP_MAX_QUEUE_SIZE: 2048,
82 OTEL_BSP_SCHEDULE_DELAY: 5000,
83 OTEL_BLRP_EXPORT_TIMEOUT: 30000,
84 OTEL_BLRP_MAX_EXPORT_BATCH_SIZE: 512,
85 OTEL_BLRP_MAX_QUEUE_SIZE: 2048,
86 OTEL_BLRP_SCHEDULE_DELAY: 5000,
87 OTEL_EXPORTER_JAEGER_AGENT_HOST: '',
88 OTEL_EXPORTER_JAEGER_AGENT_PORT: 6832,
89 OTEL_EXPORTER_JAEGER_ENDPOINT: '',
90 OTEL_EXPORTER_JAEGER_PASSWORD: '',
91 OTEL_EXPORTER_JAEGER_USER: '',
92 OTEL_EXPORTER_OTLP_ENDPOINT: '',
93 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: '',
94 OTEL_EXPORTER_OTLP_METRICS_ENDPOINT: '',
95 OTEL_EXPORTER_OTLP_LOGS_ENDPOINT: '',
96 OTEL_EXPORTER_OTLP_HEADERS: '',
97 OTEL_EXPORTER_OTLP_TRACES_HEADERS: '',
98 OTEL_EXPORTER_OTLP_METRICS_HEADERS: '',
99 OTEL_EXPORTER_OTLP_LOGS_HEADERS: '',
100 OTEL_EXPORTER_OTLP_TIMEOUT: 10000,
101 OTEL_EXPORTER_OTLP_TRACES_TIMEOUT: 10000,
102 OTEL_EXPORTER_OTLP_METRICS_TIMEOUT: 10000,
103 OTEL_EXPORTER_OTLP_LOGS_TIMEOUT: 10000,
104 OTEL_EXPORTER_ZIPKIN_ENDPOINT: 'http://localhost:9411/api/v2/spans',
105 OTEL_LOG_LEVEL: api_1.DiagLogLevel.INFO,
106 OTEL_NO_PATCH_MODULES: [],
107 OTEL_PROPAGATORS: ['tracecontext', 'baggage'],
108 OTEL_RESOURCE_ATTRIBUTES: '',
109 OTEL_SERVICE_NAME: '',
110 OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT: exports.DEFAULT_ATTRIBUTE_VALUE_LENGTH_LIMIT,
111 OTEL_ATTRIBUTE_COUNT_LIMIT: exports.DEFAULT_ATTRIBUTE_COUNT_LIMIT,
112 OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT: exports.DEFAULT_ATTRIBUTE_VALUE_LENGTH_LIMIT,
113 OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT: exports.DEFAULT_ATTRIBUTE_COUNT_LIMIT,
114 OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT: exports.DEFAULT_ATTRIBUTE_VALUE_LENGTH_LIMIT,
115 OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT: exports.DEFAULT_ATTRIBUTE_COUNT_LIMIT,
116 OTEL_SPAN_EVENT_COUNT_LIMIT: 128,
117 OTEL_SPAN_LINK_COUNT_LIMIT: 128,
118 OTEL_SPAN_ATTRIBUTE_PER_EVENT_COUNT_LIMIT: exports.DEFAULT_SPAN_ATTRIBUTE_PER_EVENT_COUNT_LIMIT,
119 OTEL_SPAN_ATTRIBUTE_PER_LINK_COUNT_LIMIT: exports.DEFAULT_SPAN_ATTRIBUTE_PER_LINK_COUNT_LIMIT,
120 OTEL_TRACES_EXPORTER: '',
121 OTEL_TRACES_SAMPLER: sampling_1.TracesSamplerValues.ParentBasedAlwaysOn,
122 OTEL_TRACES_SAMPLER_ARG: '',
123 OTEL_LOGS_EXPORTER: '',
124 OTEL_EXPORTER_OTLP_INSECURE: '',
125 OTEL_EXPORTER_OTLP_TRACES_INSECURE: '',
126 OTEL_EXPORTER_OTLP_METRICS_INSECURE: '',
127 OTEL_EXPORTER_OTLP_LOGS_INSECURE: '',
128 OTEL_EXPORTER_OTLP_CERTIFICATE: '',
129 OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE: '',
130 OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE: '',
131 OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE: '',
132 OTEL_EXPORTER_OTLP_COMPRESSION: '',
133 OTEL_EXPORTER_OTLP_TRACES_COMPRESSION: '',
134 OTEL_EXPORTER_OTLP_METRICS_COMPRESSION: '',
135 OTEL_EXPORTER_OTLP_LOGS_COMPRESSION: '',
136 OTEL_EXPORTER_OTLP_CLIENT_KEY: '',
137 OTEL_EXPORTER_OTLP_TRACES_CLIENT_KEY: '',
138 OTEL_EXPORTER_OTLP_METRICS_CLIENT_KEY: '',
139 OTEL_EXPORTER_OTLP_LOGS_CLIENT_KEY: '',
140 OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE: '',
141 OTEL_EXPORTER_OTLP_TRACES_CLIENT_CERTIFICATE: '',
142 OTEL_EXPORTER_OTLP_METRICS_CLIENT_CERTIFICATE: '',
143 OTEL_EXPORTER_OTLP_LOGS_CLIENT_CERTIFICATE: '',
144 OTEL_EXPORTER_OTLP_PROTOCOL: 'http/protobuf',
145 OTEL_EXPORTER_OTLP_TRACES_PROTOCOL: 'http/protobuf',
146 OTEL_EXPORTER_OTLP_METRICS_PROTOCOL: 'http/protobuf',
147 OTEL_EXPORTER_OTLP_LOGS_PROTOCOL: 'http/protobuf',
148 OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE: 'cumulative',
149};
150/**
151 * @param key
152 * @param environment
153 * @param values
154 */
155function parseBoolean(key, environment, values) {
156 if (typeof values[key] === 'undefined') {
157 return;
158 }
159 const value = String(values[key]);
160 // support case-insensitive "true"
161 environment[key] = value.toLowerCase() === 'true';
162}
163/**
164 * Parses a variable as number with number validation
165 * @param name
166 * @param environment
167 * @param values
168 * @param min
169 * @param max
170 */
171function parseNumber(name, environment, values, min = -Infinity, max = Infinity) {
172 if (typeof values[name] !== 'undefined') {
173 const value = Number(values[name]);
174 if (!isNaN(value)) {
175 if (value < min) {
176 environment[name] = min;
177 }
178 else if (value > max) {
179 environment[name] = max;
180 }
181 else {
182 environment[name] = value;
183 }
184 }
185 }
186}
187/**
188 * Parses list-like strings from input into output.
189 * @param name
190 * @param environment
191 * @param values
192 * @param separator
193 */
194function parseStringList(name, output, input, separator = DEFAULT_LIST_SEPARATOR) {
195 const givenValue = input[name];
196 if (typeof givenValue === 'string') {
197 output[name] = givenValue.split(separator).map(v => v.trim());
198 }
199}
200// The support string -> DiagLogLevel mappings
201const logLevelMap = {
202 ALL: api_1.DiagLogLevel.ALL,
203 VERBOSE: api_1.DiagLogLevel.VERBOSE,
204 DEBUG: api_1.DiagLogLevel.DEBUG,
205 INFO: api_1.DiagLogLevel.INFO,
206 WARN: api_1.DiagLogLevel.WARN,
207 ERROR: api_1.DiagLogLevel.ERROR,
208 NONE: api_1.DiagLogLevel.NONE,
209};
210/**
211 * Environmentally sets log level if valid log level string is provided
212 * @param key
213 * @param environment
214 * @param values
215 */
216function setLogLevelFromEnv(key, environment, values) {
217 const value = values[key];
218 if (typeof value === 'string') {
219 const theLevel = logLevelMap[value.toUpperCase()];
220 if (theLevel != null) {
221 environment[key] = theLevel;
222 }
223 }
224}
225/**
226 * Parses environment values
227 * @param values
228 */
229function parseEnvironment(values) {
230 const environment = {};
231 for (const env in exports.DEFAULT_ENVIRONMENT) {
232 const key = env;
233 switch (key) {
234 case 'OTEL_LOG_LEVEL':
235 setLogLevelFromEnv(key, environment, values);
236 break;
237 default:
238 if (isEnvVarABoolean(key)) {
239 parseBoolean(key, environment, values);
240 }
241 else if (isEnvVarANumber(key)) {
242 parseNumber(key, environment, values);
243 }
244 else if (isEnvVarAList(key)) {
245 parseStringList(key, environment, values);
246 }
247 else {
248 const value = values[key];
249 if (typeof value !== 'undefined' && value !== null) {
250 environment[key] = String(value);
251 }
252 }
253 }
254 }
255 return environment;
256}
257exports.parseEnvironment = parseEnvironment;
258//# sourceMappingURL=environment.js.map
\No newline at end of file