diff --git a/packages/instrumentation-knex/src/instrumentation.ts b/packages/instrumentation-knex/src/instrumentation.ts index cb9c3ce10f..90fd79e3d0 100644 --- a/packages/instrumentation-knex/src/instrumentation.ts +++ b/packages/instrumentation-knex/src/instrumentation.ts @@ -161,7 +161,7 @@ export class KnexInstrumentation extends InstrumentationBase { const memoryExporter = new InMemorySpanExporter(); @@ -154,6 +156,45 @@ describe('Knex instrumentation', () => { assert.ok(statement.startsWith(limitedStatement.substring(0, 50))); }); + it("should correctly capture the DB's system name even with custom client implementations", async () => { + client = knex({ + client: BetterSqlite3Dialect, + connection: { + filename: ':memory:', + }, + useNullAsDefault: true, + }); + + const parentSpan = tracer.startSpan('parentSpan'); + await context.with( + trace.setSpan(context.active(), parentSpan), + async () => { + assert.deepEqual(await client.select(client.raw('1 as testCol')), [ + { testCol: 1 }, + ]); + + parentSpan.end(); + + const instrumentationSpans = memoryExporter.getFinishedSpans(); + const last = instrumentationSpans.pop() as any; + assertSpans( + instrumentationSpans, + [ + { + op: 'select', + statement: 'select 1 as testCol', + parentSpan, + }, + ], + { dbSystem: 'better-sqlite3' } + ); + assert.strictEqual(instrumentationSpans[0].name, 'select :memory:'); + + assert(last.name, 'parentSpan'); + } + ); + }); + it('should catch errors', async () => { const parentSpan = tracer.startSpan('parentSpan'); const neverError = new Error('Query was expected to error');