Skip to content

Commit 5c90aec

Browse files
authored
feat(sdk-logs)!: do not read environment variables from window (#5472)
1 parent 5b988c8 commit 5c90aec

File tree

9 files changed

+120
-96
lines changed

9 files changed

+120
-96
lines changed

experimental/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ All notable changes to experimental packages in this project will be documented
1616
* chore!: Raise the minimum supported Node.js version to `^18.19.0 || >=20.6.0`. Support for Node.js 14, 16, and early minor versions of 18 and 20 have been dropped. This applies to all packages except the 'api' and 'semantic-conventions' packages. [#5395](https://github.com/open-telemetry/opentelemetry-js/issues/5395) @trentm
1717
* feat(sdk-node)!: use `IMetricReader` over `MetricReader` [#5311](https://github.com/open-telemetry/opentelemetry-js/pull/5311)
1818
* (user-facing): `NodeSDKConfiguration` now provides the more general `IMetricReader` type over `MetricReader`
19+
* feat(sdk-logs)!: do not read environment variables from window in browsers [#5472](https://github.com/open-telemetry/opentelemetry-js/pull/5472) @pichlermarc
20+
* (user-facing): all configuration previously possible via `window.OTEL_*` is now not supported anymore, please pass configuration options to constructors instead.
21+
* Note: Node.js environment variable configuration continues to work as-is.
1922

2023
### :rocket: (Enhancement)
2124

experimental/packages/sdk-logs/karma.conf.js

Lines changed: 4 additions & 2 deletions
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,
@@ -21,6 +21,8 @@ module.exports = config => {
2121
config.set(
2222
Object.assign({}, karmaBaseConfig, {
2323
webpack: karmaWebpackConfig,
24+
files: ['test/browser/index-webpack.ts'],
25+
preprocessors: { 'test/browser/index-webpack.ts': ['webpack'] },
2426
})
2527
);
2628
};

experimental/packages/sdk-logs/src/config.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,18 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {
18-
DEFAULT_ATTRIBUTE_COUNT_LIMIT,
19-
DEFAULT_ATTRIBUTE_VALUE_LENGTH_LIMIT,
20-
getEnv,
21-
getEnvWithoutDefaults,
22-
} from '@opentelemetry/core';
17+
import { getNumberFromEnv } from '@opentelemetry/core';
2318
import { LogRecordLimits } from './types';
2419

2520
export function loadDefaultConfig() {
2621
return {
2722
forceFlushTimeoutMillis: 30000,
2823
logRecordLimits: {
2924
attributeValueLengthLimit:
30-
getEnv().OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT,
31-
attributeCountLimit: getEnv().OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT,
25+
getNumberFromEnv('OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT') ??
26+
Infinity,
27+
attributeCountLimit:
28+
getNumberFromEnv('OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT') ?? 128,
3229
},
3330
includeTraceContext: true,
3431
};
@@ -42,24 +39,22 @@ export function loadDefaultConfig() {
4239
export function reconfigureLimits(
4340
logRecordLimits: LogRecordLimits
4441
): Required<LogRecordLimits> {
45-
const parsedEnvConfig = getEnvWithoutDefaults();
46-
4742
return {
4843
/**
4944
* Reassign log record attribute count limit to use first non null value defined by user or use default value
5045
*/
5146
attributeCountLimit:
5247
logRecordLimits.attributeCountLimit ??
53-
parsedEnvConfig.OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT ??
54-
parsedEnvConfig.OTEL_ATTRIBUTE_COUNT_LIMIT ??
55-
DEFAULT_ATTRIBUTE_COUNT_LIMIT,
48+
getNumberFromEnv('OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT') ??
49+
getNumberFromEnv('OTEL_ATTRIBUTE_COUNT_LIMIT') ??
50+
128,
5651
/**
5752
* Reassign log record attribute value length limit to use first non null value defined by user or use default value
5853
*/
5954
attributeValueLengthLimit:
6055
logRecordLimits.attributeValueLengthLimit ??
61-
parsedEnvConfig.OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT ??
62-
parsedEnvConfig.OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT ??
63-
DEFAULT_ATTRIBUTE_VALUE_LENGTH_LIMIT,
56+
getNumberFromEnv('OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT') ??
57+
getNumberFromEnv('OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT') ??
58+
Infinity,
6459
};
6560
}

experimental/packages/sdk-logs/src/export/BatchLogRecordProcessorBase.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
import type { ExportResult } from '@opentelemetry/core';
17+
import { ExportResult, getNumberFromEnv } from '@opentelemetry/core';
1818
import { diag } from '@opentelemetry/api';
1919
import {
2020
ExportResultCode,
21-
getEnv,
2221
globalErrorHandler,
2322
unrefTimer,
2423
BindOnceFuture,
@@ -47,14 +46,22 @@ export abstract class BatchLogRecordProcessorBase<T extends BufferConfig>
4746
private readonly _exporter: LogRecordExporter,
4847
config?: T
4948
) {
50-
const env = getEnv();
5149
this._maxExportBatchSize =
52-
config?.maxExportBatchSize ?? env.OTEL_BLRP_MAX_EXPORT_BATCH_SIZE;
53-
this._maxQueueSize = config?.maxQueueSize ?? env.OTEL_BLRP_MAX_QUEUE_SIZE;
50+
config?.maxExportBatchSize ??
51+
getNumberFromEnv('OTEL_BLRP_MAX_EXPORT_BATCH_SIZE') ??
52+
512;
53+
this._maxQueueSize =
54+
config?.maxQueueSize ??
55+
getNumberFromEnv('OTEL_BLRP_MAX_QUEUE_SIZE') ??
56+
2048;
5457
this._scheduledDelayMillis =
55-
config?.scheduledDelayMillis ?? env.OTEL_BLRP_SCHEDULE_DELAY;
58+
config?.scheduledDelayMillis ??
59+
getNumberFromEnv('OTEL_BLRP_SCHEDULE_DELAY') ??
60+
5000;
5661
this._exportTimeoutMillis =
57-
config?.exportTimeoutMillis ?? env.OTEL_BLRP_EXPORT_TIMEOUT;
62+
config?.exportTimeoutMillis ??
63+
getNumberFromEnv('OTEL_BLRP_EXPORT_TIMEOUT') ??
64+
30000;
5865

5966
this._shutdownOnce = new BindOnceFuture(this._shutdown, this);
6067

experimental/packages/sdk-logs/test/index-webpack.ts renamed to experimental/packages/sdk-logs/test/browser/index-webpack.ts

Lines changed: 7 additions & 4 deletions
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);

experimental/packages/sdk-logs/test/index-webpack.worker.ts renamed to experimental/packages/sdk-logs/test/browser/index-webpack.worker.ts

Lines changed: 7 additions & 4 deletions
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);

experimental/packages/sdk-logs/test/common/LoggerProvider.test.ts

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ import { MultiLogRecordProcessor } from '../../src/MultiLogRecordProcessor';
2929
import { Logger } from '../../src/Logger';
3030

3131
describe('LoggerProvider', () => {
32-
let envSource: Record<string, any>;
33-
34-
if (global.process?.versions?.node === undefined) {
35-
envSource = globalThis as unknown as Record<string, any>;
36-
} else {
37-
envSource = process.env as Record<string, any>;
38-
}
39-
4032
beforeEach(() => {
4133
// to avoid actually registering the LoggerProvider and leaking env to other tests
4234
sinon.stub(logs, 'setGlobalLoggerProvider');
@@ -134,21 +126,7 @@ describe('LoggerProvider', () => {
134126
});
135127
});
136128

137-
describe('when attribute value length limit is defined via env', () => {
138-
it('should have attribute value length limit as default of Infinity', () => {
139-
envSource.OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT = 'Infinity';
140-
const loggerProvider = new LoggerProvider();
141-
const logRecordLimits =
142-
loggerProvider['_sharedState'].logRecordLimits;
143-
assert.strictEqual(
144-
logRecordLimits.attributeValueLengthLimit,
145-
Infinity
146-
);
147-
delete envSource.OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT;
148-
});
149-
});
150-
151-
describe('when attribute value length limit is not defined via env', () => {
129+
describe('when attribute value length limit is not defined', () => {
152130
it('should use default value of Infinity', () => {
153131
const loggerProvider = new LoggerProvider();
154132
const logRecordLimits =
@@ -160,26 +138,7 @@ describe('LoggerProvider', () => {
160138
});
161139
});
162140

163-
describe('when attribute count limit is defined via env', () => {
164-
it('should have attribute count limits as defined in env', () => {
165-
envSource.OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT = '35';
166-
const loggerProvider = new LoggerProvider();
167-
const logRecordLimits =
168-
loggerProvider['_sharedState'].logRecordLimits;
169-
assert.strictEqual(logRecordLimits.attributeCountLimit, 35);
170-
delete envSource.OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT;
171-
});
172-
it('should have attribute count limit as default of 128', () => {
173-
envSource.OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT = '128';
174-
const loggerProvider = new LoggerProvider();
175-
const logRecordLimits =
176-
loggerProvider['_sharedState'].logRecordLimits;
177-
assert.strictEqual(logRecordLimits.attributeCountLimit, 128);
178-
delete envSource.OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT;
179-
});
180-
});
181-
182-
describe('when attribute count limit is not defined via env', () => {
141+
describe('when attribute count limit is not defined', () => {
183142
it('should use default value of 128', () => {
184143
const loggerProvider = new LoggerProvider();
185144
const logRecordLimits =

experimental/packages/sdk-logs/test/common/export/BatchLogRecordProcessor.test.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import * as assert from 'assert';
1818
import * as sinon from 'sinon';
1919
import {
2020
ExportResultCode,
21-
getEnv,
2221
loggingErrorHandler,
2322
setGlobalErrorHandler,
2423
} from '@opentelemetry/core';
@@ -117,26 +116,11 @@ describe('BatchLogRecordProcessorBase', () => {
117116
it('should create a BatchLogRecordProcessor instance with empty config', () => {
118117
const processor = new BatchLogRecordProcessor(exporter);
119118

120-
const {
121-
OTEL_BSP_MAX_EXPORT_BATCH_SIZE,
122-
OTEL_BSP_MAX_QUEUE_SIZE,
123-
OTEL_BSP_SCHEDULE_DELAY,
124-
OTEL_BSP_EXPORT_TIMEOUT,
125-
} = getEnv();
126119
assert.ok(processor instanceof BatchLogRecordProcessor);
127-
assert.strictEqual(
128-
processor['_maxExportBatchSize'],
129-
OTEL_BSP_MAX_EXPORT_BATCH_SIZE
130-
);
131-
assert.strictEqual(processor['_maxQueueSize'], OTEL_BSP_MAX_QUEUE_SIZE);
132-
assert.strictEqual(
133-
processor['_scheduledDelayMillis'],
134-
OTEL_BSP_SCHEDULE_DELAY
135-
);
136-
assert.strictEqual(
137-
processor['_exportTimeoutMillis'],
138-
OTEL_BSP_EXPORT_TIMEOUT
139-
);
120+
assert.strictEqual(processor['_maxExportBatchSize'], 512);
121+
assert.strictEqual(processor['_maxQueueSize'], 2048);
122+
assert.strictEqual(processor['_scheduledDelayMillis'], 5000);
123+
assert.strictEqual(processor['_exportTimeoutMillis'], 30000);
140124
processor.shutdown();
141125
});
142126

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import { logs } from '@opentelemetry/api-logs';
17+
import * as assert from 'assert';
18+
import * as sinon from 'sinon';
19+
20+
import { LoggerProvider } from '../../src';
21+
22+
describe('LoggerProvider - node', () => {
23+
beforeEach(() => {
24+
// to avoid actually registering the LoggerProvider and leaking env to other tests
25+
sinon.stub(logs, 'setGlobalLoggerProvider');
26+
});
27+
28+
afterEach(() => {
29+
sinon.restore();
30+
});
31+
32+
describe('constructor', () => {
33+
describe('logRecordLimits', () => {
34+
describe('when attribute value length limit is defined via env', () => {
35+
it('should have attribute value length limit as default of Infinity', () => {
36+
process.env.OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT = 'Infinity';
37+
const loggerProvider = new LoggerProvider();
38+
const logRecordLimits =
39+
loggerProvider['_sharedState'].logRecordLimits;
40+
assert.strictEqual(
41+
logRecordLimits.attributeValueLengthLimit,
42+
Infinity
43+
);
44+
delete process.env.OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT;
45+
});
46+
});
47+
48+
describe('when attribute count limit is defined via env', () => {
49+
it('should have attribute count limits as defined in env', () => {
50+
process.env.OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT = '35';
51+
const loggerProvider = new LoggerProvider();
52+
const logRecordLimits =
53+
loggerProvider['_sharedState'].logRecordLimits;
54+
assert.strictEqual(logRecordLimits.attributeCountLimit, 35);
55+
delete process.env.OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT;
56+
});
57+
it('should have attribute count limit as default of 128', () => {
58+
process.env.OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT = '128';
59+
const loggerProvider = new LoggerProvider();
60+
const logRecordLimits =
61+
loggerProvider['_sharedState'].logRecordLimits;
62+
assert.strictEqual(logRecordLimits.attributeCountLimit, 128);
63+
delete process.env.OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT;
64+
});
65+
});
66+
});
67+
});
68+
});

0 commit comments

Comments
 (0)