Workflow / activity middleware

Is there an equivalent of a middleware for workflow, and/or activity execution in the Go SDK?

The use-case here is to have some quick instrumentation around the execution of a workflow and/or activity, e.g. logging, custom metrics, error / exception tracking, etc.

1 Like

We don’t have a notion of lifecycle hooks in the system. As of today I think the most straightforward solution would be to code some small shims that wrap this behavior. Adding @maxim to make sure i’m not missing anything.

Go SDK supports workflow interceptors. Activity interceptors are not yet supported, but planned.

I have a use case where I need to send business events every time I enter/exit an activity, which is similar to the use case described above. Would you recommend implementing these as separate activities until activity interceptors become available?

@maxim @ryland I am wondering if there are any potential problems in having the following code to wrap activities (using OutbountCallsInterceptor)

func (t *testOutboundCallsInterceptor) ExecuteActivity(ctx workflow.Context, activityType string, args ...interface{}) workflow.Future {
    logger := workflow.GetLogger(ctx)
    logger.Info(fmt.Sprintf("Start Activity %s %v", activityType, args))
    var err error
    var result interface{}
    defer func() {
	    if err != nil {
	        logger.Error(fmt.Sprintf("End Activity %s %v", activityType, err))
	    } else {
		    logger.Info(fmt.Sprintf("End Activity %s %v", activityType, result))
	    }

     }()

    future := t.Next.ExecuteActivity(ctx, activityType, args...)
    err = future.Get(ctx, &result)

    return future
}

This code wraps activity invocation from the workflow code. It doesn’t wrap actual activity code invocation.

Thanks @maxim for the quick response. It also helped me understand the difference between workflow and activity interceptors. Do you know if anybody already started working on Add ActivityInterceptor · Issue #265 · temporalio/sdk-go · GitHub?

I"m not aware of anyone working on Issue #265.