-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathwithSentry.ts
More file actions
70 lines (63 loc) · 2.65 KB
/
withSentry.ts
File metadata and controls
70 lines (63 loc) · 2.65 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
import type { env } from 'cloudflare:workers';
import { setAsyncLocalStorageAsyncContextStrategy } from './async';
import type { CloudflareOptions } from './client';
import { isInstrumented, markAsInstrumented } from './instrument';
import { instrumentExportedHandlerEmail } from './instrumentations/worker/instrumentEmail';
import { instrumentExportedHandlerFetch } from './instrumentations/worker/instrumentFetch';
import { instrumentExportedHandlerQueue } from './instrumentations/worker/instrumentQueue';
import { instrumentExportedHandlerScheduled } from './instrumentations/worker/instrumentScheduled';
import { instrumentExportedHandlerTail } from './instrumentations/worker/instrumentTail';
import { getHonoIntegration } from './integrations/hono';
/**
* Wrapper for Cloudflare handlers.
*
* Initializes the SDK and wraps the handler with Sentry instrumentation.
*
* Automatically instruments the `fetch` method of the handler.
*
* @param optionsCallback Function that returns the options for the SDK initialization.
* @param handler {ExportedHandler} The handler to wrap.
* @returns The wrapped handler.
*/
export function withSentry<
Env = typeof env,
QueueHandlerMessage = unknown,
CfHostMetadata = unknown,
T extends ExportedHandler<Env, QueueHandlerMessage, CfHostMetadata> = ExportedHandler<
Env,
QueueHandlerMessage,
CfHostMetadata
>,
>(optionsCallback: (env: Env) => CloudflareOptions | undefined, handler: T): T {
setAsyncLocalStorageAsyncContextStrategy();
try {
instrumentExportedHandlerFetch(handler, optionsCallback);
instrumentHonoErrorHandler(handler);
instrumentExportedHandlerScheduled(handler, optionsCallback);
instrumentExportedHandlerEmail(handler, optionsCallback);
instrumentExportedHandlerQueue(handler, optionsCallback);
instrumentExportedHandlerTail(handler, optionsCallback);
// This is here because Miniflare sometimes cannot get instrumented
} catch {
// Do not console anything here, we don't want to spam the console with errors
}
return handler;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function instrumentHonoErrorHandler<T extends ExportedHandler<any, any, any>>(handler: T): void {
if (
'onError' in handler &&
'errorHandler' in handler &&
typeof handler.errorHandler === 'function' &&
!isInstrumented(handler.errorHandler)
) {
handler.errorHandler = new Proxy(handler.errorHandler, {
apply(target, thisArg, args) {
const [err, context] = args;
getHonoIntegration()?.handleHonoException(err, context);
return Reflect.apply(target, thisArg, args);
},
});
markAsInstrumented(handler.errorHandler);
}
}