From 150658e8ac3ce8bdcf32ad563c4a9462bb6792c4 Mon Sep 17 00:00:00 2001 From: Nikolay Sivko Date: Thu, 17 Oct 2024 14:11:40 +0300 Subject: [PATCH] collapsed metrics of CronJob pods --- common/otel.go | 3 ++- containers/registry.go | 19 +++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/common/otel.go b/common/otel.go index 6b3723f..14456cb 100644 --- a/common/otel.go +++ b/common/otel.go @@ -9,13 +9,14 @@ 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-]+)/.+`) ) func ContainerIdToOtelServiceName(containerId string) string { if !strings.HasPrefix(containerId, "/k8s/") { return containerId } - for _, r := range []*regexp.Regexp{deploymentPodRegex, daemonsetPodRegex, statefulsetPodRegex} { + for _, r := range []*regexp.Regexp{deploymentPodRegex, daemonsetPodRegex, statefulsetPodRegex, cronjobPodRegex} { if g := r.FindStringSubmatch(containerId); len(g) == 2 { return g[1] } diff --git a/containers/registry.go b/containers/registry.go index 857ed56..eebb994 100644 --- a/containers/registry.go +++ b/containers/registry.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "regexp" + "strconv" "strings" "sync" "time" @@ -23,10 +24,12 @@ import ( const MinTrafficStatsUpdateInterval = 5 * time.Second var ( - selfNetNs = netns.None() - hostNetNsId = netns.None().UniqueId() - agentPid = uint32(os.Getpid()) - containerIdRegexp = regexp.MustCompile(`[a-z0-9]{64}`) + selfNetNs = netns.None() + hostNetNsId = netns.None().UniqueId() + agentPid = uint32(os.Getpid()) + containerIdRegexp = regexp.MustCompile(`[a-z0-9]{64}`) + cronjobPodName = regexp.MustCompile(`([a-z0-9-]+)-([0-9]{8})-[bcdfghjklmnpqrstvwxz2456789]{5}`) + cronjobPodScheduleWindow = 7 * 24 * time.Hour ) type ProcessInfo struct { @@ -419,6 +422,14 @@ func calcId(cg *cgroup.Cgroup, md *ContainerMetadata) ContainerID { if name == "" || name == "POD" { // skip pause containers return "" } + if g := cronjobPodName.FindStringSubmatch(pod); len(g) == 3 { + now := time.Now() + tsMiniutes, _ := strconv.ParseUint(g[2], 10, 64) + scheduledAt := time.Unix(int64(tsMiniutes)*60, 0) + if scheduledAt.After(now.Add(-cronjobPodScheduleWindow)) && scheduledAt.Before(now.Add(cronjobPodScheduleWindow)) { + return ContainerID(fmt.Sprintf("/k8s-cronjob/%s/%s/%s", namespace, g[1], name)) + } + } return ContainerID(fmt.Sprintf("/k8s/%s/%s/%s", namespace, pod, name)) } if taskNameParts := strings.SplitN(md.labels["com.docker.swarm.task.name"], ".", 3); len(taskNameParts) == 3 {