Skip to content

Commit

Permalink
Merge 5492ed1 into 1fbc828
Browse files Browse the repository at this point in the history
  • Loading branch information
ravindra-dyte authored Feb 24, 2023
2 parents 1fbc828 + 5492ed1 commit bcb6e2b
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 bcb6e2b

Please sign in to comment.