Skip to content

Commit

Permalink
pkg/cmd: add metric for logical checks
Browse files Browse the repository at this point in the history
This tracks the "logical checks" which includes
not only check requests but each item in a bulk
check request.
  • Loading branch information
jzelinskie committed Dec 17, 2024
1 parent 397fefb commit 8ffa034
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pkg/cmd/server/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/jzelinskie/cobrautil/v2/cobraproclimits"
"github.com/jzelinskie/cobrautil/v2/cobrazerolog"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
Expand All @@ -30,6 +31,8 @@ import (
"google.golang.org/grpc/codes"

"github.com/authzed/authzed-go/pkg/requestmeta"
v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
"github.com/authzed/grpcutil"

"github.com/authzed/spicedb/internal/dispatch"
"github.com/authzed/spicedb/internal/logging"
Expand Down Expand Up @@ -170,6 +173,7 @@ const (
DefaultMiddlewareGRPCAuth = "grpcauth"
DefaultMiddlewareGRPCProm = "grpcprom"
DefaultMiddlewareServerVersion = "serverversion"
DefaultMiddlewareLogicalChecks = "logicalchecks"

DefaultInternalMiddlewareDispatch = "dispatch"
DefaultInternalMiddlewareDatastore = "datastore"
Expand Down Expand Up @@ -333,6 +337,11 @@ func DefaultUnaryMiddleware(opts MiddlewareOption) (*MiddlewareChain[grpc.UnaryS
WithInterceptor(serverversion.UnaryServerInterceptor(opts.EnableVersionResponse)).
Done(),

NewUnaryMiddleware().
WithName(DefaultMiddlewareLogicalChecks).
WithInterceptor(LogicalChecksMetricUnary).
Done(),

NewUnaryMiddleware().
WithName(DefaultInternalMiddlewareDispatch).
WithInternal(true).
Expand Down Expand Up @@ -406,6 +415,11 @@ func DefaultStreamingMiddleware(opts MiddlewareOption) (*MiddlewareChain[grpc.St
WithInterceptor(serverversion.StreamServerInterceptor(opts.EnableVersionResponse)).
Done(),

NewStreamMiddleware().
WithName(DefaultMiddlewareLogicalChecks).
WithInterceptor(noopStreamInterceptor).
Done(),

NewStreamMiddleware().
WithName(DefaultInternalMiddlewareDispatch).
WithInternal(true).
Expand All @@ -428,6 +442,10 @@ func DefaultStreamingMiddleware(opts MiddlewareOption) (*MiddlewareChain[grpc.St
return &chain, err
}

func noopStreamInterceptor(srv any, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
return handler(srv, stream)
}

func determineEventsToLog(opts MiddlewareOption) grpclog.Option {
eventsToLog := []grpclog.LoggableEvent{grpclog.FinishCall}
if opts.EnableRequestLog {
Expand Down Expand Up @@ -465,6 +483,29 @@ func DefaultDispatchMiddleware(logger zerolog.Logger, authFunc grpcauth.AuthFunc
}
}

var LogicalChecksCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "spicedb",
Subsystem: "logical",
Name: "permission_checks_total",
Help: "Count of the checks across individual and bulk requests",
}, []string{"grpc_method", "grpc_service", "grpc_type"})

func LogicalChecksMetricUnary(ctx context.Context, request any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
response, err := handler(ctx, request)
if err == nil {
switch m := request.(type) {
case *v1.CheckPermissionRequest:
svc, method := grpcutil.SplitMethodName(info.FullMethod)
LogicalChecksCounter.WithLabelValues(method, svc, "unary").Add(1)
case *v1.CheckBulkPermissionsRequest:
svc, method := grpcutil.SplitMethodName(info.FullMethod)
LogicalChecksCounter.WithLabelValues(method, svc, "unary").Add(float64(len(m.GetItems())))
default:
}
}
return response, err
}

func InterceptorLogger(l zerolog.Logger) grpclog.Logger {
return grpclog.LoggerFunc(func(ctx context.Context, lvl grpclog.Level, msg string, fields ...any) {
l := l.With().Fields(fields).Logger()
Expand Down

0 comments on commit 8ffa034

Please sign in to comment.