@@ -9,10 +9,12 @@ import (
9
9
"github.com/benbjohnson/clock"
10
10
"github.com/cschleiden/go-workflows/backend"
11
11
"github.com/cschleiden/go-workflows/internal/core"
12
+ "github.com/cschleiden/go-workflows/internal/metrickeys"
12
13
"github.com/cschleiden/go-workflows/internal/task"
13
14
"github.com/cschleiden/go-workflows/internal/workflow"
14
15
"github.com/cschleiden/go-workflows/internal/workflow/cache"
15
16
"github.com/cschleiden/go-workflows/log"
17
+ "github.com/cschleiden/go-workflows/metrics"
16
18
)
17
19
18
20
type WorkflowWorker struct {
@@ -36,7 +38,7 @@ func NewWorkflowWorker(backend backend.Backend, registry *workflow.Registry, opt
36
38
if options .WorkflowExecutorCache != nil {
37
39
c = options .WorkflowExecutorCache
38
40
} else {
39
- c = cache .NewWorkflowExecutorLRUCache (options .WorkflowExecutorCacheSize , options .WorkflowExecutorCacheTTL )
41
+ c = cache .NewWorkflowExecutorLRUCache (backend . Metrics (), options .WorkflowExecutorCacheSize , options .WorkflowExecutorCacheTTL )
40
42
}
41
43
42
44
return & WorkflowWorker {
@@ -123,6 +125,14 @@ func (ww *WorkflowWorker) runDispatcher() {
123
125
}
124
126
125
127
func (ww * WorkflowWorker ) handle (ctx context.Context , t * task.Workflow ) {
128
+ // Record how long this task was in the queue
129
+ scheduledAt := t .NewEvents [0 ].Timestamp // Use the timestamp of the first event as the schedule time
130
+ timeInQueue := time .Since (scheduledAt )
131
+ ww .backend .Metrics ().Distribution (metrickeys .WorkflowTaskDelay , metrics.Tags {}, float64 (timeInQueue / time .Millisecond ))
132
+
133
+ timer := metrics .Timer (ww .backend .Metrics (), metrickeys .WorkflowTaskProcessed , metrics.Tags {})
134
+ defer timer .Stop ()
135
+
126
136
result , err := ww .handleTask (ctx , t )
127
137
if err != nil {
128
138
ww .logger .Panic ("could not handle workflow task" , "error" , err )
@@ -131,8 +141,12 @@ func (ww *WorkflowWorker) handle(ctx context.Context, t *task.Workflow) {
131
141
state := core .WorkflowInstanceStateActive
132
142
if result .Completed {
133
143
state = core .WorkflowInstanceStateFinished
144
+
145
+ ww .backend .Metrics ().Counter (metrickeys .WorkflowInstanceFinished , metrics.Tags {}, 1 )
134
146
}
135
147
148
+ ww .backend .Metrics ().Counter (metrickeys .ActivityTaskScheduled , metrics.Tags {}, int64 (len (result .ActivityEvents )))
149
+
136
150
if err := ww .backend .CompleteWorkflowTask (
137
151
ctx , t , t .WorkflowInstance , state , result .Executed , result .ActivityEvents , result .TimerEvents , result .WorkflowEvents ); err != nil {
138
152
ww .logger .Panic ("could not complete workflow task" , "error" , err )
0 commit comments