otel.go 811 B

12345678910111213141516171819202122232425
  1. package common
  2. import (
  3. "regexp"
  4. "strings"
  5. )
  6. var (
  7. deploymentPodRegex = regexp.MustCompile(`(/k8s/[a-z0-9-]+/[a-z0-9-]+)-[0-9a-f]{1,10}-[bcdfghjklmnpqrstvwxz2456789]{5}/.+`)
  8. daemonsetPodRegex = regexp.MustCompile(`(/k8s/[a-z0-9-]+/[a-z0-9-]+)-[bcdfghjklmnpqrstvwxz2456789]{5}/.+`)
  9. statefulsetPodRegex = regexp.MustCompile(`(/k8s/[a-z0-9-]+/[a-z0-9-]+)-\d+/.+`)
  10. cronjobPodRegex = regexp.MustCompile(`(/k8s-cronjob/[a-z0-9-]+/[a-z0-9-]+)/.+`)
  11. )
  12. func ContainerIdToOtelServiceName(containerId string) string {
  13. if !strings.HasPrefix(containerId, "/k8s/") {
  14. return containerId
  15. }
  16. for _, r := range []*regexp.Regexp{deploymentPodRegex, daemonsetPodRegex, statefulsetPodRegex, cronjobPodRegex} {
  17. if g := r.FindStringSubmatch(containerId); len(g) == 2 {
  18. return g[1]
  19. }
  20. }
  21. return containerId
  22. }