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(plugin-ioredis): end span on response from the server and set span status according to response #239

Merged
merged 22 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion plugins/node/opentelemetry-plugin-ioredis/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,20 @@ export const traceSendCommand = (

try {
const result = original.apply(this, arguments);
endSpan(span, null);

const origResolve = cmd.resolve;
/* eslint-disable @typescript-eslint/no-explicit-any */
cmd.resolve = function (result: any) {
endSpan(span, null);
origResolve(result);
};

const origReject = cmd.reject;
cmd.reject = function (err: Error) {
endSpan(span, err);
obecny marked this conversation as resolved.
Show resolved Hide resolved
origReject(err);
};

return result;
} catch (error) {
endSpan(span, error);
Expand Down
36 changes: 31 additions & 5 deletions plugins/node/opentelemetry-plugin-ioredis/test/ioredis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@ describe('ioredis', () => {
assert.strictEqual(endedSpans.length, 3);
assert.strictEqual(endedSpans[2].name, 'test span');

client.quit(done);
assert.strictEqual(endedSpans.length, 4);
assert.strictEqual(endedSpans[3].name, 'quit');
client.quit(() => {
assert.strictEqual(endedSpans.length, 4);
assert.strictEqual(endedSpans[3].name, 'quit');
done();
});
};
const errorHandler = (err: Error) => {
assert.ifError(err);
Expand Down Expand Up @@ -259,6 +261,23 @@ describe('ioredis', () => {
});
});

it('should set span with error when redis return reject', async () => {
const span = provider.getTracer('ioredis-test').startSpan('test span');
await provider.getTracer('ioredis-test').withSpan(span, async () => {
await client.set('non-int-key', 'no-int-value');
try {
// should throw 'ReplyError: ERR value is not an integer or out of range'
// because the value im the key is not numeric and we try to increment it
await client.incr('non-int-key');
} catch (ex) {
const endedSpans = memoryExporter.getFinishedSpans();
assert.strictEqual(endedSpans.length, 2);
// redis 'incr' operation failed with exception, so span should indicate it
assert.notStrictEqual(endedSpans[1].status.code, StatusCode.UNSET);
}
});
});

it('should create a child span for streamify scanning', done => {
const attributes = {
...DEFAULT_ATTRIBUTES,
Expand Down Expand Up @@ -318,10 +337,10 @@ describe('ioredis', () => {
const spanNames = [
'connect',
'connect',
'subscribe',
'info',
'info',
'subscribe',
'subscribe',
'publish',
'publish',
'unsubscribe',
Expand Down Expand Up @@ -374,21 +393,28 @@ describe('ioredis', () => {
span.end();
const endedSpans = memoryExporter.getFinishedSpans();
// the script may be already cached on server therefore we get either 2 or 3 spans
let expectedEvalshaStatus;
if (endedSpans.length === 3) {
assert.strictEqual(endedSpans[2].name, 'test span');
assert.strictEqual(endedSpans[1].name, 'eval');
assert.strictEqual(endedSpans[0].name, 'evalsha');
// in this case, server returns NOSCRIPT error for evalsha,
// telling the client to use EVAL instead
expectedEvalshaStatus = {
code: StatusCode.ERROR,
};
} else {
assert.strictEqual(endedSpans.length, 2);
assert.strictEqual(endedSpans[1].name, 'test span');
assert.strictEqual(endedSpans[0].name, 'evalsha');
expectedEvalshaStatus = unsetStatus;
}
testUtils.assertSpan(
endedSpans[0],
SpanKind.CLIENT,
attributes,
[],
unsetStatus
expectedEvalshaStatus
);
testUtils.assertPropagation(endedSpans[0], span);
done();
Expand Down