Skip to content

Commit b287a93

Browse files
committed
feat(sdk-trace-base)!: do not read environment variables from window
1 parent afcc711 commit b287a93

14 files changed

+331
-277
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
/*!
1+
/*
22
* Copyright The OpenTelemetry Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -17,8 +17,12 @@
1717
const karmaWebpackConfig = require('../../karma.webpack');
1818
const karmaBaseConfig = require('../../karma.base');
1919

20-
module.exports = (config) => {
21-
config.set(Object.assign({}, karmaBaseConfig, {
22-
webpack: karmaWebpackConfig,
23-
}))
20+
module.exports = config => {
21+
config.set(
22+
Object.assign({}, karmaBaseConfig, {
23+
webpack: karmaWebpackConfig,
24+
files: ['test/browser/index-webpack.ts'],
25+
preprocessors: { 'test/browser/index-webpack.ts': ['webpack'] },
26+
})
27+
);
2428
};
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
/*!
1+
/*
22
* Copyright The OpenTelemetry Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -17,8 +17,17 @@
1717
const karmaWebpackConfig = require('../../karma.webpack');
1818
const karmaBaseConfig = require('../../karma.worker');
1919

20-
module.exports = (config) => {
21-
config.set(Object.assign({}, karmaBaseConfig, {
22-
webpack: karmaWebpackConfig,
23-
}))
20+
module.exports = config => {
21+
config.set(
22+
Object.assign({}, karmaBaseConfig, {
23+
webpack: karmaWebpackConfig,
24+
files: [
25+
{
26+
pattern: 'test/browser/index-webpack.worker.ts',
27+
included: false,
28+
},
29+
],
30+
preprocessors: { 'test/browser/index-webpack.worker.ts': ['webpack'] },
31+
})
32+
);
2433
};

packages/opentelemetry-sdk-trace-base/src/config.ts

+27-41
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { diag } from '@opentelemetry/api';
18-
import { getEnv, ENVIRONMENT } from '@opentelemetry/core';
18+
import { getNumberFromEnv, getStringFromEnv } from '@opentelemetry/core';
1919
import { Sampler } from './Sampler';
2020
import { AlwaysOffSampler } from './sampler/AlwaysOffSampler';
2121
import { AlwaysOnSampler } from './sampler/AlwaysOnSampler';
@@ -44,35 +44,38 @@ const DEFAULT_RATIO = 1;
4444
// object needs to be wrapped in this function and called when needed otherwise
4545
// envs are parsed before tests are ran - causes tests using these envs to fail
4646
export function loadDefaultConfig() {
47-
const env = getEnv();
48-
4947
return {
50-
sampler: buildSamplerFromEnv(env),
48+
sampler: buildSamplerFromEnv(),
5149
forceFlushTimeoutMillis: 30000,
5250
generalLimits: {
53-
attributeValueLengthLimit: env.OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT,
54-
attributeCountLimit: env.OTEL_ATTRIBUTE_COUNT_LIMIT,
51+
attributeValueLengthLimit:
52+
getNumberFromEnv('OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT') ?? Infinity,
53+
attributeCountLimit:
54+
getNumberFromEnv('OTEL_ATTRIBUTE_COUNT_LIMIT') ?? 128,
5555
},
5656
spanLimits: {
57-
attributeValueLengthLimit: env.OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT,
58-
attributeCountLimit: env.OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT,
59-
linkCountLimit: env.OTEL_SPAN_LINK_COUNT_LIMIT,
60-
eventCountLimit: env.OTEL_SPAN_EVENT_COUNT_LIMIT,
57+
attributeValueLengthLimit:
58+
getNumberFromEnv('OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT') ?? Infinity,
59+
attributeCountLimit:
60+
getNumberFromEnv('OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT') ?? 128,
61+
linkCountLimit: getNumberFromEnv('OTEL_SPAN_LINK_COUNT_LIMIT') ?? 128,
62+
eventCountLimit: getNumberFromEnv('OTEL_SPAN_EVENT_COUNT_LIMIT') ?? 128,
6163
attributePerEventCountLimit:
62-
env.OTEL_SPAN_ATTRIBUTE_PER_EVENT_COUNT_LIMIT,
63-
attributePerLinkCountLimit: env.OTEL_SPAN_ATTRIBUTE_PER_LINK_COUNT_LIMIT,
64+
getNumberFromEnv('OTEL_SPAN_ATTRIBUTE_PER_EVENT_COUNT_LIMIT') ?? 128,
65+
attributePerLinkCountLimit:
66+
getNumberFromEnv('OTEL_SPAN_ATTRIBUTE_PER_LINK_COUNT_LIMIT') ?? 128,
6467
},
6568
};
6669
}
6770

6871
/**
6972
* Based on environment, builds a sampler, complies with specification.
70-
* @param environment optional, by default uses getEnv(), but allows passing a value to reuse parsed environment
7173
*/
72-
export function buildSamplerFromEnv(
73-
environment: Required<ENVIRONMENT> = getEnv()
74-
): Sampler {
75-
switch (environment.OTEL_TRACES_SAMPLER) {
74+
export function buildSamplerFromEnv(): Sampler {
75+
const sampler =
76+
getStringFromEnv('OTEL_TRACES_SAMPLER') ??
77+
TracesSamplerValues.ParentBasedAlwaysOn;
78+
switch (sampler) {
7679
case TracesSamplerValues.AlwaysOn:
7780
return new AlwaysOnSampler();
7881
case TracesSamplerValues.AlwaysOff:
@@ -86,48 +89,31 @@ export function buildSamplerFromEnv(
8689
root: new AlwaysOffSampler(),
8790
});
8891
case TracesSamplerValues.TraceIdRatio:
89-
return new TraceIdRatioBasedSampler(
90-
getSamplerProbabilityFromEnv(environment)
91-
);
92+
return new TraceIdRatioBasedSampler(getSamplerProbabilityFromEnv());
9293
case TracesSamplerValues.ParentBasedTraceIdRatio:
9394
return new ParentBasedSampler({
94-
root: new TraceIdRatioBasedSampler(
95-
getSamplerProbabilityFromEnv(environment)
96-
),
95+
root: new TraceIdRatioBasedSampler(getSamplerProbabilityFromEnv()),
9796
});
9897
default:
9998
diag.error(
100-
`OTEL_TRACES_SAMPLER value "${environment.OTEL_TRACES_SAMPLER} invalid, defaulting to ${FALLBACK_OTEL_TRACES_SAMPLER}".`
99+
`OTEL_TRACES_SAMPLER value "${sampler} invalid, defaulting to ${FALLBACK_OTEL_TRACES_SAMPLER}".`
101100
);
102101
return new AlwaysOnSampler();
103102
}
104103
}
105104

106-
function getSamplerProbabilityFromEnv(
107-
environment: Required<ENVIRONMENT>
108-
): number | undefined {
109-
if (
110-
environment.OTEL_TRACES_SAMPLER_ARG === undefined ||
111-
environment.OTEL_TRACES_SAMPLER_ARG === ''
112-
) {
105+
function getSamplerProbabilityFromEnv(): number | undefined {
106+
const probability = getNumberFromEnv('OTEL_TRACES_SAMPLER_ARG');
107+
if (probability == null) {
113108
diag.error(
114109
`OTEL_TRACES_SAMPLER_ARG is blank, defaulting to ${DEFAULT_RATIO}.`
115110
);
116111
return DEFAULT_RATIO;
117112
}
118113

119-
const probability = Number(environment.OTEL_TRACES_SAMPLER_ARG);
120-
121-
if (isNaN(probability)) {
122-
diag.error(
123-
`OTEL_TRACES_SAMPLER_ARG=${environment.OTEL_TRACES_SAMPLER_ARG} was given, but it is invalid, defaulting to ${DEFAULT_RATIO}.`
124-
);
125-
return DEFAULT_RATIO;
126-
}
127-
128114
if (probability < 0 || probability > 1) {
129115
diag.error(
130-
`OTEL_TRACES_SAMPLER_ARG=${environment.OTEL_TRACES_SAMPLER_ARG} was given, but it is out of range ([0..1]), defaulting to ${DEFAULT_RATIO}.`
116+
`OTEL_TRACES_SAMPLER_ARG=${probability} was given, but it is out of range ([0..1]), defaulting to ${DEFAULT_RATIO}.`
131117
);
132118
return DEFAULT_RATIO;
133119
}

packages/opentelemetry-sdk-trace-base/src/export/BatchSpanProcessorBase.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { context, Context, diag, TraceFlags } from '@opentelemetry/api';
1818
import {
1919
BindOnceFuture,
2020
ExportResultCode,
21-
getEnv,
21+
getNumberFromEnv,
2222
globalErrorHandler,
2323
suppressTracing,
2424
unrefTimer,
@@ -51,23 +51,22 @@ export abstract class BatchSpanProcessorBase<T extends BufferConfig>
5151
private readonly _exporter: SpanExporter,
5252
config?: T
5353
) {
54-
const env = getEnv();
5554
this._maxExportBatchSize =
5655
typeof config?.maxExportBatchSize === 'number'
5756
? config.maxExportBatchSize
58-
: env.OTEL_BSP_MAX_EXPORT_BATCH_SIZE;
57+
: (getNumberFromEnv('OTEL_BSP_MAX_EXPORT_BATCH_SIZE') ?? 512);
5958
this._maxQueueSize =
6059
typeof config?.maxQueueSize === 'number'
6160
? config.maxQueueSize
62-
: env.OTEL_BSP_MAX_QUEUE_SIZE;
61+
: (getNumberFromEnv('OTEL_BSP_MAX_QUEUE_SIZE') ?? 2048);
6362
this._scheduledDelayMillis =
6463
typeof config?.scheduledDelayMillis === 'number'
6564
? config.scheduledDelayMillis
66-
: env.OTEL_BSP_SCHEDULE_DELAY;
65+
: (getNumberFromEnv('OTEL_BSP_SCHEDULE_DELAY') ?? 5000);
6766
this._exportTimeoutMillis =
6867
typeof config?.exportTimeoutMillis === 'number'
6968
? config.exportTimeoutMillis
70-
: env.OTEL_BSP_EXPORT_TIMEOUT;
69+
: (getNumberFromEnv('OTEL_BSP_EXPORT_TIMEOUT') ?? 30000);
7170

7271
this._shutdownOnce = new BindOnceFuture(this._shutdown, this);
7372

packages/opentelemetry-sdk-trace-base/src/utility.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { SpanLimits, TracerConfig, GeneralLimits } from './types';
2020
import {
2121
DEFAULT_ATTRIBUTE_COUNT_LIMIT,
2222
DEFAULT_ATTRIBUTE_VALUE_LENGTH_LIMIT,
23-
getEnvWithoutDefaults,
23+
getNumberFromEnv,
2424
} from '@opentelemetry/core';
2525

2626
/**
@@ -68,16 +68,14 @@ export function mergeConfig(userConfig: TracerConfig): TracerConfig & {
6868
export function reconfigureLimits(userConfig: TracerConfig): TracerConfig {
6969
const spanLimits = Object.assign({}, userConfig.spanLimits);
7070

71-
const parsedEnvConfig = getEnvWithoutDefaults();
72-
7371
/**
7472
* Reassign span attribute count limit to use first non null value defined by user or use default value
7573
*/
7674
spanLimits.attributeCountLimit =
7775
userConfig.spanLimits?.attributeCountLimit ??
7876
userConfig.generalLimits?.attributeCountLimit ??
79-
parsedEnvConfig.OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT ??
80-
parsedEnvConfig.OTEL_ATTRIBUTE_COUNT_LIMIT ??
77+
getNumberFromEnv('OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT') ??
78+
getNumberFromEnv('OTEL_ATTRIBUTE_COUNT_LIMIT') ??
8179
DEFAULT_ATTRIBUTE_COUNT_LIMIT;
8280

8381
/**
@@ -86,8 +84,8 @@ export function reconfigureLimits(userConfig: TracerConfig): TracerConfig {
8684
spanLimits.attributeValueLengthLimit =
8785
userConfig.spanLimits?.attributeValueLengthLimit ??
8886
userConfig.generalLimits?.attributeValueLengthLimit ??
89-
parsedEnvConfig.OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT ??
90-
parsedEnvConfig.OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT ??
87+
getNumberFromEnv('OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT') ??
88+
getNumberFromEnv('OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT') ??
9189
DEFAULT_ATTRIBUTE_VALUE_LENGTH_LIMIT;
9290

9391
return Object.assign({}, userConfig, { spanLimits });

packages/opentelemetry-sdk-trace-base/test/index-webpack.ts renamed to packages/opentelemetry-sdk-trace-base/test/browser/index-webpack.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
const testsContext = require.context('../browser', true, /test$/);
17+
testsContext.keys().forEach(testsContext);
1618

17-
{
18-
const testsContext = require.context('./', true, /test$/);
19-
testsContext.keys().forEach(testsContext);
20-
}
19+
const testsContextCommon = require.context('../common', true, /test$/);
20+
testsContextCommon.keys().forEach(testsContextCommon);
21+
22+
const srcContext = require.context('.', true, /src$/);
23+
srcContext.keys().forEach(srcContext);

packages/opentelemetry-sdk-trace-base/test/index-webpack.worker.ts renamed to packages/opentelemetry-sdk-trace-base/test/browser/index-webpack.worker.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
const testsContext = require.context('../browser', true, /test$/);
17+
testsContext.keys().forEach(testsContext);
1618

17-
{
18-
const testsContext = require.context('./', true, /test$/);
19-
testsContext.keys().forEach(testsContext);
20-
}
19+
const testsContextCommon = require.context('../common', true, /test$/);
20+
testsContextCommon.keys().forEach(testsContextCommon);
21+
22+
const srcContext = require.context('.', true, /src$/);
23+
srcContext.keys().forEach(srcContext);

0 commit comments

Comments
 (0)