Selectors and Filtering
This module covers advanced selector techniques and filtering patterns in PromQL. You will learn how to combine multiple label selectors, use regular expressions effectively, and filter time series based on label values.
Combining Multiple Label Selectors
You can combine multiple label selectors in a single query. All selectors must match for a time series to be included.
Regular Expression Patterns
Regular expressions are powerful tools for matching patterns in label values.
Basic Regex Patterns
# Match any pod starting with "web-"
kube_pod_info{pod=~"web-.*"}
# Match pods ending with "-prod"
kube_pod_info{pod=~".*-prod"}
# Match pods containing "api"
kube_pod_info{pod=~".*api.*"}
Filtering by Label Existence
You can check if a label exists or doesn’t exist using the label_replace function or by using negative regex patterns.
Working with OpenShift-Specific Labels
OpenShift 4.16 adds several useful labels to metrics. Understanding these helps you write effective queries.
Common OpenShift Labels
-
namespace- The Kubernetes namespace -
pod- The pod name -
container- The container name -
node- The node name -
deployment- The deployment name -
replicaset- The replica set name -
service- The service name
Combining Queries with Operators
Best Practices for Selectors
Use Specific Selectors
Be as specific as possible to reduce query execution time:
# Good: Specific namespace and pod pattern
container_memory_usage_bytes{namespace="production", pod=~"web-.*"}
# Less efficient: Too broad
container_memory_usage_bytes
Summary
In this module, you learned:
-
How to combine multiple label selectors
-
Regular expression patterns and their usage
-
Filtering by label existence and exclusion
-
OpenShift-specific labels and how to use them
-
Combining queries with operators
-
Best practices for writing efficient selectors
In the next module, you will learn about aggregation functions, which allow you to summarize data across multiple time series.