Skip to content

Commit d3cb60d

Browse files
evantorrieAmir Blum
and
Amir Blum
authored
fix(ioredis): requireParentSpan not applied to connect spans (open-telemetry#1151)
* Add tests to validate that requireParentSpan applies to connect * Copy finishedSpans * Implement requireParentSpan for connect Co-authored-by: Amir Blum <[email protected]>
1 parent d8767a9 commit d3cb60d

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

plugins/node/opentelemetry-instrumentation-ioredis/src/instrumentation.ts

+7
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,13 @@ export class IORedisInstrumentation extends InstrumentationBase<
184184
private traceConnection = (original: Function) => {
185185
const instrumentation = this;
186186
return function (this: ioredisTypes.Redis) {
187+
const config =
188+
instrumentation.getConfig() as IORedisInstrumentationConfig;
189+
const hasNoParentSpan = trace.getSpan(context.active()) === undefined;
190+
if (config?.requireParentSpan === true && hasNoParentSpan) {
191+
return original.apply(this, arguments);
192+
}
193+
187194
const span = instrumentation.tracer.startSpan('connect', {
188195
kind: SpanKind.CLIENT,
189196
attributes: {

plugins/node/opentelemetry-instrumentation-ioredis/test/ioredis.test.ts

+51-4
Original file line numberDiff line numberDiff line change
@@ -640,16 +640,24 @@ describe('ioredis', () => {
640640
};
641641
instrumentation.setConfig(config);
642642
});
643-
it('should not create child span', async () => {
643+
it('should not create child span for query', async () => {
644644
await client.set(testKeyName, 'data');
645645
const result = await client.del(testKeyName);
646646
assert.strictEqual(result, 1);
647647
assert.strictEqual(memoryExporter.getFinishedSpans().length, 0);
648648
});
649+
650+
it('should not create child span for connect', async () => {
651+
const lazyClient = new ioredis(URL, { lazyConnect: true });
652+
await lazyClient.connect();
653+
const spans = memoryExporter.getFinishedSpans();
654+
await lazyClient.quit();
655+
assert.strictEqual(spans.length, 0);
656+
});
649657
});
650658

651659
describe('Instrumentation with requireParentSpan', () => {
652-
it('should instrument with requireParentSpan equal false', async () => {
660+
it('should instrument queries with requireParentSpan equal false', async () => {
653661
const config: IORedisInstrumentationConfig = {
654662
requireParentSpan: false,
655663
};
@@ -658,23 +666,48 @@ describe('ioredis', () => {
658666
await client.set(testKeyName, 'data');
659667
const result = await client.del(testKeyName);
660668
assert.strictEqual(result, 1);
669+
const endedSpans = memoryExporter.getFinishedSpans();
661670

671+
assert.strictEqual(endedSpans.length, 2);
672+
testUtils.assertSpan(
673+
endedSpans[0],
674+
SpanKind.CLIENT,
675+
{
676+
...DEFAULT_ATTRIBUTES,
677+
[SemanticAttributes.DB_STATEMENT]: `set ${testKeyName} [1 other arguments]`,
678+
},
679+
[],
680+
unsetStatus
681+
);
682+
});
683+
684+
it('should instrument connect with requireParentSpan equal false', async () => {
685+
const config: IORedisInstrumentationConfig = {
686+
requireParentSpan: false,
687+
};
688+
instrumentation.setConfig(config);
689+
690+
const lazyClient = new ioredis(URL, { lazyConnect: true });
691+
await lazyClient.connect();
662692
const endedSpans = memoryExporter.getFinishedSpans();
663693
assert.strictEqual(endedSpans.length, 2);
694+
assert.strictEqual(endedSpans[0].name, 'connect');
695+
assert.strictEqual(endedSpans[1].name, 'info');
664696

697+
await lazyClient.quit();
665698
testUtils.assertSpan(
666699
endedSpans[0],
667700
SpanKind.CLIENT,
668701
{
669702
...DEFAULT_ATTRIBUTES,
670-
[SemanticAttributes.DB_STATEMENT]: `set ${testKeyName} [1 other arguments]`,
703+
[SemanticAttributes.DB_STATEMENT]: 'connect',
671704
},
672705
[],
673706
unsetStatus
674707
);
675708
});
676709

677-
it('should not instrument with requireParentSpan equal true', async () => {
710+
it('should not instrument queries with requireParentSpan equal true', async () => {
678711
const config: IORedisInstrumentationConfig = {
679712
requireParentSpan: true,
680713
};
@@ -686,6 +719,20 @@ describe('ioredis', () => {
686719

687720
assert.strictEqual(memoryExporter.getFinishedSpans().length, 0);
688721
});
722+
723+
it('should not instrument connect with requireParentSpan equal true', async () => {
724+
const config: IORedisInstrumentationConfig = {
725+
requireParentSpan: true,
726+
};
727+
instrumentation.setConfig(config);
728+
729+
const lazyClient = new ioredis(URL, { lazyConnect: true });
730+
await lazyClient.connect();
731+
const endedSpans = memoryExporter.getFinishedSpans();
732+
assert.strictEqual(endedSpans.length, 0);
733+
734+
await lazyClient.quit();
735+
});
689736
});
690737

691738
describe('Instrumenting with a custom db.statement serializer', () => {

0 commit comments

Comments
 (0)