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.
package metrics
import (
"context"
"maps"
"sync"
metricsspb "go.temporal.io/server/api/metrics/v1"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
type (
metricsContextKey struct{}
// metricsContext is used to propagate metrics across single gRPC call within server
metricsContext struct {
sync.Mutex
CountersInt map[string]int64
}
)
var (
// "-bin" suffix is a reserved in gRPC that signals that metadata string value is actually a byte data
// If trailer key has such a suffix, value will be base64 encoded.
metricsTrailerKey = "metrics-trailer-bin"
metricsCtxKey = metricsContextKey{}
)
// NewServerMetricsContextInjectorInterceptor returns grpc server interceptor that adds metrics context to golang
// context.
return func(
ctx context.Context,
req any,
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (any, error) {
ctxWithMetricsBaggage := AddMetricsContext(ctx)
return handler(ctxWithMetricsBaggage, req)
}
}
// NewClientMetricsTrailerPropagatorInterceptor returns grpc client interceptor that injects metrics received in trailer
// into metrics context.
func NewClientMetricsTrailerPropagatorInterceptor(logger log.Logger) grpc.UnaryClientInterceptor {
grpc.go ×1
return func(
ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {
optsWithTrailer := append(opts, grpc.Trailer(&trailer))
err := invoker(ctx, method, req, reply, cc, optsWithTrailer...)
baggageStrings := trailer.Get(metricsTrailerKey)
if len(baggageStrings) == 0 {
}
baggageBytes := []byte(baggageString)
metricsBaggage := &metricsspb.Baggage{}
unmarshalErr := metricsBaggage.Unmarshal(baggageBytes)
if unmarshalErr != nil {
logger.Error("unable to unmarshal metrics baggage from trailer", tag.Error(unmarshalErr))
continue
}
}
}
}
}
// NewServerMetricsTrailerPropagatorInterceptor returns grpc server interceptor that injects metrics from context into
// gRPC trailer.
func NewServerMetricsTrailerPropagatorInterceptor(logger log.Logger) grpc.UnaryServerInterceptor {
grpc.go ×2
return func(
ctx context.Context,
req any,
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (any, error) {
resp, err := handler(ctx, req)
select {
return resp, err
}
if metricsCtx == nil {
return resp, err
}
metricsCtx.Lock()
maps.Copy(metricsBaggage.CountersInt, metricsCtx.CountersInt)
metricsCtx.Unlock()
bytes, marshalErr := metricsBaggage.Marshal()
if marshalErr != nil {
logger.Error("unable to marshal metric baggage", tag.Error(marshalErr))
}
marshalErr = grpc.SetTrailer(ctx, md)
if marshalErr != nil {
logger.Error("unable to add metrics baggage to gRPC trailer", tag.Error(marshalErr))
}
}
}
// getMetricsContext extracts metrics context from golang context.
metricsCtx := ctx.Value(metricsCtxKey)
if metricsCtx == nil {
}
}
metricsCtx := &metricsContext{}
return context.WithValue(ctx, metricsCtxKey, metricsCtx)
}
// ContextCounterAdd adds value to counter within metrics context.
metricsCtx := getMetricsContext(ctx)
if metricsCtx == nil {
}
defer metricsCtx.Unlock()
if metricsCtx.CountersInt == nil {
metricsCtx.CountersInt = make(map[string]int64)
}
val += value
metricsCtx.CountersInt[name] = val
return true
}
// ContextCounterGet returns value and true if successfully retrieved value
metricsCtx := getMetricsContext(ctx)
if metricsCtx == nil {
}
defer metricsCtx.Unlock()
if metricsCtx.CountersInt == nil {
}
return result, ok
}