|
1 | 1 | import type { Transport } from '@sentry/core'; |
| 2 | +import { debug } from '@sentry/core'; |
2 | 3 | import * as Sentry from '@sentry/node'; |
3 | 4 | import type { NodeClientOptions } from '@sentry/node/build/types/types'; |
4 | 5 | import { CpuProfilerBindings } from '@sentry/node-cpu-profiler'; |
@@ -54,6 +55,21 @@ describe('ProfilingIntegration', () => { |
54 | 55 | expect(stopProfilingSpy).toHaveBeenCalled(); |
55 | 56 | }); |
56 | 57 |
|
| 58 | + it('logs a no-op when stopping without a running profile session', () => { |
| 59 | + const [client] = makeSpanProfilingClient({ |
| 60 | + profileLifecycle: 'manual', |
| 61 | + profileSessionSampleRate: 1, |
| 62 | + }); |
| 63 | + Sentry.setCurrentClient(client); |
| 64 | + client.init(); |
| 65 | + |
| 66 | + const debugLogSpy = vi.spyOn(debug, 'log'); |
| 67 | + |
| 68 | + Sentry.profiler.stopProfiler(); |
| 69 | + |
| 70 | + expect(debugLogSpy).toHaveBeenCalledWith('[Profiling] No profile session running, no-op.'); |
| 71 | + }); |
| 72 | + |
57 | 73 | it('calling start and stop while profile session is running does nothing', () => { |
58 | 74 | const [client] = makeSpanProfilingClient({ |
59 | 75 | profileLifecycle: 'manual', |
@@ -305,6 +321,90 @@ describe('ProfilingIntegration', () => { |
305 | 321 | expect(stopProfilingSpy).toHaveBeenCalled(); |
306 | 322 | }); |
307 | 323 |
|
| 324 | + it('logs when it skips a trace because the root span is not sampled', () => { |
| 325 | + const [client] = makeSpanProfilingClient({ |
| 326 | + profileLifecycle: 'trace', |
| 327 | + profileSessionSampleRate: 1, |
| 328 | + tracesSampleRate: 0, |
| 329 | + }); |
| 330 | + |
| 331 | + Sentry.setCurrentClient(client); |
| 332 | + client.init(); |
| 333 | + |
| 334 | + const debugLogSpy = vi.spyOn(debug, 'log'); |
| 335 | + |
| 336 | + Sentry.startInactiveSpan({ name: 'GET /users/:id' }); |
| 337 | + |
| 338 | + expect(debugLogSpy).toHaveBeenCalledWith( |
| 339 | + '[Profiling] Not profiling trace because its root span was not sampled.', |
| 340 | + ); |
| 341 | + }); |
| 342 | + |
| 343 | + it('does not log skipped unsampled child spans', () => { |
| 344 | + const [client] = makeSpanProfilingClient({ |
| 345 | + profileLifecycle: 'trace', |
| 346 | + profileSessionSampleRate: 1, |
| 347 | + tracesSampleRate: 0, |
| 348 | + }); |
| 349 | + |
| 350 | + Sentry.setCurrentClient(client); |
| 351 | + client.init(); |
| 352 | + |
| 353 | + const rootSpan = Sentry.startInactiveSpan({ name: 'GET /users/:id' }); |
| 354 | + const debugLogSpy = vi.spyOn(debug, 'log'); |
| 355 | + |
| 356 | + Sentry.startInactiveSpan({ name: 'SELECT * FROM users', parentSpan: rootSpan }); |
| 357 | + |
| 358 | + expect(debugLogSpy).not.toHaveBeenCalledWith( |
| 359 | + '[Profiling] Not profiling trace because its root span was not sampled.', |
| 360 | + ); |
| 361 | + }); |
| 362 | + |
| 363 | + it('does not start the profiler for unsampled spans', () => { |
| 364 | + const [client] = makeSpanProfilingClient({ |
| 365 | + profileLifecycle: 'trace', |
| 366 | + profileSessionSampleRate: 1, |
| 367 | + tracesSampleRate: 0, |
| 368 | + }); |
| 369 | + |
| 370 | + Sentry.setCurrentClient(client); |
| 371 | + client.init(); |
| 372 | + |
| 373 | + const startProfilingSpy = vi.spyOn(CpuProfilerBindings, 'startProfiling'); |
| 374 | + |
| 375 | + Sentry.startSpan({ name: 'root' }, () => { |
| 376 | + Sentry.startSpan({ name: 'child' }, () => {}); |
| 377 | + }); |
| 378 | + |
| 379 | + expect(startProfilingSpy).not.toHaveBeenCalled(); |
| 380 | + }); |
| 381 | + |
| 382 | + it('stops the profiler after the last sampled span ends, ignoring unsampled spans', () => { |
| 383 | + const [client] = makeSpanProfilingClient({ |
| 384 | + profileLifecycle: 'trace', |
| 385 | + profileSessionSampleRate: 1, |
| 386 | + tracesSampler: ({ name }) => (name === 'sampled' ? 1 : 0), |
| 387 | + }); |
| 388 | + |
| 389 | + Sentry.setCurrentClient(client); |
| 390 | + client.init(); |
| 391 | + |
| 392 | + const startProfilingSpy = vi.spyOn(CpuProfilerBindings, 'startProfiling'); |
| 393 | + const stopProfilingSpy = vi.spyOn(CpuProfilerBindings, 'stopProfiling'); |
| 394 | + |
| 395 | + Sentry.startSpan({ name: 'unsampled' }, () => { |
| 396 | + Sentry.startSpan({ name: 'unsampled-child' }, () => {}); |
| 397 | + }); |
| 398 | + |
| 399 | + Sentry.startSpan({ name: 'sampled' }, () => { |
| 400 | + Sentry.startSpan({ name: 'sampled-child' }, () => {}); |
| 401 | + expect(startProfilingSpy).toHaveBeenCalledTimes(1); |
| 402 | + expect(stopProfilingSpy).not.toHaveBeenCalled(); |
| 403 | + }); |
| 404 | + |
| 405 | + expect(stopProfilingSpy).toHaveBeenCalledTimes(1); |
| 406 | + }); |
| 407 | + |
308 | 408 | describe('envelope', () => { |
309 | 409 | beforeEach(() => { |
310 | 410 | vi.useRealTimers(); |
@@ -345,6 +445,96 @@ describe('ProfilingIntegration', () => { |
345 | 445 | }); |
346 | 446 | }); |
347 | 447 | }); |
| 448 | + |
| 449 | + describe('chunk restart', () => { |
| 450 | + const lifecycles = [ |
| 451 | + { |
| 452 | + profileLifecycle: 'manual', |
| 453 | + startProfiling: () => { |
| 454 | + Sentry.profiler.startProfiler(); |
| 455 | + return () => Sentry.profiler.stopProfiler(); |
| 456 | + }, |
| 457 | + }, |
| 458 | + { |
| 459 | + profileLifecycle: 'trace', |
| 460 | + startProfiling: () => { |
| 461 | + const span = Sentry.startInactiveSpan({ name: 'GET /users/:id' }); |
| 462 | + return () => span.end(); |
| 463 | + }, |
| 464 | + }, |
| 465 | + ] as const; |
| 466 | + |
| 467 | + beforeEach(() => { |
| 468 | + vi.useFakeTimers(); |
| 469 | + }); |
| 470 | + |
| 471 | + afterEach(() => { |
| 472 | + vi.useRealTimers(); |
| 473 | + }); |
| 474 | + |
| 475 | + it.each(lifecycles)( |
| 476 | + 'does not start a new chunk when $profileLifecycle profiling stops before the scheduled restart', |
| 477 | + ({ profileLifecycle, startProfiling }) => { |
| 478 | + const [client] = makeSpanProfilingClient({ profileLifecycle, profileSessionSampleRate: 1 }); |
| 479 | + Sentry.setCurrentClient(client); |
| 480 | + client.init(); |
| 481 | + |
| 482 | + const startProfilingSpy = vi.spyOn(CpuProfilerBindings, 'startProfiling'); |
| 483 | + const stopProfilingSpy = vi.spyOn(CpuProfilerBindings, 'stopProfiling'); |
| 484 | + const debugLogSpy = vi.spyOn(debug, 'log'); |
| 485 | + const stopProfiling = startProfiling(); |
| 486 | + vi.advanceTimersToNextTimer(); |
| 487 | + expect(stopProfilingSpy).toHaveBeenCalledTimes(1); |
| 488 | + |
| 489 | + stopProfiling(); |
| 490 | + vi.runOnlyPendingTimers(); |
| 491 | + |
| 492 | + expect(startProfilingSpy).toHaveBeenCalledTimes(1); |
| 493 | + expect(debugLogSpy).toHaveBeenCalledWith( |
| 494 | + '[Profiling] Profiling stopped before the next chunk started, cancelled the scheduled chunk.', |
| 495 | + ); |
| 496 | + }, |
| 497 | + ); |
| 498 | + |
| 499 | + it('does not start a second chunk when manual profiling starts between two chunks', () => { |
| 500 | + const [client] = makeSpanProfilingClient({ profileLifecycle: 'manual', profileSessionSampleRate: 1 }); |
| 501 | + Sentry.setCurrentClient(client); |
| 502 | + client.init(); |
| 503 | + |
| 504 | + const startProfilingSpy = vi.spyOn(CpuProfilerBindings, 'startProfiling'); |
| 505 | + const stopProfilingSpy = vi.spyOn(CpuProfilerBindings, 'stopProfiling'); |
| 506 | + Sentry.profiler.startProfiler(); |
| 507 | + vi.advanceTimersToNextTimer(); |
| 508 | + |
| 509 | + Sentry.profiler.startProfiler(); |
| 510 | + vi.runOnlyPendingTimers(); |
| 511 | + |
| 512 | + expect(startProfilingSpy).toHaveBeenCalledTimes(2); |
| 513 | + expect(stopProfilingSpy).toHaveBeenCalledTimes(1); |
| 514 | + }); |
| 515 | + |
| 516 | + it.each(lifecycles)( |
| 517 | + 'does not report a cancelled chunk when $profileLifecycle profiling stops after the restart', |
| 518 | + ({ profileLifecycle, startProfiling }) => { |
| 519 | + const [client] = makeSpanProfilingClient({ profileLifecycle, profileSessionSampleRate: 1 }); |
| 520 | + Sentry.setCurrentClient(client); |
| 521 | + client.init(); |
| 522 | + |
| 523 | + const startProfilingSpy = vi.spyOn(CpuProfilerBindings, 'startProfiling'); |
| 524 | + const debugLogSpy = vi.spyOn(debug, 'log'); |
| 525 | + const stopProfiling = startProfiling(); |
| 526 | + vi.advanceTimersToNextTimer(); |
| 527 | + vi.runOnlyPendingTimers(); |
| 528 | + expect(startProfilingSpy).toHaveBeenCalledTimes(2); |
| 529 | + |
| 530 | + stopProfiling(); |
| 531 | + |
| 532 | + expect(debugLogSpy).not.toHaveBeenCalledWith( |
| 533 | + '[Profiling] Profiling stopped before the next chunk started, cancelled the scheduled chunk.', |
| 534 | + ); |
| 535 | + }, |
| 536 | + ); |
| 537 | + }); |
348 | 538 | }); |
349 | 539 |
|
350 | 540 | describe('NODE_VERSION', () => { |
|
0 commit comments