Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 8 additions & 6 deletions packages/instrumentation-pg/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,15 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
values: Array.isArray(args[1]) ? args[1] : undefined,
}
: firstArgIsQueryObjectWithText
? {
...(arg0 as any),
values:
(arg0 as any).values ??
(Array.isArray(args[1]) ? args[1] : undefined),
? (() => {
const query = arg0 as any;
// If the query object has no values yet, use the second argument as values
if (query.values === undefined && Array.isArray(args[1])) {
query.values = args[1];
}
: undefined;
return query;
})()
: undefined;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already did not like this ternary soup, so if you don't mind I'd like to use this opportunity to refactor it into something more readable rather than adding an IIFE into the mix 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely, a clean refactor sounds like the right move 👍


const attributes: Attributes = {
[ATTR_DB_SYSTEM]: DB_SYSTEM_VALUE_POSTGRESQL,
Expand Down
47 changes: 47 additions & 0 deletions packages/instrumentation-pg/test/pg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,53 @@ describe('pg', () => {
done();
});
});

it('should handle SQL template literal objects', async () => {
const span = tracer.startSpan('test span');
await context.with(trace.setSpan(context.active(), span), async () => {
// Mock SQLStatement from sql-template-strings library
class SQLStatement {
strings: string[];
values: any[];

constructor(strings: string[], values: any[]) {
this.strings = strings;
this.values = values;
}

get text(): string {
return this.strings.reduce(
(prev: string, curr: string, i: number) => prev + '$' + i + curr
);
}
}

const name = 'pg_tables';
const sqlQuery = new SQLStatement(
['SELECT * FROM information_schema.tables WHERE table_name = ', ''],
[name]
);
const resPromise = await client.query(sqlQuery);
assert.ok(resPromise);

const spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans.length, 1);
const pgSpan = spans[0];

assert.strictEqual(pgSpan.name, 'pg.query:SELECT otel_pg_database');
assert.strictEqual(
pgSpan.attributes[ATTR_DB_STATEMENT],
'SELECT * FROM information_schema.tables WHERE table_name = $1'
);

const executedQueries = getExecutedQueries();
assert.ok(executedQueries.length > 0);
assert.strictEqual(
executedQueries[executedQueries.length - 1].text,
'SELECT * FROM information_schema.tables WHERE table_name = $1'
);
});
});
});

describe('exception event recording', () => {
Expand Down
Loading