Skip to content

Commit 9a044cd

Browse files
committed
kubelet: intercept DRA GRPC to record metrics
1 parent 3a67bc0 commit 9a044cd

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

pkg/kubelet/cm/dra/plugin/plugin.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ import (
2727
"google.golang.org/grpc"
2828
"google.golang.org/grpc/connectivity"
2929
"google.golang.org/grpc/credentials/insecure"
30+
"google.golang.org/grpc/status"
3031

3132
utilversion "k8s.io/apimachinery/pkg/util/version"
3233
"k8s.io/klog/v2"
3334
drapb "k8s.io/kubelet/pkg/apis/dra/v1alpha4"
35+
"k8s.io/kubernetes/pkg/kubelet/metrics"
3436
)
3537

3638
// NewDRAPluginClient returns a wrapper around those gRPC methods of a DRA
@@ -86,6 +88,7 @@ func (p *Plugin) getOrCreateGRPCConn() (*grpc.ClientConn, error) {
8688
grpc.WithContextDialer(func(ctx context.Context, target string) (net.Conn, error) {
8789
return (&net.Dialer{}).DialContext(ctx, network, target)
8890
}),
91+
grpc.WithChainUnaryInterceptor(newMetricsInterceptor(p.name)),
8992
)
9093
if err != nil {
9194
return nil, err
@@ -145,3 +148,12 @@ func (p *Plugin) NodeUnprepareResources(
145148
logger.V(4).Info("Done calling NodeUnprepareResources rpc", "response", response, "err", err)
146149
return response, err
147150
}
151+
152+
func newMetricsInterceptor(pluginName string) grpc.UnaryClientInterceptor {
153+
return func(ctx context.Context, method string, req, reply any, conn *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
154+
start := time.Now()
155+
err := invoker(ctx, method, req, reply, conn, opts...)
156+
metrics.DRAGRPCOperationsDuration.WithLabelValues(pluginName, method, status.Code(err).String()).Observe(time.Since(start).Seconds())
157+
return err
158+
}
159+
}

0 commit comments

Comments
 (0)