Skip to content

DX-1797: Add WorkflowMiddleware #97

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

CahidArda
Copy link
Collaborator

Adds WorkflowMiddleware which simplifies tracking steps and their executions. In the initial version, we omit the step results. In the future we can build on top of this middleware.

Already this middleware allows writing a simple Sentry middleware.

Sentry Middleware
import * as Sentry from "@sentry/core";
import { WorkflowMiddleware } from "@upstash/workflow";

export interface SentryMiddlewareOptions {
  disableAutomaticFlush?: boolean;
}

export const sentryMiddleware = (
  opts?: SentryMiddlewareOptions,
): WorkflowMiddleware => {
  const mw = new WorkflowMiddleware({
    name: "upstash-workflow-sentry-middleware",
    init({ workflowRunId }) {
      return Sentry.withIsolationScope((scope) => {
        const sharedTags: Record<string, string | undefined> = {
          "workflow.run.id": workflowRunId,
        };

        scope.setTags(sharedTags);

        let execSpan: Sentry.Span;

        return Sentry.startSpanManual(
          {
            name: "Upstash Workflow Run",
            op: "run",
            attributes: {
              ...sharedTags,
            },
            scope,
          },
          (reqSpan) => {
            return {
              beforeExecution({ stepName }) {
                  Sentry.startSpanManual(
                    {
                      name: "Execution",
                      op: "execution",
                      attributes: {
                        ...sharedTags,
                        "workflow.step.name": stepName,
                      },
                      scope,
                    },
                    (_execSpan) => {
                      execSpan = _execSpan;
                    },
                  );
              },
              afterExecution() {
                execSpan?.end();
              },
              async runCompleted() {
                if (!opts?.disableAutomaticFlush) {
                  await Sentry.flush();
                }
              },
              async onError({ error }) {
                Sentry.withActiveSpan(reqSpan, (scope) => {
                  scope.setTags(sharedTags);
                  scope.setTransactionName(`workflow.run.error`);
                  scope.captureException(error);
                });
              }
            };
          },
        );
    })
    },
  });

  return mw as WorkflowMiddleware;
};

Copy link

linear bot commented May 5, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant