Skip to content

Commit b46fac4

Browse files
committed
test: extend test to cover SQL template literals with added comments
1 parent 84db185 commit b46fac4

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ describe('pg', () => {
10401040
});
10411041
});
10421042

1043-
it('should handle SQL template literal objects', async () => {
1043+
it('should generate traces for SQL template literal objects ', async () => {
10441044
const span = tracer.startSpan('test span');
10451045
await context.with(trace.setSpan(context.active(), span), async () => {
10461046
// Mock SQLStatement from sql-template-strings library
@@ -1086,6 +1086,54 @@ describe('pg', () => {
10861086
);
10871087
});
10881088
});
1089+
1090+
it('should generate traces for SQL template literals with added comments', async () => {
1091+
instrumentation.setConfig({
1092+
addSqlCommenterCommentToQueries: true,
1093+
});
1094+
1095+
const span = tracer.startSpan('test span');
1096+
1097+
await context.with(trace.setSpan(context.active(), span), async () => {
1098+
// Mock SQLStatement from sql-template-strings library
1099+
class SQLStatement {
1100+
strings: string[];
1101+
values: any[];
1102+
1103+
constructor(strings: string[], values: any[]) {
1104+
this.strings = strings;
1105+
this.values = values;
1106+
}
1107+
1108+
get text(): string {
1109+
return this.strings.reduce(
1110+
(prev: string, curr: string, i: number) => prev + '$' + i + curr
1111+
);
1112+
}
1113+
}
1114+
const tableName = 'pg_tables';
1115+
const sqlQuery = new SQLStatement(
1116+
['SELECT * FROM information_schema.tables WHERE table_name = ', ''],
1117+
[tableName]
1118+
);
1119+
1120+
const res = await client.query(sqlQuery);
1121+
assert.ok(res);
1122+
1123+
const spans = memoryExporter.getFinishedSpans();
1124+
assert.strictEqual(spans.length, 1);
1125+
const pgSpan = spans[0];
1126+
1127+
const commentedQuery = addSqlCommenterComment(
1128+
trace.wrapSpanContext(pgSpan.spanContext()),
1129+
sqlQuery.text
1130+
);
1131+
const executedQueries = getExecutedQueries();
1132+
assert.ok(executedQueries.length > 0);
1133+
const lastQuery = executedQueries[executedQueries.length - 1];
1134+
assert.strictEqual(lastQuery.text, commentedQuery);
1135+
});
1136+
});
10891137
});
10901138

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

0 commit comments

Comments
 (0)