@@ -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)
@@ -557,10 +561,10 @@ func MarkDRPCGatewayRequest(ctx context.Context) context.Context {
557561 return drpcmetadata .Add (ctx , gwRequestKey , "true" )
558562}
559563
560- // DRPCGatewayRequestRecoveryInterceptor recovers from panics in DRPC handlers
564+ // drpcGatewayRequestRecoveryInterceptor recovers from panics in DRPC handlers
561565// that are invoked due to DB console requests. For these requests, we do not
562566// want an uncaught panic to crash the node.
563- func DRPCGatewayRequestRecoveryInterceptor (
567+ func drpcGatewayRequestRecoveryInterceptor (
564568 ctx context.Context , req interface {}, rpc string , handler drpcmux.UnaryHandler ,
565569) (resp interface {}, err error ) {
566570 if val , ok := drpcmetadata .GetValue (ctx , gwRequestKey ); ok && val != "" {
@@ -574,3 +578,29 @@ func DRPCGatewayRequestRecoveryInterceptor(
574578 resp , err = handler (ctx , req )
575579 return resp , err
576580}
581+
582+ // drpcGatewayRequestCounterInterceptor is a client-side interceptor that
583+ // increments telemetry counters for DRPC requests originating from the HTTP
584+ // gateway. It checks for the gateway request marker and increments
585+ // a counter named after the RPC method.
586+ func drpcGatewayRequestCounterInterceptor (
587+ ctx context.Context ,
588+ rpc string ,
589+ enc drpc.Encoding ,
590+ in , out drpc.Message ,
591+ cc * drpcclient.ClientConn ,
592+ invoker drpcclient.UnaryInvoker ,
593+ ) error {
594+ // Check if this request originated from the DRPC HTTP gateway
595+ if val , ok := drpcmetadata .GetValue (ctx , gwRequestKey ); ok && val != "" {
596+ telemetry .Inc (getDRPCGatewayEndpointCounter (rpc ))
597+ }
598+ return invoker (ctx , rpc , enc , in , out , cc )
599+ }
600+
601+ // getDRPCGatewayEndpointCounter returns a telemetry Counter corresponding to
602+ // the given DRPC method.
603+ func getDRPCGatewayEndpointCounter (method string ) telemetry.Counter {
604+ const counterPrefix = "http.drpc-gateway"
605+ return telemetry .GetCounter (fmt .Sprintf ("%s.%s" , counterPrefix , method ))
606+ }
0 commit comments