Skip to content

Commit df5ec4a

Browse files
chore(instrumentation-pg): refactor ternary & some tests
1 parent bacaab4 commit df5ec4a

File tree

2 files changed

+13
-25
lines changed

2 files changed

+13
-25
lines changed

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,18 @@ 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.
301300
if (instrumentationConfig.addSqlCommenterCommentToQueries) {
302-
args[0] = firstArgIsString
303-
? addSqlCommenterComment(span, arg0)
304-
: firstArgIsQueryObjectWithText && !('name' in arg0)
305-
? {
306-
...arg0,
307-
text: addSqlCommenterComment(span, arg0.text),
308-
}
309-
: args[0];
301+
if (firstArgIsString) {
302+
args[0] = addSqlCommenterComment(span, arg0);
303+
} else if (firstArgIsQueryObjectWithText && !('name' in arg0)) {
304+
// In the case of a query object, we need to ensure there's no name field
305+
// as this indicates a prepared query, where the comment would remain the same
306+
// for every invocation and contain an outdated trace context.
307+
args[0] = {
308+
...arg0,
309+
text: addSqlCommenterComment(span, arg0.text),
310+
};
311+
}
310312
}
311313

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

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -849,15 +849,9 @@ describe('pg', () => {
849849
const [span] = memoryExporter.getFinishedSpans();
850850
assert.ok(span);
851851

852-
const commentedQuery = addSqlCommenterComment(
853-
trace.wrapSpanContext(span.spanContext()),
854-
query
855-
);
856-
857852
const executedQueries = getExecutedQueries();
858853
assert.equal(executedQueries.length, 1);
859854
assert.equal(executedQueries[0].text, query);
860-
assert.notEqual(query, commentedQuery);
861855
} catch (e: any) {
862856
assert.ok(false, e.message);
863857
}
@@ -875,15 +869,11 @@ describe('pg', () => {
875869
assert.ok(res);
876870

877871
const [span] = memoryExporter.getFinishedSpans();
878-
const commentedQuery = addSqlCommenterComment(
879-
trace.wrapSpanContext(span.spanContext()),
880-
query
881-
);
872+
assert.ok(span);
882873

883874
const executedQueries = getExecutedQueries();
884875
assert.equal(executedQueries.length, 1);
885876
assert.equal(executedQueries[0].text, query);
886-
assert.notEqual(query, commentedQuery);
887877
done();
888878
},
889879
} as pg.QueryConfig);
@@ -964,15 +954,11 @@ describe('pg', () => {
964954
assert.ok(resPromise);
965955

966956
const [span] = memoryExporter.getFinishedSpans();
967-
const commentedQuery = addSqlCommenterComment(
968-
trace.wrapSpanContext(span.spanContext()),
969-
query
970-
);
957+
assert.ok(span);
971958

972959
const executedQueries = getExecutedQueries();
973960
assert.equal(executedQueries.length, 1);
974961
assert.equal(executedQueries[0].text, query);
975-
assert.notEqual(query, commentedQuery);
976962
} catch (e: any) {
977963
assert.ok(false, e.message);
978964
}

0 commit comments

Comments
 (0)