registry_apm.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package containers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/coroot/coroot-node-agent/utils"
  6. "github.com/coroot/coroot-node-agent/utils/modelse"
  7. "github.com/prometheus/client_golang/prometheus"
  8. "github.com/sirupsen/logrus"
  9. "os"
  10. "path"
  11. )
  12. const (
  13. Namespace = "namespace"
  14. PodName = "pod_name"
  15. Workload = "workload"
  16. ProcessName = "process_name"
  17. )
  18. func setLabels(container_id, ns, pod_name, process_name, pid string) prometheus.Labels {
  19. return map[string]string{
  20. "container_id": container_id,
  21. "ns": ns,
  22. "pod_name": pod_name,
  23. "process_name": process_name,
  24. "pid": pid,
  25. }
  26. }
  27. func setK8sTag(c *Container, tag map[string]string, pid uint32) {
  28. sPid := fmt.Sprintf("%d", pid)
  29. c.K8sContainer.ns = tag[Namespace]
  30. c.K8sContainer.podName = tag[PodName]
  31. c.K8sContainer.containerName = tag[ProcessName]
  32. c.K8sContainer.pid = sPid
  33. }
  34. func saveAppInfo(runtimeApps map[uint32]modelse.AppStatusInfo) {
  35. appStr, _ := json.Marshal(runtimeApps)
  36. dumpPath := path.Join(utils.GetDefaultLogPath(), "memdump")
  37. err := os.MkdirAll(dumpPath, 0755)
  38. if err != nil {
  39. logrus.Error(err)
  40. }
  41. fileName := fmt.Sprintf("%s.snap", "app")
  42. metricsFileName := path.Join(dumpPath, fileName)
  43. err = os.WriteFile(metricsFileName, appStr, 0644)
  44. if err != nil {
  45. logrus.Error(err)
  46. }
  47. }