@@ -9,13 +9,16 @@ import (
99 middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2"
1010 "github.com/prometheus/client_golang/prometheus"
1111 "github.com/prometheus/client_golang/prometheus/promauto"
12+ "go.opentelemetry.io/otel"
1213 "google.golang.org/grpc"
1314 "google.golang.org/grpc/codes"
1415 "google.golang.org/grpc/status"
1516
1617 log "github.com/authzed/spicedb/internal/logging"
1718)
1819
20+ var tracer = otel .Tracer ("spicedb/internal/middleware/memory_protection" )
21+
1922// RequestsProcessed tracks requests that were processed by this middleware.
2023var RequestsProcessed = promauto .NewCounterVec (prometheus.CounterOpts {
2124 Namespace : "spicedb" ,
@@ -44,7 +47,7 @@ func New(usageProvider MemoryUsageProvider, name string) *MemoryProtectionMiddle
4447// UnaryServerInterceptor returns a unary server interceptor that rejects incoming requests is memory usage is too high
4548func (am * MemoryProtectionMiddleware ) UnaryServerInterceptor () grpc.UnaryServerInterceptor {
4649 return func (ctx context.Context , req any , info * grpc.UnaryServerInfo , handler grpc.UnaryHandler ) (any , error ) {
47- if err := am .checkAdmission (info .FullMethod ); err != nil {
50+ if err := am .checkAdmission (ctx , info .FullMethod ); err != nil {
4851 return nil , err
4952 }
5053
@@ -55,7 +58,7 @@ func (am *MemoryProtectionMiddleware) UnaryServerInterceptor() grpc.UnaryServerI
5558// StreamServerInterceptor returns a stream server interceptor that rejects incoming requests is memory usage is too high
5659func (am * MemoryProtectionMiddleware ) StreamServerInterceptor () grpc.StreamServerInterceptor {
5760 return func (srv any , stream grpc.ServerStream , info * grpc.StreamServerInfo , handler grpc.StreamHandler ) error {
58- if err := am .checkAdmission (info .FullMethod ); err != nil {
61+ if err := am .checkAdmission (stream . Context (), info .FullMethod ); err != nil {
5962 return err
6063 }
6164
@@ -65,7 +68,10 @@ func (am *MemoryProtectionMiddleware) StreamServerInterceptor() grpc.StreamServe
6568}
6669
6770// checkAdmission returns an error if the request should be denied because memory usage is too high.
68- func (am * MemoryProtectionMiddleware ) checkAdmission (method string ) error {
71+ func (am * MemoryProtectionMiddleware ) checkAdmission (ctx context.Context , method string ) error {
72+ _ , span := tracer .Start (ctx , "checkMemoryUsage" )
73+ defer span .End ()
74+
6975 accept := true
7076 defer func () {
7177 am .recordMetric (method , accept )
0 commit comments