Skip to content

Commit a300788

Browse files
author
cod1k
committed
Integrate copyExecutionContext across handlers and workflows
Replaced direct usage of `ExecutionContext` with `copyExecutionContext` for consistency and improved flexibility. Applied changes across handlers, durable objects, and workflows to enhance context management and method overriding.
1 parent cb97187 commit a300788

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

packages/cloudflare/src/durableobject.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { isInstrumented, markAsInstrumented } from './instrument';
1818
import { getFinalOptions } from './options';
1919
import { wrapRequestHandler } from './request';
2020
import { init } from './sdk';
21+
import { copyExecutionContext } from './utils/copyExecutionContext';
2122

2223
type MethodWrapperOptions = {
2324
spanName?: string;
@@ -192,8 +193,9 @@ export function instrumentDurableObjectWithSentry<
192193
C extends new (state: DurableObjectState, env: E) => T,
193194
>(optionsCallback: (env: E) => CloudflareOptions, DurableObjectClass: C): C {
194195
return new Proxy(DurableObjectClass, {
195-
construct(target, [context, env]) {
196+
construct(target, [ctx, env]) {
196197
setAsyncLocalStorageAsyncContextStrategy();
198+
const context = copyExecutionContext(ctx);
197199

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

packages/cloudflare/src/handler.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { getFinalOptions } from './options';
1515
import { wrapRequestHandler } from './request';
1616
import { addCloudResourceContext } from './scope-utils';
1717
import { init } from './sdk';
18+
import { copyExecutionContext } from './utils/copyExecutionContext';
1819

1920
/**
2021
* Wrapper for Cloudflare handlers.
@@ -38,7 +39,8 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
3839
if ('fetch' in handler && typeof handler.fetch === 'function' && !isInstrumented(handler.fetch)) {
3940
handler.fetch = new Proxy(handler.fetch, {
4041
apply(target, thisArg, args: Parameters<ExportedHandlerFetchHandler<Env, CfHostMetadata>>) {
41-
const [request, env, context] = args;
42+
const [request, env, ctx] = args;
43+
const context = copyExecutionContext(ctx);
4244

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

@@ -72,7 +74,9 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
7274
if ('scheduled' in handler && typeof handler.scheduled === 'function' && !isInstrumented(handler.scheduled)) {
7375
handler.scheduled = new Proxy(handler.scheduled, {
7476
apply(target, thisArg, args: Parameters<ExportedHandlerScheduledHandler<Env>>) {
75-
const [event, env, context] = args;
77+
const [event, env, ctx] = args;
78+
const context = copyExecutionContext(ctx);
79+
7680
return withIsolationScope(isolationScope => {
7781
const options = getFinalOptions(optionsCallback(env), env);
7882
const waitUntil = context.waitUntil.bind(context);
@@ -115,7 +119,9 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
115119
if ('email' in handler && typeof handler.email === 'function' && !isInstrumented(handler.email)) {
116120
handler.email = new Proxy(handler.email, {
117121
apply(target, thisArg, args: Parameters<EmailExportedHandler<Env>>) {
118-
const [emailMessage, env, context] = args;
122+
const [emailMessage, env, ctx] = args;
123+
const context = copyExecutionContext(ctx);
124+
119125
return withIsolationScope(isolationScope => {
120126
const options = getFinalOptions(optionsCallback(env), env);
121127
const waitUntil = context.waitUntil.bind(context);
@@ -156,7 +162,8 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
156162
if ('queue' in handler && typeof handler.queue === 'function' && !isInstrumented(handler.queue)) {
157163
handler.queue = new Proxy(handler.queue, {
158164
apply(target, thisArg, args: Parameters<ExportedHandlerQueueHandler<Env, QueueHandlerMessage>>) {
159-
const [batch, env, context] = args;
165+
const [batch, env, ctx] = args;
166+
const context = copyExecutionContext(ctx);
160167

161168
return withIsolationScope(isolationScope => {
162169
const options = getFinalOptions(optionsCallback(env), env);
@@ -206,7 +213,8 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
206213
if ('tail' in handler && typeof handler.tail === 'function' && !isInstrumented(handler.tail)) {
207214
handler.tail = new Proxy(handler.tail, {
208215
apply(target, thisArg, args: Parameters<ExportedHandlerTailHandler<Env>>) {
209-
const [, env, context] = args;
216+
const [, env, ctx] = args;
217+
const context = copyExecutionContext(ctx);
210218

211219
return withIsolationScope(async isolationScope => {
212220
const options = getFinalOptions(optionsCallback(env), env);

packages/cloudflare/src/workflows.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { setAsyncLocalStorageAsyncContextStrategy } from './async';
2222
import type { CloudflareOptions } from './client';
2323
import { addCloudResourceContext } from './scope-utils';
2424
import { init } from './sdk';
25+
import { copyExecutionContext } from './utils/copyExecutionContext';
2526

2627
const UUID_REGEX = /^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$/i;
2728

@@ -157,6 +158,8 @@ export function instrumentWorkflowWithSentry<
157158
return new Proxy(WorkFlowClass, {
158159
construct(target: C, args: [ctx: ExecutionContext, env: E], newTarget) {
159160
const [ctx, env] = args;
161+
const context = copyExecutionContext(ctx);
162+
160163
const options = optionsCallback(env);
161164
const instance = Reflect.construct(target, args, newTarget) as T;
162165
return new Proxy(instance, {
@@ -179,10 +182,10 @@ export function instrumentWorkflowWithSentry<
179182
return await obj.run.call(
180183
obj,
181184
event,
182-
new WrappedWorkflowStep(event.instanceId, ctx, options, step),
185+
new WrappedWorkflowStep(event.instanceId, context, options, step),
183186
);
184187
} finally {
185-
ctx.waitUntil(flush(2000));
188+
context.waitUntil(flush(2000));
186189
}
187190
});
188191
});

0 commit comments

Comments
 (0)