otel.go 709 B

123456789101112131415161718192021222324
  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. )
  11. func ContainerIdToOtelServiceName(containerId string) string {
  12. if !strings.HasPrefix(containerId, "/k8s/") {
  13. return containerId
  14. }
  15. for _, r := range []*regexp.Regexp{deploymentPodRegex, daemonsetPodRegex, statefulsetPodRegex} {
  16. if g := r.FindStringSubmatch(containerId); len(g) == 2 {
  17. return g[1]
  18. }
  19. }
  20. return containerId
  21. }