|
| 1 | +import { continueTrace, getCurrentScope, setCurrentClient } from '@sentry/core'; |
| 2 | + |
| 3 | +import { getDefaultTestClientOptions, TestClient } from './mocks/client'; |
| 4 | + |
| 5 | +describe('strictTraceContinuation', () => { |
| 6 | + let client: TestClient; |
| 7 | + |
| 8 | + afterEach(() => { |
| 9 | + jest.clearAllMocks(); |
| 10 | + }); |
| 11 | + |
| 12 | + describe('with matching org IDs', () => { |
| 13 | + beforeEach(() => { |
| 14 | + client = new TestClient( |
| 15 | + getDefaultTestClientOptions({ |
| 16 | + tracesSampleRate: 1.0, |
| 17 | + dsn: 'https://abc@o123.ingest.sentry.io/1234', |
| 18 | + }), |
| 19 | + ); |
| 20 | + setCurrentClient(client); |
| 21 | + client.init(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('continues trace when baggage org_id matches DSN org ID', () => { |
| 25 | + const scope = continueTrace( |
| 26 | + { |
| 27 | + sentryTrace: '12312012123120121231201212312012-1121201211212012-1', |
| 28 | + baggage: 'sentry-org_id=123', |
| 29 | + }, |
| 30 | + () => { |
| 31 | + return getCurrentScope(); |
| 32 | + }, |
| 33 | + ); |
| 34 | + |
| 35 | + expect(scope.getPropagationContext().traceId).toBe('12312012123120121231201212312012'); |
| 36 | + expect(scope.getPropagationContext().parentSpanId).toBe('1121201211212012'); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('with mismatching org IDs', () => { |
| 41 | + beforeEach(() => { |
| 42 | + client = new TestClient( |
| 43 | + getDefaultTestClientOptions({ |
| 44 | + tracesSampleRate: 1.0, |
| 45 | + dsn: 'https://abc@o123.ingest.sentry.io/1234', |
| 46 | + }), |
| 47 | + ); |
| 48 | + setCurrentClient(client); |
| 49 | + client.init(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('starts new trace when baggage org_id does not match DSN org ID', () => { |
| 53 | + const scope = continueTrace( |
| 54 | + { |
| 55 | + sentryTrace: '12312012123120121231201212312012-1121201211212012-1', |
| 56 | + baggage: 'sentry-org_id=456', |
| 57 | + }, |
| 58 | + () => { |
| 59 | + return getCurrentScope(); |
| 60 | + }, |
| 61 | + ); |
| 62 | + |
| 63 | + expect(scope.getPropagationContext().traceId).not.toBe('12312012123120121231201212312012'); |
| 64 | + expect(scope.getPropagationContext().parentSpanId).toBeUndefined(); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('with orgId option override', () => { |
| 69 | + beforeEach(() => { |
| 70 | + client = new TestClient( |
| 71 | + getDefaultTestClientOptions({ |
| 72 | + tracesSampleRate: 1.0, |
| 73 | + dsn: 'https://abc@o123.ingest.sentry.io/1234', |
| 74 | + orgId: '999', |
| 75 | + }), |
| 76 | + ); |
| 77 | + setCurrentClient(client); |
| 78 | + client.init(); |
| 79 | + }); |
| 80 | + |
| 81 | + it('uses orgId option over DSN-extracted org ID', () => { |
| 82 | + // baggage org_id=123 matches DSN but NOT the orgId option (999) |
| 83 | + const scope = continueTrace( |
| 84 | + { |
| 85 | + sentryTrace: '12312012123120121231201212312012-1121201211212012-1', |
| 86 | + baggage: 'sentry-org_id=123', |
| 87 | + }, |
| 88 | + () => { |
| 89 | + return getCurrentScope(); |
| 90 | + }, |
| 91 | + ); |
| 92 | + |
| 93 | + // Should start new trace because orgId option (999) != baggage org_id (123) |
| 94 | + expect(scope.getPropagationContext().traceId).not.toBe('12312012123120121231201212312012'); |
| 95 | + expect(scope.getPropagationContext().parentSpanId).toBeUndefined(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('continues trace when baggage matches orgId option', () => { |
| 99 | + const scope = continueTrace( |
| 100 | + { |
| 101 | + sentryTrace: '12312012123120121231201212312012-1121201211212012-1', |
| 102 | + baggage: 'sentry-org_id=999', |
| 103 | + }, |
| 104 | + () => { |
| 105 | + return getCurrentScope(); |
| 106 | + }, |
| 107 | + ); |
| 108 | + |
| 109 | + expect(scope.getPropagationContext().traceId).toBe('12312012123120121231201212312012'); |
| 110 | + expect(scope.getPropagationContext().parentSpanId).toBe('1121201211212012'); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('strictTraceContinuation=true', () => { |
| 115 | + beforeEach(() => { |
| 116 | + client = new TestClient( |
| 117 | + getDefaultTestClientOptions({ |
| 118 | + tracesSampleRate: 1.0, |
| 119 | + dsn: 'https://abc@o123.ingest.sentry.io/1234', |
| 120 | + strictTraceContinuation: true, |
| 121 | + }), |
| 122 | + ); |
| 123 | + setCurrentClient(client); |
| 124 | + client.init(); |
| 125 | + }); |
| 126 | + |
| 127 | + it('starts new trace when baggage has no org_id', () => { |
| 128 | + const scope = continueTrace( |
| 129 | + { |
| 130 | + sentryTrace: '12312012123120121231201212312012-1121201211212012-1', |
| 131 | + baggage: 'sentry-environment=production', |
| 132 | + }, |
| 133 | + () => { |
| 134 | + return getCurrentScope(); |
| 135 | + }, |
| 136 | + ); |
| 137 | + |
| 138 | + expect(scope.getPropagationContext().traceId).not.toBe('12312012123120121231201212312012'); |
| 139 | + expect(scope.getPropagationContext().parentSpanId).toBeUndefined(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('starts new trace when SDK has no org_id but baggage does', () => { |
| 143 | + // Use a DSN without org ID in hostname |
| 144 | + const clientWithoutOrgId = new TestClient( |
| 145 | + getDefaultTestClientOptions({ |
| 146 | + tracesSampleRate: 1.0, |
| 147 | + dsn: 'https://abc@sentry.example.com/1234', |
| 148 | + strictTraceContinuation: true, |
| 149 | + }), |
| 150 | + ); |
| 151 | + setCurrentClient(clientWithoutOrgId); |
| 152 | + clientWithoutOrgId.init(); |
| 153 | + |
| 154 | + const scope = continueTrace( |
| 155 | + { |
| 156 | + sentryTrace: '12312012123120121231201212312012-1121201211212012-1', |
| 157 | + baggage: 'sentry-org_id=123', |
| 158 | + }, |
| 159 | + () => { |
| 160 | + return getCurrentScope(); |
| 161 | + }, |
| 162 | + ); |
| 163 | + |
| 164 | + expect(scope.getPropagationContext().traceId).not.toBe('12312012123120121231201212312012'); |
| 165 | + expect(scope.getPropagationContext().parentSpanId).toBeUndefined(); |
| 166 | + }); |
| 167 | + |
| 168 | + it('continues trace when both org IDs are missing', () => { |
| 169 | + const clientWithoutOrgId = new TestClient( |
| 170 | + getDefaultTestClientOptions({ |
| 171 | + tracesSampleRate: 1.0, |
| 172 | + dsn: 'https://abc@sentry.example.com/1234', |
| 173 | + strictTraceContinuation: true, |
| 174 | + }), |
| 175 | + ); |
| 176 | + setCurrentClient(clientWithoutOrgId); |
| 177 | + clientWithoutOrgId.init(); |
| 178 | + |
| 179 | + const scope = continueTrace( |
| 180 | + { |
| 181 | + sentryTrace: '12312012123120121231201212312012-1121201211212012-1', |
| 182 | + baggage: 'sentry-environment=production', |
| 183 | + }, |
| 184 | + () => { |
| 185 | + return getCurrentScope(); |
| 186 | + }, |
| 187 | + ); |
| 188 | + |
| 189 | + expect(scope.getPropagationContext().traceId).toBe('12312012123120121231201212312012'); |
| 190 | + expect(scope.getPropagationContext().parentSpanId).toBe('1121201211212012'); |
| 191 | + }); |
| 192 | + }); |
| 193 | + |
| 194 | + describe('strictTraceContinuation=false (default)', () => { |
| 195 | + beforeEach(() => { |
| 196 | + client = new TestClient( |
| 197 | + getDefaultTestClientOptions({ |
| 198 | + tracesSampleRate: 1.0, |
| 199 | + dsn: 'https://abc@o123.ingest.sentry.io/1234', |
| 200 | + strictTraceContinuation: false, |
| 201 | + }), |
| 202 | + ); |
| 203 | + setCurrentClient(client); |
| 204 | + client.init(); |
| 205 | + }); |
| 206 | + |
| 207 | + it('continues trace when baggage has no org_id', () => { |
| 208 | + const scope = continueTrace( |
| 209 | + { |
| 210 | + sentryTrace: '12312012123120121231201212312012-1121201211212012-1', |
| 211 | + baggage: 'sentry-environment=production', |
| 212 | + }, |
| 213 | + () => { |
| 214 | + return getCurrentScope(); |
| 215 | + }, |
| 216 | + ); |
| 217 | + |
| 218 | + expect(scope.getPropagationContext().traceId).toBe('12312012123120121231201212312012'); |
| 219 | + expect(scope.getPropagationContext().parentSpanId).toBe('1121201211212012'); |
| 220 | + }); |
| 221 | + |
| 222 | + it('continues trace when SDK has no org_id but baggage does', () => { |
| 223 | + const clientWithoutOrgId = new TestClient( |
| 224 | + getDefaultTestClientOptions({ |
| 225 | + tracesSampleRate: 1.0, |
| 226 | + dsn: 'https://abc@sentry.example.com/1234', |
| 227 | + strictTraceContinuation: false, |
| 228 | + }), |
| 229 | + ); |
| 230 | + setCurrentClient(clientWithoutOrgId); |
| 231 | + clientWithoutOrgId.init(); |
| 232 | + |
| 233 | + const scope = continueTrace( |
| 234 | + { |
| 235 | + sentryTrace: '12312012123120121231201212312012-1121201211212012-1', |
| 236 | + baggage: 'sentry-org_id=123', |
| 237 | + }, |
| 238 | + () => { |
| 239 | + return getCurrentScope(); |
| 240 | + }, |
| 241 | + ); |
| 242 | + |
| 243 | + expect(scope.getPropagationContext().traceId).toBe('12312012123120121231201212312012'); |
| 244 | + expect(scope.getPropagationContext().parentSpanId).toBe('1121201211212012'); |
| 245 | + }); |
| 246 | + |
| 247 | + it('still starts new trace when org IDs mismatch', () => { |
| 248 | + const scope = continueTrace( |
| 249 | + { |
| 250 | + sentryTrace: '12312012123120121231201212312012-1121201211212012-1', |
| 251 | + baggage: 'sentry-org_id=456', |
| 252 | + }, |
| 253 | + () => { |
| 254 | + return getCurrentScope(); |
| 255 | + }, |
| 256 | + ); |
| 257 | + |
| 258 | + expect(scope.getPropagationContext().traceId).not.toBe('12312012123120121231201212312012'); |
| 259 | + expect(scope.getPropagationContext().parentSpanId).toBeUndefined(); |
| 260 | + }); |
| 261 | + }); |
| 262 | +}); |
0 commit comments