-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
exports.ts
211 lines (194 loc) · 7.36 KB
/
exports.ts
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import type {
Breadcrumb,
CaptureContext,
CheckIn,
CustomSamplingContext,
Event,
EventHint,
Extra,
Extras,
MonitorConfig,
Primitive,
Severity,
SeverityLevel,
TransactionContext,
User,
} from '@sentry/types';
import { logger, uuid4 } from '@sentry/utils';
import type { Hub } from './hub';
import { getCurrentHub } from './hub';
import type { Scope } from './scope';
// Note: All functions in this file are typed with a return value of `ReturnType<Hub[HUB_FUNCTION]>`,
// where HUB_FUNCTION is some method on the Hub class.
//
// This is done to make sure the top level SDK methods stay in sync with the hub methods.
// Although every method here has an explicit return type, some of them (that map to void returns) do not
// contain `return` keywords. This is done to save on bundle size, as `return` is not minifiable.
/**
* Captures an exception event and sends it to Sentry.
*
* @param exception An exception-like object.
* @param captureContext Additional scope data to apply to exception event.
* @returns The generated eventId.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
export function captureException(exception: any, captureContext?: CaptureContext): ReturnType<Hub['captureException']> {
return getCurrentHub().captureException(exception, { captureContext });
}
/**
* Captures a message event and sends it to Sentry.
*
* @param message The message to send to Sentry.
* @param Severity Define the level of the message.
* @returns The generated eventId.
*/
export function captureMessage(
message: string,
// eslint-disable-next-line deprecation/deprecation
captureContext?: CaptureContext | Severity | SeverityLevel,
): ReturnType<Hub['captureMessage']> {
// This is necessary to provide explicit scopes upgrade, without changing the original
// arity of the `captureMessage(message, level)` method.
const level = typeof captureContext === 'string' ? captureContext : undefined;
const context = typeof captureContext !== 'string' ? { captureContext } : undefined;
return getCurrentHub().captureMessage(message, level, context);
}
/**
* Captures a manually created event and sends it to Sentry.
*
* @param event The event to send to Sentry.
* @returns The generated eventId.
*/
export function captureEvent(event: Event, hint?: EventHint): ReturnType<Hub['captureEvent']> {
return getCurrentHub().captureEvent(event, hint);
}
/**
* Callback to set context information onto the scope.
* @param callback Callback function that receives Scope.
*/
export function configureScope(callback: (scope: Scope) => void): ReturnType<Hub['configureScope']> {
getCurrentHub().configureScope(callback);
}
/**
* Records a new breadcrumb which will be attached to future events.
*
* Breadcrumbs will be added to subsequent events to provide more context on
* user's actions prior to an error or crash.
*
* @param breadcrumb The breadcrumb to record.
*/
export function addBreadcrumb(breadcrumb: Breadcrumb): ReturnType<Hub['addBreadcrumb']> {
getCurrentHub().addBreadcrumb(breadcrumb);
}
/**
* Sets context data with the given name.
* @param name of the context
* @param context Any kind of data. This data will be normalized.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function setContext(name: string, context: { [key: string]: any } | null): ReturnType<Hub['setContext']> {
getCurrentHub().setContext(name, context);
}
/**
* Set an object that will be merged sent as extra data with the event.
* @param extras Extras object to merge into current context.
*/
export function setExtras(extras: Extras): ReturnType<Hub['setExtras']> {
getCurrentHub().setExtras(extras);
}
/**
* Set key:value that will be sent as extra data with the event.
* @param key String of extra
* @param extra Any kind of data. This data will be normalized.
*/
export function setExtra(key: string, extra: Extra): ReturnType<Hub['setExtra']> {
getCurrentHub().setExtra(key, extra);
}
/**
* Set an object that will be merged sent as tags data with the event.
* @param tags Tags context object to merge into current context.
*/
export function setTags(tags: { [key: string]: Primitive }): ReturnType<Hub['setTags']> {
getCurrentHub().setTags(tags);
}
/**
* Set key:value that will be sent as tags data with the event.
*
* Can also be used to unset a tag, by passing `undefined`.
*
* @param key String key of tag
* @param value Value of tag
*/
export function setTag(key: string, value: Primitive): ReturnType<Hub['setTag']> {
getCurrentHub().setTag(key, value);
}
/**
* Updates user context information for future events.
*
* @param user User context object to be set in the current context. Pass `null` to unset the user.
*/
export function setUser(user: User | null): ReturnType<Hub['setUser']> {
getCurrentHub().setUser(user);
}
/**
* Creates a new scope with and executes the given operation within.
* The scope is automatically removed once the operation
* finishes or throws.
*
* This is essentially a convenience function for:
*
* pushScope();
* callback();
* popScope();
*
* @param callback that will be enclosed into push/popScope.
*/
export function withScope(callback: (scope: Scope) => void): ReturnType<Hub['withScope']> {
getCurrentHub().withScope(callback);
}
/**
* Starts a new `Transaction` and returns it. This is the entry point to manual tracing instrumentation.
*
* A tree structure can be built by adding child spans to the transaction, and child spans to other spans. To start a
* new child span within the transaction or any span, call the respective `.startChild()` method.
*
* Every child span must be finished before the transaction is finished, otherwise the unfinished spans are discarded.
*
* The transaction must be finished with a call to its `.finish()` method, at which point the transaction with all its
* finished child spans will be sent to Sentry.
*
* NOTE: This function should only be used for *manual* instrumentation. Auto-instrumentation should call
* `startTransaction` directly on the hub.
*
* @param context Properties of the new `Transaction`.
* @param customSamplingContext Information given to the transaction sampling function (along with context-dependent
* default values). See {@link Options.tracesSampler}.
*
* @returns The transaction which was just started
*/
export function startTransaction(
context: TransactionContext,
customSamplingContext?: CustomSamplingContext,
): ReturnType<Hub['startTransaction']> {
return getCurrentHub().startTransaction({ ...context }, customSamplingContext);
}
/**
* Create a cron monitor check in and send it to Sentry.
*
* @param checkIn An object that describes a check in.
* @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want
* to create a monitor automatically when sending a check in.
*/
export function captureCheckIn(checkIn: CheckIn, upsertMonitorConfig?: MonitorConfig): string {
const hub = getCurrentHub();
const scope = hub.getScope();
const client = hub.getClient();
if (!client) {
__DEBUG_BUILD__ && logger.warn('Cannot capture check-in. No client defined.');
} else if (!client.captureCheckIn) {
__DEBUG_BUILD__ && logger.warn('Cannot capture check-in. Client does not support sending check-ins.');
} else {
return client.captureCheckIn(checkIn, upsertMonitorConfig, scope);
}
return uuid4();
}