Skip to content

Commit a3f2d18

Browse files
authored
Add translation latency metric (#150)
1 parent 8f8427c commit a3f2d18

4 files changed

Lines changed: 36 additions & 15 deletions

File tree

interceptor/translation_interceptor.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package interceptor
22

33
import (
44
"context"
5-
"fmt"
65
"strings"
6+
"time"
77

88
"go.temporal.io/server/common/api"
99
"go.temporal.io/server/common/log"
@@ -47,17 +47,19 @@ func (i *TranslationInterceptor) Intercept(
4747

4848
for _, tr := range i.translators {
4949
if tr.MatchMethod(info.FullMethod) {
50+
start := time.Now()
5051
changed, trErr := tr.TranslateRequest(req)
51-
logTranslateResult(tr, i.logger, changed, trErr, methodName+"Request", req)
52+
logTranslateResult(tr, i.logger, changed, trErr, methodName+"Request", req, time.Since(start))
5253
}
5354
}
5455

5556
resp, err := handler(ctx, req)
5657

5758
for _, tr := range i.translators {
5859
if tr.MatchMethod(info.FullMethod) {
60+
start := time.Now()
5961
changed, trErr := tr.TranslateResponse(resp)
60-
logTranslateResult(tr, i.logger, changed, trErr, methodName+"Response", resp)
62+
logTranslateResult(tr, i.logger, changed, trErr, methodName+"Response", resp, time.Since(start))
6163
}
6264
}
6365

@@ -84,16 +86,18 @@ type streamTranslator struct {
8486

8587
func (w *streamTranslator) RecvMsg(m any) error {
8688
for _, tr := range w.translators {
89+
start := time.Now()
8790
changed, trErr := tr.TranslateRequest(m)
88-
logTranslateResult(tr, w.logger, changed, trErr, "RecvMsg", m)
91+
logTranslateResult(tr, w.logger, changed, trErr, "RecvMsg", m, time.Since(start))
8992
}
9093
return w.ServerStream.RecvMsg(m)
9194
}
9295

9396
func (w *streamTranslator) SendMsg(m any) error {
9497
for _, tr := range w.translators {
98+
start := time.Now()
9599
changed, trErr := tr.TranslateResponse(m)
96-
logTranslateResult(tr, w.logger, changed, trErr, "SendMsg", m)
100+
logTranslateResult(tr, w.logger, changed, trErr, "SendMsg", m, time.Since(start))
97101
}
98102
return w.ServerStream.SendMsg(m)
99103
}
@@ -110,14 +114,17 @@ func newStreamTranslator(
110114
}
111115
}
112116

113-
func logTranslateResult(tr Translator, logger log.Logger, changed bool, err error, methodName string, obj any) {
117+
func logTranslateResult(tr Translator, logger log.Logger, changed bool, err error, methodName string, obj any, duration time.Duration) {
118+
msgType := metrics.SanitizedTypeName(obj)
119+
metrics.TranslationLatency.WithLabelValues(tr.Kind(), msgType).Observe(duration.Seconds())
120+
114121
methodTag := tag.NewStringTag("method", methodName)
115122
if err != nil {
116-
logger.Error("translation error", methodTag, tag.Error(err), tag.NewStringTag("type", fmt.Sprintf("%T", obj)))
117-
metrics.TranslationErrors.WithLabelValues(tr.Kind(), methodName).Inc()
123+
logger.Error("translation error", methodTag, tag.Error(err), tag.NewStringTag("type", msgType))
124+
metrics.TranslationErrors.WithLabelValues(tr.Kind(), msgType).Inc()
118125
} else if changed {
119126
logger.Debug("translation applied", methodTag, tag.NewAnyTag("obj", obj))
120-
metrics.TranslationCount.WithLabelValues(tr.Kind(), methodName).Inc()
127+
metrics.TranslationCount.WithLabelValues(tr.Kind(), msgType).Inc()
121128
} else {
122129
logger.Debug("translation not applied", methodTag, tag.NewAnyTag("obj", obj))
123130
}

metrics/metrics.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"net/http"
66
"regexp"
7+
"strings"
78

89
grpcprom "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
910
"github.com/prometheus/client_golang/prometheus"
@@ -33,6 +34,12 @@ func SanitizeForPrometheus(value string) string {
3334
return prometheusReplacePattern.ReplaceAllLiteralString(value, "_")
3435
}
3536

37+
func SanitizedTypeName(m any) string {
38+
name := fmt.Sprintf("%T", m)
39+
name = strings.ReplaceAll(name, "*", "")
40+
return SanitizeForPrometheus(name)
41+
}
42+
3643
// GetStandardGRPCInterceptor returns a ServerMetrics with our preferred standard config for monitoring gRPC servers.
3744
// Want to change/add options? Check the docs at https://pkg.go.dev/github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus@v1.1.0#section-documentation
3845
// Some more handy links: https://prometheus.io/docs/concepts/metric_types/#histogram

metrics/prometheus_defs.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ var (
5656
MuxErrors = DefaultCounterVec("mux_errors", "Number of errors observed from mux", muxManagerLabels...)
5757
MuxConnectionEstablish = DefaultCounterVec("mux_connection_establish", "Number of times mux has established", muxManagerLabels...)
5858

59-
translationLabels = []string{"kind", "message_type"}
60-
TranslationCount = DefaultCounterVec("translation_success", "Count of message translations", translationLabels...)
61-
TranslationErrors = DefaultCounterVec("translation_error", "Count of message translation errors", translationLabels...)
59+
translationLabels = []string{"kind", "message_type"}
60+
TranslationCount = DefaultCounterVec("translation_success", "Count of message translations", translationLabels...)
61+
TranslationErrors = DefaultCounterVec("translation_error", "Count of message translation errors", translationLabels...)
62+
TranslationLatency = DefaultHistogramVec("translation_latency", "Latency of message translations", translationLabels...)
6263

6364
UTF8RepairTranslationKind = "utf8repair"
6465
NamespaceTranslationKind = "namespace"
@@ -102,4 +103,5 @@ func init() {
102103

103104
prometheus.MustRegister(TranslationCount)
104105
prometheus.MustRegister(TranslationErrors)
106+
prometheus.MustRegister(TranslationLatency)
105107
}

proto/compat/codec.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package compat
22

33
import (
4-
"fmt"
4+
"time"
55

66
"go.temporal.io/server/common/log"
77
"go.temporal.io/server/common/log/tag"
@@ -60,8 +60,13 @@ func (c *RepairUTF8Codec) Name() string {
6060
func (c *RepairUTF8Codec) Unmarshal(data mem.BufferSlice, v any) error {
6161
err := c.delegate.Unmarshal(data, v)
6262
if common.IsInvalidUTF8Error(err) {
63-
msgType := fmt.Sprintf("%T", v)
64-
if err := convertAndRepairInvalidUTF8(data.Materialize(), v); err != nil {
63+
start := time.Now()
64+
err := convertAndRepairInvalidUTF8(data.Materialize(), v)
65+
duration := time.Since(start)
66+
67+
msgType := metrics.SanitizedTypeName(v)
68+
metrics.TranslationLatency.WithLabelValues(metrics.UTF8RepairTranslationKind, msgType).Observe(duration.Seconds())
69+
if err != nil {
6570
c.Logger.Error("during UTF-8 repair", tag.Error(err))
6671
metrics.TranslationErrors.WithLabelValues(metrics.UTF8RepairTranslationKind, msgType).Inc()
6772
} else {

0 commit comments

Comments
 (0)