Skip to content

Commit 93c08b0

Browse files
authoredApr 24, 2024··
Ensure consistent labels for rpc metrics (#1444)
1 parent 69bc6c3 commit 93c08b0

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed
 

‎internal/common/metrics/grpc.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ import (
2727
"strings"
2828
"time"
2929

30-
"google.golang.org/grpc/status"
3130
enumspb "go.temporal.io/api/enums/v1"
3231
"go.temporal.io/api/serviceerror"
3332
"google.golang.org/grpc"
3433
"google.golang.org/grpc/codes"
34+
"google.golang.org/grpc/status"
3535
)
3636

3737
// HandlerContextKey is the context key for a MetricHandler value.
@@ -63,16 +63,17 @@ func NewGRPCInterceptor(defaultHandler Handler, suffix string) grpc.UnaryClientI
6363

6464
// Only take method name after the last slash
6565
operation := method[strings.LastIndex(method, "/")+1:]
66-
tags := map[string]string{OperationTagName: operation}
6766

6867
// Since this interceptor can be used for clients of different name, we
6968
// attempt to extract the namespace out of the request. All namespace-based
7069
// requests have been confirmed to have a top-level namespace field.
70+
namespace := "_unknown_"
7171
if nsReq, _ := req.(interface{ GetNamespace() string }); nsReq != nil {
72-
tags[NamespaceTagName] = nsReq.GetNamespace()
72+
namespace = nsReq.GetNamespace()
7373
}
7474

7575
// Capture time, record start, run, and record end
76+
tags := map[string]string{OperationTagName: operation, NamespaceTagName: namespace}
7677
handler = handler.WithTags(tags)
7778
start := time.Now()
7879
recordRequestStart(handler, longPoll, suffix)

‎internal/common/metrics/grpc_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ func TestGRPCInterceptor(t *testing.T) {
6565
counters := handler.Counters()
6666
require.Len(t, counters, 1)
6767
require.Equal(t, metrics.TemporalRequest+"_my_suffix", counters[0].Name)
68-
require.Equal(t, map[string]string{metrics.OperationTagName: "Check"}, counters[0].Tags)
68+
require.Equal(t, map[string]string{metrics.OperationTagName: "Check", metrics.NamespaceTagName: "_unknown_"}, counters[0].Tags)
6969
require.Equal(t, int64(1), counters[0].Value())
7070
timers := handler.Timers()
7171
require.Len(t, timers, 1)
7272
require.Equal(t, metrics.TemporalRequestLatency+"_my_suffix", timers[0].Name)
73-
require.Equal(t, map[string]string{metrics.OperationTagName: "Check"}, timers[0].Tags)
73+
require.Equal(t, map[string]string{metrics.OperationTagName: "Check", metrics.NamespaceTagName: "_unknown_"}, timers[0].Tags)
7474

7575
// Now clear the metrics and set a handler with tags and long poll on the
7676
// context and make a known failing call
@@ -85,9 +85,9 @@ func TestGRPCInterceptor(t *testing.T) {
8585
counters = handler.Counters()
8686
require.Len(t, counters, 2)
8787
require.Equal(t, metrics.TemporalLongRequest+"_my_suffix", counters[0].Name)
88-
require.Equal(t, map[string]string{metrics.OperationTagName: "Check", "roottag": "roottagval"}, counters[0].Tags)
88+
require.Equal(t, map[string]string{metrics.OperationTagName: "Check", "roottag": "roottagval", metrics.NamespaceTagName: "_unknown_"}, counters[0].Tags)
8989
require.Equal(t, int64(1), counters[0].Value())
9090
require.Equal(t, metrics.TemporalLongRequestFailure+"_my_suffix", counters[1].Name)
91-
require.Equal(t, map[string]string{metrics.OperationTagName: "Check", "roottag": "roottagval"}, counters[1].Tags)
91+
require.Equal(t, map[string]string{metrics.OperationTagName: "Check", "roottag": "roottagval", metrics.NamespaceTagName: "_unknown_"}, counters[1].Tags)
9292
require.Equal(t, int64(1), counters[1].Value())
9393
}

0 commit comments

Comments
 (0)
Please sign in to comment.