From 24cb82b145ac5bd5c5bf3a300c004accc7944a82 Mon Sep 17 00:00:00 2001 From: reckless-sherixx <141176362+reckless-sherixx@users.noreply.github.com> Date: Tue, 3 Nov 2026 22:50:12 +0530 Subject: [PATCH] fix(otel): name a CronJob pod's service after its CronJob A CronJob pod within its schedule window has a /k8s-cronjob/// container id, but ContainerIdToOtelServiceName returned early for anything not starting with /k8s/, so its CronJob pattern never ran. Those pods' logs, traces and profiles carried the raw container id as service.name, and their metrics an empty app_id, apart from every other run of the same CronJob. They now get /k8s//, the name a run outside the window already gets from its pod name. k8s.cronjob.name comes from the pod's resolved owner, not the container id, so it was not affected. --- common/otel.go | 10 ++++++++-- common/otel_test.go | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/common/otel.go b/common/otel.go index 14456cb5..41afc48a 100644 --- a/common/otel.go +++ b/common/otel.go @@ -9,14 +9,20 @@ var ( deploymentPodRegex = regexp.MustCompile(`(/k8s/[a-z0-9-]+/[a-z0-9-]+)-[0-9a-f]{1,10}-[bcdfghjklmnpqrstvwxz2456789]{5}/.+`) daemonsetPodRegex = regexp.MustCompile(`(/k8s/[a-z0-9-]+/[a-z0-9-]+)-[bcdfghjklmnpqrstvwxz2456789]{5}/.+`) statefulsetPodRegex = regexp.MustCompile(`(/k8s/[a-z0-9-]+/[a-z0-9-]+)-\d+/.+`) - cronjobPodRegex = regexp.MustCompile(`(/k8s-cronjob/[a-z0-9-]+/[a-z0-9-]+)/.+`) + cronjobPodRegex = regexp.MustCompile(`^/k8s-cronjob/([a-z0-9-]+/[a-z0-9-]+)/.+`) ) func ContainerIdToOtelServiceName(containerId string) string { + // A CronJob pod within its schedule window has a /k8s-cronjob/// + // id (containers/registry.go); a run outside it keeps its pod name and maps to + // /k8s// below, so both get that name. + if g := cronjobPodRegex.FindStringSubmatch(containerId); len(g) == 2 { + return "/k8s/" + g[1] + } if !strings.HasPrefix(containerId, "/k8s/") { return containerId } - for _, r := range []*regexp.Regexp{deploymentPodRegex, daemonsetPodRegex, statefulsetPodRegex, cronjobPodRegex} { + for _, r := range []*regexp.Regexp{deploymentPodRegex, daemonsetPodRegex, statefulsetPodRegex} { if g := r.FindStringSubmatch(containerId); len(g) == 2 { return g[1] } diff --git a/common/otel_test.go b/common/otel_test.go index dfb5ec4c..649ad31e 100644 --- a/common/otel_test.go +++ b/common/otel_test.go @@ -28,6 +28,20 @@ func TestContainerIdToServiceName(t *testing.T) { f("/k8s/default/hello-28283967-khz2f/xz"), "/k8s/default/hello") + // A CronJob pod within its schedule window has a /k8s-cronjob/ id + // (containers/registry.go); it gets the same name as the run above. + assert.Equal(t, + f("/k8s-cronjob/default/hello/xz"), + "/k8s/default/hello") + + assert.Equal(t, + f("/k8s-cronjob/default/hello/sandbox"), + "/k8s/default/hello") + + assert.Equal(t, + f("/k8s-cronjob/default"), + "/k8s-cronjob/default") + assert.Equal(t, f("/system.slice/k3s.service"), "/system.slice/k3s.service")