-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(cloudflare): Add instrumentWorkflowWithSentry
to instrument workflows
#16672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3cc9b68
feat(cloudflare): Add `instrumentWorkflowWithSentry` to instrument wo…
timfish 3320311
check mechanism
timfish 2d76224
Lint
timfish 57baae6
Fix sveltkit cloudflare types
timfish 0812b6d
PR review
timfish 37cb24e
Merge branch 'develop' into timfish/feat/cloudflare-workflows
timfish 5e86900
More PR review
timfish dd1fb9e
Merge branch 'timfish/feat/cloudflare-workflows' of github.com:getsen…
timfish fff1438
Final fixes
timfish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
import type { PropagationContext } from '@sentry/core'; | ||
import { | ||
captureException, | ||
flush, | ||
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, | ||
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, | ||
startSpan, | ||
withIsolationScope, | ||
withScope, | ||
} from '@sentry/core'; | ||
import type { | ||
WorkflowEntrypoint, | ||
WorkflowEvent, | ||
WorkflowSleepDuration, | ||
WorkflowStep, | ||
WorkflowStepConfig, | ||
WorkflowStepEvent, | ||
WorkflowTimeoutDuration, | ||
} from 'cloudflare:workers'; | ||
import { setAsyncLocalStorageAsyncContextStrategy } from './async'; | ||
import type { CloudflareOptions } from './client'; | ||
import { addCloudResourceContext } from './scope-utils'; | ||
import { init } from './sdk'; | ||
|
||
const UUID_REGEX = /^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$/i; | ||
|
||
function propagationContextFromInstanceId(instanceId: string): PropagationContext { | ||
// Validate and normalize traceId - should be a valid UUID with or without hyphens | ||
if (!UUID_REGEX.test(instanceId)) { | ||
throw new Error("Invalid 'instanceId' for workflow: Sentry requires random UUIDs for instanceId."); | ||
} | ||
|
||
// Remove hyphens to get UUID without hyphens | ||
const traceId = instanceId.replace(/-/g, ''); | ||
|
||
// Derive sampleRand from last 4 characters of the random UUID | ||
// | ||
// We cannot store any state between workflow steps, so we derive the | ||
// sampleRand from the traceId itself. This ensures that the sampling is | ||
// consistent across all steps in the same workflow instance. | ||
const sampleRand = parseInt(traceId.slice(-4), 16) / 0xffff; | ||
|
||
return { | ||
traceId, | ||
sampleRand, | ||
}; | ||
} | ||
|
||
async function workflowStepWithSentry<V>( | ||
instanceId: string, | ||
options: CloudflareOptions, | ||
callback: () => V, | ||
): Promise<V> { | ||
setAsyncLocalStorageAsyncContextStrategy(); | ||
|
||
return withIsolationScope(async isolationScope => { | ||
const client = init({ ...options, enableDedupe: false }); | ||
isolationScope.setClient(client); | ||
|
||
addCloudResourceContext(isolationScope); | ||
|
||
return withScope(async scope => { | ||
const propagationContext = propagationContextFromInstanceId(instanceId); | ||
scope.setPropagationContext(propagationContext); | ||
|
||
// eslint-disable-next-line no-return-await | ||
return await callback(); | ||
}); | ||
}); | ||
} | ||
|
||
class WrappedWorkflowStep implements WorkflowStep { | ||
public constructor( | ||
private _instanceId: string, | ||
private _ctx: ExecutionContext, | ||
private _options: CloudflareOptions, | ||
private _step: WorkflowStep, | ||
) {} | ||
|
||
public async do<T extends Rpc.Serializable<T>>(name: string, callback: () => Promise<T>): Promise<T>; | ||
public async do<T extends Rpc.Serializable<T>>( | ||
name: string, | ||
config: WorkflowStepConfig, | ||
callback: () => Promise<T>, | ||
): Promise<T>; | ||
public async do<T extends Rpc.Serializable<T>>( | ||
name: string, | ||
configOrCallback: WorkflowStepConfig | (() => Promise<T>), | ||
maybeCallback?: () => Promise<T>, | ||
): Promise<T> { | ||
const userCallback = (maybeCallback || configOrCallback) as () => Promise<T>; | ||
const config = typeof configOrCallback === 'function' ? undefined : configOrCallback; | ||
|
||
const instrumentedCallback: () => Promise<T> = async () => { | ||
return workflowStepWithSentry(this._instanceId, this._options, async () => { | ||
return startSpan( | ||
{ | ||
op: 'function.step.do', | ||
name, | ||
attributes: { | ||
'cloudflare.workflow.timeout': config?.timeout, | ||
'cloudflare.workflow.retries.backoff': config?.retries?.backoff, | ||
'cloudflare.workflow.retries.delay': config?.retries?.delay, | ||
'cloudflare.workflow.retries.limit': config?.retries?.limit, | ||
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.faas.cloudflare.workflow', | ||
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'task', | ||
}, | ||
}, | ||
async span => { | ||
try { | ||
const result = await userCallback(); | ||
span.setStatus({ code: 1 }); | ||
return result; | ||
} catch (error) { | ||
captureException(error, { mechanism: { handled: true, type: 'cloudflare' } }); | ||
throw error; | ||
} finally { | ||
this._ctx.waitUntil(flush(2000)); | ||
} | ||
}, | ||
); | ||
}); | ||
}; | ||
|
||
return config ? this._step.do(name, config, instrumentedCallback) : this._step.do(name, instrumentedCallback); | ||
} | ||
|
||
public async sleep(name: string, duration: WorkflowSleepDuration): Promise<void> { | ||
return this._step.sleep(name, duration); | ||
} | ||
|
||
public async sleepUntil(name: string, timestamp: Date | number): Promise<void> { | ||
return this._step.sleepUntil(name, timestamp); | ||
} | ||
|
||
public async waitForEvent<T extends Rpc.Serializable<T>>( | ||
name: string, | ||
options: { type: string; timeout?: WorkflowTimeoutDuration | number }, | ||
): Promise<WorkflowStepEvent<T>> { | ||
return this._step.waitForEvent<T>(name, options); | ||
} | ||
} | ||
|
||
/** | ||
* Instruments a Cloudflare Workflow class with Sentry. | ||
* | ||
* @example | ||
* ```typescript | ||
* const InstrumentedWorkflow = instrumentWorkflowWithSentry( | ||
* (env) => ({ dsn: env.SENTRY_DSN }), | ||
* MyWorkflowClass | ||
* ); | ||
* | ||
* export default InstrumentedWorkflow; | ||
* ``` | ||
* | ||
* @param optionsCallback - Function that returns Sentry options to initialize Sentry | ||
* @param WorkflowClass - The workflow class to instrument | ||
* @returns Instrumented workflow class with the same interface | ||
*/ | ||
export function instrumentWorkflowWithSentry< | ||
E, // Environment type | ||
P, // Payload type | ||
T extends WorkflowEntrypoint<E, P>, // WorkflowEntrypoint type | ||
C extends new (ctx: ExecutionContext, env: E) => T, // Constructor type of the WorkflowEntrypoint class | ||
>(optionsCallback: (env: E) => CloudflareOptions, WorkFlowClass: C): C { | ||
return new Proxy(WorkFlowClass, { | ||
construct(target: C, args: [ctx: ExecutionContext, env: E], newTarget) { | ||
const [ctx, env] = args; | ||
const options = optionsCallback(env); | ||
const instance = Reflect.construct(target, args, newTarget) as T; | ||
return new Proxy(instance, { | ||
get(obj, prop, receiver) { | ||
if (prop === 'run') { | ||
return async function (event: WorkflowEvent<P>, step: WorkflowStep): Promise<unknown> { | ||
return obj.run.call(obj, event, new WrappedWorkflowStep(event.instanceId, ctx, options, step)); | ||
}; | ||
} | ||
return Reflect.get(obj, prop, receiver); | ||
}, | ||
}); | ||
}, | ||
}) as C; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change was due to updating to the latest Cloudflare types.
ExecutionContext
now has aprops
property which isn't onEventPluginContext
.