mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 19:17:28 +01:00
* 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>
169 lines
6.7 KiB
Go
169 lines
6.7 KiB
Go
package weed_server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/testutil"
|
|
"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"
|
|
)
|
|
|
|
func grpcRequests(method, code string) float64 {
|
|
return testutil.ToFloat64(stats.FilerGrpcRequestCounter.WithLabelValues(method, code))
|
|
}
|
|
|
|
// grpcTimed is how many calls of method the histogram has observed.
|
|
func grpcTimed(t *testing.T, method string) uint64 {
|
|
t.Helper()
|
|
families, err := stats.Gather.Gather()
|
|
if err != nil {
|
|
t.Fatalf("gather: %v", err)
|
|
}
|
|
for _, family := range families {
|
|
if family.GetName() != "SeaweedFS_filer_grpc_request_seconds" {
|
|
continue
|
|
}
|
|
for _, metric := range family.GetMetric() {
|
|
for _, label := range metric.GetLabel() {
|
|
if label.GetName() == "method" && label.GetValue() == method {
|
|
return metric.GetHistogram().GetSampleCount()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// A unary call is counted under its method and the status code it ends with,
|
|
// and timed, whether the handler answers or fails.
|
|
func TestFilerGrpcMetricsCountUnaryCallsByMethodAndCode(t *testing.T) {
|
|
info := &grpc.UnaryServerInfo{FullMethod: "/filer_pb.SeaweedFiler/MetricsTestUnary"}
|
|
ok0, aborted0, unknown0 := grpcRequests("MetricsTestUnary", "OK"), grpcRequests("MetricsTestUnary", "Aborted"), grpcRequests("MetricsTestUnary", "Unknown")
|
|
timed0 := grpcTimed(t, "MetricsTestUnary")
|
|
|
|
resp, err := filerGrpcMetricsUnary(context.Background(), "req", info, func(ctx context.Context, req any) (any, error) {
|
|
return "resp", nil
|
|
})
|
|
if resp != "resp" || err != nil {
|
|
t.Fatalf("the handler's answer did not come through: %v, %v", resp, err)
|
|
}
|
|
conflict := status.Error(codes.Aborted, "transaction conflict")
|
|
if _, err := filerGrpcMetricsUnary(context.Background(), "req", info, func(ctx context.Context, req any) (any, error) {
|
|
return nil, conflict
|
|
}); err != conflict {
|
|
t.Fatalf("the handler's error did not come through: %v", err)
|
|
}
|
|
// an error without a gRPC status is what the client sees as Unknown
|
|
filerGrpcMetricsUnary(context.Background(), "req", info, func(ctx context.Context, req any) (any, error) {
|
|
return nil, errors.New("plain")
|
|
})
|
|
|
|
if got := grpcRequests("MetricsTestUnary", "OK") - ok0; got != 1 {
|
|
t.Errorf("OK calls counted: %v, want 1", got)
|
|
}
|
|
if got := grpcRequests("MetricsTestUnary", "Aborted") - aborted0; got != 1 {
|
|
t.Errorf("Aborted calls counted: %v, want 1", got)
|
|
}
|
|
if got := grpcRequests("MetricsTestUnary", "Unknown") - unknown0; got != 1 {
|
|
t.Errorf("Unknown calls counted: %v, want 1", got)
|
|
}
|
|
if got := grpcTimed(t, "MetricsTestUnary") - timed0; got != 3 {
|
|
t.Errorf("calls timed: %d, want 3", got)
|
|
}
|
|
}
|
|
|
|
// A stream is counted when it ends. A listing is timed; a subscription or a
|
|
// health watch, which the client holds open, and a client stream are not.
|
|
func TestFilerGrpcMetricsCountStreamsAndTimeTheFiniteOnes(t *testing.T) {
|
|
run := func(method string, clientStream bool, err error) {
|
|
fullMethod := "/filer_pb.SeaweedFiler/" + method
|
|
if strings.Contains(method, "/") {
|
|
fullMethod = "/" + method
|
|
}
|
|
info := &grpc.StreamServerInfo{FullMethod: fullMethod, IsServerStream: true, IsClientStream: clientStream}
|
|
if got := filerGrpcMetricsStream(nil, nil, info, func(srv any, stream grpc.ServerStream) error { return err }); got != err {
|
|
t.Fatalf("%s: the handler's error did not come through: %v", method, got)
|
|
}
|
|
}
|
|
tests := []struct {
|
|
method string
|
|
clientStream bool
|
|
err error
|
|
code string
|
|
timed uint64
|
|
}{
|
|
{"MetricsTestList", false, nil, "OK", 1},
|
|
{"SubscribeMetricsTest", false, status.Error(codes.Canceled, "client left"), "Canceled", 0},
|
|
{"MetricsTestBidi", true, nil, "OK", 0},
|
|
{"grpc.health.v1.Health/Watch", false, status.Error(codes.Canceled, "client left"), "Canceled", 0},
|
|
}
|
|
for _, tt := range tests {
|
|
counted0, timed0 := grpcRequests(tt.method, tt.code), grpcTimed(t, tt.method)
|
|
run(tt.method, tt.clientStream, tt.err)
|
|
if got := grpcRequests(tt.method, tt.code) - counted0; got != 1 {
|
|
t.Errorf("%s: %s streams counted: %v, want 1", tt.method, tt.code, got)
|
|
}
|
|
if got := grpcTimed(t, tt.method) - timed0; got != tt.timed {
|
|
t.Errorf("%s: streams timed: %d, want %d", tt.method, got, tt.timed)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A lookup that finds nothing returns filer_pb.ErrNotFound without a status:
|
|
// it is counted as NotFound, not with the errors that have no code.
|
|
func TestFilerGrpcMetricsCountAMissAsNotFound(t *testing.T) {
|
|
info := &grpc.UnaryServerInfo{FullMethod: "/filer_pb.SeaweedFiler/MetricsTestLookup"}
|
|
notFound0, unknown0 := grpcRequests("MetricsTestLookup", "NotFound"), grpcRequests("MetricsTestLookup", "Unknown")
|
|
for _, err := range []error{filer_pb.ErrNotFound, fmt.Errorf("lookup /a/b: %w", filer_pb.ErrNotFound)} {
|
|
if _, got := filerGrpcMetricsUnary(context.Background(), "req", info, func(ctx context.Context, req any) (any, error) {
|
|
return nil, err
|
|
}); got != err {
|
|
t.Fatalf("the handler's error did not come through: %v", got)
|
|
}
|
|
}
|
|
if got := grpcRequests("MetricsTestLookup", "NotFound") - notFound0; got != 2 {
|
|
t.Errorf("NotFound calls counted: %v, want 2", got)
|
|
}
|
|
if got := grpcRequests("MetricsTestLookup", "Unknown") - unknown0; got != 0 {
|
|
t.Errorf("Unknown calls counted: %v, want 0", got)
|
|
}
|
|
}
|
|
|
|
// A handler that returns its context's error reaches the client as Canceled or
|
|
// DeadlineExceeded, and is counted so, wrapped or not.
|
|
func TestFilerGrpcMetricsCountContextErrorsAsTheClientSeesThem(t *testing.T) {
|
|
for err, want := range map[error]string{
|
|
context.Canceled: "Canceled",
|
|
context.DeadlineExceeded: "DeadlineExceeded",
|
|
fmt.Errorf("batch: %w", context.Canceled): "Canceled",
|
|
fmt.Errorf("cache: %w", context.DeadlineExceeded): "DeadlineExceeded",
|
|
fmt.Errorf("op: %w", status.Error(codes.Aborted, "conflict")): "Aborted",
|
|
errors.New("plain"): "Unknown",
|
|
} {
|
|
if got := filerGrpcCodeLabel(err); got != want {
|
|
t.Errorf("filerGrpcCodeLabel(%v) = %q, want %q", err, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFilerGrpcMethodLabel(t *testing.T) {
|
|
for full, want := range map[string]string{
|
|
"/filer_pb.SeaweedFiler/CreateEntry": "CreateEntry",
|
|
"/filer_pb.SeaweedFiler/ListEntries": "ListEntries",
|
|
"/iam_pb.SeaweedIdentityAccessManagement/GetUser": "iam_pb.SeaweedIdentityAccessManagement/GetUser",
|
|
"/grpc.health.v1.Health/Check": "grpc.health.v1.Health/Check",
|
|
} {
|
|
if got := filerGrpcMethodLabel(full); got != want {
|
|
t.Errorf("filerGrpcMethodLabel(%q) = %q, want %q", full, got, want)
|
|
}
|
|
}
|
|
}
|