Skip to content
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

fix: redis instrumentation loses context when using callbacks #580

Merged
Expand Up @@ -127,6 +127,7 @@ export const getTracedInternalSendCommand = (

const originalCallback = arguments[0].callback;
if (originalCallback) {
const originalContext = context.active();
(arguments[0] as RedisCommand).callback = function callback<T>(
this: unknown,
err: Error | null,
Expand All @@ -146,7 +147,12 @@ export const getTracedInternalSendCommand = (
}

endSpan(span, err);
return originalCallback.apply(this, arguments);
return context.with(
originalContext,
originalCallback,
this,
...arguments
);
};
}
try {
Expand Down
Expand Up @@ -212,6 +212,16 @@ describe('redis@2.x', () => {
});
});
});

it('should invoke callback with original command context', () => {
const rootSpan = tracer.startSpan('test span');
context.with(trace.setSpan(context.active(), rootSpan), () => {
client.set('callbacksTestKey', 'value', () => {
const activeSpan = trace.getSpan(context.active());
assert.strictEqual(activeSpan, rootSpan);
});
});
});
});

describe('Removing instrumentation', () => {
Expand Down