Skip to content

Commit 373562c

Browse files
authored
Merge pull request #1450 from huww98/metric-attach-pending
metric: only count pending pod for attachment time
2 parents a23fbae + 2bc8e72 commit 373562c

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

pkg/common/nodeserver.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ import (
2020
)
2121

2222
func 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 {
3642
func (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

6479
func WrapNodeServerWithValidator(server csi.NodeServer) csi.NodeServer {

pkg/metric/volume_stat_collector.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import "github.com/prometheus/client_golang/prometheus"
55
const (
66
VolumeStatsCollectorName = "volume_stats"
77
VolumeStatsLabelType = "type"
8-
VolumeStatsLabelCode = "error_code"
98
)
109

11-
var volumeStatLabels = []string{VolumeStatsLabelType, VolumeStatsLabelCode}
10+
var volumeStatLabels = []string{VolumeStatsLabelType}
1211

1312
type VolumeStatType uint8
1413

0 commit comments

Comments
 (0)