Skip to content

Commit

Permalink
fix: redis instrumentation loses context when using callbacks (open-t…
Browse files Browse the repository at this point in the history
…elemetry#580)

* fix: redis instrumentation loses context when using callbacks

* fix: rename test

* fix: merge new test into existing describe

* fix: refactor context.with as requested

* fix: remove redundant callbackThis const

* fix: move originalContext to inside if
  • Loading branch information
aspectom authored Aug 5, 2021
1 parent a63b828 commit f9bc4a1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@ describe('[email protected]', () => {
});
});
});

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

0 comments on commit f9bc4a1

Please sign in to comment.