File size: 5,632 Bytes
e89cd08 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | // _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2025 Weaviate B.V. All rights reserved.
//
// CONTACT: hello@weaviate.io
//
package monitoring
import (
"context"
"errors"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/stats"
"google.golang.org/grpc/status"
)
// Make sure `GrpcStatsHandler always implements stats.Handler
var _ stats.Handler = &GrpcStatsHandler{}
type key int
const (
keyMethodName key = 1
keyRouteName key = 2
)
// InstrumentGrpc accepts server metrics and returns the few `[]grpc.ServerOption` which you can
// then wrap it with any `grpc.Server` to get these metrics instrumented automatically.
//
// ```
//
// svrMetrics := monitoring.NewGRPCServerMetrics(metrics, prometheus.DefaultRegisterer)
// grpcServer := grpc.NewServer(monitoring.InstrumentGrpc(*svrMetrics)...)
//
// grpcServer.Serve(listener)
//
// ```
func InstrumentGrpc(svrMetrics *GRPCServerMetrics) []grpc.ServerOption {
grpcOptions := []grpc.ServerOption{
grpc.StatsHandler(NewGrpcStatsHandler(
svrMetrics.InflightRequests,
svrMetrics.RequestBodySize,
svrMetrics.ResponseBodySize,
)),
}
grpcInterceptUnary := grpc.ChainUnaryInterceptor(
UnaryServerInstrument(svrMetrics.RequestDuration),
)
grpcOptions = append(grpcOptions, grpcInterceptUnary)
grpcInterceptStream := grpc.ChainStreamInterceptor(
StreamServerInstrument(svrMetrics.RequestDuration),
)
grpcOptions = append(grpcOptions, grpcInterceptStream)
return grpcOptions
}
func NewGrpcStatsHandler(inflight *prometheus.GaugeVec, requestSize *prometheus.HistogramVec, responseSize *prometheus.HistogramVec) *GrpcStatsHandler {
return &GrpcStatsHandler{
inflightRequests: inflight,
requestSize: requestSize,
responseSize: responseSize,
}
}
type GrpcStatsHandler struct {
inflightRequests *prometheus.GaugeVec
// in bytes
requestSize *prometheus.HistogramVec
responseSize *prometheus.HistogramVec
}
func (g *GrpcStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
return context.WithValue(ctx, keyMethodName, info.FullMethodName)
}
func (g *GrpcStatsHandler) HandleRPC(ctx context.Context, rpcStats stats.RPCStats) {
fullMethodName, ok := ctx.Value(keyMethodName).(string)
if !ok {
return
}
service, method := splitFullMethodName(fullMethodName)
switch s := rpcStats.(type) {
case *stats.Begin:
g.inflightRequests.WithLabelValues(service, method).Inc()
case *stats.End:
g.inflightRequests.WithLabelValues(service, method).Dec()
case *stats.InHeader:
// Ignore incoming headers.
case *stats.InPayload:
g.requestSize.WithLabelValues(service, method).Observe(float64(s.WireLength))
case *stats.InTrailer:
// Ignore incoming trailers.
case *stats.OutHeader:
// Ignore outgoing headers.
case *stats.OutPayload:
g.responseSize.WithLabelValues(service, method).Observe(float64(s.WireLength))
case *stats.OutTrailer:
// Ignore outgoing trailers. OutTrailer doesn't have valid WireLength (there is a deprecated field, always set to 0).
}
}
func (g *GrpcStatsHandler) TagConn(ctx context.Context, _ *stats.ConnTagInfo) context.Context {
return ctx
}
func (g *GrpcStatsHandler) HandleConn(_ context.Context, _ stats.ConnStats) {
// Don't need
}
func UnaryServerInstrument(hist *prometheus.HistogramVec) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
begin := time.Now()
resp, err := handler(ctx, req)
observe(hist, info.FullMethod, err, time.Since(begin))
return resp, err
}
}
func StreamServerInstrument(hist *prometheus.HistogramVec) grpc.StreamServerInterceptor {
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
begin := time.Now()
err := handler(srv, ss)
observe(hist, info.FullMethod, err, time.Since(begin))
return err
}
}
func observe(hist *prometheus.HistogramVec, fullMethod string, err error, duration time.Duration) {
service, method := splitFullMethodName(fullMethod)
// `hist` has following labels
// service - gRPC service name (e.g: weaviate.v1.Weaviate, weaviate.internal.cluster.ClusterService)
// method - Method from the gRPC service that got invoked. (e.g: Search, RemovePeer)
// status - grpc status (e.g: "OK", "CANCELED", "UNKNOWN", etc)
labelValues := []string{
service,
method,
errorToStatus(err),
}
hist.WithLabelValues(labelValues...).Observe(duration.Seconds())
}
func errorToStatus(err error) string {
code := errorToGrpcCode(err)
return code.String()
}
func errorToGrpcCode(err error) codes.Code {
if err == nil {
return codes.OK
}
if errors.Is(err, context.Canceled) {
return codes.Canceled
}
type grpcStatus interface {
GRPCStatus() *status.Status
}
var g grpcStatus
if errors.As(err, &g) {
st := g.GRPCStatus()
if st != nil {
return st.Code()
}
}
return codes.Unknown
}
// splitFullMethodName converts full gRPC method call into `service` and `method`
// e.g: "/weaviate.v1.Weaviate/Search" -> "weaviate.v1.Weaviate", "/Search"
func splitFullMethodName(fullMethod string) (string, string) {
fullMethod = strings.TrimPrefix(fullMethod, "/") // remove leading slash
if i := strings.Index(fullMethod, "/"); i >= 0 {
return fullMethod[:i], fullMethod[i+1:]
}
return "unknown", "unknown"
}
|