|
| 1 | +import { bench, describe } from 'vitest'; |
| 2 | +import { |
| 3 | + addBreadcrumb, |
| 4 | + getCurrentScope, |
| 5 | + getIsolationScope, |
| 6 | + setCurrentClient, |
| 7 | + startSpan, |
| 8 | +} from '../../src'; |
| 9 | +import { getDefaultTestClientOptions, TestClient } from '../mocks/client'; |
| 10 | +import { clearGlobalScope } from '../testutils'; |
| 11 | + |
| 12 | +function setupClient() { |
| 13 | + clearGlobalScope(); |
| 14 | + getCurrentScope().clear(); |
| 15 | + getIsolationScope().clear(); |
| 16 | + |
| 17 | + const client = new TestClient( |
| 18 | + getDefaultTestClientOptions({ |
| 19 | + dsn: 'https://username@domain/123', |
| 20 | + enableSend: true, |
| 21 | + tracesSampleRate: 1, |
| 22 | + release: '1.0.0', |
| 23 | + environment: 'production', |
| 24 | + }), |
| 25 | + ); |
| 26 | + setCurrentClient(client); |
| 27 | + client.init(); |
| 28 | + return client; |
| 29 | +} |
| 30 | + |
| 31 | +function addRealisticScopeData() { |
| 32 | + getCurrentScope().setUser({ id: '123', email: 'user@example.com' }); |
| 33 | + getCurrentScope().setTag('service', 'api-gateway'); |
| 34 | + getCurrentScope().setTag('region', 'us-east-1'); |
| 35 | + getCurrentScope().setTag('version', '2.1.0'); |
| 36 | + getCurrentScope().setExtra('request_id', 'req-abc-123'); |
| 37 | + for (let i = 0; i < 10; i++) { |
| 38 | + addBreadcrumb({ message: `Action ${i}`, category: 'http', level: 'info' }); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +describe('startSpan pipeline - realistic scope', () => { |
| 43 | + setupClient(); |
| 44 | + addRealisticScopeData(); |
| 45 | + |
| 46 | + bench('single root span', () => { |
| 47 | + startSpan({ name: 'GET /api/users', op: 'http.server' }, () => { |
| 48 | + // span lifecycle only |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + bench('root span + 5 child spans', () => { |
| 53 | + startSpan({ name: 'GET /api/users', op: 'http.server' }, () => { |
| 54 | + for (let i = 0; i < 5; i++) { |
| 55 | + startSpan({ name: `SELECT * FROM users WHERE id = $${i + 1}`, op: 'db' }, span => { |
| 56 | + span.setAttribute('db.system', 'postgresql'); |
| 57 | + span.setAttribute('db.name', 'mydb'); |
| 58 | + }); |
| 59 | + } |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + bench('root span + 10 child spans', () => { |
| 64 | + startSpan({ name: 'GET /api/users', op: 'http.server' }, () => { |
| 65 | + for (let i = 0; i < 10; i++) { |
| 66 | + startSpan( |
| 67 | + { name: i < 5 ? `db.query.${i}` : `http.request.${i}`, op: i < 5 ? 'db' : 'http.client' }, |
| 68 | + span => { |
| 69 | + span.setAttribute('key', 'value'); |
| 70 | + }, |
| 71 | + ); |
| 72 | + } |
| 73 | + }); |
| 74 | + }); |
| 75 | +}); |
0 commit comments