Skip to content

Commit 87bf589

Browse files
committed
Add metrics pod_deleted_running_total
1 parent abdc894 commit 87bf589

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

exporter/metrics.go

+22
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ var (
1818
Help: "Total number of API requests to the scheduler",
1919
}, []string{"cluster", "namespace", "user", "verb", "resource", "code"})
2020

21+
podDeletedRunning = prometheus.NewCounterVec(prometheus.CounterOpts{
22+
Name: "pod_deleted_running_total",
23+
Help: "Total number of pods deleted while in running state",
24+
}, []string{"cluster", "namespace", "user"})
25+
2126
podSchedulingLatency = prometheus.NewHistogramVec(prometheus.HistogramOpts{
2227
Name: "pod_scheduling_latency_seconds",
2328
Help: "Duration from pod creation to scheduled on node in seconds",
@@ -35,6 +40,7 @@ func init() {
3540
registry.MustRegister(
3641
apiRequests,
3742
podSchedulingLatency,
43+
podDeletedRunning,
3844
batchJobCompleteLatency,
3945
)
4046
}
@@ -107,6 +113,22 @@ func (p *Exporter) updateMetrics(clusterLabel string, event auditv1.Event) {
107113
}
108114
} else if event.Verb == "delete" {
109115
delete(p.podCreationTimes, buildTarget(event.ObjectRef))
116+
117+
if event.ResponseObject != nil {
118+
var pod Pod
119+
if err := json.Unmarshal(event.ResponseObject.Raw, &pod); err != nil {
120+
slog.Error("failed to unmarshal pod during delete", "err", err)
121+
return
122+
}
123+
if pod.Status.Phase == "Running" {
124+
user := extractUserAgent(event.UserAgent)
125+
podDeletedRunning.WithLabelValues(
126+
clusterLabel,
127+
ns,
128+
user,
129+
).Inc()
130+
}
131+
}
110132
}
111133
}
112134

exporter/model.go

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type PodSpec struct {
2323
}
2424

2525
type PodStatus struct {
26+
Phase string `json:"phase"`
2627
Condition []Condition `json:"conditions"`
2728
}
2829

0 commit comments

Comments
 (0)