From b68161d71078a746959536e7660d087440a17a31 Mon Sep 17 00:00:00 2001 From: vmarchaud Date: Sat, 30 May 2020 17:31:56 +0200 Subject: [PATCH 1/2] improv(ioredis): require parent span --- .../opentelemetry-plugin-ioredis/src/utils.ts | 60 ++++++++++--------- .../test/ioredis.test.ts | 18 +++++- 2 files changed, 49 insertions(+), 29 deletions(-) diff --git a/plugins/node/opentelemetry-plugin-ioredis/src/utils.ts b/plugins/node/opentelemetry-plugin-ioredis/src/utils.ts index 1a8f991725..5f7a4b73b8 100644 --- a/plugins/node/opentelemetry-plugin-ioredis/src/utils.ts +++ b/plugins/node/opentelemetry-plugin-ioredis/src/utils.ts @@ -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; + } }; }; diff --git a/plugins/node/opentelemetry-plugin-ioredis/test/ioredis.test.ts b/plugins/node/opentelemetry-plugin-ioredis/test/ioredis.test.ts index ca7309b6a5..398d8f2ca4 100644 --- a/plugins/node/opentelemetry-plugin-ioredis/test/ioredis.test.ts +++ b/plugins/node/opentelemetry-plugin-ioredis/test/ioredis.test.ts @@ -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'); @@ -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, [], @@ -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]}`; From 56c48459ab3d8a29de5f5c8651ff3b8d770f4224 Mon Sep 17 00:00:00 2001 From: vmarchaud Date: Thu, 18 Jun 2020 15:32:14 +0200 Subject: [PATCH 2/2] chore(deps): bump @types/ioredis version --- plugins/node/opentelemetry-plugin-ioredis/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/node/opentelemetry-plugin-ioredis/package.json b/plugins/node/opentelemetry-plugin-ioredis/package.json index 5df986fcc6..a01035a2fd 100644 --- a/plugins/node/opentelemetry-plugin-ioredis/package.json +++ b/plugins/node/opentelemetry-plugin-ioredis/package.json @@ -48,7 +48,7 @@ "@opentelemetry/node": "0.9.0", "@opentelemetry/test-utils": "^0.8.0", "@opentelemetry/tracing": "0.9.0", - "@types/ioredis": "4.16.6", + "@types/ioredis": "4.16.7", "@types/mocha": "7.0.2", "@types/node": "13.13.12", "@types/shimmer": "1.0.1",