-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathinterceptor.go
95 lines (80 loc) · 2.48 KB
/
interceptor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package interceptor
import (
"context"
"go.temporal.io/sdk/interceptor"
"go.temporal.io/sdk/log"
"go.temporal.io/sdk/workflow"
)
type workerInterceptor struct {
interceptor.WorkerInterceptorBase
options InterceptorOptions
}
type InterceptorOptions struct {
GetExtraLogTagsForWorkflow func(workflow.Context) []interface{}
GetExtraLogTagsForActivity func(context.Context) []interface{}
}
func NewWorkerInterceptor(options InterceptorOptions) interceptor.WorkerInterceptor {
return &workerInterceptor{options: options}
}
func (w *workerInterceptor) InterceptActivity(
ctx context.Context,
next interceptor.ActivityInboundInterceptor,
) interceptor.ActivityInboundInterceptor {
i := &activityInboundInterceptor{root: w}
i.Next = next
return i
}
type activityInboundInterceptor struct {
interceptor.ActivityInboundInterceptorBase
root *workerInterceptor
}
func (a *activityInboundInterceptor) Init(outbound interceptor.ActivityOutboundInterceptor) error {
i := &activityOutboundInterceptor{root: a.root}
i.Next = outbound
return a.Next.Init(i)
}
type activityOutboundInterceptor struct {
interceptor.ActivityOutboundInterceptorBase
root *workerInterceptor
}
func (a *activityOutboundInterceptor) GetLogger(ctx context.Context) log.Logger {
logger := a.Next.GetLogger(ctx)
// Add extra tags if any
if a.root.options.GetExtraLogTagsForActivity != nil {
if extraTags := a.root.options.GetExtraLogTagsForActivity(ctx); len(extraTags) > 0 {
logger = log.With(logger, extraTags...)
}
}
return logger
}
func (w *workerInterceptor) InterceptWorkflow(
ctx workflow.Context,
next interceptor.WorkflowInboundInterceptor,
) interceptor.WorkflowInboundInterceptor {
i := &workflowInboundInterceptor{root: w}
i.Next = next
return i
}
type workflowInboundInterceptor struct {
interceptor.WorkflowInboundInterceptorBase
root *workerInterceptor
}
func (w *workflowInboundInterceptor) Init(outbound interceptor.WorkflowOutboundInterceptor) error {
i := &workflowOutboundInterceptor{root: w.root}
i.Next = outbound
return w.Next.Init(i)
}
type workflowOutboundInterceptor struct {
interceptor.WorkflowOutboundInterceptorBase
root *workerInterceptor
}
func (w *workflowOutboundInterceptor) GetLogger(ctx workflow.Context) log.Logger {
logger := w.Next.GetLogger(ctx)
// Add extra tags if any
if w.root.options.GetExtraLogTagsForWorkflow != nil {
if extraTags := w.root.options.GetExtraLogTagsForWorkflow(ctx); len(extraTags) > 0 {
logger = log.With(logger, extraTags...)
}
}
return logger
}