-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
workflow.go
118 lines (103 loc) · 4.51 KB
/
workflow.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
Copyright 2024 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package metrics
import (
"context"
"fmt"
"testing"
"github.com/microsoft/durabletask-go/api"
"github.com/microsoft/durabletask-go/backend"
"github.com/microsoft/durabletask-go/client"
"github.com/microsoft/durabletask-go/task"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/dapr/dapr/tests/integration/framework"
"github.com/dapr/dapr/tests/integration/framework/process/daprd"
"github.com/dapr/dapr/tests/integration/framework/process/http/app"
"github.com/dapr/dapr/tests/integration/framework/process/placement"
"github.com/dapr/dapr/tests/integration/suite"
)
func init() {
suite.Register(new(workflow))
}
// workflow tests daprd metrics for workflows
type workflow struct {
daprd *daprd.Daprd
place *placement.Placement
}
func (w *workflow) Setup(t *testing.T) []framework.Option {
w.place = placement.New(t)
app := app.New(t)
w.daprd = daprd.New(t,
daprd.WithAppPort(app.Port()),
daprd.WithAppProtocol("http"),
daprd.WithAppID("myapp"),
daprd.WithPlacementAddresses(w.place.Address()),
daprd.WithInMemoryActorStateStore("mystore"),
)
return []framework.Option{
framework.WithProcesses(w.place, app, w.daprd),
}
}
func (w *workflow) Run(t *testing.T, ctx context.Context) {
w.place.WaitUntilRunning(t, ctx)
w.daprd.WaitUntilRunning(t, ctx)
// Register workflow
r := task.NewTaskRegistry()
r.AddActivityN("activity_success", func(ctx task.ActivityContext) (any, error) {
return "success", nil
})
r.AddActivityN("activity_failure", func(ctx task.ActivityContext) (any, error) {
return nil, fmt.Errorf("failure")
})
r.AddOrchestratorN("workflow", func(ctx *task.OrchestrationContext) (any, error) {
var input string
if err := ctx.GetInput(&input); err != nil {
return nil, err
}
activityName := input
err := ctx.CallActivity(activityName).Await(nil)
if err != nil {
return nil, err
}
return nil, nil
})
taskhubClient := client.NewTaskHubGrpcClient(w.daprd.GRPCConn(t, ctx), backend.DefaultLogger())
taskhubClient.StartWorkItemListener(ctx, r)
t.Run("successful workflow execution", func(t *testing.T) {
id, err := taskhubClient.ScheduleNewOrchestration(ctx, "workflow", api.WithInput("activity_success"))
require.NoError(t, err)
metadata, err := taskhubClient.WaitForOrchestrationCompletion(ctx, id, api.WithFetchPayloads(true))
require.NoError(t, err)
assert.True(t, metadata.IsComplete())
// Verify metrics
metrics := w.daprd.Metrics(t, ctx)
assert.Equal(t, 1, int(metrics["dapr_runtime_workflow_operation_count|app_id:myapp|namespace:|operation:create_workflow|status:success"]))
assert.Equal(t, 1, int(metrics["dapr_runtime_workflow_execution_count|app_id:myapp|namespace:|status:success|workflow_name:workflow"]))
assert.Equal(t, 1, int(metrics["dapr_runtime_workflow_activity_execution_count|activity_name:activity_success|app_id:myapp|namespace:|status:success"]))
assert.GreaterOrEqual(t, 1, int(metrics["dapr_runtime_workflow_execution_latency|app_id:myapp|namespace:|status:success|workflow_name:workflow"]))
assert.GreaterOrEqual(t, 1, int(metrics["dapr_runtime_workflow_scheduling_latency|app_id:myapp|namespace:|workflow_name:workflow"]))
})
t.Run("failed workflow execution", func(t *testing.T) {
id, err := taskhubClient.ScheduleNewOrchestration(ctx, "workflow", api.WithInput("activity_failure"))
require.NoError(t, err)
metadata, err := taskhubClient.WaitForOrchestrationCompletion(ctx, id, api.WithFetchPayloads(true))
require.NoError(t, err)
assert.True(t, metadata.IsComplete())
// Verify metrics
metrics := w.daprd.Metrics(t, ctx)
assert.Equal(t, 2, int(metrics["dapr_runtime_workflow_operation_count|app_id:myapp|namespace:|operation:create_workflow|status:success"]))
assert.Equal(t, 1, int(metrics["dapr_runtime_workflow_execution_count|app_id:myapp|namespace:|status:failed|workflow_name:workflow"]))
assert.Equal(t, 1, int(metrics["dapr_runtime_workflow_activity_execution_count|activity_name:activity_failure|app_id:myapp|namespace:|status:failed"]))
})
}