-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(node): Add generic-pool
integration
#13465
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
Changes from 2 commits
aaae010
4d7ddbf
017c7d2
c7aac54
17dc1ad
66ae4b8
1450f3c
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const { loggingTransport } = require('@sentry-internal/node-integration-tests'); | ||
const Sentry = require('@sentry/node'); | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
release: '1.0', | ||
tracesSampleRate: 1.0, | ||
transport: loggingTransport, | ||
}); | ||
|
||
// Stop the process from exiting before the transaction is sent | ||
setInterval(() => {}, 1000); | ||
|
||
const mysql = require('mysql'); | ||
const genericPool = require('generic-pool'); | ||
|
||
const factory = { | ||
create: function () { | ||
return mysql.createConnection({ | ||
user: 'root', | ||
|
||
password: 'docker', | ||
|
||
}); | ||
}, | ||
destroy: function (client) { | ||
client.end(err => { | ||
if (err) { | ||
// eslint-disable-next-line no-console | ||
console.error('Error while disconnecting MySQL:', err); | ||
} | ||
}); | ||
}, | ||
}; | ||
|
||
const opts = { | ||
max: 10, | ||
min: 2, | ||
}; | ||
|
||
const myPool = genericPool.createPool(factory, opts); | ||
|
||
async function run() { | ||
await Sentry.startSpan( | ||
{ | ||
op: 'transaction', | ||
name: 'Test Transaction', | ||
}, | ||
async () => { | ||
try { | ||
const client1 = await myPool.acquire(); | ||
const client2 = await myPool.acquire(); | ||
|
||
client1.query('SELECT NOW()', function () { | ||
myPool.release(client1); | ||
}); | ||
|
||
client2.query('SELECT 1 + 1 AS solution', function () { | ||
myPool.release(client2); | ||
}); | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.error('Error while pooling MySQL:', err); | ||
} finally { | ||
await myPool.drain(); | ||
await myPool.clear(); | ||
} | ||
}, | ||
); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
run(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { cleanupChildProcesses, createRunner } from '../../../utils/runner'; | ||
|
||
describe('genericPool auto instrumentation', () => { | ||
afterAll(() => { | ||
cleanupChildProcesses(); | ||
}); | ||
|
||
test('should auto-instrument `genericPool` package when calling pool.require()', done => { | ||
const EXPECTED_TRANSACTION = { | ||
transaction: 'Test Transaction', | ||
spans: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
description: 'generic-pool.aquire', | ||
origin: 'auto.db.otel.generic-pool', | ||
data: { | ||
'sentry.origin': 'auto.db.otel.generic-pool', | ||
}, | ||
status: 'ok', | ||
}), | ||
|
||
expect.objectContaining({ | ||
description: 'generic-pool.aquire', | ||
origin: 'auto.db.otel.generic-pool', | ||
data: { | ||
'sentry.origin': 'auto.db.otel.generic-pool', | ||
}, | ||
status: 'ok', | ||
}), | ||
]), | ||
}; | ||
|
||
createRunner(__dirname, 'scenario.js').expect({ transaction: EXPECTED_TRANSACTION }).start(done); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { GenericPoolInstrumentation } from '@opentelemetry/instrumentation-generic-pool'; | ||
import { defineIntegration, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, spanToJSON } from '@sentry/core'; | ||
import type { IntegrationFn } from '@sentry/types'; | ||
import { generateInstrumentOnce } from '../../otel/instrument'; | ||
|
||
const INTEGRATION_NAME = 'GenericPool'; | ||
|
||
export const instrumentGenericPool = generateInstrumentOnce(INTEGRATION_NAME, () => new GenericPoolInstrumentation({})); | ||
|
||
const _genericPoolIntegration = (() => { | ||
return { | ||
name: INTEGRATION_NAME, | ||
setupOnce() { | ||
instrumentGenericPool(); | ||
}, | ||
|
||
setup(client) { | ||
client.on('spanStart', span => { | ||
const spanJSON = spanToJSON(span); | ||
if (spanJSON.description === 'generic-pool.aquire') { | ||
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. The description has a typo - missing 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. Fixed in upstream PR. Landed in instrumentation-generic-pool-v0.38.1. |
||
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.db.otel.generic-pool'); | ||
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. According to the docs, trace origin name cannot contain "-". |
||
} | ||
}); | ||
}, | ||
}; | ||
}) satisfies IntegrationFn; | ||
|
||
/** | ||
* GenericPool integration | ||
* | ||
* Capture tracing data for GenericPool. | ||
*/ | ||
export const genericPoolIntegration = defineIntegration(_genericPoolIntegration); |
Uh oh!
There was an error while loading. Please reload this page.