Great question! Let’s break down what this Prometheus expression does:
your_metric_with_driver_name{driver_name=~".*"}
unless on(driver_name)
kube_pod_status_phase{
pod=~".*driver.*",
phase!~"Succeeded|Failed|Completed"
}
How the unless Operator Works
- The
unless operator in Prometheus returns all time series from the left-hand side that do not have a matching time series on the right-hand side, based on the specified labels (here, driver_name).
What Each Side Means
Left Side:
your_metric_with_driver_name{driver_name=~".*"}
- This selects all time series from
your_metric_with_driver_name that have a driver_name label (i.e., all known drivers from your custom metric).
Right Side:
kube_pod_status_phase{pod=~".*driver.*", phase!~"Succeeded|Failed|Completed"}
-
This selects all pods whose name contains "driver" and whose phase is not Succeeded, Failed, or Completed (i.e., pods that are still running or pending).
-
The on(driver_name) clause means the join is done on the driver_name label. For this to work, your kube_pod_status_phase metric must have a driver_name label that matches the one in your custom metric. If it doesn't, you may need to use on(pod) or relabel accordingly.
What the Expression Returns
-
For each driver_name in your custom metric, if there is no corresponding pod with that driver_name that is running or pending, the time series is returned.
-
In other words:
"Show me all driver names that are present in my custom metric, but do not have a corresponding running/pending pod."
Use Case
- This is useful for alerting when a Spark driver (or any driver) is expected (because it appears in your metrics), but there is no corresponding pod running for it in Kubernetes.
Example Alert
- alert: SparkDriverMetricWithoutPod
expr: your_metric_with_driver_name{driver_name=~".*"}
unless on(driver_name)
kube_pod_status_phase{pod=~".*driver.*", phase!~"Succeeded|Failed|Completed"}
for: 2m
labels:
severity: warning
annotations:
summary: "Metric for driver {{ $labels.driver_name }} exists, but no pod is running"
description: "A metric for driver {{ $labels.driver_name }} exists, but there is no corresponding running or pending pod."
Caveats
- Label Matching: The
on(driver_name) join requires both metrics to have a driver_name label. If your pod metric does not have this label, you may need to use on(pod) or relabel in your Prometheus config.
- Pod Name vs. Driver Name: If your pod name and driver name are not identical, you may need to use a regex or relabeling to match them.
- Metric Freshness: This does not check if the metric is recent. If you want to ensure the metric is fresh, you may want to add a
time() or timestamp() check.
In summary:
This expression finds all driver names that are present in your custom metric but do not have a corresponding running or pending pod, which is exactly what you want for detecting "orphaned" drivers in your monitoring!