|
1 | 1 | import { ProxyTracer } from '@opentelemetry/api';
|
2 | 2 | import * as opentelemetryInstrumentationPackage from '@opentelemetry/instrumentation';
|
| 3 | +import type { BasicTracerProvider } from '@opentelemetry/sdk-trace-base'; |
3 | 4 | import type { Event, EventHint, Log } from '@sentry/core';
|
4 | 5 | import { getCurrentScope, getGlobalScope, getIsolationScope, Scope, SDK_VERSION } from '@sentry/core';
|
5 | 6 | import { setOpenTelemetryContextAsyncContextStrategy } from '@sentry/opentelemetry';
|
@@ -321,4 +322,65 @@ describe('NodeClient', () => {
|
321 | 322 | });
|
322 | 323 | });
|
323 | 324 | });
|
| 325 | + |
| 326 | + describe('close', () => { |
| 327 | + beforeEach(() => { |
| 328 | + vi.clearAllMocks(); |
| 329 | + }); |
| 330 | + |
| 331 | + it('shuts down the OTel trace provider', async () => { |
| 332 | + const shutdownSpy = vi.fn().mockResolvedValue(true); |
| 333 | + const forceFlushSpy = vi.fn().mockResolvedValue(undefined); |
| 334 | + |
| 335 | + const client = new NodeClient(getDefaultNodeClientOptions()); |
| 336 | + |
| 337 | + client.traceProvider = { |
| 338 | + shutdown: shutdownSpy, |
| 339 | + forceFlush: forceFlushSpy, |
| 340 | + } as unknown as BasicTracerProvider; |
| 341 | + |
| 342 | + const result = await client.close(); |
| 343 | + |
| 344 | + // ensure we return the flush result rather than void from the traceProvider shutdown |
| 345 | + expect(result).toBe(true); |
| 346 | + |
| 347 | + expect(shutdownSpy).toHaveBeenCalledTimes(1); |
| 348 | + |
| 349 | + // close calls flush and flush force-flushes the traceProvider |
| 350 | + expect(forceFlushSpy).toHaveBeenCalledTimes(1); |
| 351 | + }); |
| 352 | + |
| 353 | + it('stops client report tracking if it was started', async () => { |
| 354 | + const processOffSpy = vi.spyOn(process, 'off'); |
| 355 | + const clearIntervalSpy = vi.spyOn(globalThis, 'clearInterval'); |
| 356 | + |
| 357 | + const client = new NodeClient(getDefaultNodeClientOptions({ sendClientReports: true })); |
| 358 | + |
| 359 | + client.startClientReportTracking(); |
| 360 | + |
| 361 | + const result = await client.close(); |
| 362 | + |
| 363 | + expect(result).toBe(true); |
| 364 | + |
| 365 | + // once call directly in close to stop client reports, |
| 366 | + // the other in core client `_isClientDoneProcessing` |
| 367 | + expect(clearIntervalSpy).toHaveBeenCalledTimes(2); |
| 368 | + |
| 369 | + // removes `_clientReportOnExitFlushListener` |
| 370 | + expect(processOffSpy).toHaveBeenNthCalledWith(1, 'beforeExit', expect.any(Function)); |
| 371 | + }); |
| 372 | + |
| 373 | + it('stops log capture if it was started', async () => { |
| 374 | + const processOffSpy = vi.spyOn(process, 'off'); |
| 375 | + |
| 376 | + const client = new NodeClient(getDefaultNodeClientOptions({ enableLogs: true })); |
| 377 | + |
| 378 | + const result = await client.close(); |
| 379 | + |
| 380 | + expect(result).toBe(true); |
| 381 | + |
| 382 | + // removes `_logOnExitFlushListener` |
| 383 | + expect(processOffSpy).toHaveBeenNthCalledWith(1, 'beforeExit', expect.any(Function)); |
| 384 | + }); |
| 385 | + }); |
324 | 386 | });
|
0 commit comments