Hey just wanted to clarify my understanding of the use of interceptors and context propagators in go.
If i use an interceptor to add values to the workflow context on ExecuteWorkflow, then i would need to add a context propagator to propagate those values throughout the workflow and activities right? I can’t do that in the interceptor itself?
How would i do it through an interceptor? In the code below i’m taking the workflow type and adding to the context. However, the context passed into activities don’t have this value and that’s what context propagators are used for right? Just wondering if there’s a way to do it through interceptors, thanks!
type tokenInterceptor struct {
interceptor.WorkerInterceptorBase
}
type activityInboundInterceptor struct {
interceptor.ActivityInboundInterceptorBase
}
type workflowInboundInterceptor struct {
interceptor.WorkflowInboundInterceptorBase
outbound interceptor.WorkflowOutboundInterceptor
}
type workflowOutboundInterceptor struct {
interceptor.WorkflowOutboundInterceptorBase
}
type activityOutboundInterceptor struct {
interceptor.ActivityOutboundInterceptorBase
}
func NewTokenInterceptor() interceptor.WorkerInterceptor {
return &tokenInterceptor{}
}
func (*tokenInterceptor) InterceptActivity(ctx context.Context, next interceptor.ActivityInboundInterceptor) interceptor.ActivityInboundInterceptor {
i := &activityInboundInterceptor{}
i.Next = next
return i
}
func (*tokenInterceptor) InterceptWorkflow(ctx workflow.Context, next interceptor.WorkflowInboundInterceptor) interceptor.WorkflowInboundInterceptor {
i := &workflowInboundInterceptor{}
i.Next = next
return i
}
func (w *workflowInboundInterceptor) Init(outbound interceptor.WorkflowOutboundInterceptor) error {
i := &workflowOutboundInterceptor{}
i.Next = outbound
w.outbound = i
return w.Next.Init(i)
}
func (a *activityInboundInterceptor) Init(outbound interceptor.ActivityOutboundInterceptor) error {
i := &activityOutboundInterceptor{}
i.Next = outbound
return a.Next.Init(i)
}
func (w *workflowInboundInterceptor) ExecuteWorkflow(ctx workflow.Context, in *interceptor.ExecuteWorkflowInput) (interface{}, error) {
workflowName := w.outbound.GetInfo(ctx).WorkflowType.Name
ctx = workflow.WithValue(ctx, "workflowName", workflowName)
ex, err := w.Next.ExecuteWorkflow(ctx, in)
return ex, err
}
func (a *activityInboundInterceptor) ExecuteActivity(ctx context.Context, in *interceptor.ExecuteActivityInput) (interface{}, error) {
ex, err := a.Next.ExecuteActivity(ctx, in)
workflowName := ctx.Value("workflowName")
fmt.Println(workflowName)
return ex, err
}
You must take it from the context and insert it into the header when starting a workflow or activity. Then, take it from the header and insert it into the context. See opentracing support which is implemented as an interceptor.