statsummary.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package scrape
  2. import (
  3. "github.com/opencost/opencost/core/pkg/log"
  4. "github.com/opencost/opencost/core/pkg/nodestats"
  5. "github.com/opencost/opencost/core/pkg/source"
  6. "github.com/opencost/opencost/modules/collector-source/pkg/metric"
  7. stats "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
  8. )
  9. // Stat Summary Metrics
  10. const (
  11. NodeCPUSecondsTotal = "node_cpu_seconds_total"
  12. NodeFSCapacityBytes = "node_fs_capacity_bytes" // replaces container_fs_limit_bytes
  13. ContainerNetworkReceiveBytesTotal = "container_network_receive_bytes_total"
  14. ContainerNetworkTransmitBytesTotal = "container_network_transmit_bytes_total"
  15. ContainerCPUUsageSecondsTotal = "container_cpu_usage_seconds_total"
  16. ContainerMemoryWorkingSetBytes = "container_memory_working_set_bytes"
  17. ContainerFSUsageBytes = "container_fs_usage_bytes"
  18. KubeletVolumeStatsUsedBytes = "kubelet_volume_stats_used_bytes"
  19. )
  20. type StatSummaryScraper struct {
  21. client nodestats.StatSummaryClient
  22. }
  23. func newStatSummaryScraper(client nodestats.StatSummaryClient) Scraper {
  24. return &StatSummaryScraper{
  25. client: client,
  26. }
  27. }
  28. func (s *StatSummaryScraper) Scrape() []metric.Update {
  29. var scrapeResults []metric.Update
  30. nodeStats, err := s.client.GetNodeData()
  31. if err != nil {
  32. log.Errorf("error retrieving node stat data: %s", err.Error())
  33. return scrapeResults
  34. }
  35. // track if a pvc has already been seen when updating KubeletVolumeStatsUsedBytes
  36. seenPVC := map[stats.PVCReference]struct{}{}
  37. for _, stat := range nodeStats {
  38. nodeName := stat.Node.NodeName
  39. if stat.Node.CPU != nil && stat.Node.CPU.UsageCoreNanoSeconds != nil {
  40. scrapeResults = append(scrapeResults, metric.Update{
  41. Name: NodeCPUSecondsTotal,
  42. Labels: map[string]string{
  43. source.KubernetesNodeLabel: nodeName,
  44. source.ModeLabel: "", // TODO
  45. },
  46. Value: float64(*stat.Node.CPU.UsageCoreNanoSeconds) * 1e-9,
  47. })
  48. }
  49. if stat.Node.Fs != nil && stat.Node.Fs.CapacityBytes != nil {
  50. scrapeResults = append(scrapeResults, metric.Update{
  51. Name: NodeFSCapacityBytes,
  52. Labels: map[string]string{
  53. source.InstanceLabel: nodeName,
  54. source.DeviceLabel: "local", // This value has to be populated but isn't important here
  55. },
  56. Value: float64(*stat.Node.Fs.CapacityBytes),
  57. })
  58. }
  59. for _, pod := range stat.Pods {
  60. podName := pod.PodRef.Name
  61. namespace := pod.PodRef.Namespace
  62. podUID := pod.PodRef.UID
  63. if pod.Network != nil {
  64. if pod.Network.RxBytes != nil {
  65. scrapeResults = append(scrapeResults, metric.Update{
  66. Name: ContainerNetworkReceiveBytesTotal,
  67. Labels: map[string]string{
  68. source.UIDLabel: podUID,
  69. source.PodLabel: podName,
  70. source.NamespaceLabel: namespace,
  71. },
  72. Value: float64(*pod.Network.RxBytes),
  73. })
  74. }
  75. if pod.Network.TxBytes != nil {
  76. scrapeResults = append(scrapeResults, metric.Update{
  77. Name: ContainerNetworkTransmitBytesTotal,
  78. Labels: map[string]string{
  79. source.UIDLabel: podUID,
  80. source.PodLabel: podName,
  81. source.NamespaceLabel: namespace,
  82. },
  83. Value: float64(*pod.Network.TxBytes),
  84. })
  85. }
  86. }
  87. for _, volumeStats := range pod.VolumeStats {
  88. if volumeStats.PVCRef == nil || volumeStats.UsedBytes == nil {
  89. continue
  90. }
  91. if _, ok := seenPVC[*volumeStats.PVCRef]; ok {
  92. continue
  93. }
  94. scrapeResults = append(scrapeResults, metric.Update{
  95. Name: KubeletVolumeStatsUsedBytes,
  96. Labels: map[string]string{
  97. source.PVCLabel: volumeStats.PVCRef.Name,
  98. source.NamespaceLabel: volumeStats.PVCRef.Namespace,
  99. },
  100. Value: float64(*volumeStats.UsedBytes),
  101. })
  102. seenPVC[*volumeStats.PVCRef] = struct{}{}
  103. }
  104. for _, container := range pod.Containers {
  105. if container.CPU != nil && container.CPU.UsageCoreNanoSeconds != nil {
  106. scrapeResults = append(scrapeResults, metric.Update{
  107. Name: ContainerCPUUsageSecondsTotal,
  108. Labels: map[string]string{
  109. source.ContainerLabel: container.Name,
  110. source.PodLabel: podName,
  111. source.NamespaceLabel: namespace,
  112. source.NodeLabel: nodeName,
  113. source.InstanceLabel: nodeName,
  114. },
  115. Value: float64(*container.CPU.UsageCoreNanoSeconds) * 1e-9,
  116. })
  117. }
  118. if container.Memory != nil && container.Memory.WorkingSetBytes != nil {
  119. scrapeResults = append(scrapeResults, metric.Update{
  120. Name: ContainerMemoryWorkingSetBytes,
  121. Labels: map[string]string{
  122. source.ContainerLabel: container.Name,
  123. source.PodLabel: podName,
  124. source.NamespaceLabel: namespace,
  125. source.NodeLabel: nodeName,
  126. source.InstanceLabel: nodeName,
  127. },
  128. Value: float64(*container.Memory.WorkingSetBytes),
  129. })
  130. }
  131. if container.Rootfs != nil && container.Rootfs.UsedBytes != nil {
  132. scrapeResults = append(scrapeResults, metric.Update{
  133. Name: ContainerFSUsageBytes,
  134. Labels: map[string]string{
  135. source.InstanceLabel: nodeName,
  136. source.DeviceLabel: "local",
  137. },
  138. Value: float64(*container.Rootfs.UsedBytes),
  139. })
  140. }
  141. }
  142. }
  143. }
  144. return scrapeResults
  145. }