Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions common/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/<ns>/<cronjob>/<container>
// id (containers/registry.go); a run outside it keeps its pod name and maps to
// /k8s/<ns>/<cronjob> below, so both get that name.
if g := cronjobPodRegex.FindStringSubmatch(containerId); len(g) == 2 {
return "/k8s/" + g[1]
}
Comment thread
blue4209211 marked this conversation as resolved.
Resolved
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]
}
Expand Down
14 changes: 14 additions & 0 deletions common/otel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Comment thread
blue4209211 marked this conversation as resolved.
Resolved

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")
Expand Down
Loading