|
1 | 1 | /* eslint-disable @typescript-eslint/unbound-method */
|
2 | 2 | import { BrowserClient } from '@sentry/browser';
|
3 | 3 | import { getMainCarrier, Hub } from '@sentry/hub';
|
| 4 | +import * as hubModule from '@sentry/hub'; |
4 | 5 | import * as utilsModule from '@sentry/utils'; // for mocking
|
5 | 6 | import { getGlobalObject, isNodeEnv, logger } from '@sentry/utils';
|
6 | 7 | import * as nodeHttpModule from 'http';
|
7 | 8 |
|
| 9 | +import { BrowserTracing } from '../src/browser/browsertracing'; |
8 | 10 | import { addExtensionMethods } from '../src/hubextensions';
|
| 11 | +import { extractTraceparentData, TRACEPARENT_REGEXP } from '../src/utils'; |
| 12 | +import { addDOMPropertiesToGlobal, getSymbolObjectKeyByName } from './testutils'; |
9 | 13 |
|
10 | 14 | addExtensionMethods();
|
11 | 15 |
|
| 16 | +// we have to add things into the real global object (rather than mocking the return value of getGlobalObject) |
| 17 | +// because there are modules which call getGlobalObject as they load, which is too early for jest to intervene |
| 18 | +addDOMPropertiesToGlobal(['XMLHttpRequest', 'Event', 'location', 'document']); |
| 19 | + |
12 | 20 | describe('Hub', () => {
|
13 | 21 | beforeEach(() => {
|
14 | 22 | jest.spyOn(logger, 'warn');
|
@@ -306,12 +314,80 @@ describe('Hub', () => {
|
306 | 314 | expect(child.sampled).toBe(transaction.sampled);
|
307 | 315 | });
|
308 | 316 |
|
309 |
| - it('should propagate sampling decision to child transactions in XHR header', () => { |
310 |
| - // TODO fix this and write the test |
| 317 | + it('should propagate positive sampling decision to child transactions in XHR header', () => { |
| 318 | + const hub = new Hub( |
| 319 | + new BrowserClient({ |
| 320 | + dsn: 'https://[email protected]/1121', |
| 321 | + tracesSampleRate: 1, |
| 322 | + integrations: [new BrowserTracing()], |
| 323 | + }), |
| 324 | + ); |
| 325 | + jest.spyOn(hubModule, 'getCurrentHub').mockReturnValue(hub); |
| 326 | + |
| 327 | + const transaction = hub.startTransaction({ name: 'dogpark' }); |
| 328 | + hub.configureScope(scope => { |
| 329 | + scope.setSpan(transaction); |
| 330 | + }); |
| 331 | + |
| 332 | + const request = new XMLHttpRequest(); |
| 333 | + request.open('GET', '/chase-partners'); |
| 334 | + |
| 335 | + // mock a response having been received successfully (we have to do it in this roundabout way because readyState |
| 336 | + // is readonly and changing it doesn't trigger a readystatechange event) |
| 337 | + Object.defineProperty(request, 'readyState', { value: 4 }); |
| 338 | + request.dispatchEvent(new Event('readystatechange')); |
| 339 | + |
| 340 | + // this looks weird, it's true, but it's really just `request.impl.flag.requestHeaders` - it's just that the |
| 341 | + // `impl` key is a symbol rather than a string, and therefore needs to be referred to by reference rather than |
| 342 | + // value |
| 343 | + const headers = (request as any)[getSymbolObjectKeyByName(request, 'impl') as symbol].flag.requestHeaders; |
| 344 | + |
| 345 | + // check that sentry-trace header is added to request |
| 346 | + expect(headers).toEqual(expect.objectContaining({ 'sentry-trace': expect.stringMatching(TRACEPARENT_REGEXP) })); |
| 347 | + |
| 348 | + // check that sampling decision is passed down correctly |
| 349 | + expect(transaction.sampled).toBe(true); |
| 350 | + expect(extractTraceparentData(headers['sentry-trace'])!.parentSampled).toBe(true); |
| 351 | + }); |
| 352 | + |
| 353 | + it('should propagate negative sampling decision to child transactions in XHR header', () => { |
| 354 | + const hub = new Hub( |
| 355 | + new BrowserClient({ |
| 356 | + dsn: 'https://[email protected]/1121', |
| 357 | + tracesSampleRate: 1, |
| 358 | + integrations: [new BrowserTracing()], |
| 359 | + }), |
| 360 | + ); |
| 361 | + jest.spyOn(hubModule, 'getCurrentHub').mockReturnValue(hub); |
| 362 | + |
| 363 | + const transaction = hub.startTransaction({ name: 'dogpark', sampled: false }); |
| 364 | + hub.configureScope(scope => { |
| 365 | + scope.setSpan(transaction); |
| 366 | + }); |
| 367 | + |
| 368 | + const request = new XMLHttpRequest(); |
| 369 | + request.open('GET', '/chase-partners'); |
| 370 | + |
| 371 | + // mock a response having been received successfully (we have to do it in this roundabout way because readyState |
| 372 | + // is readonly and changing it doesn't trigger a readystatechange event) |
| 373 | + Object.defineProperty(request, 'readyState', { value: 4 }); |
| 374 | + request.dispatchEvent(new Event('readystatechange')); |
| 375 | + |
| 376 | + // this looks weird, it's true, but it's really just `request.impl.flag.requestHeaders` - it's just that the |
| 377 | + // `impl` key is a symbol rather than a string, and therefore needs to be referred to by reference rather than |
| 378 | + // value |
| 379 | + const headers = (request as any)[getSymbolObjectKeyByName(request, 'impl') as symbol].flag.requestHeaders; |
| 380 | + |
| 381 | + // check that sentry-trace header is added to request |
| 382 | + expect(headers).toEqual(expect.objectContaining({ 'sentry-trace': expect.stringMatching(TRACEPARENT_REGEXP) })); |
| 383 | + |
| 384 | + // check that sampling decision is passed down correctly |
| 385 | + expect(transaction.sampled).toBe(false); |
| 386 | + expect(extractTraceparentData(headers['sentry-trace'])!.parentSampled).toBe(false); |
311 | 387 | });
|
312 | 388 |
|
313 | 389 | it('should propagate sampling decision to child transactions in fetch header', () => {
|
314 |
| - // TODO fix this and write the test |
| 390 | + // TODO (kmclb) |
315 | 391 | });
|
316 | 392 |
|
317 | 393 | it("should inherit parent's sampling decision when creating a new transaction if tracesSampler is undefined", () => {
|
|
0 commit comments