|
| 1 | +package workflow |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + enumspb "go.temporal.io/api/enums/v1" |
| 9 | + "go.temporal.io/server/common/dynamicconfig" |
| 10 | + "go.temporal.io/server/common/log" |
| 11 | + "go.temporal.io/server/common/metrics" |
| 12 | + "go.temporal.io/server/common/metrics/metricstest" |
| 13 | + "go.temporal.io/server/common/namespace" |
| 14 | + "go.temporal.io/server/service/history/configs" |
| 15 | + "google.golang.org/protobuf/types/known/timestamppb" |
| 16 | +) |
| 17 | + |
| 18 | +func TestEmitWorkflowCompletionStats_WorkflowDuration(t *testing.T) { |
| 19 | + logger := log.NewTestLogger() |
| 20 | + testHandler, err := metricstest.NewHandler(logger, metrics.ClientConfig{}) |
| 21 | + require.NoError(t, err) |
| 22 | + testNamespace := namespace.Name("test-namespace") |
| 23 | + config := &configs.Config{ |
| 24 | + BreakdownMetricsByTaskQueue: dynamicconfig.GetBoolPropertyFnFilteredByTaskQueue(true), |
| 25 | + } |
| 26 | + |
| 27 | + completionMetric := completionMetric{ |
| 28 | + initialized: true, |
| 29 | + isWorkflow: true, |
| 30 | + taskQueue: "test-task-queue", |
| 31 | + namespaceState: "active", |
| 32 | + workflowTypeName: "test-workflow", |
| 33 | + status: enumspb.WORKFLOW_EXECUTION_STATUS_COMPLETED, |
| 34 | + startTime: timestamppb.New(time.Unix(100, 0)), |
| 35 | + closeTime: timestamppb.New(time.Unix(130, 0)), |
| 36 | + } |
| 37 | + |
| 38 | + emitWorkflowCompletionStats(testHandler, testNamespace, completionMetric, config) |
| 39 | + |
| 40 | + snapshot, err := testHandler.Snapshot() |
| 41 | + require.NoError(t, err) |
| 42 | + buckets, err := snapshot.Histogram("workflow_duration_milliseconds", |
| 43 | + |
| 44 | + metrics.StringTag("namespace", "test-namespace"), |
| 45 | + metrics.StringTag("namespace_state", "active"), |
| 46 | + metrics.StringTag("workflowType", "test-workflow"), |
| 47 | + metrics.StringTag("operation", "CompletionStats"), |
| 48 | + metrics.StringTag("taskqueue", "test-task-queue"), |
| 49 | + metrics.StringTag("otel_scope_name", "temporal"), |
| 50 | + metrics.StringTag("otel_scope_version", ""), |
| 51 | + ) |
| 52 | + require.NoError(t, err) |
| 53 | + require.NotEmpty(t, buckets) |
| 54 | +} |
| 55 | + |
| 56 | +func TestEmitWorkflowCompletionStats_SkipNonWorkflow(t *testing.T) { |
| 57 | + logger := log.NewTestLogger() |
| 58 | + testHandler, err := metricstest.NewHandler(logger, metrics.ClientConfig{}) |
| 59 | + require.NoError(t, err) |
| 60 | + testNamespace := namespace.Name("test-namespace") |
| 61 | + config := &configs.Config{ |
| 62 | + BreakdownMetricsByTaskQueue: dynamicconfig.GetBoolPropertyFnFilteredByTaskQueue(true), |
| 63 | + } |
| 64 | + |
| 65 | + // Test with isWorkflow: false (should not emit metrics) |
| 66 | + completionMetric := completionMetric{ |
| 67 | + initialized: true, |
| 68 | + isWorkflow: false, |
| 69 | + taskQueue: "test-task-queue", |
| 70 | + namespaceState: "active", |
| 71 | + workflowTypeName: "test-workflow", |
| 72 | + status: enumspb.WORKFLOW_EXECUTION_STATUS_COMPLETED, |
| 73 | + startTime: timestamppb.New(time.Unix(100, 0)), |
| 74 | + closeTime: timestamppb.New(time.Unix(130, 0)), |
| 75 | + } |
| 76 | + |
| 77 | + emitWorkflowCompletionStats(testHandler, testNamespace, completionMetric, config) |
| 78 | + |
| 79 | + snapshot, err := testHandler.Snapshot() |
| 80 | + require.NoError(t, err) |
| 81 | + |
| 82 | + // When isWorkflow is false, no metrics should be emitted at all |
| 83 | + // So the metric shouldn't even exist in the snapshot |
| 84 | + buckets, err := snapshot.Histogram("workflow_duration_milliseconds", |
| 85 | + metrics.StringTag("namespace", "test-namespace"), |
| 86 | + metrics.StringTag("namespace_state", "active"), |
| 87 | + metrics.StringTag("workflowType", "test-workflow"), |
| 88 | + metrics.StringTag("operation", "CompletionStats"), |
| 89 | + metrics.StringTag("taskqueue", "test-task-queue"), |
| 90 | + metrics.StringTag("otel_scope_name", "temporal"), |
| 91 | + metrics.StringTag("otel_scope_version", ""), |
| 92 | + ) |
| 93 | + require.Error(t, err) // Should error because metric doesn't exist |
| 94 | + require.Nil(t, buckets) |
| 95 | +} |
0 commit comments