Skip to content

Commit

Permalink
fix(instr-knex): handle config provider functions
Browse files Browse the repository at this point in the history
  • Loading branch information
reberhardt7 committed Jun 20, 2024
1 parent caf7cb5 commit 9a2050d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,17 @@ export class KnexInstrumentation extends InstrumentationBase {
return function wrapQuery(original: () => any) {
return function wrapped_logging_method(this: any, query: any) {
const config = this.client.config;
// connection settings are in config.connection, but since that can
// potentially be an async function that resolves to the actual
// connection settings, it's easier to access client.connectionSettings
const connectionSettings = this.client.connectionSettings;

const table = utils.extractTableName(this.builder);
// `method` actually refers to the knex API method - Not exactly "operation"
// in the spec sense, but matches most of the time.
const operation = query?.method;
const name =
config?.connection?.filename || config?.connection?.database;
connectionSettings?.filename || connectionSettings?.database;
const maxLen = (
instrumentation._config as types.KnexInstrumentationConfig
).maxQueryLength!;
Expand All @@ -139,12 +143,12 @@ export class KnexInstrumentation extends InstrumentationBase {
[SEMATTRS_DB_SYSTEM]: utils.mapSystem(config.client),
[SEMATTRS_DB_SQL_TABLE]: table,
[SEMATTRS_DB_OPERATION]: operation,
[SEMATTRS_DB_USER]: config?.connection?.user,
[SEMATTRS_DB_USER]: connectionSettings?.user,
[SEMATTRS_DB_NAME]: name,
[SEMATTRS_NET_PEER_NAME]: config?.connection?.host,
[SEMATTRS_NET_PEER_PORT]: config?.connection?.port,
[SEMATTRS_NET_PEER_NAME]: connectionSettings?.host,
[SEMATTRS_NET_PEER_PORT]: connectionSettings?.port,
[SEMATTRS_NET_TRANSPORT]:
config?.connection?.filename === ':memory:' ? 'inproc' : undefined,
connectionSettings?.filename === ':memory:' ? 'inproc' : undefined,
};
if (maxLen !== 0) {
attributes[SEMATTRS_DB_STATEMENT] = utils.limitLength(
Expand Down
18 changes: 18 additions & 0 deletions plugins/node/opentelemetry-instrumentation-knex/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,24 @@ describe('Knex instrumentation', () => {
);
});
});

it('works when knex is configured with connection function', async () => {
client = knex({
client: 'sqlite3',
// Use a function to configure the connection
connection: async () => ({
filename: ':memory:',
}),
useNullAsDefault: true,
});

await client.raw('SELECT 1')

const instrumentationSpans = memoryExporter.getFinishedSpans();
assert.strictEqual(instrumentationSpans.length, 1);
assert.strictEqual(instrumentationSpans[0].name, 'raw :memory:');
assert.strictEqual(instrumentationSpans[0].attributes['net.transport'], 'inproc')
})
});

const assertSpans = (actualSpans: any[], expectedSpans: any[]) => {
Expand Down

0 comments on commit 9a2050d

Please sign in to comment.