Skip to content

Commit 8070c7f

Browse files
fix(pg): Do not add SQLCommenter comments to prepared statements (#2456)
Co-authored-by: Amir Blum <[email protected]>
1 parent a5b5614 commit 8070c7f

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,14 +307,17 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
307307
// Modify query text w/ a tracing comment before invoking original for
308308
// tracing, but only if args[0] has one of our expected shapes.
309309
if (instrumentationConfig.addSqlCommenterCommentToQueries) {
310-
args[0] = firstArgIsString
311-
? addSqlCommenterComment(span, arg0)
312-
: firstArgIsQueryObjectWithText
313-
? {
314-
...arg0,
315-
text: addSqlCommenterComment(span, arg0.text),
316-
}
317-
: args[0];
310+
if (firstArgIsString) {
311+
args[0] = addSqlCommenterComment(span, arg0);
312+
} else if (firstArgIsQueryObjectWithText && !('name' in arg0)) {
313+
// In the case of a query object, we need to ensure there's no name field
314+
// as this indicates a prepared query, where the comment would remain the same
315+
// for every invocation and contain an outdated trace context.
316+
args[0] = {
317+
...arg0,
318+
text: addSqlCommenterComment(span, arg0.text),
319+
};
320+
}
318321
}
319322

320323
// Bind callback (if any) to parent span (if any)

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

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -853,15 +853,9 @@ describe('pg', () => {
853853
const [span] = memoryExporter.getFinishedSpans();
854854
assert.ok(span);
855855

856-
const commentedQuery = addSqlCommenterComment(
857-
trace.wrapSpanContext(span.spanContext()),
858-
query
859-
);
860-
861856
const executedQueries = getExecutedQueries();
862857
assert.equal(executedQueries.length, 1);
863858
assert.equal(executedQueries[0].text, query);
864-
assert.notEqual(query, commentedQuery);
865859
} catch (e: any) {
866860
assert.ok(false, e.message);
867861
}
@@ -879,15 +873,11 @@ describe('pg', () => {
879873
assert.ok(res);
880874

881875
const [span] = memoryExporter.getFinishedSpans();
882-
const commentedQuery = addSqlCommenterComment(
883-
trace.wrapSpanContext(span.spanContext()),
884-
query
885-
);
876+
assert.ok(span);
886877

887878
const executedQueries = getExecutedQueries();
888879
assert.equal(executedQueries.length, 1);
889880
assert.equal(executedQueries[0].text, query);
890-
assert.notEqual(query, commentedQuery);
891881
done();
892882
},
893883
} as pg.QueryConfig);
@@ -952,6 +942,33 @@ describe('pg', () => {
952942
});
953943
});
954944

945+
it('should not add sqlcommenter comment when addSqlCommenterCommentToQueries=true is specified with a prepared statement', async () => {
946+
instrumentation.setConfig({
947+
addSqlCommenterCommentToQueries: true,
948+
});
949+
950+
const span = tracer.startSpan('test span');
951+
await context.with(trace.setSpan(context.active(), span), async () => {
952+
try {
953+
const query = 'SELECT NOW()';
954+
const resPromise = await client.query({
955+
text: query,
956+
name: 'prepared sqlcommenter',
957+
});
958+
assert.ok(resPromise);
959+
960+
const [span] = memoryExporter.getFinishedSpans();
961+
assert.ok(span);
962+
963+
const executedQueries = getExecutedQueries();
964+
assert.equal(executedQueries.length, 1);
965+
assert.equal(executedQueries[0].text, query);
966+
} catch (e: any) {
967+
assert.ok(false, e.message);
968+
}
969+
});
970+
});
971+
955972
it('should not generate traces for client.query() when requireParentSpan=true is specified', done => {
956973
instrumentation.setConfig({
957974
requireParentSpan: true,

0 commit comments

Comments
 (0)