-
Notifications
You must be signed in to change notification settings - Fork 621
feat(instrumentation-pg): add skipConnectSpans option #3280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -249,7 +249,9 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf | |||||
| const plugin = this; | ||||||
| return (original: PgClientConnect) => { | ||||||
| return function connect(this: pgTypes.Client, callback?: Function) { | ||||||
| if (utils.shouldSkipInstrumentation(plugin.getConfig())) { | ||||||
| const config = plugin.getConfig(); | ||||||
|
|
||||||
| if (utils.shouldSkipInstrumentation(config) || config.skipConnectSpans) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| return original.call(this, callback); | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -574,7 +576,16 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf | |||||
| const plugin = this; | ||||||
| return (originalConnect: typeof pgPoolTypes.prototype.connect) => { | ||||||
| return function connect(this: PgPoolExtended, callback?: PgPoolCallback) { | ||||||
| if (utils.shouldSkipInstrumentation(plugin.getConfig())) { | ||||||
| const config = plugin.getConfig(); | ||||||
|
|
||||||
| if (utils.shouldSkipInstrumentation(config)) { | ||||||
| return originalConnect.call(this, callback as any); | ||||||
| } | ||||||
|
|
||||||
| // Still set up event listeners for metrics even when skipping spans | ||||||
| plugin._setPoolConnectEventListeners(this); | ||||||
|
|
||||||
| if (config.skipConnectSpans) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| return originalConnect.call(this, callback as any); | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -587,8 +598,6 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf | |||||
| ), | ||||||
| }); | ||||||
|
|
||||||
| plugin._setPoolConnectEventListeners(this); | ||||||
|
|
||||||
| if (callback) { | ||||||
| const parentSpan = trace.getSpan(context.active()); | ||||||
| callback = utils.patchCallbackPGPool( | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -79,4 +79,12 @@ export interface PgInstrumentationConfig extends InstrumentationConfig { | |||||
| * the tracing context, following the {@link https://github.com/open-telemetry/opentelemetry-sqlcommenter sqlcommenter} format | ||||||
| */ | ||||||
| addSqlCommenterCommentToQueries?: boolean; | ||||||
|
|
||||||
| /** | ||||||
| * If true, `pg.connect` and `pg-pool.connect` spans will not be created. | ||||||
| * Query spans and pool metrics are still recorded. | ||||||
| * | ||||||
| * @default false | ||||||
| */ | ||||||
| skipConnectSpans?: boolean; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -327,6 +327,67 @@ describe('pg-pool', () => { | |||||
| const spans = memoryExporter.getFinishedSpans(); | ||||||
| assert.strictEqual(spans.length, 0); | ||||||
| }); | ||||||
|
|
||||||
| it('should not create connect spans when skipConnectSpans=true', async () => { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| const newPool = new pgPool(CONFIG); | ||||||
| create({ | ||||||
| skipConnectSpans: true, | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| }); | ||||||
| const span = provider.getTracer('test-pg-pool').startSpan('test span'); | ||||||
| await context.with(trace.setSpan(context.active(), span), async () => { | ||||||
| const client = await newPool.connect(); | ||||||
| try { | ||||||
| await client.query('SELECT NOW()'); | ||||||
| } finally { | ||||||
| client.release(); | ||||||
| } | ||||||
| }); | ||||||
| await newPool.end(); | ||||||
|
|
||||||
| const spans = memoryExporter.getFinishedSpans(); | ||||||
| const poolConnectSpans = spans.filter(s => s.name === 'pg-pool.connect'); | ||||||
| const clientConnectSpans = spans.filter(s => s.name === 'pg.connect'); | ||||||
| const querySpans = spans.filter(s => s.name.startsWith('pg.query')); | ||||||
|
|
||||||
| assert.strictEqual( | ||||||
| poolConnectSpans.length, | ||||||
| 0, | ||||||
| 'Expected no pg-pool.connect spans' | ||||||
| ); | ||||||
| assert.strictEqual( | ||||||
| clientConnectSpans.length, | ||||||
| 0, | ||||||
| 'Expected no pg.connect spans' | ||||||
| ); | ||||||
| assert.ok(querySpans.length > 0, 'Expected query spans to be created'); | ||||||
| }); | ||||||
|
|
||||||
| it('should still record pool metrics when skipConnectSpans=true', async () => { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| const metricReader = testUtils.initMeterProvider(instrumentation); | ||||||
| const newPool = new pgPool(CONFIG); | ||||||
| create({ | ||||||
| skipConnectSpans: true, | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| }); | ||||||
|
|
||||||
| const client = await newPool.connect(); | ||||||
| try { | ||||||
| await client.query('SELECT NOW()'); | ||||||
| } finally { | ||||||
| client.release(); | ||||||
| } | ||||||
|
|
||||||
| const { resourceMetrics, errors } = await metricReader.collect(); | ||||||
| assert.deepEqual(errors, [], 'expected no errors during metric collection'); | ||||||
|
|
||||||
| const metrics = resourceMetrics.scopeMetrics[0].metrics; | ||||||
| assert.strictEqual( | ||||||
| metrics[1].descriptor.name, | ||||||
| METRIC_DB_CLIENT_CONNECTION_COUNT, | ||||||
| 'Expected connection count metric' | ||||||
| ); | ||||||
|
|
||||||
| await newPool.end(); | ||||||
| }); | ||||||
| }); | ||||||
|
|
||||||
| describe('#pool.query()', () => { | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.