diff --git a/common/otel.go b/common/otel.go index 14456cb..41afc48 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 dfb5ec4..649ad31 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")