-
-
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
dataloader
integration (#13664)
Adds integration for `dataloader` using [`@opentelemetry/instrumentation-dataloader`](https://www.npmjs.com/package/@opentelemetry/instrumentation-dataloader) on the background. A few notes: - We currently don't have access to the lookup / request as there is no hook from `@opentelemetry/instrumentation-dataloader`. So, we don't have `cache.hit`, `cache.key`, `cache.item_size` and so on, in this integration. I can try to implement those upstream, but if you have another way in mind to access those please let me know. - `@opentelemetry/instrumentation-dataloader` only records spans for `load`, `loadMany` and `batch`, which all are `cache.get` operations. There are also `prime`, `clear`, `clearAll`. We also can implement those upstream and update the integration in future.
- Loading branch information
1 parent
37c4c42
commit 2ab7518
Showing
12 changed files
with
152 additions
and
0 deletions.
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
33 changes: 33 additions & 0 deletions
33
dev-packages/node-integration-tests/suites/tracing/dataloader/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,33 @@ | ||
const { loggingTransport, startExpressServerAndSendPortToRunner } = 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, | ||
}); | ||
|
||
const PORT = 8008; | ||
|
||
// Stop the process from exiting before the transaction is sent | ||
setInterval(() => {}, 1000); | ||
|
||
const run = async () => { | ||
const express = require('express'); | ||
const Dataloader = require('dataloader'); | ||
|
||
const app = express(); | ||
const dataloader = new Dataloader(async keys => keys.map((_, idx) => idx), { | ||
cache: false, | ||
}); | ||
|
||
app.get('/', (req, res) => { | ||
const user = dataloader.load('user-1'); | ||
res.send(user); | ||
}); | ||
|
||
startExpressServerAndSendPortToRunner(app, PORT); | ||
}; | ||
|
||
run(); |
40 changes: 40 additions & 0 deletions
40
dev-packages/node-integration-tests/suites/tracing/dataloader/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,40 @@ | ||
import { cleanupChildProcesses, createRunner } from '../../../utils/runner'; | ||
|
||
describe('dataloader auto-instrumentation', () => { | ||
afterAll(async () => { | ||
cleanupChildProcesses(); | ||
}); | ||
|
||
const EXPECTED_TRANSACTION = { | ||
transaction: 'GET /', | ||
spans: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
'sentry.origin': 'auto.db.otel.dataloader', | ||
'sentry.op': 'cache.get', | ||
}), | ||
description: 'dataloader.load', | ||
origin: 'auto.db.otel.dataloader', | ||
op: 'cache.get', | ||
status: 'ok', | ||
}), | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
'sentry.origin': 'auto.db.otel.dataloader', | ||
'sentry.op': 'cache.get', | ||
}), | ||
description: 'dataloader.batch', | ||
origin: 'auto.db.otel.dataloader', | ||
op: 'cache.get', | ||
status: 'ok', | ||
}), | ||
]), | ||
}; | ||
|
||
test('should auto-instrument `dataloader` package.', done => { | ||
createRunner(__dirname, 'scenario.js') | ||
.expect({ transaction: EXPECTED_TRANSACTION }) | ||
.start(done) | ||
.makeRequest('get', '/'); | ||
}); | ||
}); |
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,57 @@ | ||
import { DataloaderInstrumentation } from '@opentelemetry/instrumentation-dataloader'; | ||
import { | ||
SEMANTIC_ATTRIBUTE_SENTRY_OP, | ||
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, | ||
defineIntegration, | ||
spanToJSON, | ||
} from '@sentry/core'; | ||
import type { IntegrationFn } from '@sentry/types'; | ||
import { generateInstrumentOnce } from '../../otel/instrument'; | ||
|
||
const INTEGRATION_NAME = 'Dataloader'; | ||
|
||
export const instrumentDataloader = generateInstrumentOnce( | ||
INTEGRATION_NAME, | ||
() => | ||
new DataloaderInstrumentation({ | ||
requireParentSpan: true, | ||
}), | ||
); | ||
|
||
const _dataloaderIntegration = (() => { | ||
return { | ||
name: INTEGRATION_NAME, | ||
setupOnce() { | ||
instrumentDataloader(); | ||
}, | ||
|
||
setup(client) { | ||
client.on('spanStart', span => { | ||
const spanJSON = spanToJSON(span); | ||
if (spanJSON.description?.startsWith('dataloader')) { | ||
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.db.otel.dataloader'); | ||
} | ||
|
||
// These are all possible dataloader span descriptions | ||
// Still checking for the future versions | ||
// in case they add support for `clear` and `prime` | ||
if ( | ||
spanJSON.description === 'dataloader.load' || | ||
spanJSON.description === 'dataloader.loadMany' || | ||
spanJSON.description === 'dataloader.batch' | ||
) { | ||
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'cache.get'); | ||
// TODO: We can try adding `key` to the `data` attribute upstream. | ||
// Or alternatively, we can add `requestHook` to the dataloader instrumentation. | ||
} | ||
}); | ||
}, | ||
}; | ||
}) satisfies IntegrationFn; | ||
|
||
/** | ||
* Dataloader integration | ||
* | ||
* Capture tracing data for Dataloader. | ||
*/ | ||
export const dataloaderIntegration = defineIntegration(_dataloaderIntegration); |
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 |
---|---|---|
|
@@ -7101,6 +7101,13 @@ | |
"@opentelemetry/semantic-conventions" "^1.27.0" | ||
"@types/connect" "3.4.36" | ||
|
||
"@opentelemetry/[email protected]": | ||
version "0.12.0" | ||
resolved "https://registry.yarnpkg.com/@opentelemetry/instrumentation-dataloader/-/instrumentation-dataloader-0.12.0.tgz#de03a3948dec4f15fed80aa424d6bd5d6a8d10c7" | ||
integrity sha512-pnPxatoFE0OXIZDQhL2okF//dmbiWFzcSc8pUg9TqofCLYZySSxDCgQc69CJBo5JnI3Gz1KP+mOjS4WAeRIH4g== | ||
dependencies: | ||
"@opentelemetry/instrumentation" "^0.53.0" | ||
|
||
"@opentelemetry/[email protected]": | ||
version "0.42.0" | ||
resolved "https://registry.yarnpkg.com/@opentelemetry/instrumentation-express/-/instrumentation-express-0.42.0.tgz#279f195aa66baee2b98623a16666c6229c8e7564" | ||
|
@@ -15235,6 +15242,11 @@ data-urls@^4.0.0: | |
whatwg-mimetype "^3.0.0" | ||
whatwg-url "^12.0.0" | ||
|
||
[email protected]: | ||
version "2.2.2" | ||
resolved "https://registry.yarnpkg.com/dataloader/-/dataloader-2.2.2.tgz#216dc509b5abe39d43a9b9d97e6e5e473dfbe3e0" | ||
integrity sha512-8YnDaaf7N3k/q5HnTJVuzSyLETjoZjVmHc4AeKAzOvKHEFQKcn64OKBfzHYtE9zGjctNM7V9I0MfnUVLpi7M5g== | ||
|
||
date-fns@^2.29.2: | ||
version "2.29.3" | ||
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.29.3.tgz#27402d2fc67eb442b511b70bbdf98e6411cd68a8" | ||
|