|
| 1 | +import type { TransactionEvent } from '@sentry/core'; |
| 2 | +import { MongoMemoryServer } from 'mongodb-memory-server-global'; |
| 3 | +import { afterAll, beforeAll, describe, expect } from 'vitest'; |
| 4 | +import { isOrchestrionEnabled } from '../../../utils'; |
| 5 | +import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner'; |
| 6 | + |
| 7 | +// Pins mongodb 5 so the >= 4.0 < 6.4 callback-based command band and the |
| 8 | +// pool-checkout context propagation are exercised against a real mongodb. |
| 9 | +describe('MongoDB v5 auto-instrumentation', () => { |
| 10 | + let mongoServer: MongoMemoryServer; |
| 11 | + |
| 12 | + beforeAll(async () => { |
| 13 | + mongoServer = await MongoMemoryServer.create(); |
| 14 | + process.env.MONGO_URL = mongoServer.getUri(); |
| 15 | + }, 30000); |
| 16 | + |
| 17 | + afterAll(async () => { |
| 18 | + if (mongoServer) { |
| 19 | + await mongoServer.stop(); |
| 20 | + } |
| 21 | + cleanupChildProcesses(); |
| 22 | + }); |
| 23 | + |
| 24 | + const origin = isOrchestrionEnabled() ? 'auto.db.orchestrion.mongo' : 'auto.db.otel.mongo'; |
| 25 | + |
| 26 | + const spanFor = (operation: string): unknown => |
| 27 | + expect.objectContaining({ |
| 28 | + data: expect.objectContaining({ |
| 29 | + 'sentry.origin': origin, |
| 30 | + 'sentry.op': 'db', |
| 31 | + 'db.system': 'mongodb', |
| 32 | + 'db.name': 'admin', |
| 33 | + 'db.mongodb.collection': 'movies', |
| 34 | + 'db.operation': operation, |
| 35 | + 'db.connection_string': expect.any(String), |
| 36 | + 'db.statement': expect.any(String), |
| 37 | + }), |
| 38 | + op: 'db', |
| 39 | + origin, |
| 40 | + }); |
| 41 | + |
| 42 | + createEsmAndCjsTests( |
| 43 | + __dirname, |
| 44 | + 'scenario.mjs', |
| 45 | + 'instrument.mjs', |
| 46 | + (createTestRunner, test) => { |
| 47 | + test('auto-instruments `mongodb` (>= 4.0 < 6.4 callback command) and parents pooled ops correctly.', async () => { |
| 48 | + await createTestRunner() |
| 49 | + .expect({ |
| 50 | + transaction: (event: TransactionEvent) => { |
| 51 | + const spans = event.spans || []; |
| 52 | + expect(spans).toContainEqual(spanFor('insert')); |
| 53 | + expect(spans).toContainEqual(spanFor('find')); |
| 54 | + expect(spans).toContainEqual(spanFor('update')); |
| 55 | + |
| 56 | + // Checkout context propagation: each `op-*` span must be the |
| 57 | + // parent of its own find command. A lost context would collapse |
| 58 | + // them onto one parent (or the transaction). |
| 59 | + const opIds = new Set(spans.filter(s => /^op-[abc]$/.test(s.description ?? '')).map(s => s.span_id)); |
| 60 | + expect(opIds.size).toBe(3); |
| 61 | + const pooledFinds = spans.filter( |
| 62 | + s => |
| 63 | + s.origin === origin && |
| 64 | + (s.data as Record<string, unknown>)?.['db.operation'] === 'find' && |
| 65 | + opIds.has(s.parent_span_id as string), |
| 66 | + ); |
| 67 | + expect(new Set(pooledFinds.map(s => s.parent_span_id)).size).toBe(3); |
| 68 | + }, |
| 69 | + }) |
| 70 | + .start() |
| 71 | + .completed(); |
| 72 | + }); |
| 73 | + }, |
| 74 | + { additionalDependencies: { mongodb: '5.9.2' } }, |
| 75 | + ); |
| 76 | +}); |
0 commit comments