diff --git a/.changeset/early-mayflies-tickle.md b/.changeset/early-mayflies-tickle.md new file mode 100644 index 00000000..eadc4d96 --- /dev/null +++ b/.changeset/early-mayflies-tickle.md @@ -0,0 +1,6 @@ +--- +'@hyperdx/node-opentelemetry': patch +--- + +revert: fix: setTraceAttributes cross trace attributes leakage issue + diff --git a/packages/node-opentelemetry/src/MutableAsyncLocalStorageContextManager.ts b/packages/node-opentelemetry/src/MutableAsyncLocalStorageContextManager.ts index 8a439536..1b49eaf9 100644 --- a/packages/node-opentelemetry/src/MutableAsyncLocalStorageContextManager.ts +++ b/packages/node-opentelemetry/src/MutableAsyncLocalStorageContextManager.ts @@ -44,9 +44,8 @@ export class MutableAsyncLocalStorageContextManager extends AbstractAsyncHooksCo thisArg?: ThisParameterType, ...args: A ): ReturnType { - // Create a fresh mutableContext for each new context to prevent - // cross-contamination between concurrent requests - const mutableContext = { + const mutableContext = this._asyncLocalStorage.getStore() + ?.mutableContext ?? { traceAttributes: new Map(), }; diff --git a/packages/node-opentelemetry/src/__tests__/setTraceAttributes.test.ts b/packages/node-opentelemetry/src/__tests__/setTraceAttributes.test.ts index 1f585f33..1e20bb0f 100644 --- a/packages/node-opentelemetry/src/__tests__/setTraceAttributes.test.ts +++ b/packages/node-opentelemetry/src/__tests__/setTraceAttributes.test.ts @@ -75,9 +75,9 @@ describe('MutableAsyncLocalStorageContextManager - trace attributes isolation', contextManager.with(ROOT_CONTEXT, () => { const ctx2 = contextManager.getMutableContext(); - // Should NOT have parent's attributes (fresh Map) - expect(ctx2?.traceAttributes.get('userId')).toBeUndefined(); - expect(ctx2?.traceAttributes.get('sharedKey')).toBeUndefined(); + // FIXME: the ctx2 should be fresh + expect(ctx2?.traceAttributes.get('userId')).toBe('user-1'); + expect(ctx2?.traceAttributes.get('sharedKey')).toBe('parent-value'); // Set new attributes in child context ctx2?.traceAttributes.set('userId', 'user-2'); @@ -87,10 +87,10 @@ describe('MutableAsyncLocalStorageContextManager - trace attributes isolation', expect(ctx2?.traceAttributes.get('sharedKey')).toBe('child-value'); }); - // After exiting child context, parent should still have original attributes + // FIXME: After exiting child context, parent context shouldn't be overwritten const ctx1Again = contextManager.getMutableContext(); - expect(ctx1Again?.traceAttributes.get('userId')).toBe('user-1'); - expect(ctx1Again?.traceAttributes.get('sharedKey')).toBe('parent-value'); + expect(ctx1Again?.traceAttributes.get('userId')).toBe('user-2'); + expect(ctx1Again?.traceAttributes.get('sharedKey')).toBe('child-value'); }); });