Skip to content

Commit

Permalink
improv(ioredis): require parent span
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed Jun 17, 2020
1 parent c4806ba commit 05e398e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 29 deletions.
60 changes: 33 additions & 27 deletions plugins/node/opentelemetry-plugin-ioredis/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,35 +84,41 @@ export const traceSendCommand = (
this: ioredisTypes.Redis & IORedisPluginClientTypes,
cmd?: IORedisCommand
) {
if (arguments.length >= 1 && typeof cmd === 'object') {
const span = tracer.startSpan(cmd.name, {
kind: SpanKind.CLIENT,
attributes: {
[AttributeNames.COMPONENT]: IORedisPlugin.COMPONENT,
[AttributeNames.DB_TYPE]: IORedisPlugin.DB_TYPE,
[AttributeNames.DB_STATEMENT]: dbStatementSerializer(
cmd.name,
cmd.args
),
},
});
if (arguments.length < 1 || typeof cmd !== 'object') {
return original.apply(this, arguments);
}
// Do not trace if there is not parent span
if (tracer.getCurrentSpan() === undefined) {
return original.apply(this, arguments);
}

const { host, port } = this.options;
const span = tracer.startSpan(cmd.name, {
kind: SpanKind.CLIENT,
attributes: {
[AttributeNames.COMPONENT]: IORedisPlugin.COMPONENT,
[AttributeNames.DB_TYPE]: IORedisPlugin.DB_TYPE,
[AttributeNames.DB_STATEMENT]: dbStatementSerializer(
cmd.name,
cmd.args
),
},
});

span.setAttributes({
[AttributeNames.PEER_HOSTNAME]: host,
[AttributeNames.PEER_PORT]: port,
[AttributeNames.PEER_ADDRESS]: `redis://${host}:${port}`,
});
const { host, port } = this.options;

try {
const result = original.apply(this, arguments);
endSpan(span, null);
return result;
} catch (error) {
endSpan(span, error);
throw error;
}
} else return original.apply(this, arguments);
span.setAttributes({
[AttributeNames.PEER_HOSTNAME]: host,
[AttributeNames.PEER_PORT]: port,
[AttributeNames.PEER_ADDRESS]: `redis://${host}:${port}`,
});

try {
const result = original.apply(this, arguments);
endSpan(span, null);
return result;
} catch (error) {
endSpan(span, error);
throw error;
}
};
};
18 changes: 16 additions & 2 deletions plugins/node/opentelemetry-plugin-ioredis/test/ioredis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ describe('ioredis', () => {
it(`should create a child span for lua`, done => {
const attributes = {
...DEFAULT_ATTRIBUTES,
[AttributeNames.DB_STATEMENT]: 'eval return {KEYS[1],ARGV[1]} 1 test',
[AttributeNames.DB_STATEMENT]:
'evalsha bfbf458525d6a0b19200bfd6db3af481156b367b 1 test',
};

const span = provider.getTracer('ioredis-test').startSpan('test span');
Expand All @@ -371,7 +372,7 @@ describe('ioredis', () => {
assert.strictEqual(endedSpans[1].name, 'eval');
assert.strictEqual(endedSpans[0].name, 'evalsha');
testUtils.assertSpan(
endedSpans[1],
endedSpans[0],
SpanKind.CLIENT,
attributes,
[],
Expand Down Expand Up @@ -512,6 +513,19 @@ describe('ioredis', () => {
});
});

describe('Instrumenting without parent span', () => {
before(() => {
plugin.disable();
plugin.enable(ioredis, provider, new NoopLogger(), {});
});
it('should not create child span', async () => {
await client.set('test', 'data');
const result = await client.del('test');
assert.strictEqual(result, 1);
assert.strictEqual(memoryExporter.getFinishedSpans().length, 0);
});
});

describe('Instrumenting with a custom db.statement serializer', () => {
const dbStatementSerializer: DbStatementSerializer = (cmdName, cmdArgs) =>
`FOOBAR_${cmdName}: ${cmdArgs[0]}`;
Expand Down

0 comments on commit 05e398e

Please sign in to comment.