Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(instr-knex): handle config provider functions #2286

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,27 @@ 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