mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
## What changed?
Extracting workflow id during redirection based on interface
## Why?
Configurable checks if needed based on workflow id
## How did you test it?
- [ ] built
- [ ] run locally and tested manually
- [ ] covered by existing tests
- [ ] added new unit test(s)
- [ ] added new functional test(s)
```
michaely520 ~/projects/temporal % TEMPORAL_TEST_LOG_LEVEL=debug go test -tags test_dep -v ./tests/xdc -run TestStreamBasedReplicationTestSuite/EnableTransitionHistory/TestCloseTransferTaskAckedReplication -timeout 5m -count=1 | grep "workflow ID extraction"
2025-12-17T08:29:04.827-0800 debug workflow ID extraction: adding workflow ID to context {"cluster-name": "active_odmrd", "host": "127.0.0.1:63201", "operation": "StartWorkflowExecution", "wf-id": "test-replication-40c67c0f-1730-4b61-9a84-fde179771032", "logging-call-at": "/Users/michaely520/projects/temporal/common/rpc/interceptor/workflow_id_interceptor.go:129"}
2025-12-17T08:29:04.837-0800 debug workflow ID extraction: adding workflow ID to context {"cluster-name": "active_odmrd", "host": "127.0.0.1:63201", "operation": "RespondWorkflowTaskCompleted", "wf-id": "test-replication-40c67c0f-1730-4b61-9a84-fde179771032", "logging-call-at": "/Users/michaely520/projects/temporal/common/rpc/interceptor/workflow_id_interceptor.go:129"}
2025-12-17T08:29:04.940-0800 debug workflow ID extraction: adding workflow ID to context {"cluster-name": "active_odmrd", "host": "127.0.0.1:63201", "operation": "DescribeWorkflowExecution", "wf-id": "test-replication-40c67c0f-1730-4b61-9a84-fde179771032", "logging-call-at": "/Users/michaely520/projects/temporal/common/rpc/interceptor/workflow_id_interceptor.go:129"}
2025-12-17T08:29:09.949-0800 debug workflow ID extraction: adding workflow ID to context {"cluster-name": "standby_odmrd", "host": "127.0.0.1:63210", "operation": "DescribeWorkflowExecution", "wf-id": "test-replication-40c67c0f-1730-4b61-9a84-fde179771032", "logging-call-at": "/Users/michaely520/projects/temporal/common/rpc/interceptor/workflow_id_interceptor.go:129"}
2025-12-17T08:29:12.061-0800 debug workflow ID extraction: adding workflow ID to context {"cluster-name": "standby_odmrd", "host": "127.0.0.1:63210", "operation": "DescribeWorkflowExecution", "wf-id": "test-replication-40c67c0f-1730-4b61-9a84-fde179771032", "logging-call-at": "/Users/michaely520/projects/temporal/common/rpc/interceptor/workflow_id_interceptor.go:129"}
```
142 lines
3.6 KiB
Go
142 lines
3.6 KiB
Go
package interceptor
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
commonpb "go.temporal.io/api/common/v1"
|
|
"go.temporal.io/api/workflowservice/v1"
|
|
"go.temporal.io/server/common/api"
|
|
"go.temporal.io/server/common/namespace"
|
|
"go.temporal.io/server/common/tasktoken"
|
|
)
|
|
|
|
type BusinessIDExtractor struct {
|
|
serializer tasktoken.Serializer
|
|
}
|
|
|
|
func NewBusinessIDExtractor() BusinessIDExtractor {
|
|
return BusinessIDExtractor{
|
|
serializer: *tasktoken.NewSerializer(),
|
|
}
|
|
}
|
|
|
|
// WorkflowServiceExtractor returns a BusinessIDExtractorFunc that extracts business ID
|
|
// from WorkflowService API requests using the provided BusinessIDExtractor.
|
|
func WorkflowServiceExtractor(extractor BusinessIDExtractor) BusinessIDExtractorFunc {
|
|
return func(_ context.Context, req any, fullMethod string) string {
|
|
// Only process WorkflowService APIs
|
|
if !strings.HasPrefix(fullMethod, api.WorkflowServicePrefix) {
|
|
return ""
|
|
}
|
|
|
|
methodName := api.MethodName(fullMethod)
|
|
pattern, hasPattern := methodToPattern[methodName]
|
|
if !hasPattern {
|
|
return ""
|
|
}
|
|
|
|
return extractor.Extract(req, pattern)
|
|
}
|
|
}
|
|
|
|
// Interfaces for extracting business ID from different request types.
|
|
type (
|
|
workflowIDGetter interface {
|
|
GetWorkflowId() string
|
|
}
|
|
|
|
workflowExecutionGetter interface {
|
|
GetWorkflowExecution() *commonpb.WorkflowExecution
|
|
}
|
|
|
|
executionGetter interface {
|
|
GetExecution() *commonpb.WorkflowExecution
|
|
}
|
|
|
|
taskTokenGetter interface {
|
|
GetTaskToken() []byte
|
|
}
|
|
)
|
|
|
|
// Extract extracts business ID from the request using the specified pattern.
|
|
// Returns the business ID or namespace.EmptyBusinessID if not found.
|
|
func (e BusinessIDExtractor) Extract(req any, pattern BusinessIDPattern) string {
|
|
if req == nil {
|
|
return namespace.EmptyBusinessID
|
|
}
|
|
|
|
//nolint:revive // identical-switch-branches: PatternNone and default both fall through intentionally
|
|
switch pattern {
|
|
case PatternWorkflowID:
|
|
if getter, ok := req.(workflowIDGetter); ok {
|
|
return getter.GetWorkflowId()
|
|
}
|
|
|
|
case PatternWorkflowExecution:
|
|
if getter, ok := req.(workflowExecutionGetter); ok {
|
|
if exec := getter.GetWorkflowExecution(); exec != nil {
|
|
return exec.GetWorkflowId()
|
|
}
|
|
}
|
|
|
|
case PatternExecution:
|
|
if getter, ok := req.(executionGetter); ok {
|
|
if exec := getter.GetExecution(); exec != nil {
|
|
return exec.GetWorkflowId()
|
|
}
|
|
}
|
|
|
|
case PatternTaskToken:
|
|
if getter, ok := req.(taskTokenGetter); ok {
|
|
if tokenBytes := getter.GetTaskToken(); len(tokenBytes) > 0 {
|
|
if taskToken, err := e.serializer.Deserialize(tokenBytes); err == nil {
|
|
return taskToken.GetWorkflowId()
|
|
}
|
|
}
|
|
}
|
|
|
|
case PatternMultiOperation:
|
|
return e.extractMultiOperation(req)
|
|
|
|
case PatternNone:
|
|
// No extraction needed
|
|
|
|
default:
|
|
// Unknown pattern
|
|
}
|
|
|
|
return namespace.EmptyBusinessID
|
|
}
|
|
|
|
// extractMultiOperation extracts business ID from ExecuteMultiOperationRequest.
|
|
// The business ID is extracted from the first operation's StartWorkflow request.
|
|
func (e BusinessIDExtractor) extractMultiOperation(req any) string {
|
|
multiOpReq, ok := req.(*workflowservice.ExecuteMultiOperationRequest)
|
|
if !ok {
|
|
return namespace.EmptyBusinessID
|
|
}
|
|
|
|
ops := multiOpReq.GetOperations()
|
|
if len(ops) == 0 {
|
|
return namespace.EmptyBusinessID
|
|
}
|
|
|
|
firstOp := ops[0]
|
|
if firstOp == nil {
|
|
return namespace.EmptyBusinessID
|
|
}
|
|
|
|
startWorkflow := firstOp.GetStartWorkflow()
|
|
if startWorkflow == nil {
|
|
// First operation is not StartWorkflow - try to get from UpdateWorkflow
|
|
updateWorkflow := firstOp.GetUpdateWorkflow()
|
|
if updateWorkflow != nil && updateWorkflow.GetWorkflowExecution() != nil {
|
|
return updateWorkflow.GetWorkflowExecution().GetWorkflowId()
|
|
}
|
|
return namespace.EmptyBusinessID
|
|
}
|
|
|
|
return startWorkflow.GetWorkflowId()
|
|
}
|