statsummary.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package scrape
  2. import (
  3. "github.com/kubecost/events"
  4. "github.com/opencost/opencost/core/pkg/log"
  5. "github.com/opencost/opencost/core/pkg/nodestats"
  6. "github.com/opencost/opencost/core/pkg/source"
  7. "github.com/opencost/opencost/modules/collector-source/pkg/event"
  8. "github.com/opencost/opencost/modules/collector-source/pkg/metric"
  9. stats "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
  10. )
  11. type StatSummaryScraper struct {
  12. client nodestats.StatSummaryClient
  13. }
  14. func newStatSummaryScraper(client nodestats.StatSummaryClient) Scraper {
  15. return &StatSummaryScraper{
  16. client: client,
  17. }
  18. }
  19. func (s *StatSummaryScraper) Scrape() []metric.Update {
  20. var scrapeResults []metric.Update
  21. nodeStats, err := s.client.GetNodeData()
  22. if err != nil {
  23. var errs []error
  24. if multiErr, ok := err.(interface{ Unwrap() []error }); ok {
  25. errs = multiErr.Unwrap()
  26. } else {
  27. errs = []error{err}
  28. }
  29. events.Dispatch(event.ScrapeEvent{
  30. ScraperName: event.NodeStatsScraperName,
  31. Targets: len(nodeStats) + len(errs),
  32. Errors: errs,
  33. })
  34. log.Errorf("error retrieving node stat data: %s", err.Error())
  35. return scrapeResults
  36. }
  37. // track if a pvc has already been seen when updating KubeletVolumeStatsUsedBytes
  38. seenPVC := map[stats.PVCReference]struct{}{}
  39. for _, stat := range nodeStats {
  40. nodeName := stat.Node.NodeName
  41. if stat.Node.CPU != nil && stat.Node.CPU.UsageCoreNanoSeconds != nil {
  42. scrapeResults = append(scrapeResults, metric.Update{
  43. Name: metric.NodeCPUSecondsTotal,
  44. Labels: map[string]string{
  45. source.KubernetesNodeLabel: nodeName,
  46. source.ModeLabel: "", // TODO
  47. },
  48. Value: float64(*stat.Node.CPU.UsageCoreNanoSeconds) * 1e-9,
  49. })
  50. }
  51. if stat.Node.Fs != nil && stat.Node.Fs.CapacityBytes != nil {
  52. scrapeResults = append(scrapeResults, metric.Update{
  53. Name: metric.NodeFSCapacityBytes,
  54. Labels: map[string]string{
  55. source.InstanceLabel: nodeName,
  56. source.DeviceLabel: "local", // This value has to be populated but isn't important here
  57. },
  58. Value: float64(*stat.Node.Fs.CapacityBytes),
  59. })
  60. }
  61. for _, pod := range stat.Pods {
  62. podName := pod.PodRef.Name
  63. namespace := pod.PodRef.Namespace
  64. podUID := pod.PodRef.UID
  65. if pod.Network != nil {
  66. networkLabels := map[string]string{
  67. source.UIDLabel: podUID,
  68. source.PodLabel: podName,
  69. source.NamespaceLabel: namespace,
  70. }
  71. // The network may contain a list of stats or itself be a single stat, if the list is not present
  72. // scrape the object itself
  73. if pod.Network.Interfaces != nil {
  74. for _, networkStat := range pod.Network.Interfaces {
  75. scrapeNetworkStats(&scrapeResults, networkLabels, networkStat)
  76. }
  77. } else {
  78. scrapeNetworkStats(&scrapeResults, networkLabels, pod.Network.InterfaceStats)
  79. }
  80. }
  81. for _, volumeStats := range pod.VolumeStats {
  82. if volumeStats.PVCRef == nil || volumeStats.UsedBytes == nil {
  83. continue
  84. }
  85. if _, ok := seenPVC[*volumeStats.PVCRef]; ok {
  86. continue
  87. }
  88. scrapeResults = append(scrapeResults, metric.Update{
  89. Name: metric.KubeletVolumeStatsUsedBytes,
  90. Labels: map[string]string{
  91. source.PVCLabel: volumeStats.PVCRef.Name,
  92. source.NamespaceLabel: volumeStats.PVCRef.Namespace,
  93. },
  94. Value: float64(*volumeStats.UsedBytes),
  95. })
  96. seenPVC[*volumeStats.PVCRef] = struct{}{}
  97. }
  98. for _, container := range pod.Containers {
  99. if container.CPU != nil && container.CPU.UsageCoreNanoSeconds != nil {
  100. scrapeResults = append(scrapeResults, metric.Update{
  101. Name: metric.ContainerCPUUsageSecondsTotal,
  102. Labels: map[string]string{
  103. source.ContainerLabel: container.Name,
  104. source.PodLabel: podName,
  105. source.NamespaceLabel: namespace,
  106. source.NodeLabel: nodeName,
  107. source.InstanceLabel: nodeName,
  108. },
  109. Value: float64(*container.CPU.UsageCoreNanoSeconds) * 1e-9,
  110. })
  111. }
  112. if container.Memory != nil && container.Memory.WorkingSetBytes != nil {
  113. scrapeResults = append(scrapeResults, metric.Update{
  114. Name: metric.ContainerMemoryWorkingSetBytes,
  115. Labels: map[string]string{
  116. source.ContainerLabel: container.Name,
  117. source.PodLabel: podName,
  118. source.NamespaceLabel: namespace,
  119. source.NodeLabel: nodeName,
  120. source.InstanceLabel: nodeName,
  121. },
  122. Value: float64(*container.Memory.WorkingSetBytes),
  123. })
  124. }
  125. if container.Rootfs != nil && container.Rootfs.UsedBytes != nil {
  126. scrapeResults = append(scrapeResults, metric.Update{
  127. Name: metric.ContainerFSUsageBytes,
  128. Labels: map[string]string{
  129. source.InstanceLabel: nodeName,
  130. source.DeviceLabel: "local",
  131. },
  132. Value: float64(*container.Rootfs.UsedBytes),
  133. })
  134. }
  135. }
  136. }
  137. }
  138. events.Dispatch(event.ScrapeEvent{
  139. ScraperName: event.NodeStatsScraperName,
  140. Targets: len(nodeStats),
  141. Errors: []error{},
  142. })
  143. return scrapeResults
  144. }
  145. func scrapeNetworkStats(scrapeResults *[]metric.Update, labels map[string]string, networkStats stats.InterfaceStats) {
  146. // Skip stats for cni0 which tracks internal cluster traffic
  147. if networkStats.Name == "cni0" {
  148. return
  149. }
  150. if networkStats.RxBytes != nil {
  151. *scrapeResults = append(*scrapeResults, metric.Update{
  152. Name: metric.ContainerNetworkReceiveBytesTotal,
  153. Labels: labels,
  154. Value: float64(*networkStats.RxBytes),
  155. })
  156. }
  157. if networkStats.TxBytes != nil {
  158. *scrapeResults = append(*scrapeResults, metric.Update{
  159. Name: metric.ContainerNetworkTransmitBytesTotal,
  160. Labels: labels,
  161. Value: float64(*networkStats.TxBytes),
  162. })
  163. }
  164. }