Skip to content
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

feat(node): Add dataloader integration #13664

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dev-packages/node-integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"connect": "^3.7.0",
"cors": "^2.8.5",
"cron": "^3.1.6",
"dataloader": "2.2.2",
"express": "^4.17.3",
"generic-pool": "^3.9.0",
"graphql": "^16.3.0",
Expand Down
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();
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', '/');
});
});
1 change: 1 addition & 0 deletions packages/astro/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export {
createGetModuleFromFilename,
createTransport,
cron,
dataloaderIntegration,
debugIntegration,
dedupeIntegration,
DEFAULT_USER_INCLUDES,
Expand Down
1 change: 1 addition & 0 deletions packages/aws-serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export {
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE,
dataloaderIntegration,
expressIntegration,
expressErrorHandler,
setupExpressErrorHandler,
Expand Down
1 change: 1 addition & 0 deletions packages/bun/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export {
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE,
dataloaderIntegration,
expressIntegration,
expressErrorHandler,
setupExpressErrorHandler,
Expand Down
1 change: 1 addition & 0 deletions packages/google-cloud-serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export {
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE,
dataloaderIntegration,
expressIntegration,
expressErrorHandler,
setupExpressErrorHandler,
Expand Down
1 change: 1 addition & 0 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"@opentelemetry/core": "^1.25.1",
"@opentelemetry/instrumentation": "^0.53.0",
"@opentelemetry/instrumentation-connect": "0.39.0",
"@opentelemetry/instrumentation-dataloader": "0.12.0",
"@opentelemetry/instrumentation-express": "0.42.0",
"@opentelemetry/instrumentation-fastify": "0.39.0",
"@opentelemetry/instrumentation-fs": "0.15.0",
Expand Down
1 change: 1 addition & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export { koaIntegration, setupKoaErrorHandler } from './integrations/tracing/koa
export { connectIntegration, setupConnectErrorHandler } from './integrations/tracing/connect';
export { spotlightIntegration } from './integrations/spotlight';
export { genericPoolIntegration } from './integrations/tracing/genericPool';
export { dataloaderIntegration } from './integrations/tracing/dataloader';

export { SentryContextManager } from './otel/contextManager';
export { generateInstrumentOnce } from './otel/instrument';
Expand Down
51 changes: 51 additions & 0 deletions packages/node/src/integrations/tracing/dataloader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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({}));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should set requireParentSpan to be true by default, and then expose config to change it accordingly, but I'm open to other opinions.

@mydea what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's set requireParentSpan to be true by default @onurtemizkan. We don't need to expose config for now, we can add that later based on user request.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realised it's also true by default in DataloaderIntegration. Still, passed true from our side, and we can expose it later if we need to.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this sounds reasonable to me, I guess you rarely want this to be a transaction!


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);
3 changes: 3 additions & 0 deletions packages/node/src/integrations/tracing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Integration } from '@sentry/types';
import { instrumentHttp } from '../http';

import { connectIntegration, instrumentConnect } from './connect';
import { dataloaderIntegration, instrumentDataloader } from './dataloader';
import { expressIntegration, instrumentExpress } from './express';
import { fastifyIntegration, instrumentFastify } from './fastify';
import { genericPoolIntegration, instrumentGenericPool } from './genericPool';
Expand Down Expand Up @@ -41,6 +42,7 @@ export function getAutoPerformanceIntegrations(): Integration[] {
connectIntegration(),
genericPoolIntegration(),
kafkaIntegration(),
dataloaderIntegration(),
];
}

Expand All @@ -67,5 +69,6 @@ export function getOpenTelemetryInstrumentationToPreload(): (((options?: any) =>
instrumentGraphql,
instrumentRedis,
instrumentGenericPool,
instrumentDataloader,
];
}
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
Loading