Skip to content

Commit

Permalink
chore(instrumentation-pg): refactor ternary & some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael-theriault-swi committed Nov 1, 2024
1 parent bacaab4 commit df5ec4a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,18 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf

// Modify query text w/ a tracing comment before invoking original for
// tracing, but only if args[0] has one of our expected shapes.
// Also omit the tracing comment if args[0] is a prepared query object.
if (instrumentationConfig.addSqlCommenterCommentToQueries) {
args[0] = firstArgIsString
? addSqlCommenterComment(span, arg0)
: firstArgIsQueryObjectWithText && !('name' in arg0)
? {
...arg0,
text: addSqlCommenterComment(span, arg0.text),
}
: args[0];
if (firstArgIsString) {
args[0] = addSqlCommenterComment(span, arg0);
} else if (firstArgIsQueryObjectWithText && !('name' in arg0)) {
// In the case of a query object, we need to ensure there's no name field
// as this indicates a prepared query, where the comment would remain the same
// for every invocation and contain an outdated trace context.
args[0] = {
...arg0,
text: addSqlCommenterComment(span, arg0.text),
};
}
}

// Bind callback (if any) to parent span (if any)
Expand Down
18 changes: 2 additions & 16 deletions plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -849,15 +849,9 @@ describe('pg', () => {
const [span] = memoryExporter.getFinishedSpans();
assert.ok(span);

const commentedQuery = addSqlCommenterComment(
trace.wrapSpanContext(span.spanContext()),
query
);

const executedQueries = getExecutedQueries();
assert.equal(executedQueries.length, 1);
assert.equal(executedQueries[0].text, query);
assert.notEqual(query, commentedQuery);
} catch (e: any) {
assert.ok(false, e.message);
}
Expand All @@ -875,15 +869,11 @@ describe('pg', () => {
assert.ok(res);

const [span] = memoryExporter.getFinishedSpans();
const commentedQuery = addSqlCommenterComment(
trace.wrapSpanContext(span.spanContext()),
query
);
assert.ok(span);

const executedQueries = getExecutedQueries();
assert.equal(executedQueries.length, 1);
assert.equal(executedQueries[0].text, query);
assert.notEqual(query, commentedQuery);
done();
},
} as pg.QueryConfig);
Expand Down Expand Up @@ -964,15 +954,11 @@ describe('pg', () => {
assert.ok(resPromise);

const [span] = memoryExporter.getFinishedSpans();
const commentedQuery = addSqlCommenterComment(
trace.wrapSpanContext(span.spanContext()),
query
);
assert.ok(span);

const executedQueries = getExecutedQueries();
assert.equal(executedQueries.length, 1);
assert.equal(executedQueries[0].text, query);
assert.notEqual(query, commentedQuery);
} catch (e: any) {
assert.ok(false, e.message);
}
Expand Down

0 comments on commit df5ec4a

Please sign in to comment.