@@ -20,6 +20,12 @@ import (
2020)
2121
2222func WrapNodeServerWithMetricRecorder (server csi.NodeServer , driverType string , client kubernetes.Interface ) csi.NodeServer {
23+ labels := prometheus.Labels {
24+ metric .VolumeStatsLabelType : driverType ,
25+ }
26+ metric .VolumeStatCollector .AttachmentCountMetric .With (labels ).Add (0 )
27+ metric .VolumeStatCollector .AttachmentTimeTotalMetric .With (labels ).Add (0 )
28+
2329 return & NodeServerWithMetricRecorder {
2430 NodeServer : server ,
2531 driverType : driverType ,
@@ -36,29 +42,38 @@ type NodeServerWithMetricRecorder struct {
3642func (s * NodeServerWithMetricRecorder ) NodePublishVolume (ctx context.Context , req * csi.NodePublishVolumeRequest ) (* csi.NodePublishVolumeResponse , error ) {
3743 ctx , pod := utils .WithPodInfo (ctx , s .client , req )
3844 resp , err := s .NodeServer .NodePublishVolume (ctx , req )
39- s .recordVolumeAttachmentTime (ctx , req , pod , err )
45+ if err == nil {
46+ s .recordVolumeAttachmentTime (ctx , req , pod )
47+ }
4048 return resp , err
4149}
4250
43- func (s * NodeServerWithMetricRecorder ) recordVolumeAttachmentTime (ctx context.Context , req * csi.NodePublishVolumeRequest , pod * v1.Pod , respErr error ) {
51+ func (s * NodeServerWithMetricRecorder ) recordVolumeAttachmentTime (ctx context.Context , req * csi.NodePublishVolumeRequest , pod * v1.Pod ) {
4452 var err error
53+ logger := klog .FromContext (ctx )
4554 if pod == nil {
4655 if pod , err = utils .GetPodFromContextOrK8s (ctx , s .client , req ); err != nil {
47- klog . Errorf ( "recordVolumeAttachmentTime: failed to get pod from context or k8s: %v" , err )
56+ logger . Error ( err , "recordVolumeAttachmentTime: failed to get pod from context or k8s" )
4857 return
4958 }
5059 }
60+ logger = logger .WithValues ("pod" , klog .KObj (pod ))
61+ if pod .Status .Phase != v1 .PodPending {
62+ logger .V (4 ).Info ("recordVolumeAttachmentTime: pod is not pending" )
63+ return
64+ }
5165 podStartTime := pod .Status .StartTime
5266 if podStartTime == nil {
53- klog . Errorf ( "recordVolumeAttachmentTime: no start time found for pod %s/%s" , pod . GetNamespace (), pod . GetName () )
67+ logger . Error ( nil , "recordVolumeAttachmentTime: no start time found" )
5468 return
5569 }
70+ t := time .Since (podStartTime .Time )
5671 labels := prometheus.Labels {
5772 metric .VolumeStatsLabelType : s .driverType ,
58- metric .VolumeStatsLabelCode : status .Code (respErr ).String (),
5973 }
74+ logger .V (3 ).Info ("E2E attachment time" , "time" , t )
6075 metric .VolumeStatCollector .AttachmentCountMetric .With (labels ).Inc ()
61- metric .VolumeStatCollector .AttachmentTimeTotalMetric .With (labels ).Add (time . Since ( podStartTime . Time ) .Seconds ())
76+ metric .VolumeStatCollector .AttachmentTimeTotalMetric .With (labels ).Add (t .Seconds ())
6277}
6378
6479func WrapNodeServerWithValidator (server csi.NodeServer ) csi.NodeServer {
0 commit comments