|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { estimateSerializedSpanSizeInBytes } from '../../../../src/tracing/spans/estimateSize'; |
| 3 | +import type { SerializedStreamedSpan } from '../../../../src/types-hoist/span'; |
| 4 | + |
| 5 | +// Produces a realistic trace_id (32 hex chars) and span_id (16 hex chars) |
| 6 | +const TRACE_ID = 'a1b2c3d4e5f607189a0b1c2d3e4f5060'; |
| 7 | +const SPAN_ID = 'a1b2c3d4e5f60718'; |
| 8 | + |
| 9 | +describe('estimateSerializedSpanSizeInBytes', () => { |
| 10 | + it('estimates a minimal span (no attributes, no links, no parent) within a reasonable range of JSON.stringify', () => { |
| 11 | + const span: SerializedStreamedSpan = { |
| 12 | + trace_id: TRACE_ID, |
| 13 | + span_id: SPAN_ID, |
| 14 | + name: 'GET /api/users', |
| 15 | + start_timestamp: 1740000000.123, |
| 16 | + end_timestamp: 1740000001.456, |
| 17 | + status: 'ok', |
| 18 | + is_segment: true, |
| 19 | + }; |
| 20 | + |
| 21 | + const estimate = estimateSerializedSpanSizeInBytes(span); |
| 22 | + const actual = JSON.stringify(span).length; |
| 23 | + |
| 24 | + expect(estimate).toBe(184); |
| 25 | + expect(actual).toBe(196); |
| 26 | + |
| 27 | + expect(estimate).toBeLessThanOrEqual(actual * 1.2); |
| 28 | + expect(estimate).toBeGreaterThanOrEqual(actual * 0.8); |
| 29 | + }); |
| 30 | + |
| 31 | + it('estimates a span with a parent_span_id within a reasonable range', () => { |
| 32 | + const span: SerializedStreamedSpan = { |
| 33 | + trace_id: TRACE_ID, |
| 34 | + span_id: SPAN_ID, |
| 35 | + parent_span_id: 'b2c3d4e5f6071890', |
| 36 | + name: 'db.query', |
| 37 | + start_timestamp: 1740000000.0, |
| 38 | + end_timestamp: 1740000000.05, |
| 39 | + status: 'ok', |
| 40 | + is_segment: false, |
| 41 | + }; |
| 42 | + |
| 43 | + const estimate = estimateSerializedSpanSizeInBytes(span); |
| 44 | + const actual = JSON.stringify(span).length; |
| 45 | + |
| 46 | + expect(estimate).toBe(172); |
| 47 | + expect(actual).toBe(222); |
| 48 | + |
| 49 | + expect(estimate).toBeLessThanOrEqual(actual * 1.1); |
| 50 | + expect(estimate).toBeGreaterThanOrEqual(actual * 0.7); |
| 51 | + }); |
| 52 | + |
| 53 | + it('estimates a span with string attributes within a reasonable range', () => { |
| 54 | + const span: SerializedStreamedSpan = { |
| 55 | + trace_id: TRACE_ID, |
| 56 | + span_id: SPAN_ID, |
| 57 | + name: 'GET /api/users', |
| 58 | + start_timestamp: 1740000000.0, |
| 59 | + end_timestamp: 1740000000.1, |
| 60 | + status: 'ok', |
| 61 | + is_segment: false, |
| 62 | + attributes: { |
| 63 | + 'http.method': { type: 'string', value: 'GET' }, |
| 64 | + 'http.url': { type: 'string', value: 'https://example.com/api/users?page=1&limit=100' }, |
| 65 | + 'http.status_code': { type: 'integer', value: 200 }, |
| 66 | + 'db.statement': { type: 'string', value: 'SELECT * FROM users WHERE id = $1' }, |
| 67 | + 'sentry.origin': { type: 'string', value: 'auto.http.fetch' }, |
| 68 | + }, |
| 69 | + }; |
| 70 | + |
| 71 | + const estimate = estimateSerializedSpanSizeInBytes(span); |
| 72 | + const actual = JSON.stringify(span).length; |
| 73 | + |
| 74 | + expect(estimate).toBeLessThanOrEqual(actual * 1.2); |
| 75 | + expect(estimate).toBeGreaterThanOrEqual(actual * 0.8); |
| 76 | + }); |
| 77 | + |
| 78 | + it('estimates a span with numeric attributes within a reasonable range', () => { |
| 79 | + const span: SerializedStreamedSpan = { |
| 80 | + trace_id: TRACE_ID, |
| 81 | + span_id: SPAN_ID, |
| 82 | + name: 'process.task', |
| 83 | + start_timestamp: 1740000000.0, |
| 84 | + end_timestamp: 1740000005.0, |
| 85 | + status: 'ok', |
| 86 | + is_segment: false, |
| 87 | + attributes: { |
| 88 | + 'items.count': { type: 'integer', value: 42 }, |
| 89 | + 'duration.ms': { type: 'double', value: 5000.5 }, |
| 90 | + 'retry.count': { type: 'integer', value: 3 }, |
| 91 | + }, |
| 92 | + }; |
| 93 | + |
| 94 | + const estimate = estimateSerializedSpanSizeInBytes(span); |
| 95 | + const actual = JSON.stringify(span).length; |
| 96 | + |
| 97 | + expect(estimate).toBeLessThanOrEqual(actual * 1.2); |
| 98 | + expect(estimate).toBeGreaterThanOrEqual(actual * 0.8); |
| 99 | + }); |
| 100 | + |
| 101 | + it('estimates a span with boolean attributes within a reasonable range', () => { |
| 102 | + const span: SerializedStreamedSpan = { |
| 103 | + trace_id: TRACE_ID, |
| 104 | + span_id: SPAN_ID, |
| 105 | + name: 'cache.get', |
| 106 | + start_timestamp: 1740000000.0, |
| 107 | + end_timestamp: 1740000000.002, |
| 108 | + status: 'ok', |
| 109 | + is_segment: false, |
| 110 | + attributes: { |
| 111 | + 'cache.hit': { type: 'boolean', value: true }, |
| 112 | + 'cache.miss': { type: 'boolean', value: false }, |
| 113 | + }, |
| 114 | + }; |
| 115 | + |
| 116 | + const estimate = estimateSerializedSpanSizeInBytes(span); |
| 117 | + const actual = JSON.stringify(span).length; |
| 118 | + |
| 119 | + expect(estimate).toBeLessThanOrEqual(actual * 1.2); |
| 120 | + expect(estimate).toBeGreaterThanOrEqual(actual * 0.8); |
| 121 | + }); |
| 122 | + |
| 123 | + it('estimates a span with array attributes within a reasonable range', () => { |
| 124 | + const span: SerializedStreamedSpan = { |
| 125 | + trace_id: TRACE_ID, |
| 126 | + span_id: SPAN_ID, |
| 127 | + name: 'batch.process', |
| 128 | + start_timestamp: 1740000000.0, |
| 129 | + end_timestamp: 1740000002.0, |
| 130 | + status: 'ok', |
| 131 | + is_segment: false, |
| 132 | + attributes: { |
| 133 | + 'item.ids': { type: 'string[]', value: ['id-001', 'id-002', 'id-003', 'id-004', 'id-005'] }, |
| 134 | + scores: { type: 'double[]', value: [1.1, 2.2, 3.3, 4.4] }, |
| 135 | + flags: { type: 'boolean[]', value: [true, false, true] }, |
| 136 | + }, |
| 137 | + }; |
| 138 | + |
| 139 | + const estimate = estimateSerializedSpanSizeInBytes(span); |
| 140 | + const actual = JSON.stringify(span).length; |
| 141 | + |
| 142 | + expect(estimate).toBeLessThanOrEqual(actual * 1.2); |
| 143 | + expect(estimate).toBeGreaterThanOrEqual(actual * 0.8); |
| 144 | + }); |
| 145 | + |
| 146 | + it('estimates a span with links within a reasonable range', () => { |
| 147 | + const span: SerializedStreamedSpan = { |
| 148 | + trace_id: TRACE_ID, |
| 149 | + span_id: SPAN_ID, |
| 150 | + name: 'linked.operation', |
| 151 | + start_timestamp: 1740000000.0, |
| 152 | + end_timestamp: 1740000001.0, |
| 153 | + status: 'ok', |
| 154 | + is_segment: true, |
| 155 | + links: [ |
| 156 | + { |
| 157 | + trace_id: 'b2c3d4e5f607189a0b1c2d3e4f506070', |
| 158 | + span_id: 'c3d4e5f607189a0b', |
| 159 | + sampled: true, |
| 160 | + attributes: { |
| 161 | + 'sentry.link.type': { type: 'string', value: 'previous_trace' }, |
| 162 | + }, |
| 163 | + }, |
| 164 | + { |
| 165 | + trace_id: 'c3d4e5f607189a0b1c2d3e4f50607080', |
| 166 | + span_id: 'd4e5f607189a0b1c', |
| 167 | + }, |
| 168 | + ], |
| 169 | + }; |
| 170 | + |
| 171 | + const estimate = estimateSerializedSpanSizeInBytes(span); |
| 172 | + const actual = JSON.stringify(span).length; |
| 173 | + |
| 174 | + expect(estimate).toBeLessThanOrEqual(actual * 1.2); |
| 175 | + expect(estimate).toBeGreaterThanOrEqual(actual * 0.8); |
| 176 | + }); |
| 177 | +}); |
0 commit comments