Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions packages/instrumentation-knex/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentation

if (instrumentation._semconvStability & SemconvStability.OLD) {
Object.assign(attributes, {
[ATTR_DB_SYSTEM]: utils.mapSystem(config.client),
[ATTR_DB_SYSTEM]: utils.mapSystem(this.client.driverName),
[ATTR_DB_SQL_TABLE]: table,
[ATTR_DB_OPERATION]: operation,
[ATTR_DB_USER]: config?.connection?.user,
Expand All @@ -173,7 +173,7 @@ export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentation
}
if (instrumentation._semconvStability & SemconvStability.STABLE) {
Object.assign(attributes, {
[ATTR_DB_SYSTEM_NAME]: utils.mapSystem(config.client),
[ATTR_DB_SYSTEM_NAME]: utils.mapSystem(this.client.driverName),
[ATTR_DB_COLLECTION_NAME]: table,
[ATTR_DB_OPERATION_NAME]: operation,
[ATTR_DB_NAMESPACE]: name,
Expand Down
41 changes: 41 additions & 0 deletions packages/instrumentation-knex/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const plugin = new KnexInstrumentation({
});

import knex from 'knex';
// @ts-ignore
import * as BetterSqlite3Dialect from 'knex/lib/dialects/better-sqlite3';

describe('Knex instrumentation', () => {
const memoryExporter = new InMemorySpanExporter();
Expand Down Expand Up @@ -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');
Expand Down