| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| 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" |
| ) |
|
|
| |
| var _ stats.Handler = &GrpcStatsHandler{} |
|
|
| type key int |
|
|
| const ( |
| keyMethodName key = 1 |
| keyRouteName key = 2 |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 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 |
|
|
| |
| 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: |
| |
| case *stats.InPayload: |
| g.requestSize.WithLabelValues(service, method).Observe(float64(s.WireLength)) |
| case *stats.InTrailer: |
| |
| case *stats.OutHeader: |
| |
| case *stats.OutPayload: |
| g.responseSize.WithLabelValues(service, method).Observe(float64(s.WireLength)) |
| case *stats.OutTrailer: |
| |
| } |
| } |
|
|
| func (g *GrpcStatsHandler) TagConn(ctx context.Context, _ *stats.ConnTagInfo) context.Context { |
| return ctx |
| } |
|
|
| func (g *GrpcStatsHandler) HandleConn(_ context.Context, _ stats.ConnStats) { |
| |
| } |
|
|
| 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) |
|
|
| |
| |
| |
| |
|
|
| 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 |
| } |
|
|
| |
| |
| func splitFullMethodName(fullMethod string) (string, string) { |
| fullMethod = strings.TrimPrefix(fullMethod, "/") |
| if i := strings.Index(fullMethod, "/"); i >= 0 { |
| return fullMethod[:i], fullMethod[i+1:] |
| } |
| return "unknown", "unknown" |
| } |
|
|