go.temporal.io/server/common/metrics/grpc.go

178 LOC · 102 covered · 76 uncovered · 29 ranges · 1429 concepts · 19 introducers · 561 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 package metrics
2
3 import (
4 "context"
5 "maps"
6 "sync"
7
8 metricsspb "go.temporal.io/server/api/metrics/v1"
9 "go.temporal.io/server/common/log"
10 "go.temporal.io/server/common/log/tag"
11 "google.golang.org/grpc"
12 "google.golang.org/grpc/metadata"
13 )
14
15 type (
16 metricsContextKey struct{}
17
18 // metricsContext is used to propagate metrics across single gRPC call within server
19 metricsContext struct {
20 sync.Mutex
21 CountersInt map[string]int64
22 }
23 )
24
25 var (
26 // "-bin" suffix is a reserved in gRPC that signals that metadata string value is actually a byte data
27 // If trailer key has such a suffix, value will be base64 encoded.
28 metricsTrailerKey = "metrics-trailer-bin"
29 metricsCtxKey = metricsContextKey{}
30 )
31
32 // NewServerMetricsContextInjectorInterceptor returns grpc server interceptor that adds metrics context to golang
33 // context.
34 > func NewServerMetricsContextInjectorInterceptor() grpc.UnaryServerInterceptor { grpc.go ×2
35 > return func(
36 > ctx context.Context,
37 > req any,
38 > info *grpc.UnaryServerInfo,
39 > handler grpc.UnaryHandler,
40 > ) (any, error) {
41 > ctxWithMetricsBaggage := AddMetricsContext(ctx)
42 > return handler(ctxWithMetricsBaggage, req)
43 > }
44 }
45
46 // NewClientMetricsTrailerPropagatorInterceptor returns grpc client interceptor that injects metrics received in trailer
47 // into metrics context.
48 > func NewClientMetricsTrailerPropagatorInterceptor(logger log.Logger) grpc.UnaryClientInterceptor { grpc.go ×1
49 > return func(
50 > ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker,
51 > opts ...grpc.CallOption,
52 > ) error {
53 > var trailer metadata.MD grpc.go ×1
54 > optsWithTrailer := append(opts, grpc.Trailer(&trailer))
55 > err := invoker(ctx, method, req, reply, cc, optsWithTrailer...)
56 >
57 > baggageStrings := trailer.Get(metricsTrailerKey)
58 > if len(baggageStrings) == 0 {
59 > return err grpc.go ×1
60 > }
61
62 > for _, baggageString := range baggageStrings { grpc.go ×3
63 > baggageBytes := []byte(baggageString)
64 > metricsBaggage := &metricsspb.Baggage{}
65 > unmarshalErr := metricsBaggage.Unmarshal(baggageBytes)
66 > if unmarshalErr != nil {
67 logger.Error("unable to unmarshal metrics baggage from trailer", tag.Error(unmarshalErr))
68 continue
69 }
70 > for counterName, counterValue := range metricsBaggage.CountersInt { grpc.go ×3
71 > ContextCounterAdd(ctx, counterName, counterValue) grpc.go ×1
72 > }
73 }
74
75 > return err grpc.go ×3
76 }
77 }
78
79 // NewServerMetricsTrailerPropagatorInterceptor returns grpc server interceptor that injects metrics from context into
80 // gRPC trailer.
81 > func NewServerMetricsTrailerPropagatorInterceptor(logger log.Logger) grpc.UnaryServerInterceptor { grpc.go ×2
82 > return func(
83 > ctx context.Context,
84 > req any,
85 > info *grpc.UnaryServerInfo,
86 > handler grpc.UnaryHandler,
87 > ) (any, error) {
88 > // we want to return original handler response, so don't override err grpc.go ×6
89 > resp, err := handler(ctx, req)
90 >
91 > select {
92 > case <-ctx.Done(): service_grpc.pb.go ×19
93 > return resp, err
94 > default: grpc.go ×6
95 }
96
97 > metricsCtx := getMetricsContext(ctx) grpc.go ×6
98 > if metricsCtx == nil {
99 return resp, err
100 }
101
102 > metricsBaggage := &metricsspb.Baggage{CountersInt: make(map[string]int64)} grpc.go ×6
103 >
104 > metricsCtx.Lock()
105 > maps.Copy(metricsBaggage.CountersInt, metricsCtx.CountersInt)
106 > metricsCtx.Unlock()
107 >
108 > bytes, marshalErr := metricsBaggage.Marshal()
109 > if marshalErr != nil {
110 logger.Error("unable to marshal metric baggage", tag.Error(marshalErr))
111 }
112
113 > md := metadata.Pairs(metricsTrailerKey, string(bytes)) grpc.go ×6
114 >
115 > marshalErr = grpc.SetTrailer(ctx, md)
116 > if marshalErr != nil {
117 logger.Error("unable to add metrics baggage to gRPC trailer", tag.Error(marshalErr))
118 }
119
120 > return resp, err grpc.go ×6
121 }
122 }
123
124 // getMetricsContext extracts metrics context from golang context.
125 > func getMetricsContext(ctx context.Context) *metricsContext { grpc.go ×1
126 > metricsCtx := ctx.Value(metricsCtxKey)
127 > if metricsCtx == nil {
128 > return nil grpc.go ×2
129 > }
130
131 > return metricsCtx.(*metricsContext) grpc.go ×1
132 }
133
134 > func AddMetricsContext(ctx context.Context) context.Context { grpc.go ×1
135 > metricsCtx := &metricsContext{}
136 > return context.WithValue(ctx, metricsCtxKey, metricsCtx)
137 > }
138
139 // ContextCounterAdd adds value to counter within metrics context.
140 > func ContextCounterAdd(ctx context.Context, name string, value int64) bool { grpc.go ×1
141 > metricsCtx := getMetricsContext(ctx)
142 >
143 > if metricsCtx == nil {
144 > return false grpc.go ×2
145 > }
146
147 > metricsCtx.Lock() grpc.go ×2
148 > defer metricsCtx.Unlock()
149 >
150 > if metricsCtx.CountersInt == nil {
151 > metricsCtx.CountersInt = make(map[string]int64)
152 > }
153
154 > val := metricsCtx.CountersInt[name] grpc.go ×2
155 > val += value
156 > metricsCtx.CountersInt[name] = val
157 >
158 > return true
159 }
160
161 // ContextCounterGet returns value and true if successfully retrieved value
162 > func ContextCounterGet(ctx context.Context, name string) (int64, bool) { grpc.go ×1
163 > metricsCtx := getMetricsContext(ctx)
164 >
165 > if metricsCtx == nil {
166 > return 0, false api.go ×2
167 > }
168
169 > metricsCtx.Lock() grpc.go ×1
170 > defer metricsCtx.Unlock()
171 >
172 > if metricsCtx.CountersInt == nil {
173 > return 0, false grpc.go ×1
174 > }
175
176 > result, ok := metricsCtx.CountersInt[name] grpc.go ×1
177 > return result, ok
178 }