|
1 | 1 | import { addTracingExtensions, Hub, makeMain } from '../../../src';
|
2 |
| -import { startSpan } from '../../../src/tracing'; |
| 2 | +import { continueTrace, startSpan } from '../../../src/tracing'; |
3 | 3 | import { getDefaultTestClientOptions, TestClient } from '../../mocks/client';
|
4 | 4 |
|
5 | 5 | beforeAll(() => {
|
@@ -170,3 +170,154 @@ describe('startSpan', () => {
|
170 | 170 | });
|
171 | 171 | });
|
172 | 172 | });
|
| 173 | + |
| 174 | +describe('continueTrace', () => { |
| 175 | + beforeEach(() => { |
| 176 | + const options = getDefaultTestClientOptions({ tracesSampleRate: 0.0 }); |
| 177 | + client = new TestClient(options); |
| 178 | + hub = new Hub(client); |
| 179 | + makeMain(hub); |
| 180 | + }); |
| 181 | + |
| 182 | + it('works without trace & baggage data', () => { |
| 183 | + const expectedContext = { |
| 184 | + metadata: {}, |
| 185 | + }; |
| 186 | + |
| 187 | + const result = continueTrace({ sentryTrace: undefined, baggage: undefined }, ctx => { |
| 188 | + expect(ctx).toEqual(expectedContext); |
| 189 | + return ctx; |
| 190 | + }); |
| 191 | + |
| 192 | + expect(result).toEqual(expectedContext); |
| 193 | + |
| 194 | + const scope = hub.getScope(); |
| 195 | + |
| 196 | + expect(scope.getPropagationContext()).toEqual({ |
| 197 | + sampled: undefined, |
| 198 | + spanId: expect.any(String), |
| 199 | + traceId: expect.any(String), |
| 200 | + }); |
| 201 | + |
| 202 | + expect(scope['_sdkProcessingMetadata']).toEqual({}); |
| 203 | + }); |
| 204 | + |
| 205 | + it('works with trace data', () => { |
| 206 | + const expectedContext = { |
| 207 | + metadata: { |
| 208 | + dynamicSamplingContext: {}, |
| 209 | + }, |
| 210 | + parentSampled: false, |
| 211 | + parentSpanId: '1121201211212012', |
| 212 | + traceId: '12312012123120121231201212312012', |
| 213 | + }; |
| 214 | + |
| 215 | + const result = continueTrace( |
| 216 | + { |
| 217 | + sentryTrace: '12312012123120121231201212312012-1121201211212012-0', |
| 218 | + baggage: undefined, |
| 219 | + }, |
| 220 | + ctx => { |
| 221 | + expect(ctx).toEqual(expectedContext); |
| 222 | + return ctx; |
| 223 | + }, |
| 224 | + ); |
| 225 | + |
| 226 | + expect(result).toEqual(expectedContext); |
| 227 | + |
| 228 | + const scope = hub.getScope(); |
| 229 | + |
| 230 | + expect(scope.getPropagationContext()).toEqual({ |
| 231 | + sampled: false, |
| 232 | + parentSpanId: '1121201211212012', |
| 233 | + spanId: expect.any(String), |
| 234 | + traceId: '12312012123120121231201212312012', |
| 235 | + }); |
| 236 | + |
| 237 | + expect(scope['_sdkProcessingMetadata']).toEqual({}); |
| 238 | + }); |
| 239 | + |
| 240 | + it('works with trace & baggage data', () => { |
| 241 | + const expectedContext = { |
| 242 | + metadata: { |
| 243 | + dynamicSamplingContext: { |
| 244 | + environment: 'production', |
| 245 | + version: '1.0', |
| 246 | + }, |
| 247 | + }, |
| 248 | + parentSampled: true, |
| 249 | + parentSpanId: '1121201211212012', |
| 250 | + traceId: '12312012123120121231201212312012', |
| 251 | + }; |
| 252 | + |
| 253 | + const result = continueTrace( |
| 254 | + { |
| 255 | + sentryTrace: '12312012123120121231201212312012-1121201211212012-1', |
| 256 | + baggage: 'sentry-version=1.0,sentry-environment=production', |
| 257 | + }, |
| 258 | + ctx => { |
| 259 | + expect(ctx).toEqual(expectedContext); |
| 260 | + return ctx; |
| 261 | + }, |
| 262 | + ); |
| 263 | + |
| 264 | + expect(result).toEqual(expectedContext); |
| 265 | + |
| 266 | + const scope = hub.getScope(); |
| 267 | + |
| 268 | + expect(scope.getPropagationContext()).toEqual({ |
| 269 | + dsc: { |
| 270 | + environment: 'production', |
| 271 | + version: '1.0', |
| 272 | + }, |
| 273 | + sampled: true, |
| 274 | + parentSpanId: '1121201211212012', |
| 275 | + spanId: expect.any(String), |
| 276 | + traceId: '12312012123120121231201212312012', |
| 277 | + }); |
| 278 | + |
| 279 | + expect(scope['_sdkProcessingMetadata']).toEqual({}); |
| 280 | + }); |
| 281 | + |
| 282 | + it('works with trace & 3rd party baggage data', () => { |
| 283 | + const expectedContext = { |
| 284 | + metadata: { |
| 285 | + dynamicSamplingContext: { |
| 286 | + environment: 'production', |
| 287 | + version: '1.0', |
| 288 | + }, |
| 289 | + }, |
| 290 | + parentSampled: true, |
| 291 | + parentSpanId: '1121201211212012', |
| 292 | + traceId: '12312012123120121231201212312012', |
| 293 | + }; |
| 294 | + |
| 295 | + const result = continueTrace( |
| 296 | + { |
| 297 | + sentryTrace: '12312012123120121231201212312012-1121201211212012-1', |
| 298 | + baggage: 'sentry-version=1.0,sentry-environment=production,dogs=great,cats=boring', |
| 299 | + }, |
| 300 | + ctx => { |
| 301 | + expect(ctx).toEqual(expectedContext); |
| 302 | + return ctx; |
| 303 | + }, |
| 304 | + ); |
| 305 | + |
| 306 | + expect(result).toEqual(expectedContext); |
| 307 | + |
| 308 | + const scope = hub.getScope(); |
| 309 | + |
| 310 | + expect(scope.getPropagationContext()).toEqual({ |
| 311 | + dsc: { |
| 312 | + environment: 'production', |
| 313 | + version: '1.0', |
| 314 | + }, |
| 315 | + sampled: true, |
| 316 | + parentSpanId: '1121201211212012', |
| 317 | + spanId: expect.any(String), |
| 318 | + traceId: '12312012123120121231201212312012', |
| 319 | + }); |
| 320 | + |
| 321 | + expect(scope['_sdkProcessingMetadata']).toEqual({}); |
| 322 | + }); |
| 323 | +}); |
0 commit comments