-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathprofiling.ts
More file actions
77 lines (67 loc) · 2.46 KB
/
profiling.ts
File metadata and controls
77 lines (67 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import type { Client } from './client';
import { getClient } from './currentScopes';
import { DEBUG_BUILD } from './debug-build';
import type { Profiler, ProfilingIntegration } from './types-hoist/profiling';
import { debug } from './utils/debug-logger';
function isProfilingIntegrationWithProfiler(
integration: ProfilingIntegration | undefined,
): integration is ProfilingIntegration {
return (
!!integration &&
typeof integration['_profiler'] !== 'undefined' &&
typeof integration['_profiler']['start'] === 'function' &&
typeof integration['_profiler']['stop'] === 'function'
);
}
/**
* Starts the Sentry continuous profiler.
* This mode is exclusive with the transaction profiler and will only work if the profilesSampleRate is set to a falsy value.
* In continuous profiling mode, the profiler will keep reporting profile chunks to Sentry until it is stopped, which allows for continuous profiling of the application.
*/
function startProfiler(): void {
const client = getClient();
if (!client) {
DEBUG_BUILD && debug.warn('No Sentry client available, profiling is not started');
return;
}
const integration = client.getIntegrationByName<ProfilingIntegration>('ProfilingIntegration');
if (!integration) {
DEBUG_BUILD && debug.warn('ProfilingIntegration is not available');
return;
}
if (!isProfilingIntegrationWithProfiler(integration)) {
DEBUG_BUILD && debug.warn('Profiler is not available on profiling integration.');
return;
}
integration._profiler.start();
}
/**
* Stops the Sentry continuous profiler.
* Calls to stop will stop the profiler and flush the currently collected profile data to Sentry.
*/
function stopProfiler(): void {
const client = getClient();
if (!client) {
DEBUG_BUILD && debug.warn('No Sentry client available, profiling is not started');
return;
}
const integration = client.getIntegrationByName<ProfilingIntegration>('ProfilingIntegration');
if (!integration) {
DEBUG_BUILD && debug.warn('ProfilingIntegration is not available');
return;
}
if (!isProfilingIntegrationWithProfiler(integration)) {
DEBUG_BUILD && debug.warn('Profiler is not available on profiling integration.');
return;
}
integration._profiler.stop();
}
/**
* Profiler namespace for controlling the profiler in 'manual' mode.
*
* Requires the `nodeProfilingIntegration` from the `@sentry/profiling-node` package.
*/
export const profiler: Profiler = {
startProfiler,
stopProfiler,
};