Repository navigation
fix(spanner): ensure OpenTelemetry context manager on cached tracing path #9728
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 all commits
0e0e49c
a86787a
ce9c359
d7483de
a868341
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,7 @@ | |
| context, | ||
| trace, | ||
| INVALID_SPAN_CONTEXT, | ||
| ROOT_CONTEXT, | ||
| SpanAttributes, | ||
| TimeInput, | ||
| TracerProvider, | ||
|
|
@@ -115,7 +115,7 @@ | |
| return {}; | ||
| } | ||
|
|
||
| const SPAN_NAMESPACE_PREFIX = 'CloudSpanner'; // TODO: discuss & standardize this prefix. | ||
| const SPAN_NAMESPACE_PREFIX = 'CloudSpanner'; // TODO(https://github.com/googleapis/google-cloud-node/issues/9610): discuss & standardize this prefix. | ||
| export { | ||
| SPAN_NAMESPACE_PREFIX, | ||
| traceConfig, | ||
|
|
@@ -145,6 +145,15 @@ | |
| * was called. | ||
| */ | ||
| function ensureInitialContextManagerSet() { | ||
| const currentContextManager = ( | ||
| context as unknown as {_getContextManager?: () => ContextManager} | ||
| )._getContextManager?.(); | ||
| if ( | ||
| currentContextManager && | ||
| currentContextManager.constructor?.name !== 'NoopContextManager' | ||
| ) { | ||
| return; | ||
| } | ||
| if (!defaultContextManager) { | ||
| const ContextManagerClass = | ||
| AsyncLocalStorageContextManager || AsyncHooksContextManager; | ||
|
|
@@ -169,6 +178,7 @@ | |
| */ | ||
| function isGlobalTracingEnabled(): boolean { | ||
| if (globalTracingEnabled === true) { | ||
| ensureInitialContextManagerSet(); | ||
|
Contributor
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. Calling
We can update function ensureInitialContextManagerSet() {
const currentContextManager = (
context as unknown as {_getContextManager?: () => ContextManager}
)._getContextManager?.();
if (
currentContextManager &&
currentContextManager.constructor?.name !== 'NoopContextManager'
) {
return;
}
if (!defaultContextManager) {
const ContextManagerClass =
AsyncLocalStorageContextManager || AsyncHooksContextManager;
defaultContextManager = new ContextManagerClass();
}
if (context.setGlobalContextManager(defaultContextManager!)) {
defaultContextManager!.enable();
}
}
Contributor
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. Great catch, thank you! Updated |
||
| return true; | ||
| } | ||
| const now = Date.now(); | ||
|
|
@@ -183,8 +193,8 @@ | |
| const globalProvider = trace.getTracerProvider(); | ||
| if (globalProvider) { | ||
| let delegate = globalProvider; | ||
| if (typeof (globalProvider as any).getDelegate === 'function') { | ||
| delegate = (globalProvider as any).getDelegate(); | ||
| } | ||
| if (delegate) { | ||
| const name = delegate.constructor.name; | ||
|
|
@@ -374,23 +384,23 @@ | |
| return INVALID_SPAN_CONTEXT; | ||
| } | ||
|
|
||
| setAttribute(key: string, value: unknown): this { | ||
|
Check warning on line 387 in handwritten/spanner/src/instrument.ts
|
||
| return this; | ||
| } | ||
|
|
||
| setAttributes(attributes: SpanAttributes): this { | ||
| return this; | ||
| } | ||
|
|
||
| addEvent(name: string, attributes?: SpanAttributes): this { | ||
|
Check warning on line 395 in handwritten/spanner/src/instrument.ts
|
||
| return this; | ||
| } | ||
|
|
||
| addLink(link: Link): this { | ||
| return this; | ||
| } | ||
|
|
||
| addLinks(links: Link[]): this { | ||
| return this; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,9 +15,11 @@ | |
| */ | ||
|
|
||
| import * as assert from 'assert'; | ||
| import {context} from '@opentelemetry/api'; | ||
| import {Span, TracerProvider, context, diag, trace} from '@opentelemetry/api'; | ||
| import {describe, it} from 'mocha'; | ||
| import * as sinon from 'sinon'; | ||
| import { | ||
| _resetTracingEnabledForTest, | ||
| ensureInitialContextManagerSet, | ||
| getActiveOrNoopSpan, | ||
| getQueryTraceConfig, | ||
|
|
@@ -115,40 +117,168 @@ describe('instrument', () => { | |
| setAttribute: () => fakeSpan, | ||
| setAttributes: () => fakeSpan, | ||
| addEvent: () => fakeSpan, | ||
| addLink: () => fakeSpan, | ||
| addLinks: () => fakeSpan, | ||
| setStatus: () => fakeSpan, | ||
| updateName: () => fakeSpan, | ||
| end: () => {}, | ||
| isRecording: () => true, | ||
| recordException: () => {}, | ||
| }; | ||
| return context.with(traceSetSpan(context.active(), fakeSpan), () => | ||
| return context.with(trace.setSpan(context.active(), fakeSpan), () => | ||
| fn(fakeSpan), | ||
| ); | ||
| }, | ||
| }), | ||
| }; | ||
|
|
||
| const {trace} = require('@opentelemetry/api'); | ||
| function traceSetSpan(ctx: unknown, span: unknown) { | ||
| return trace.setSpan(ctx, span); | ||
| try { | ||
| let activeSpanAfterAwait: Span | undefined; | ||
| await startTrace( | ||
| 'Test.asyncSpan', | ||
| { | ||
| opts: { | ||
| tracerProvider: fakeTracerProvider as unknown as TracerProvider, | ||
| }, | ||
| }, | ||
| async span => { | ||
| await Promise.resolve(); | ||
| activeSpanAfterAwait = getActiveOrNoopSpan(); | ||
| span.end(); | ||
| }, | ||
| ); | ||
|
|
||
| assert.strictEqual( | ||
| activeSpanAfterAwait?.spanContext().spanId, | ||
| '2'.repeat(16), | ||
| ); | ||
| } finally { | ||
| ensureInitialContextManagerSet(); | ||
| } | ||
| }); | ||
|
|
||
| let activeSpanAfterAwait: unknown; | ||
| await startTrace( | ||
| 'Test.asyncSpan', | ||
| {opts: {tracerProvider: fakeTracerProvider as any}}, | ||
| async span => { | ||
| it('should re-enable context manager on cached global tracing check after context.disable()', async () => { | ||
| const fakeTracerProvider = { | ||
| getTracer: () => ({ | ||
| startActiveSpan: ( | ||
| _name: string, | ||
| _opts: unknown, | ||
| fn: (span: unknown) => unknown, | ||
| ) => { | ||
| const fakeSpan = { | ||
| spanContext: () => ({ | ||
| traceId: '3'.repeat(32), | ||
| spanId: '4'.repeat(16), | ||
| traceFlags: 1, | ||
| }), | ||
| setAttribute: () => fakeSpan, | ||
| setAttributes: () => fakeSpan, | ||
| addEvent: () => fakeSpan, | ||
| addLink: () => fakeSpan, | ||
| addLinks: () => fakeSpan, | ||
| setStatus: () => fakeSpan, | ||
| updateName: () => fakeSpan, | ||
| end: () => {}, | ||
| isRecording: () => true, | ||
| recordException: () => {}, | ||
| }; | ||
| return context.with(trace.setSpan(context.active(), fakeSpan), () => | ||
| fn(fakeSpan), | ||
| ); | ||
| }, | ||
| }), | ||
| }; | ||
|
|
||
| _resetTracingEnabledForTest(); | ||
| const getTracerProviderStub = sinon | ||
| .stub(trace, 'getTracerProvider') | ||
| .returns(fakeTracerProvider as unknown as TracerProvider); | ||
| try { | ||
| // First trace primes globalTracingEnabled = true | ||
| await startTrace('Test.primedSpan', undefined, async span => { | ||
| span.end(); | ||
| }); | ||
| // Disable context manager while globalTracingEnabled remains cached as true | ||
| context.disable(); | ||
|
|
||
| let activeSpanAfterAwait: Span | undefined; | ||
| await startTrace('Test.cachedGlobalSpan', undefined, async span => { | ||
| await Promise.resolve(); | ||
| activeSpanAfterAwait = getActiveOrNoopSpan(); | ||
| span.end(); | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| assert.strictEqual( | ||
| activeSpanAfterAwait?.spanContext().spanId, | ||
| '4'.repeat(16), | ||
| ); | ||
| } finally { | ||
| getTracerProviderStub.restore(); | ||
| _resetTracingEnabledForTest(); | ||
| ensureInitialContextManagerSet(); | ||
| } | ||
| }); | ||
|
|
||
| it('should not call setGlobalContextManager or diag.error when a context manager is already registered', async () => { | ||
| const fakeTracerProvider = { | ||
| getTracer: () => ({ | ||
| startActiveSpan: ( | ||
| _name: string, | ||
| _opts: unknown, | ||
| fn: (span: unknown) => unknown, | ||
| ) => { | ||
| const fakeSpan = { | ||
| spanContext: () => ({ | ||
| traceId: '5'.repeat(32), | ||
| spanId: '6'.repeat(16), | ||
| traceFlags: 1, | ||
| }), | ||
| setAttribute: () => fakeSpan, | ||
| setAttributes: () => fakeSpan, | ||
| addEvent: () => fakeSpan, | ||
| addLink: () => fakeSpan, | ||
| addLinks: () => fakeSpan, | ||
| setStatus: () => fakeSpan, | ||
| updateName: () => fakeSpan, | ||
| end: () => {}, | ||
| isRecording: () => true, | ||
| recordException: () => {}, | ||
| }; | ||
| return context.with(trace.setSpan(context.active(), fakeSpan), () => | ||
| fn(fakeSpan), | ||
| ); | ||
| }, | ||
| }), | ||
| }; | ||
|
|
||
| assert.strictEqual( | ||
| (activeSpanAfterAwait as any)?.spanContext?.().spanId, | ||
| '2'.repeat(16), | ||
| ); | ||
| context.disable(); | ||
| ensureInitialContextManagerSet(); | ||
| _resetTracingEnabledForTest(); | ||
|
|
||
| const sandbox = sinon.createSandbox(); | ||
| const setGlobalSpy = sandbox.spy(context, 'setGlobalContextManager'); | ||
| const diagErrorSpy = sandbox.spy(diag, 'error'); | ||
| sandbox | ||
| .stub(trace, 'getTracerProvider') | ||
| .returns(fakeTracerProvider as unknown as TracerProvider); | ||
|
|
||
| try { | ||
| // First trace sets globalTracingEnabled = true; subsequent traces hit the cached fast path | ||
| await startTrace('Test.first', undefined, async span => { | ||
| span.end(); | ||
| }); | ||
| await startTrace('Test.second', undefined, async span => { | ||
| span.end(); | ||
| }); | ||
| ensureInitialContextManagerSet(); | ||
|
|
||
| assert.strictEqual(setGlobalSpy.callCount, 0); | ||
| assert.strictEqual(diagErrorSpy.callCount, 0); | ||
| } finally { | ||
| sandbox.restore(); | ||
| _resetTracingEnabledForTest(); | ||
| ensureInitialContextManagerSet(); | ||
| } | ||
| }); | ||
|
Contributor
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. [Generated] Add unit test verifying that |
||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Generated] Call
ensureInitialContextManagerSet()inside the cachedglobalTracingEnabled === truefast path of_checkIfOpenTelemetryTracingIsEnabled()so that ifcontext.disable()was invoked between tracing checks (e.g., in test teardowns),AsyncLocalStorageContextManageris re-registered under Bun and Node.js.