Skip to content

Commit

Permalink
collapsed metrics of CronJob pods
Browse files Browse the repository at this point in the history
  • Loading branch information
def committed Oct 17, 2024
1 parent ad6ad6a commit 150658e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
3 changes: 2 additions & 1 deletion common/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down
19 changes: 15 additions & 4 deletions containers/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
"sync"
"time"
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 150658e

Please sign in to comment.