|
| 1 | +import 'package:sentry/sentry.dart'; |
| 2 | +import 'package:sentry/src/sentry_tracer.dart'; |
| 3 | +import 'package:test/test.dart'; |
| 4 | + |
| 5 | +import 'test_utils.dart'; |
| 6 | + |
| 7 | +void main() { |
| 8 | + group('PropagationContext', () { |
| 9 | + group('traceId', () { |
| 10 | + test('is a new trace id by default', () { |
| 11 | + final hub = Hub(defaultTestOptions()); |
| 12 | + final sut = hub.scope.propagationContext; |
| 13 | + final traceId = sut.traceId; |
| 14 | + expect(traceId, isNotNull); |
| 15 | + }); |
| 16 | + |
| 17 | + test('is reused for transactions within the same trace', () { |
| 18 | + final options = defaultTestOptions()..tracesSampleRate = 1.0; |
| 19 | + final hub = Hub(options); |
| 20 | + final sut = hub.scope.propagationContext; |
| 21 | + |
| 22 | + final tx1 = hub.startTransaction('tx1', 'op') as SentryTracer; |
| 23 | + final traceId1 = sut.traceId; |
| 24 | + |
| 25 | + final tx2 = hub.startTransaction('tx2', 'op') as SentryTracer; |
| 26 | + final traceId2 = sut.traceId; |
| 27 | + |
| 28 | + expect(tx1.context.traceId, equals(tx2.context.traceId)); |
| 29 | + expect(tx1.context.traceId, equals(traceId1)); |
| 30 | + expect(traceId1, equals(traceId2)); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + group('sampleRand', () { |
| 35 | + test('is null by default', () { |
| 36 | + final hub = Hub(defaultTestOptions()); |
| 37 | + final sut = hub.scope.propagationContext; |
| 38 | + final sampleRand = sut.sampleRand; |
| 39 | + expect(sampleRand, isNull); |
| 40 | + }); |
| 41 | + |
| 42 | + test('is set by the first transaction and stays unchanged', () { |
| 43 | + final options = defaultTestOptions()..tracesSampleRate = 1.0; |
| 44 | + final hub = Hub(options); |
| 45 | + final sut = hub.scope.propagationContext; |
| 46 | + |
| 47 | + final tx1 = hub.startTransaction('tx1', 'op') as SentryTracer; |
| 48 | + final rand1 = tx1.samplingDecision?.sampleRand; |
| 49 | + expect(rand1, isNotNull); |
| 50 | + |
| 51 | + final tx2 = hub.startTransaction('tx2', 'op') as SentryTracer; |
| 52 | + final rand2 = tx2.samplingDecision?.sampleRand; |
| 53 | + |
| 54 | + expect(rand2, equals(rand1)); |
| 55 | + expect(rand1, equals(sut.sampleRand)); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + group('sampled', () { |
| 60 | + test('is null by default', () { |
| 61 | + final hub = Hub(defaultTestOptions()..tracesSampleRate = 1.0); |
| 62 | + final sut = hub.scope.propagationContext; |
| 63 | + expect(sut.sampled, isNull); |
| 64 | + }); |
| 65 | + |
| 66 | + test('is set by the first transaction and stays unchanged', () { |
| 67 | + final hub = Hub(defaultTestOptions()..tracesSampleRate = 1.0); |
| 68 | + final sut = hub.scope.propagationContext; |
| 69 | + // 1. Start the first (root) transaction with an explicit sampled = true. |
| 70 | + final txContextTrue = SentryTransactionContext( |
| 71 | + 'trx', |
| 72 | + 'op', |
| 73 | + samplingDecision: SentryTracesSamplingDecision(true), |
| 74 | + ); |
| 75 | + hub.startTransactionWithContext(txContextTrue); |
| 76 | + |
| 77 | + expect(sut.sampled, isTrue); |
| 78 | + |
| 79 | + // 2. Start a second transaction with sampled = false – the flag must not change. |
| 80 | + final txContextFalse = SentryTransactionContext( |
| 81 | + 'trx-2', |
| 82 | + 'op', |
| 83 | + samplingDecision: SentryTracesSamplingDecision(false), |
| 84 | + ); |
| 85 | + hub.startTransactionWithContext(txContextFalse); |
| 86 | + |
| 87 | + expect(sut.sampled, isTrue, |
| 88 | + reason: 'sampled flag must remain unchanged for the trace'); |
| 89 | + }); |
| 90 | + |
| 91 | + test('is reset when a new trace is generated', () { |
| 92 | + final hub = Hub(defaultTestOptions()..tracesSampleRate = 1.0); |
| 93 | + final sut = hub.scope.propagationContext; |
| 94 | + final txContext = SentryTransactionContext( |
| 95 | + 'trx', |
| 96 | + 'op', |
| 97 | + samplingDecision: SentryTracesSamplingDecision(true), |
| 98 | + ); |
| 99 | + hub.startTransactionWithContext(txContext); |
| 100 | + expect(sut.sampled, isTrue); |
| 101 | + |
| 102 | + // Simulate new trace. |
| 103 | + hub.generateNewTrace(); |
| 104 | + expect(sut.sampled, isNull); |
| 105 | + }); |
| 106 | + |
| 107 | + test('applySamplingDecision only sets sampled flag once', () { |
| 108 | + final hub = Hub(defaultTestOptions()..tracesSampleRate = 1.0); |
| 109 | + final sut = hub.scope.propagationContext; |
| 110 | + |
| 111 | + expect(sut.sampled, isNull); |
| 112 | + sut.applySamplingDecision(true); |
| 113 | + expect(sut.sampled, isTrue); |
| 114 | + sut.applySamplingDecision(false); |
| 115 | + expect(sut.sampled, isTrue); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + group('resetTrace', () { |
| 120 | + test('resets values', () { |
| 121 | + final hub = Hub(defaultTestOptions()..tracesSampleRate = 1.0); |
| 122 | + final sut = hub.scope.propagationContext; |
| 123 | + |
| 124 | + final traceId = SentryId.newId(); |
| 125 | + sut.traceId = traceId; |
| 126 | + sut.sampleRand = 1.0; |
| 127 | + sut.applySamplingDecision(true); |
| 128 | + |
| 129 | + sut.resetTrace(); |
| 130 | + |
| 131 | + expect(sut.traceId, isNot(traceId)); |
| 132 | + expect(sut.sampleRand, isNull); |
| 133 | + expect(sut.sampled, isNull); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + group('toSentryTrace', () { |
| 138 | + test('header reflects values', () { |
| 139 | + final options = defaultTestOptions()..tracesSampleRate = 1.0; |
| 140 | + final hub = Hub(options); |
| 141 | + final sut = hub.scope.propagationContext; |
| 142 | + |
| 143 | + final txContext = SentryTransactionContext( |
| 144 | + 'trx', |
| 145 | + 'op', |
| 146 | + samplingDecision: SentryTracesSamplingDecision(true), |
| 147 | + ); |
| 148 | + hub.startTransactionWithContext(txContext); |
| 149 | + |
| 150 | + final header = sut.toSentryTrace(); |
| 151 | + expect(header.sampled, isTrue); |
| 152 | + expect(header.value.split('-').length, 3, |
| 153 | + reason: 'header must contain the sampled decision'); |
| 154 | + }); |
| 155 | + }); |
| 156 | + }); |
| 157 | +} |
0 commit comments