Skip to content

Commit

Permalink
fix(core): added falsy check to make otel core work with browser wher…
Browse files Browse the repository at this point in the history
…e webpack config had process as false or null (#3617)

Co-authored-by: Chengzhong Wu <[email protected]>
Co-authored-by: Daniel Dyla <[email protected]>
  • Loading branch information
3 people authored Feb 27, 2023
1 parent 1fbc828 commit 7f63a6f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/

### :bug: (Bug Fix)

* fix(core): added falsy check to make otel core work with browser where webpack config had process as false or null [#3613](https://github.com/open-telemetry/opentelemetry-js/issues/3613) @ravindra-dyte

### :books: (Refine Doc)

### :house: (Internal)
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-core/src/utils/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ export function parseEnvironment(values: RAW_ENVIRONMENT): ENVIRONMENT {
* populating default values.
*/
export function getEnvWithoutDefaults(): ENVIRONMENT {
return typeof process !== 'undefined'
return typeof process !== 'undefined' && process && process.env
? parseEnvironment(process.env as RAW_ENVIRONMENT)
: parseEnvironment(_globalThis as typeof globalThis & RAW_ENVIRONMENT);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {
context,
propagation,
trace,
ProxyTracerProvider,
} from '@opentelemetry/api';
import { Resource } from '@opentelemetry/resources';
import { Tracer } from '@opentelemetry/sdk-trace-base';
import * as assert from 'assert';
import { StackContextManager, WebTracerProvider } from '../src';

describe('Node Globals Foolproofing', () => {
const originalProcess = globalThis?.process;
before(() => {
Object.assign(globalThis, { process: false });
});

after(() => {
Object.assign(globalThis, { process: originalProcess });
});

beforeEach(() => {
context.disable();
trace.disable();
propagation.disable();
});

it('Can get TraceProvider without node globals such as process', () => {
const propagator = propagation['_getGlobalPropagator']();
const tracerProvider = new WebTracerProvider();
tracerProvider.register({
propagator: null,
});

assert.strictEqual(
propagation['_getGlobalPropagator'](),
propagator,
'propagator should not change'
);

assert.ok(context['_getContextManager']() instanceof StackContextManager);
const apiTracerProvider = trace.getTracerProvider() as ProxyTracerProvider;
assert.ok(apiTracerProvider.getDelegate() === tracerProvider);
});

it('Can get TraceProvider with custom id generator and without node globals such as process', () => {
const getRandomString = (length: number) => {
const alphanumericsList = 'abcdefghijklmnopqrstuvwxyz0123456789'.split(
''
);
alphanumericsList.sort(() => 0.5 - Math.random());

return alphanumericsList.slice(0, length).join('');
};

const tracer = new WebTracerProvider({
resource: new Resource({
'service.name': 'web-core',
}),
idGenerator: {
generateTraceId: () => getRandomString(32),
generateSpanId: () => getRandomString(16),
},
}).getTracer('default');

assert.ok(tracer instanceof Tracer);
});
});

0 comments on commit 7f63a6f

Please sign in to comment.