Skip to content

Commit bb0334b

Browse files
jtomaszewskiclaude
andauthored
feat(instrumentation-pg): add skipConnectSpans option (open-telemetry#3280)
Co-authored-by: Claude <noreply@anthropic.com>
1 parent 64576c4 commit bb0334b

File tree

4 files changed

+96
-4
lines changed

4 files changed

+96
-4
lines changed

packages/instrumentation-pg/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ PostgreSQL instrumentation has few options available to choose from. You can set
6262
| `responseHook` | `PgInstrumentationExecutionResponseHook` (function) | Function for adding custom span attributes from db response |
6363
| `requireParentSpan` | `boolean` | If true, requires a parent span to create new spans (default false) |
6464
| `addSqlCommenterCommentToQueries` | `boolean` | If true, adds [sqlcommenter](https://github.com/open-telemetry/opentelemetry-sqlcommenter) specification compliant comment to queries with tracing context (default false). _NOTE: A comment will not be added to queries that already contain `--` or `/* ... */` in them, even if these are not actually part of comments_ |
65+
| `ignoreConnectSpans` | `boolean` | If true, `pg.connect` and `pg-pool.connect` spans will not be created. Query spans and pool metrics are still recorded (default false) |
6566

6667
## Semantic Conventions
6768

packages/instrumentation-pg/src/instrumentation.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,12 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
249249
const plugin = this;
250250
return (original: PgClientConnect) => {
251251
return function connect(this: pgTypes.Client, callback?: Function) {
252-
if (utils.shouldSkipInstrumentation(plugin.getConfig())) {
252+
const config = plugin.getConfig();
253+
254+
if (
255+
utils.shouldSkipInstrumentation(config) ||
256+
config.ignoreConnectSpans
257+
) {
253258
return original.call(this, callback);
254259
}
255260

@@ -571,7 +576,16 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
571576
const plugin = this;
572577
return (originalConnect: typeof pgPoolTypes.prototype.connect) => {
573578
return function connect(this: PgPoolExtended, callback?: PgPoolCallback) {
574-
if (utils.shouldSkipInstrumentation(plugin.getConfig())) {
579+
const config = plugin.getConfig();
580+
581+
if (utils.shouldSkipInstrumentation(config)) {
582+
return originalConnect.call(this, callback as any);
583+
}
584+
585+
// Still set up event listeners for metrics even when skipping spans
586+
plugin._setPoolConnectEventListeners(this);
587+
588+
if (config.ignoreConnectSpans) {
575589
return originalConnect.call(this, callback as any);
576590
}
577591

@@ -584,8 +598,6 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
584598
),
585599
});
586600

587-
plugin._setPoolConnectEventListeners(this);
588-
589601
if (callback) {
590602
const parentSpan = trace.getSpan(context.active());
591603
callback = utils.patchCallbackPGPool(

packages/instrumentation-pg/src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,12 @@ export interface PgInstrumentationConfig extends InstrumentationConfig {
7979
* the tracing context, following the {@link https://github.com/open-telemetry/opentelemetry-sqlcommenter sqlcommenter} format
8080
*/
8181
addSqlCommenterCommentToQueries?: boolean;
82+
83+
/**
84+
* If true, `pg.connect` and `pg-pool.connect` spans will not be created.
85+
* Query spans and pool metrics are still recorded.
86+
*
87+
* @default false
88+
*/
89+
ignoreConnectSpans?: boolean;
8290
}

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,77 @@ describe('pg-pool', () => {
327327
const spans = memoryExporter.getFinishedSpans();
328328
assert.strictEqual(spans.length, 0);
329329
});
330+
331+
it('should not create connect spans when ignoreConnectSpans=true', async () => {
332+
const newPool = new pgPool(CONFIG);
333+
create({
334+
ignoreConnectSpans: true,
335+
});
336+
const span = provider.getTracer('test-pg-pool').startSpan('test span');
337+
await context.with(trace.setSpan(context.active(), span), async () => {
338+
const client = await newPool.connect();
339+
try {
340+
await client.query('SELECT NOW()');
341+
} finally {
342+
client.release();
343+
}
344+
});
345+
await newPool.end();
346+
347+
const spans = memoryExporter.getFinishedSpans();
348+
const poolConnectSpans = spans.filter(s => s.name === 'pg-pool.connect');
349+
const clientConnectSpans = spans.filter(s => s.name === 'pg.connect');
350+
const querySpans = spans.filter(s => s.name.startsWith('pg.query'));
351+
352+
assert.strictEqual(
353+
poolConnectSpans.length,
354+
0,
355+
'Expected no pg-pool.connect spans'
356+
);
357+
assert.strictEqual(
358+
clientConnectSpans.length,
359+
0,
360+
'Expected no pg.connect spans'
361+
);
362+
assert.ok(querySpans.length > 0, 'Expected query spans to be created');
363+
364+
// Reset config for subsequent tests
365+
create({});
366+
});
367+
368+
it('should still record pool metrics when ignoreConnectSpans=true', async () => {
369+
const metricReader = testUtils.initMeterProvider(instrumentation);
370+
const newPool = new pgPool(CONFIG);
371+
create({
372+
ignoreConnectSpans: true,
373+
});
374+
375+
const client = await newPool.connect();
376+
try {
377+
await client.query('SELECT NOW()');
378+
} finally {
379+
client.release();
380+
}
381+
382+
const { resourceMetrics, errors } = await metricReader.collect();
383+
assert.deepEqual(
384+
errors,
385+
[],
386+
'expected no errors during metric collection'
387+
);
388+
389+
const metrics = resourceMetrics.scopeMetrics[0].metrics;
390+
assert.strictEqual(
391+
metrics[1].descriptor.name,
392+
METRIC_DB_CLIENT_CONNECTION_COUNT,
393+
'Expected connection count metric'
394+
);
395+
396+
await newPool.end();
397+
398+
// Reset config for subsequent tests
399+
create({});
400+
});
330401
});
331402

332403
describe('#pool.query()', () => {

0 commit comments

Comments
 (0)