Skip to content

Commit

Permalink
feat(resources)!: do not read environment variables from window (#5466)
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc authored Feb 13, 2025
1 parent 3c65b56 commit 60f2ce9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 71 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ For semantic convention package changes, see the [semconv CHANGELOG](packages/se
* Renames `IResource` interface to `Resource`
* Export function `resourceFromAttributes` to create a `Resource` from a `DetectedAttributes` object
* Only export types and functions. This aids in cross-version compatibility and makes it more easily extensible in the future.
* feat(resources)!: do not read environment variables from window in browsers [#5466](https://github.com/open-telemetry/opentelemetry-js/pull/5466) @pichlermarc
* (user-facing): all configuration previously possible via `window.OTEL_*` is now not supported anymore
* If you have been using the `envDetector` in browser environments, please migrate to manually creating a resource.
* Note: Node.js environment variable configuration continues to work as-is.

### :rocket: (Enhancement)

Expand Down
7 changes: 3 additions & 4 deletions packages/opentelemetry-resources/src/detectors/EnvDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/

import { Attributes, diag } from '@opentelemetry/api';
import { getEnv } from '@opentelemetry/core';
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
import { ResourceDetectionConfig } from '../config';
import { DetectedResource, ResourceDetector } from '../types';
import { getStringFromEnv } from '@opentelemetry/core';

/**
* EnvDetector can be used to detect the presence of and create a Resource
Expand Down Expand Up @@ -53,10 +53,9 @@ class EnvDetector implements ResourceDetector {
*/
detect(_config?: ResourceDetectionConfig): DetectedResource {
const attributes: Attributes = {};
const env = getEnv();

const rawAttributes = env.OTEL_RESOURCE_ATTRIBUTES;
const serviceName = env.OTEL_SERVICE_NAME;
const rawAttributes = getStringFromEnv('OTEL_RESOURCE_ATTRIBUTES');
const serviceName = getStringFromEnv('OTEL_SERVICE_NAME');

if (rawAttributes) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,76 +14,13 @@
* limitations under the License.
*/

import { RAW_ENVIRONMENT } from '@opentelemetry/core';
import * as assert from 'assert';
import { envDetector } from '../../../src';
import { describeBrowser } from '../../util';
import {
assertEmptyResource,
assertWebEngineResource,
} from '../../util/resource-assertions';
import { assertEmptyResource } from '../../util/resource-assertions';

describeBrowser('envDetector() on web browser', () => {
describe('with valid env', () => {
before(() => {
(
globalThis as typeof globalThis & RAW_ENVIRONMENT
).OTEL_RESOURCE_ATTRIBUTES =
'webengine.name="chromium",webengine.version="99",webengine.description="Chromium",custom.key="custom%20value"';
});

after(() => {
delete (globalThis as typeof globalThis & RAW_ENVIRONMENT)
.OTEL_RESOURCE_ATTRIBUTES;
});

it('should return resource information from environment variable', async () => {
const resource = envDetector.detect();
assert.ok(resource.attributes);
assertWebEngineResource(resource, {
name: 'chromium',
version: '99',
description: 'Chromium',
});
assert.strictEqual(resource.attributes['custom.key'], 'custom value');
});
});

describe('with invalid env', () => {
const values = ['webengine.description="with spaces"'];

for (const value of values) {
describe(`value: '${value}'`, () => {
before(() => {
(
globalThis as typeof globalThis & RAW_ENVIRONMENT
).OTEL_RESOURCE_ATTRIBUTES = value;
});

after(() => {
delete (globalThis as typeof globalThis & RAW_ENVIRONMENT)
.OTEL_RESOURCE_ATTRIBUTES;
});

it('should return empty resource', async () => {
const resource = envDetector.detect();
assertEmptyResource(resource);
});
});
}
});

describe('with empty env', () => {
it('should return empty resource', async () => {
const resource = envDetector.detect();
assertEmptyResource(resource);
});
});

describe('with empty env', () => {
it('should return empty resource', async () => {
const resource = envDetector.detect();
assertEmptyResource(resource);
});
it('should return empty resource', async () => {
const resource = envDetector.detect();
assertEmptyResource(resource);
});
});

0 comments on commit 60f2ce9

Please sign in to comment.