Skip to content

Commit 353e7ae

Browse files
committed
fix(instrumentation-pg): preserve SQL template text property
1 parent 73c7edc commit 353e7ae

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

packages/instrumentation-pg/src/instrumentation.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,15 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
339339
values: Array.isArray(args[1]) ? args[1] : undefined,
340340
}
341341
: firstArgIsQueryObjectWithText
342-
? {
343-
...(arg0 as any),
344-
values:
345-
(arg0 as any).values ??
346-
(Array.isArray(args[1]) ? args[1] : undefined),
342+
? (() => {
343+
const query = arg0 as any;
344+
// If the query object has no values yet, use the second argument as values
345+
if (query.values === undefined && Array.isArray(args[1])) {
346+
query.values = args[1];
347347
}
348-
: undefined;
348+
return query;
349+
})()
350+
: undefined;
349351

350352
const attributes: Attributes = {
351353
[ATTR_DB_SYSTEM]: DB_SYSTEM_VALUE_POSTGRESQL,

packages/instrumentation-pg/test/pg.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,53 @@ describe('pg', () => {
10391039
done();
10401040
});
10411041
});
1042+
1043+
it('should handle SQL template literal objects', async () => {
1044+
const span = tracer.startSpan('test span');
1045+
await context.with(trace.setSpan(context.active(), span), async () => {
1046+
// Mock SQLStatement from sql-template-strings library
1047+
class SQLStatement {
1048+
strings: string[];
1049+
values: any[];
1050+
1051+
constructor(strings: string[], values: any[]) {
1052+
this.strings = strings;
1053+
this.values = values;
1054+
}
1055+
1056+
get text(): string {
1057+
return this.strings.reduce(
1058+
(prev: string, curr: string, i: number) => prev + '$' + i + curr
1059+
);
1060+
}
1061+
}
1062+
1063+
const name = 'pg_tables';
1064+
const sqlQuery = new SQLStatement(
1065+
['SELECT * FROM information_schema.tables WHERE table_name = ', ''],
1066+
[name]
1067+
);
1068+
const resPromise = await client.query(sqlQuery);
1069+
assert.ok(resPromise);
1070+
1071+
const spans = memoryExporter.getFinishedSpans();
1072+
assert.strictEqual(spans.length, 1);
1073+
const pgSpan = spans[0];
1074+
1075+
assert.strictEqual(pgSpan.name, 'pg.query:SELECT otel_pg_database');
1076+
assert.strictEqual(
1077+
pgSpan.attributes[ATTR_DB_STATEMENT],
1078+
'SELECT * FROM information_schema.tables WHERE table_name = $1'
1079+
);
1080+
1081+
const executedQueries = getExecutedQueries();
1082+
assert.ok(executedQueries.length > 0);
1083+
assert.strictEqual(
1084+
executedQueries[executedQueries.length - 1].text,
1085+
'SELECT * FROM information_schema.tables WHERE table_name = $1'
1086+
);
1087+
});
1088+
});
10421089
});
10431090

10441091
describe('exception event recording', () => {

0 commit comments

Comments
 (0)