-
Notifications
You must be signed in to change notification settings - Fork 986
feat(sdk-trace-base)!: drop ability to auto-instantiate propagators beyond defaults #5355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
64df121
c0abc8d
ffe6c84
fb662af
4072001
78aca13
6a4a5af
58ac3f0
eec8421
c5c5f43
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
… defaults
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
| * 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 { getPropgagatorFromEnv } from '../src/utils'; | ||
| import * as assert from 'assert'; | ||
| import * as sinon from 'sinon'; | ||
| import { diag } from '@opentelemetry/api'; | ||
|
|
||
| describe('getPropagatorFromEnv', function () { | ||
| afterEach(() => { | ||
| delete process.env.OTEL_PROPAGATORS; | ||
| sinon.restore(); | ||
| }); | ||
|
|
||
| describe('should default to undefined', function () { | ||
| it('when not defined', function () { | ||
| delete process.env.OTEL_PROPAGATORS; | ||
|
|
||
| const propagator = getPropgagatorFromEnv(); | ||
|
|
||
| assert.deepStrictEqual(propagator, undefined); | ||
| }); | ||
|
|
||
| it('on empty string', function () { | ||
| (process.env as any).OTEL_PROPAGATORS = ''; | ||
|
|
||
| const propagator = getPropgagatorFromEnv(); | ||
|
|
||
| assert.deepStrictEqual(propagator, undefined); | ||
| }); | ||
|
|
||
| it('on space-only string', function () { | ||
| (process.env as any).OTEL_PROPAGATORS = ' '; | ||
|
|
||
| const propagator = getPropgagatorFromEnv(); | ||
|
|
||
| assert.deepStrictEqual(propagator, undefined); | ||
| }); | ||
| }); | ||
|
|
||
| it('should return the selected propagator when one is in the list', () => { | ||
| process.env.OTEL_PROPAGATORS = 'tracecontext'; | ||
| assert.deepStrictEqual(getPropgagatorFromEnv()?.fields(), [ | ||
| 'traceparent', | ||
| 'tracestate', | ||
| ]); | ||
| }); | ||
|
|
||
| it('should return the selected propagators when multiple are in the list', () => { | ||
| process.env.OTEL_PROPAGATORS = 'b3,jaeger'; | ||
| assert.deepStrictEqual(getPropgagatorFromEnv()?.fields(), [ | ||
| 'b3', | ||
| 'uber-trace-id', | ||
| ]); | ||
| }); | ||
|
|
||
| it('should return the selected propagators when multiple are in the list', () => { | ||
| process.env.OTEL_PROPAGATORS = 'b3,jaeger'; | ||
| assert.deepStrictEqual(getPropgagatorFromEnv()?.fields(), [ | ||
| 'b3', | ||
| 'uber-trace-id', | ||
| ]); | ||
| }); | ||
|
|
||
| it('should return null and warn if propagators are unknown', () => { | ||
| const warnStub = sinon.stub(diag, 'warn'); | ||
|
|
||
| process.env.OTEL_PROPAGATORS = 'my, unknown, propagators'; | ||
| assert.deepStrictEqual(getPropgagatorFromEnv(), null); | ||
| sinon.assert.calledWithExactly( | ||
| warnStub, | ||
| 'Propagator "my" requested through environment variable is unavailable.' | ||
| ); | ||
| sinon.assert.calledWithExactly( | ||
| warnStub, | ||
| 'Propagator "unknown" requested through environment variable is unavailable.' | ||
| ); | ||
| sinon.assert.calledWithExactly( | ||
| warnStub, | ||
| 'Propagator "propagators" requested through environment variable is unavailable.' | ||
| ); | ||
| sinon.assert.calledThrice(warnStub); | ||
| }); | ||
|
|
||
| it('should return null if only "none" is selected', () => { | ||
| process.env.OTEL_PROPAGATORS = 'none'; | ||
|
|
||
| assert.deepStrictEqual(getPropgagatorFromEnv(), null); | ||
| }); | ||
| }); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,6 @@ | |
|
|
||
| import { | ||
| context, | ||
| diag, | ||
| propagation, | ||
| TextMapPropagator, | ||
| trace, | ||
|
|
@@ -26,7 +25,6 @@ import { | |
| CompositePropagator, | ||
| W3CBaggagePropagator, | ||
| W3CTraceContextPropagator, | ||
| getEnv, | ||
| merge, | ||
| } from '@opentelemetry/core'; | ||
| import { IResource, Resource } from '@opentelemetry/resources'; | ||
|
|
@@ -48,18 +46,14 @@ export enum ForceFlushState { | |
| 'unresolved', | ||
| } | ||
|
|
||
| function getDefaultPropagators(): TextMapPropagator[] { | ||
| return [new W3CTraceContextPropagator(), new W3CBaggagePropagator()]; | ||
| } | ||
|
|
||
| /** | ||
| * This class represents a basic tracer provider which platform libraries can extend | ||
| */ | ||
| export class BasicTracerProvider implements TracerProvider { | ||
| protected static readonly _registeredPropagators = new Map< | ||
| string, | ||
| PROPAGATOR_FACTORY | ||
| >([ | ||
| ['tracecontext', () => new W3CTraceContextPropagator()], | ||
| ['baggage', () => new W3CBaggagePropagator()], | ||
| ]); | ||
|
|
||
| private readonly _config: TracerConfig; | ||
| private readonly _tracers: Map<string, Tracer> = new Map(); | ||
| private readonly _resource: IResource; | ||
|
|
@@ -117,16 +111,19 @@ export class BasicTracerProvider implements TracerProvider { | |
| */ | ||
| register(config: SDKRegistrationConfig = {}): void { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing/replacing this will likely take 3 more PRs to make the change somewhat reviewable
But this PR is still an improvement for now as we can shed the imports of non-default propagators/context managers. |
||
| trace.setGlobalTracerProvider(this); | ||
| if (config.propagator === undefined) { | ||
| config.propagator = this._buildPropagatorFromEnv(); | ||
| } | ||
|
|
||
| if (config.contextManager) { | ||
| context.setGlobalContextManager(config.contextManager); | ||
| } | ||
|
|
||
| if (config.propagator) { | ||
| propagation.setGlobalPropagator(config.propagator); | ||
| // undefined means "unset", null means don't register propagator | ||
| if (config.propagator !== null) { | ||
| propagation.setGlobalPropagator( | ||
| config.propagator ?? | ||
| new CompositePropagator({ | ||
| propagators: getDefaultPropagators(), | ||
| }) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -182,54 +179,4 @@ export class BasicTracerProvider implements TracerProvider { | |
| shutdown(): Promise<void> { | ||
| return this._activeSpanProcessor.shutdown(); | ||
| } | ||
|
|
||
| /** | ||
| * TS cannot yet infer the type of this.constructor: | ||
| * https://github.com/Microsoft/TypeScript/issues/3841#issuecomment-337560146 | ||
| * There is no need to override either of the getters in your child class. | ||
| * The type of the registered component maps should be the same across all | ||
| * classes in the inheritance tree. | ||
| */ | ||
| protected _getPropagator(name: string): TextMapPropagator | undefined { | ||
| return ( | ||
| this.constructor as typeof BasicTracerProvider | ||
| )._registeredPropagators.get(name)?.(); | ||
| } | ||
|
|
||
| protected _buildPropagatorFromEnv(): TextMapPropagator | undefined { | ||
| // per spec, propagators from env must be deduplicated | ||
| const uniquePropagatorNames = Array.from( | ||
| new Set(getEnv().OTEL_PROPAGATORS) | ||
| ); | ||
|
|
||
| const propagators = uniquePropagatorNames.map(name => { | ||
| const propagator = this._getPropagator(name); | ||
| if (!propagator) { | ||
| diag.warn( | ||
| `Propagator "${name}" requested through environment variable is unavailable.` | ||
| ); | ||
| } | ||
|
|
||
| return propagator; | ||
| }); | ||
| const validPropagators = propagators.reduce<TextMapPropagator[]>( | ||
| (list, item) => { | ||
| if (item) { | ||
| list.push(item); | ||
| } | ||
| return list; | ||
| }, | ||
| [] | ||
| ); | ||
|
|
||
| if (validPropagators.length === 0) { | ||
| return; | ||
| } else if (uniquePropagatorNames.length === 1) { | ||
| return validPropagators[0]; | ||
| } else { | ||
| return new CompositePropagator({ | ||
| propagators: validPropagators, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.