-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node): Add
generic-pool
integration (#13465)
Resolves #13308 Implement genericPool OTL instrumentation in `packages/node` - [x] If you've added code that should be tested, please add tests. - [x] Ensure your code lints and the test suite passes (`yarn lint`) & (`yarn test`). --------- Signed-off-by: Kaung Zin Hein <[email protected]>
- Loading branch information
1 parent
ce8579c
commit 4774399
Showing
15 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
dev-packages/node-integration-tests/suites/tracing/genericPool/scenario.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
34 changes: 34 additions & 0 deletions
34
dev-packages/node-integration-tests/suites/tracing/genericPool/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: expect.stringMatching(/^generic-pool\.ac?quire/), | ||
origin: 'auto.db.otel.generic-pool', | ||
data: { | ||
'sentry.origin': 'auto.db.otel.generic-pool', | ||
}, | ||
status: 'ok', | ||
}), | ||
|
||
expect.objectContaining({ | ||
description: expect.stringMatching(/^generic-pool\.ac?quire/), | ||
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { GenericPoolInstrumentation } from '@opentelemetry/instrumentation-generic-pool'; | ||
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, defineIntegration, 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); | ||
|
||
const spanDescription = spanJSON.description; | ||
|
||
// typo in emitted span for version <= 0.38.0 of @opentelemetry/instrumentation-generic-pool | ||
const isGenericPoolSpan = | ||
spanDescription === 'generic-pool.aquire' || spanDescription === 'generic-pool.acquire'; | ||
|
||
if (isGenericPoolSpan) { | ||
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.db.otel.generic-pool'); | ||
} | ||
}); | ||
}, | ||
}; | ||
}) satisfies IntegrationFn; | ||
|
||
/** | ||
* GenericPool integration | ||
* | ||
* Capture tracing data for GenericPool. | ||
*/ | ||
export const genericPoolIntegration = defineIntegration(_genericPoolIntegration); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7113,6 +7113,13 @@ | |
"@opentelemetry/core" "^1.8.0" | ||
"@opentelemetry/instrumentation" "^0.52.0" | ||
|
||
"@opentelemetry/[email protected]": | ||
version "0.38.0" | ||
resolved "https://registry.yarnpkg.com/@opentelemetry/instrumentation-generic-pool/-/instrumentation-generic-pool-0.38.0.tgz#9ea4d82da23541cda613d553bd405b2cbc0da184" | ||
integrity sha512-0/ULi6pIco1fEnDPmmAul8ZoudFL7St0hjgBbWZlZPBCSyslDll1J7DFeEbjiRSSyUd+0tu73ae0DOKVKNd7VA== | ||
dependencies: | ||
"@opentelemetry/instrumentation" "^0.52.0" | ||
|
||
"@opentelemetry/[email protected]": | ||
version "0.42.0" | ||
resolved "https://registry.yarnpkg.com/@opentelemetry/instrumentation-graphql/-/instrumentation-graphql-0.42.0.tgz#588a18c39e3b3f655bc09243566172ab0b638d35" | ||
|
@@ -19084,7 +19091,7 @@ generate-function@^2.3.1: | |
dependencies: | ||
is-property "^1.0.2" | ||
|
||
[email protected]: | ||
[email protected], generic-pool@^3.9.0: | ||
version "3.9.0" | ||
resolved "https://registry.yarnpkg.com/generic-pool/-/generic-pool-3.9.0.tgz#36f4a678e963f4fdb8707eab050823abc4e8f5e4" | ||
integrity sha512-hymDOu5B53XvN4QT9dBmZxPX4CWhBPPLguTZ9MMFeFa/Kg0xWVfylOVNlJji/E7yTZWFd/q9GO5TxDLq156D7g== | ||
|