Files
seaweedfs/weed/server/filer_grpc_metrics.go
T
db0ec9e9b3 filer: count and time the gRPC requests by method (#13107)
* filer: count and time the gRPC requests by method

The filer's request metrics cover its HTTP API. The mounts and the S3
gateway reach it over gRPC, so on a cluster used through them the filer
reported its store operations and nothing about the requests behind
them: how many lookups, creates, renames or listings arrive, how long
each kind takes, how many end in an error.

Two metrics for the gRPC port, filled by a unary and a stream
interceptor: SeaweedFS_filer_grpc_request_total{method, code} and
SeaweedFS_filer_grpc_request_seconds{method}. A stream is counted when
it ends, and timed unless the client holds it open (a subscription, a
client stream). A lookup that finds nothing returns ErrNotFound without
a status; it is labelled NotFound rather than Unknown.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* filer: label a call ending in its context's error as gRPC reports it

A handler that returns ctx.Err() reaches the client as Canceled or
DeadlineExceeded; status.Code labelled it Unknown.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: count a health watch without timing it

grpc.health.v1.Health/Watch stays open until the client leaves, so its
duration is the connection's lifetime, like a subscription's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* metrics: dashboard panels for the filer's gRPC requests

Calls, errors by code, and p99/p50 durations by method, in the Filer row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: MapleWhiteZZZ <18653045+MapleWhiteZZZ@users.noreply.github.com>
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
2026-11-29 10:26:52 -08:00

81 lines
3.0 KiB
Go

package weed_server
import (
"context"
"errors"
"strings"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/stats"
)
// FilerGrpcMetricsServerOptions returns the gRPC server options that count the
// filer's gRPC calls and time them (SeaweedFS_filer_grpc_request_total and
// SeaweedFS_filer_grpc_request_seconds). The mounts and the S3 gateway work
// through this port: without it the filer's request metrics show its HTTP API
// and say nothing of a lookup, a create, a rename or a listing they send.
//
// The options go before the interceptors that may refuse a call, so a refused
// call is counted with its code.
func FilerGrpcMetricsServerOptions() []grpc.ServerOption {
return []grpc.ServerOption{
grpc.ChainUnaryInterceptor(filerGrpcMetricsUnary),
grpc.ChainStreamInterceptor(filerGrpcMetricsStream),
}
}
func filerGrpcMetricsUnary(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
start := time.Now()
resp, err := handler(ctx, req)
method := filerGrpcMethodLabel(info.FullMethod)
stats.FilerGrpcRequestCounter.WithLabelValues(method, filerGrpcCodeLabel(err)).Inc()
stats.FilerGrpcRequestHistogram.WithLabelValues(method).Observe(time.Since(start).Seconds())
return resp, err
}
// A stream is counted when it ends, and timed unless the client holds it open
// for as long as it runs: how long a subscription lasted says nothing about
// the filer, and would fill the top bucket.
func filerGrpcMetricsStream(srv any, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
start := time.Now()
err := handler(srv, ss)
method := filerGrpcMethodLabel(info.FullMethod)
stats.FilerGrpcRequestCounter.WithLabelValues(method, filerGrpcCodeLabel(err)).Inc()
if !info.IsClientStream && !strings.HasPrefix(method, "Subscribe") && method != "grpc.health.v1.Health/Watch" {
stats.FilerGrpcRequestHistogram.WithLabelValues(method).Observe(time.Since(start).Seconds())
}
return err
}
// filerGrpcMethodLabel names a call by its method for the filer's own
// service, "CreateEntry" for "/filer_pb.SeaweedFiler/CreateEntry", and by
// service and method for any other service on the port.
func filerGrpcMethodLabel(fullMethod string) string {
name := strings.TrimPrefix(fullMethod, "/")
if method, ok := strings.CutPrefix(name, "filer_pb.SeaweedFiler/"); ok {
return method
}
return name
}
// filerGrpcCodeLabel is the gRPC status code a call ends with. A lookup of a
// name that does not exist returns filer_pb.ErrNotFound as it is, which
// carries no status and reaches the client as Unknown; it is labelled
// NotFound, so that the everyday miss stays apart from the errors nothing
// names.
func filerGrpcCodeLabel(err error) string {
if errors.Is(err, filer_pb.ErrNotFound) {
return codes.NotFound.String()
}
s, ok := status.FromError(err)
if !ok {
s = status.FromContextError(err)
}
return s.Code().String()
}