Improve heartbeat workload name/kind detection (pod-sandbox labels + configurable label keys) - #81
Merged
Merged
Conversation
prashantbytesyntax
approved these changes
Sep 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Improve heartbeat workload name/kind detection (pod-sandbox labels + configurable label keys)
Summary
The heartbeat workload inventory previously derived each container's
workload_name/workload_kindfrom a small set of container-level labels plus a brittle pod-nameregex. Investigation across three staging clusters showed this is unreliable:
io.kubernetes.*keys — the pod's real identity labels (app.kubernetes.io/name,app, vendor CRD labels, …) live on the pod sandbox, not on the container.acceptable for an open-source agent.
for the ReplicaSet hash.
This PR makes workload detection accurate and cluster-agnostic by (1) consuming
pod-sandbox labels (added in the companion
granulate-utilsPR), (2) improving thelabel priority list and pod-name normalization, and (3) making the vendor-specific label
keys configurable via CLI instead of hardcoded.
Motivation
Empirical findings from live nodes (
crictl):io.kubernetes.*+ vendor CRD labelsio.kubernetes.*app,app.kubernetes.io/name,k8s-app, vendor CRD, …io.kubernetes.*k8s-app-only pods likekube-proxy)Conclusion: there is no single container-label convention across clusters. The pod
sandbox is the reliable source, and the exact label keys carrying the workload name/kind
vary per deployment, so they must be configurable.
Changes
gprofiler/metadata/heartbeat_metadata.pyDEFAULT_WORKLOAD_NAME_LABELS=app.kubernetes.io/name,app.kubernetes.io/instance,app,k8s-app(standard Kubernetes labels only).DEFAULT_WORKLOAD_KIND_LABELS= empty (kind is inferred from the pod-name shape bydefault).
_best_effort_workload_name/_best_effort_workload_kindnow probepod_labels(sandbox) first, then containerlabels, then fall back to pod-name normalization. Placeholder values(
unknown/none/ empty) are skipped._best_effort_workload_kindmirrors the name function: label lookup →pod-name-shape inference (
Deployment/StatefulSet/DaemonSet) →generic
k8s/containerfallback. Shared label-probing logic factored into_first_label_value."safe" alphabet, not hex), added a DaemonSet/
generateNamesingle-suffix pattern(previously unhandled), and constrained the random suffix to the safe alphabet so real
name tails (e.g.
service-redis) are not stripped.HeartbeatMetadataCollector.__init__acceptsworkload_name_labels/workload_kind_labels; configured keys are probed beforethe built-in defaults, so custom keys take priority without losing standard k8s coverage.
gprofiler/main.pyTwo new CLI arguments in the heartbeat argument group (settable via config file / env var
through configargparse):
dest--heartbeat-workload-name-labelsheartbeat_workload_name_labels[]--heartbeat-workload-kind-labelsheartbeat_workload_kind_labels[]Both accept a comma-separated list of label keys (priority order) and are threaded into
HeartbeatClient.gprofiler/dynamic_profiling_management/heartbeat.pyHeartbeatClientacceptsworkload_name_labels/workload_kind_labelsand forwards themto
HeartbeatMetadataCollector.gprofiler/gprofiler_types.pyAdded
comma_separated_listargparse type helper (splits/strips a comma-separated string).Usage
Deployments that expose workload identity under vendor keys can opt in, e.g.:
With no configuration, the agent uses the standard Kubernetes labels and pod-name
inference — no company-specific behavior is baked in.
Backwards compatibility
workload_kindis now richer than before (Deployment/StatefulSet/DaemonSet/configured kind, instead of only
k8s). Consumers that keyed off the literal"k8s"should accept these values.
containeris still emitted for non-k8s containers.Testing
tests_fast/test_workload_inventory_spec.pyextended to cover: sandbox-vs-container labelprecedence, configured-vs-unconfigured vendor keys (name and kind), placeholder-value
skipping, DaemonSet/ReplicaSet/StatefulSet inference, and the vowel-tail guard.
black,isort,flake8, andmypypass on the changed files.