Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
261 changes: 0 additions & 261 deletions packages/cloudflare/src/handler.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export {
instrumentLangGraph,
} from '@sentry/core';

export { withSentry } from './handler';
export { withSentry } from './withSentry';
export { instrumentDurableObjectWithSentry } from './durableobject';
export { sentryPagesPlugin } from './pages-plugin';

Expand Down
83 changes: 83 additions & 0 deletions packages/cloudflare/src/instrumentations/worker/instrumentEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import type { EmailMessage, ExportedHandler } from '@cloudflare/workers-types';
import {
captureException,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
startSpan,
withIsolationScope,
} from '@sentry/core';
import type { CloudflareOptions } from '../../client';
import { flushAndDispose } from '../../flush';
import { isInstrumented, markAsInstrumented } from '../../instrument';
import { getFinalOptions } from '../../options';
import { addCloudResourceContext } from '../../scope-utils';
import { init } from '../../sdk';
import { instrumentContext } from '../../utils/instrumentContext';

/**
* Core email handler logic - wraps execution with Sentry instrumentation.
*/
function wrapEmailHandler(
emailMessage: EmailMessage,
options: CloudflareOptions,
context: ExecutionContext,
fn: () => unknown,
): unknown {
return withIsolationScope(isolationScope => {
const waitUntil = context.waitUntil.bind(context);

const client = init({ ...options, ctx: context });
isolationScope.setClient(client);

addCloudResourceContext(isolationScope);

return startSpan(
{
op: 'faas.email',
name: `Handle Email ${emailMessage.to}`,
attributes: {
'faas.trigger': 'email',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.faas.cloudflare.email',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'task',
},
},
async () => {
try {
return await fn();
} catch (e) {
captureException(e, { mechanism: { handled: false, type: 'auto.faas.cloudflare.email' } });
throw e;
} finally {
waitUntil(flushAndDispose(client));
}
},
);
});
}

/**
* Instruments an email handler for ExportedHandler (env/ctx come from args).
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function instrumentExportedHandlerEmail<T extends ExportedHandler<any, any, any>>(
handler: T,
optionsCallback: (env: Parameters<NonNullable<T['email']>>[1]) => CloudflareOptions | undefined,
): void {
if (!('email' in handler) || typeof handler.email !== 'function' || isInstrumented(handler.email)) {
return;
}

handler.email = new Proxy(handler.email, {
apply(target, thisArg, args: Parameters<NonNullable<T['email']>>) {
const [emailMessage, env, ctx] = args;
const context = instrumentContext(ctx);
args[2] = context;

const options = getFinalOptions(optionsCallback(env), env);

return wrapEmailHandler(emailMessage, options, context, () => target.apply(thisArg, args));
},
});

markAsInstrumented(handler.email);
}
33 changes: 33 additions & 0 deletions packages/cloudflare/src/instrumentations/worker/instrumentFetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { ExportedHandler } from '@cloudflare/workers-types';
import type { CloudflareOptions } from '../../client';
import { isInstrumented, markAsInstrumented } from '../../instrument';
import { getFinalOptions } from '../../options';
import { wrapRequestHandler } from '../../request';
import { instrumentContext } from '../../utils/instrumentContext';

/**
* Instruments a fetch handler for ExportedHandler (env/ctx come from args).
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function instrumentExportedHandlerFetch<T extends ExportedHandler<any, any, any>>(
handler: T,
optionsCallback: (env: Parameters<NonNullable<T['fetch']>>[1]) => CloudflareOptions | undefined,
): void {
if (!('fetch' in handler) || typeof handler.fetch !== 'function' || isInstrumented(handler.fetch)) {
return;
}

handler.fetch = new Proxy(handler.fetch, {
apply(target, thisArg, args: Parameters<NonNullable<T['fetch']>>) {
const [request, env, ctx] = args;
const context = instrumentContext(ctx);
args[2] = context;

const options = getFinalOptions(optionsCallback(env), env);

return wrapRequestHandler({ options, request, context }, () => target.apply(thisArg, args));
},
});

markAsInstrumented(handler.fetch);
}
Loading
Loading