Skip to content

Commit 0a53bac

Browse files
committed
fix metrics for workflow
1 parent bcd44f2 commit 0a53bac

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

internal/metrickeys/metrickeys.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ const (
3232
ContinuedAsNew = "continued_as_new"
3333

3434
ActivityName = "activity"
35+
EventName = "event"
3536
)

internal/worker/workflow.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/benbjohnson/clock"
1111
"github.com/cschleiden/go-workflows/backend"
1212
"github.com/cschleiden/go-workflows/internal/core"
13+
"github.com/cschleiden/go-workflows/internal/history"
1314
"github.com/cschleiden/go-workflows/internal/metrickeys"
1415
"github.com/cschleiden/go-workflows/internal/task"
1516
"github.com/cschleiden/go-workflows/internal/workflow"
@@ -133,9 +134,19 @@ func (ww *WorkflowWorker) runDispatcher() {
133134

134135
func (ww *WorkflowWorker) handle(ctx context.Context, t *task.Workflow) {
135136
// Record how long this task was in the queue
136-
scheduledAt := t.NewEvents[0].Timestamp // Use the timestamp of the first event as the schedule time
137+
firstEvent := t.NewEvents[0]
138+
var scheduledAt time.Time
139+
if firstEvent.Type == history.EventType_TimerFired {
140+
timerFiredAttributes := firstEvent.Attributes.(*history.TimerFiredAttributes)
141+
scheduledAt = timerFiredAttributes.At // Use the timestamp of the timer fired event as the schedule time
142+
} else {
143+
scheduledAt = firstEvent.Timestamp // Use the timestamp of the first event as the schedule time
144+
}
145+
137146
timeInQueue := time.Since(scheduledAt)
138-
ww.backend.Metrics().Distribution(metrickeys.WorkflowTaskDelay, metrics.Tags{}, float64(timeInQueue/time.Millisecond))
147+
ww.backend.Metrics().Distribution(metrickeys.WorkflowTaskDelay, metrics.Tags{
148+
metrickeys.EventName: fmt.Sprint(t.NewEvents[0].Type),
149+
}, float64(timeInQueue/time.Millisecond))
139150

140151
timer := metrics.Timer(ww.backend.Metrics(), metrickeys.WorkflowTaskProcessed, metrics.Tags{})
141152

0 commit comments

Comments
 (0)