|
| 1 | +import type { Envelope } from '@sentry/core'; |
| 2 | +import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core'; |
| 3 | +import { expect, it } from 'vitest'; |
| 4 | +import { createRunner } from '../../../runner'; |
| 5 | + |
| 6 | +const flushMarkerMatcher = (envelope: Envelope): void => { |
| 7 | + const [, items] = envelope; |
| 8 | + const [itemHeader, itemBody] = items[0] as [{ type: string }, Record<string, unknown>]; |
| 9 | + |
| 10 | + expect(itemHeader.type).toBe('event'); |
| 11 | + expect(itemBody.message).toBe('flush-marker'); |
| 12 | +}; |
| 13 | + |
| 14 | +it('instruments SQL exec operations on Durable Object storage', async ({ signal }) => { |
| 15 | + const runner = createRunner(__dirname) |
| 16 | + .expect(envelope => { |
| 17 | + const transactionEvent = envelope[1]?.[0]?.[1]; |
| 18 | + const spans = transactionEvent?.spans ?? []; |
| 19 | + |
| 20 | + expect(transactionEvent).toEqual( |
| 21 | + expect.objectContaining({ |
| 22 | + type: 'transaction', |
| 23 | + transaction: 'GET /exec', |
| 24 | + }), |
| 25 | + ); |
| 26 | + |
| 27 | + const sqlSpans = (spans as Array<Record<string, unknown>>).filter( |
| 28 | + s => s.origin === 'auto.db.cloudflare.durable_object.sql', |
| 29 | + ); |
| 30 | + |
| 31 | + expect(sqlSpans).toHaveLength(3); |
| 32 | + expect(sqlSpans).toEqual( |
| 33 | + expect.arrayContaining([ |
| 34 | + expect.objectContaining({ |
| 35 | + description: 'CREATE TABLE users', |
| 36 | + op: 'db.query', |
| 37 | + origin: 'auto.db.cloudflare.durable_object.sql', |
| 38 | + data: expect.objectContaining({ |
| 39 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'db.query', |
| 40 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.db.cloudflare.durable_object.sql', |
| 41 | + 'db.system.name': 'cloudflare-durable-object-sql', |
| 42 | + 'db.operation.name': 'exec', |
| 43 | + 'db.query.text': 'CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)', |
| 44 | + 'db.query.summary': 'CREATE TABLE users', |
| 45 | + 'cloudflare.durable_object.query.bindings': 0, |
| 46 | + }), |
| 47 | + }), |
| 48 | + expect.objectContaining({ |
| 49 | + description: 'INSERT users', |
| 50 | + op: 'db.query', |
| 51 | + origin: 'auto.db.cloudflare.durable_object.sql', |
| 52 | + data: expect.objectContaining({ |
| 53 | + 'db.system.name': 'cloudflare-durable-object-sql', |
| 54 | + 'db.operation.name': 'exec', |
| 55 | + 'db.query.text': 'INSERT INTO users (name) VALUES (?)', |
| 56 | + 'db.query.summary': 'INSERT users', |
| 57 | + 'cloudflare.durable_object.query.bindings': 1, |
| 58 | + }), |
| 59 | + }), |
| 60 | + expect.objectContaining({ |
| 61 | + description: 'SELECT users', |
| 62 | + op: 'db.query', |
| 63 | + origin: 'auto.db.cloudflare.durable_object.sql', |
| 64 | + data: expect.objectContaining({ |
| 65 | + 'db.system.name': 'cloudflare-durable-object-sql', |
| 66 | + 'db.operation.name': 'exec', |
| 67 | + 'db.query.text': 'SELECT * FROM users', |
| 68 | + 'db.query.summary': 'SELECT users', |
| 69 | + 'cloudflare.durable_object.query.bindings': 0, |
| 70 | + }), |
| 71 | + }), |
| 72 | + ]), |
| 73 | + ); |
| 74 | + }) |
| 75 | + .expect(flushMarkerMatcher) |
| 76 | + .unordered() |
| 77 | + .start(signal); |
| 78 | + |
| 79 | + await runner.makeRequest('get', '/exec'); |
| 80 | + await runner.makeRequest('get', '/flush-marker'); |
| 81 | + await runner.completed(); |
| 82 | +}); |
0 commit comments