Skip to content

Commit 76d568a

Browse files
rkannan82claude
andauthored
Use task queue kind instead of name prefix for nexus metrics (#10141)
## What Use `TASK_QUEUE_KIND_WORKER_COMMANDS` enum check instead of string prefix matching on the task queue to determine if its internal queue. ## Why PR #9899 introduced `TASK_QUEUE_KIND_WORKER_COMMANDS`. The original code (PR #9760) predated this and relied on the `/temporal-sys/` name prefix as a workaround. ## How did you test it? Existing tests 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c14c923 commit 76d568a

3 files changed

Lines changed: 26 additions & 16 deletions

File tree

common/primitives/task_queues.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package primitives
33
import (
44
"fmt"
55

6+
enumspb "go.temporal.io/api/enums/v1"
67
"go.temporal.io/api/serviceerror"
78
)
89

@@ -17,6 +18,21 @@ const (
1718
DLQActivityTQ = "temporal-sys-dlq-activity-tq"
1819
)
1920

21+
// IsInternalTaskQueueKind returns true if the task queue kind identifies a
22+
// server-internal task queue (e.g. worker commands queues).
23+
// All kinds are listed explicitly so that adding a new kind produces a compile error.
24+
func IsInternalTaskQueueKind(kind enumspb.TaskQueueKind) bool {
25+
switch kind {
26+
case enumspb.TASK_QUEUE_KIND_WORKER_COMMANDS:
27+
return true
28+
case enumspb.TASK_QUEUE_KIND_UNSPECIFIED,
29+
enumspb.TASK_QUEUE_KIND_NORMAL,
30+
enumspb.TASK_QUEUE_KIND_STICKY:
31+
return false
32+
}
33+
return false
34+
}
35+
2036
func IsInternalPerNsTaskQueue(taskQueue string) bool {
2137
return taskQueue == PerNSWorkerTaskQueue
2238
}

service/matching/handler.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package matching
22

33
import (
44
"context"
5-
"strings"
65
"sync"
76
"time"
87

@@ -21,6 +20,7 @@ import (
2120
"go.temporal.io/server/common/persistence"
2221
"go.temporal.io/server/common/persistence/serialization"
2322
"go.temporal.io/server/common/persistence/visibility/manager"
23+
"go.temporal.io/server/common/primitives"
2424
"go.temporal.io/server/common/resource"
2525
"go.temporal.io/server/common/searchattribute"
2626
"go.temporal.io/server/common/testing/testhooks"
@@ -156,18 +156,12 @@ func (h *Handler) opMetricsHandler(
156156
)
157157
}
158158

159-
// internalTaskQueuePrefix identifies server-internal task queues
160-
// (e.g. /temporal-sys/worker-commands/{namespace}/{worker_grouping_key}).
161-
// Note: BreakdownMetricsByTaskQueue should NOT be enabled for these queues as
162-
// they are per-worker and will cause cardinality explosion.
163-
const internalTaskQueuePrefix = "/temporal-sys/"
164-
165159
// recordNexusTaskRequest emits the nexus_task_requests metric with namespace,
166160
// operation, client_name, and is_internal tags.
167-
func (h *Handler) recordNexusTaskRequest(ctx context.Context, namespaceID string, taskQueueName string, operation string) {
161+
func (h *Handler) recordNexusTaskRequest(ctx context.Context, namespaceID string, taskQueueKind enumspb.TaskQueueKind, operation string) {
168162
nsName := h.namespaceName(namespace.ID(namespaceID))
169163
clientName, _ := headers.GetClientNameAndVersion(ctx)
170-
isInternal := strings.HasPrefix(taskQueueName, internalTaskQueuePrefix)
164+
isInternal := primitives.IsInternalTaskQueueKind(taskQueueKind)
171165
metrics.NexusTaskRequests.With(h.metricsHandler).Record(1,
172166
metrics.NamespaceTag(nsName.String()),
173167
metrics.OperationTag(operation),
@@ -531,7 +525,7 @@ func (h *Handler) PollNexusTaskQueue(ctx context.Context, request *matchingservi
531525
// Only record on the initial handler call (ForwardedSource == ""), not on
532526
// the forwarded call to the root partition, to avoid double-counting.
533527
if request.GetForwardedSource() == "" {
534-
h.recordNexusTaskRequest(ctx, request.GetNamespaceId(), request.GetRequest().GetTaskQueue().GetName(), "PollNexusTaskQueue")
528+
h.recordNexusTaskRequest(ctx, request.GetNamespaceId(), request.GetRequest().GetTaskQueue().GetKind(), "PollNexusTaskQueue")
535529
}
536530

537531
if request.GetForwardedSource() != "" {
@@ -556,7 +550,7 @@ func (h *Handler) RespondNexusTaskCompleted(ctx context.Context, request *matchi
556550
enumspb.TASK_QUEUE_TYPE_NEXUS,
557551
metrics.MatchingRespondNexusTaskCompletedScope,
558552
)
559-
h.recordNexusTaskRequest(ctx, request.GetNamespaceId(), request.GetTaskQueue().GetName(), "RespondNexusTaskCompleted")
553+
h.recordNexusTaskRequest(ctx, request.GetNamespaceId(), request.GetTaskQueue().GetKind(), "RespondNexusTaskCompleted")
560554

561555
return h.engine.RespondNexusTaskCompleted(ctx, request, opMetrics)
562556
}
@@ -569,7 +563,7 @@ func (h *Handler) RespondNexusTaskFailed(ctx context.Context, request *matchings
569563
enumspb.TASK_QUEUE_TYPE_NEXUS,
570564
metrics.MatchingRespondNexusTaskFailedScope,
571565
)
572-
h.recordNexusTaskRequest(ctx, request.GetNamespaceId(), request.GetTaskQueue().GetName(), "RespondNexusTaskFailed")
566+
h.recordNexusTaskRequest(ctx, request.GetNamespaceId(), request.GetTaskQueue().GetKind(), "RespondNexusTaskFailed")
573567

574568
return h.engine.RespondNexusTaskFailed(ctx, request, opMetrics)
575569
}

service/matching/handler_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,16 @@ func TestNexusHandlersEmitClientNameMetric(t *testing.T) {
178178
"should not have client_name tag when header is absent, got: %v", snap)
179179
})
180180

181-
t.Run("is_internal for /temporal-sys/ task queue", func(t *testing.T) {
181+
t.Run("is_internal for worker commands task queue kind", func(t *testing.T) {
182182
captureHandler := metricstest.NewCaptureHandler()
183183
capture := captureHandler.StartCapture()
184184
defer captureHandler.StopCapture(capture)
185185

186186
h, _ := newTestHandler(t, captureHandler)
187187
ctx := ctxWithClientName(t, expectedClientName)
188188
internalTQ := &taskqueuepb.TaskQueue{
189-
Name: "/temporal-sys/worker-commands/ns/grouping-key",
190-
Kind: enumspb.TASK_QUEUE_KIND_NORMAL,
189+
Name: "some-task-queue",
190+
Kind: enumspb.TASK_QUEUE_KIND_WORKER_COMMANDS,
191191
}
192192

193193
_, err := h.PollNexusTaskQueue(ctx, &matchingservice.PollNexusTaskQueueRequest{
@@ -201,7 +201,7 @@ func TestNexusHandlersEmitClientNameMetric(t *testing.T) {
201201
snap := capture.Snapshot()
202202
require.NotEmpty(t, snap[nexusTaskRequestsMetric])
203203
require.True(t, findMetricWithTag(snap, nexusTaskRequestsMetric, "is_internal", "true"),
204-
"should have is_internal=true for /temporal-sys/ task queue, got: %v", snap)
204+
"should have is_internal=true for worker commands task queue kind, got: %v", snap)
205205
})
206206
}
207207

0 commit comments

Comments
 (0)