|
| 1 | +import { DataloaderInstrumentation } from '@opentelemetry/instrumentation-dataloader'; |
| 2 | +import { |
| 3 | + SEMANTIC_ATTRIBUTE_SENTRY_OP, |
| 4 | + SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, |
| 5 | + defineIntegration, |
| 6 | + spanToJSON, |
| 7 | +} from '@sentry/core'; |
| 8 | +import type { IntegrationFn } from '@sentry/types'; |
| 9 | +import { generateInstrumentOnce } from '../../otel/instrument'; |
| 10 | + |
| 11 | +const INTEGRATION_NAME = 'Dataloader'; |
| 12 | + |
| 13 | +export const instrumentDataloader = generateInstrumentOnce( |
| 14 | + INTEGRATION_NAME, |
| 15 | + () => |
| 16 | + new DataloaderInstrumentation({ |
| 17 | + requireParentSpan: true, |
| 18 | + }), |
| 19 | +); |
| 20 | + |
| 21 | +const _dataloaderIntegration = (() => { |
| 22 | + return { |
| 23 | + name: INTEGRATION_NAME, |
| 24 | + setupOnce() { |
| 25 | + instrumentDataloader(); |
| 26 | + }, |
| 27 | + |
| 28 | + setup(client) { |
| 29 | + client.on('spanStart', span => { |
| 30 | + const spanJSON = spanToJSON(span); |
| 31 | + if (spanJSON.description?.startsWith('dataloader')) { |
| 32 | + span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.db.otel.dataloader'); |
| 33 | + } |
| 34 | + |
| 35 | + // These are all possible dataloader span descriptions |
| 36 | + // Still checking for the future versions |
| 37 | + // in case they add support for `clear` and `prime` |
| 38 | + if ( |
| 39 | + spanJSON.description === 'dataloader.load' || |
| 40 | + spanJSON.description === 'dataloader.loadMany' || |
| 41 | + spanJSON.description === 'dataloader.batch' |
| 42 | + ) { |
| 43 | + span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'cache.get'); |
| 44 | + // TODO: We can try adding `key` to the `data` attribute upstream. |
| 45 | + // Or alternatively, we can add `requestHook` to the dataloader instrumentation. |
| 46 | + } |
| 47 | + }); |
| 48 | + }, |
| 49 | + }; |
| 50 | +}) satisfies IntegrationFn; |
| 51 | + |
| 52 | +/** |
| 53 | + * Dataloader integration |
| 54 | + * |
| 55 | + * Capture tracing data for Dataloader. |
| 56 | + */ |
| 57 | +export const dataloaderIntegration = defineIntegration(_dataloaderIntegration); |
0 commit comments