@@ -7,11 +7,13 @@ package rpc
77
88import (
99 "context"
10+ "fmt"
1011 "net/http"
1112 "strings"
1213
1314 "github.com/VividCortex/ewma"
1415 "github.com/cockroachdb/cockroach/pkg/roachpb"
16+ "github.com/cockroachdb/cockroach/pkg/server/telemetry"
1517 "github.com/cockroachdb/cockroach/pkg/util/log/logcrash"
1618 "github.com/cockroachdb/cockroach/pkg/util/metric"
1719 "github.com/cockroachdb/cockroach/pkg/util/metric/aggmetric"
@@ -23,6 +25,8 @@ import (
2325 "google.golang.org/grpc/codes"
2426 "google.golang.org/grpc/metadata"
2527 "google.golang.org/grpc/status"
28+ "storj.io/drpc"
29+ "storj.io/drpc/drpcclient"
2630 "storj.io/drpc/drpcmetadata"
2731 "storj.io/drpc/drpcmux"
2832)
@@ -521,10 +525,10 @@ func MarkDRPCGatewayRequest(ctx context.Context) context.Context {
521525 return drpcmetadata .Add (ctx , gwRequestKey , "true" )
522526}
523527
524- // DRPCGatewayRequestRecoveryInterceptor recovers from panics in DRPC handlers
528+ // drpcGatewayRequestRecoveryInterceptor recovers from panics in DRPC handlers
525529// that are invoked due to DB console requests. For these requests, we do not
526530// want an uncaught panic to crash the node.
527- func DRPCGatewayRequestRecoveryInterceptor (
531+ func drpcGatewayRequestRecoveryInterceptor (
528532 ctx context.Context , req interface {}, rpc string , handler drpcmux.UnaryHandler ,
529533) (resp interface {}, err error ) {
530534 if val , ok := drpcmetadata .GetValue (ctx , gwRequestKey ); ok && val != "" {
@@ -538,3 +542,29 @@ func DRPCGatewayRequestRecoveryInterceptor(
538542 resp , err = handler (ctx , req )
539543 return resp , err
540544}
545+
546+ // drpcGatewayRequestCounterInterceptor is a client-side interceptor that
547+ // increments telemetry counters for DRPC requests originating from the HTTP
548+ // gateway. It checks for the gateway request marker and increments
549+ // a counter named after the RPC method.
550+ func drpcGatewayRequestCounterInterceptor (
551+ ctx context.Context ,
552+ rpc string ,
553+ enc drpc.Encoding ,
554+ in , out drpc.Message ,
555+ cc * drpcclient.ClientConn ,
556+ invoker drpcclient.UnaryInvoker ,
557+ ) error {
558+ // Check if this request originated from the DRPC HTTP gateway
559+ if val , ok := drpcmetadata .GetValue (ctx , gwRequestKey ); ok && val != "" {
560+ telemetry .Inc (getDRPCGatewayEndpointCounter (rpc ))
561+ }
562+ return invoker (ctx , rpc , enc , in , out , cc )
563+ }
564+
565+ // getDRPCGatewayEndpointCounter returns a telemetry Counter corresponding to
566+ // the given DRPC method.
567+ func getDRPCGatewayEndpointCounter (method string ) telemetry.Counter {
568+ const counterPrefix = "http.drpc-gateway"
569+ return telemetry .GetCounter (fmt .Sprintf ("%s.%s" , counterPrefix , method ))
570+ }
0 commit comments