Skip to content

Commit bacaab4

Browse files
fix(pg): do not add comment to prepared queries
1 parent 138c6c5 commit bacaab4

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,11 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
297297

298298
// Modify query text w/ a tracing comment before invoking original for
299299
// tracing, but only if args[0] has one of our expected shapes.
300+
// Also omit the tracing comment if args[0] is a prepared query object.
300301
if (instrumentationConfig.addSqlCommenterCommentToQueries) {
301302
args[0] = firstArgIsString
302303
? addSqlCommenterComment(span, arg0)
303-
: firstArgIsQueryObjectWithText
304+
: firstArgIsQueryObjectWithText && !('name' in arg0)
304305
? {
305306
...arg0,
306307
text: addSqlCommenterComment(span, arg0.text),

plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,37 @@ describe('pg', () => {
948948
});
949949
});
950950

951+
it('should not add sqlcommenter comment when addSqlCommenterCommentToQueries=true is specified with a prepared statement', async () => {
952+
instrumentation.setConfig({
953+
addSqlCommenterCommentToQueries: true,
954+
});
955+
956+
const span = tracer.startSpan('test span');
957+
await context.with(trace.setSpan(context.active(), span), async () => {
958+
try {
959+
const query = 'SELECT NOW()';
960+
const resPromise = await client.query({
961+
text: query,
962+
name: 'prepared sqlcommenter',
963+
});
964+
assert.ok(resPromise);
965+
966+
const [span] = memoryExporter.getFinishedSpans();
967+
const commentedQuery = addSqlCommenterComment(
968+
trace.wrapSpanContext(span.spanContext()),
969+
query
970+
);
971+
972+
const executedQueries = getExecutedQueries();
973+
assert.equal(executedQueries.length, 1);
974+
assert.equal(executedQueries[0].text, query);
975+
assert.notEqual(query, commentedQuery);
976+
} catch (e: any) {
977+
assert.ok(false, e.message);
978+
}
979+
});
980+
});
981+
951982
it('should not generate traces for client.query() when requireParentSpan=true is specified', done => {
952983
instrumentation.setConfig({
953984
requireParentSpan: true,

0 commit comments

Comments
 (0)