collector.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package metric
  2. import (
  3. "maps"
  4. "time"
  5. "github.com/opencost/opencost/modules/collector-source/pkg/metric/aggregator"
  6. "github.com/opencost/opencost/modules/collector-source/pkg/util"
  7. )
  8. // MetricCollectorID is a unique identifier for a specific metric collector instance. We
  9. // use this identifier to register and unregister metric instances from the metrics metric
  10. // instead of the metric name and aggregation type to allow selectable cardinality (via Labels)
  11. // across multiple instances of the same aggregation type and metric name.
  12. type MetricCollectorID string
  13. const (
  14. PVPricePerGiBHourID MetricCollectorID = "PVPricePerGiBHour"
  15. PVUsedAverageID MetricCollectorID = "PVUsedAverage"
  16. PVUsedMaxID MetricCollectorID = "PVUsedMax"
  17. PVCInfoID MetricCollectorID = "PVCInfo"
  18. KMPVCInfoID MetricCollectorID = "KMPVCInfo"
  19. PVCUptimeID MetricCollectorID = "PVCUptime"
  20. PVActiveMinutesID MetricCollectorID = "PVActiveMinutes"
  21. PVUptimeID MetricCollectorID = "PVUptime"
  22. LocalStorageUsedActiveMinutesID MetricCollectorID = "LocalStorageUsedCost"
  23. LocalStorageUsedAverageID MetricCollectorID = "LocalStorageUsedAverage"
  24. LocalStorageUsedMaxID MetricCollectorID = "LocalStorageUsedMax"
  25. LocalStorageBytesID MetricCollectorID = "LocalStorageBytesID"
  26. LocalStorageActiveMinutesID MetricCollectorID = "LocalStorageActiveMinutes"
  27. KMLocalStorageUsedAverageID MetricCollectorID = "KMLocalStorageUsedAverage"
  28. KMLocalStorageUsedMaxID MetricCollectorID = "KMLocalStorageUsedMax"
  29. KMLocalStorageBytesID MetricCollectorID = "KMLocalStorageBytes"
  30. NodeCPUCoresCapacityID MetricCollectorID = "NodeCPUCoresCapacity"
  31. NodeCPUCoresAllocatableID MetricCollectorID = "NodeCPUCoresAllocatable"
  32. NodeRAMBytesCapacityID MetricCollectorID = "NodeRAMBytesCapacity"
  33. NodeRAMBytesAllocatableID MetricCollectorID = "NodeRAMBytesAllocatable"
  34. NodeGPUCountID MetricCollectorID = "NodeGPUCount"
  35. NodeLabelsID MetricCollectorID = "NodeLabels"
  36. NodeInfoID MetricCollectorID = "NodeInfo"
  37. NodeUptimeID MetricCollectorID = "NodeUptime"
  38. NodeActiveMinutesID MetricCollectorID = "NodeActiveMinutes"
  39. NodeCPUModeTotalID MetricCollectorID = "NodeCPUModeTotal"
  40. NodeRAMSystemUsageAverageID MetricCollectorID = "NodeRAMSystemUsageAverage"
  41. NodeRAMUserUsageAverageID MetricCollectorID = "NodeRAMUserUsageAverage"
  42. NodeResourceCapacitiesID MetricCollectorID = "NodeResourceCapacities"
  43. NodeResourcesAllocatableID MetricCollectorID = "NodeResourcesAllocatable"
  44. LBPricePerHourID MetricCollectorID = "LBPricePerHour"
  45. LBActiveMinutesID MetricCollectorID = "LBActiveMinutes"
  46. ClusterInfoID MetricCollectorID = "ClusterInfo"
  47. ClusterCompleteKubeModelID MetricCollectorID = "ClusterCompleteKubeModel"
  48. ClusterUptimeID MetricCollectorID = "ClusterUptime"
  49. ClusterManagementDurationID MetricCollectorID = "ClusterManagementDuration"
  50. ClusterManagementPricePerHourID MetricCollectorID = "ClusterManagementPricePerHour"
  51. PodActiveMinutesID MetricCollectorID = "PodActiveMinutes"
  52. PodInfoID MetricCollectorID = "PodInfo"
  53. PodUptimeID MetricCollectorID = "PodUptime"
  54. PodOwnerID MetricCollectorID = "PodOwner"
  55. PodPVCVolumeID MetricCollectorID = "PodPVCVolume"
  56. ContainerUptimeID MetricCollectorID = "ContainerUptime"
  57. PodNetworkEgressBytesID MetricCollectorID = "PodNetworkEgressBytes"
  58. PodNetworkIngressBytesID MetricCollectorID = "PodNetworkIngressBytes"
  59. RAMBytesAllocatedID MetricCollectorID = "RAMBytesAllocated"
  60. RAMRequestsID MetricCollectorID = "RAMRequests"
  61. RAMLimitsID MetricCollectorID = "RAMLimits"
  62. RAMUsageAverageID MetricCollectorID = "RAMUsageAverage"
  63. RAMUsageMaxID MetricCollectorID = "RAMBytesUsageMax"
  64. CPUCoresAllocatedID MetricCollectorID = "CPUCoreAllocationAvg"
  65. CPURequestsID MetricCollectorID = "CPURequestsID"
  66. CPULimitsID MetricCollectorID = "CPULimitsID"
  67. CPUUsageAverageID MetricCollectorID = "CPUUsageAverage"
  68. CPUUsageMaxID MetricCollectorID = "CPUUsageMax"
  69. GPUsRequestedID MetricCollectorID = "GPUsRequested"
  70. GPUsUsageAverageID MetricCollectorID = "GPUsUsageAverage"
  71. GPUsUsageMaxID MetricCollectorID = "GPUsUsageMax"
  72. GPUsAllocatedID MetricCollectorID = "GPUsAllocated"
  73. IsGPUSharedID MetricCollectorID = "IsGPUShared"
  74. GPUInfoID MetricCollectorID = "GPUInfo"
  75. NodeCPUPricePerHourID MetricCollectorID = "NodeCPUPricePerHour"
  76. NodeRAMPricePerGiBHourID MetricCollectorID = "NodeRAMPricePerGiBHour"
  77. NodeGPUPricePerHourID MetricCollectorID = "NodeGPUPricePerHour"
  78. NodeIsSpotID MetricCollectorID = "NodeIsSpot"
  79. DCGMInfoID MetricCollectorID = "DCGMInfo"
  80. DCGMUptimeID MetricCollectorID = "DCGMUptime"
  81. DCGMContainerUsageAvgID MetricCollectorID = "DCGMContainerUsageAvg"
  82. DCGMContainerUsageMaxID MetricCollectorID = "DCGMContainerUsageMax"
  83. PodPVCAllocationID MetricCollectorID = "PodPVCAllocation"
  84. PVCBytesRequestedID MetricCollectorID = "PVCBytesRequested"
  85. PVCBytesUsedAverageID MetricCollectorID = "PVCBytesUsedAverage"
  86. PVCBytesUsedMaxID MetricCollectorID = "PVCBytesUsedMax"
  87. PVBytesID MetricCollectorID = "PVBytesID"
  88. PVInfoID MetricCollectorID = "PVInfo"
  89. KMPVInfoID MetricCollectorID = "KMPVInfo"
  90. NetZoneGiBID MetricCollectorID = "NetZoneGiB"
  91. NetZonePricePerGiBID MetricCollectorID = "NetZonePricePerGiB"
  92. NetRegionGiBID MetricCollectorID = "NetRegionGiB"
  93. NetRegionPricePerGiBID MetricCollectorID = "NetRegionPricePerGiB"
  94. NetInternetGiBID MetricCollectorID = "NetInternetGiB"
  95. NetInternetPricePerGiBID MetricCollectorID = "NetInternetPricePerGiB"
  96. NetInternetServiceGiBID MetricCollectorID = "NetInternetServiceGiB"
  97. NetNatGatewayPricePerGiBID MetricCollectorID = "NetNatGatewayPricePerGiB"
  98. NetNatGatewayIngressPricePerGiBID MetricCollectorID = "NetNatGatewayIngressPricePerGiB"
  99. NetNatGatewayGiBID MetricCollectorID = "NetNatGatewayGiB"
  100. NetTransferBytesID MetricCollectorID = "NetTransferBytes"
  101. NetZoneIngressGiBID MetricCollectorID = "NetZoneIngressGiB"
  102. NetRegionIngressGiBID MetricCollectorID = "NetRegionIngressGiB"
  103. NetInternetIngressGiBID MetricCollectorID = "NetInternetIngressGiB"
  104. NetInternetServiceIngressGiBID MetricCollectorID = "NetInternetServiceIngressGiB"
  105. NetNatGatewayIngressGiBID MetricCollectorID = "NetNatGatewayIngressGiB"
  106. NetReceiveBytesID MetricCollectorID = "NetReceiveBytes"
  107. NamespaceInfoID MetricCollectorID = "NamespaceInfo"
  108. NamespaceUptimeID MetricCollectorID = "NamespaceUptime"
  109. NamespaceLabelsID MetricCollectorID = "NamespaceLabels"
  110. NamespaceAnnotationsID MetricCollectorID = "NamespaceAnnotations"
  111. PodLabelsID MetricCollectorID = "PodLabels"
  112. PodAnnotationsID MetricCollectorID = "PodAnnotations"
  113. ServiceLabelsID MetricCollectorID = "ServiceLabels"
  114. ServiceInfoID MetricCollectorID = "ServiceInfo"
  115. ServiceUptimeID MetricCollectorID = "ServiceUptime"
  116. DeploymentInfoID MetricCollectorID = "DeploymentInfo"
  117. DeploymentUptimeID MetricCollectorID = "DeploymentUptime"
  118. DeploymentLabelsID MetricCollectorID = "DeploymentLabels"
  119. DeploymentAnnotationsID MetricCollectorID = "DeploymentAnnotations"
  120. DeploymentMatchLabelsID MetricCollectorID = "DeploymentMatchLabels"
  121. StatefulSetInfoID MetricCollectorID = "StatefulSetInfo"
  122. StatefulSetUptimeID MetricCollectorID = "StatefulSetUptime"
  123. StatefulSetLabelsID MetricCollectorID = "StatefulSetLabels"
  124. StatefulSetAnnotationsID MetricCollectorID = "StatefulSetAnnotations"
  125. StatefulSetMatchLabelsID MetricCollectorID = "StatefulSetMatchLabels"
  126. DaemonSetInfoID MetricCollectorID = "DaemonSetInfo"
  127. DaemonSetUptimeID MetricCollectorID = "DaemonSetUptime"
  128. DaemonSetLabelsID MetricCollectorID = "DaemonSetLabels"
  129. DaemonSetAnnotationsID MetricCollectorID = "DaemonSetAnnotations"
  130. DaemonSetArgumentsID MetricCollectorID = "DaemonSetArguments"
  131. JobInfoID MetricCollectorID = "JobInfo"
  132. JobUptimeID MetricCollectorID = "JobUptime"
  133. JobLabelsID MetricCollectorID = "JobLabels"
  134. JobAnnotationsID MetricCollectorID = "JobAnnotations"
  135. CronJobInfoID MetricCollectorID = "CronJobInfo"
  136. CronJobUptimeID MetricCollectorID = "CronJobUptime"
  137. CronJobLabelsID MetricCollectorID = "CronJobLabels"
  138. CronJobAnnotationsID MetricCollectorID = "CronJobAnnotations"
  139. ReplicaSetInfoID MetricCollectorID = "ReplicaSetInfo"
  140. ReplicaSetUptimeID MetricCollectorID = "ReplicaSetUptime"
  141. ReplicaSetLabelsID MetricCollectorID = "ReplicaSetLabels"
  142. ReplicaSetAnnotationsID MetricCollectorID = "ReplicaSetAnnotations"
  143. ReplicaSetOwnerID MetricCollectorID = "ReplicaSetOwner"
  144. PodsWithDaemonSetOwnerID MetricCollectorID = "PodsWithDaemonSetOwner"
  145. PodsWithJobOwnerID MetricCollectorID = "PodsWithJobOwner"
  146. PodsWithReplicaSetOwnerID MetricCollectorID = "PodsWithReplicaSetOwner"
  147. ReplicaSetsWithoutOwnersID MetricCollectorID = "ReplicaSetsWithoutOwners"
  148. ReplicaSetsWithRolloutID MetricCollectorID = "ReplicaSetsWithRollout"
  149. ContainerResourceRequestsID MetricCollectorID = "ContainerResourceRequests"
  150. ContainerResourceLimitsID MetricCollectorID = "ContainerResourceLimits"
  151. ResourceQuotaInfoID MetricCollectorID = "ResourceQuotaInfo"
  152. ResourceQuotaUptimeID MetricCollectorID = "ResourceQuotaUptime"
  153. ResourceQuotaSpecCPURequestAverageID MetricCollectorID = "ResourceQuotaSpecCPURequestAverage"
  154. ResourceQuotaSpecCPURequestMaxID MetricCollectorID = "ResourceQuotaSpecCPURequestMax"
  155. ResourceQuotaSpecRAMRequestAverageID MetricCollectorID = "ResourceQuotaSpecRAMRequestAverage"
  156. ResourceQuotaSpecRAMRequestMaxID MetricCollectorID = "ResourceQuotaSpecRAMRequestMax"
  157. ResourceQuotaSpecCPULimitAverageID MetricCollectorID = "ResourceQuotaSpecCPULimitAverage"
  158. ResourceQuotaSpecCPULimitMaxID MetricCollectorID = "ResourceQuotaSpecCPULimitMax"
  159. ResourceQuotaSpecRAMLimitAverageID MetricCollectorID = "ResourceQuotaSpecRAMLimitAverage"
  160. ResourceQuotaSpecRAMLimitMaxID MetricCollectorID = "ResourceQuotaSpecRAMLimitMax"
  161. ResourceQuotaStatusUsedCPURequestAverageID MetricCollectorID = "ResourceQuotaStatusUsedCPURequestAverage"
  162. ResourceQuotaStatusUsedCPURequestMaxID MetricCollectorID = "ResourceQuotaStatusUsedCPURequestMax"
  163. ResourceQuotaStatusUsedRAMRequestAverageID MetricCollectorID = "ResourceQuotaStatusUsedRAMRequestAverage"
  164. ResourceQuotaStatusUsedRAMRequestMaxID MetricCollectorID = "ResourceQuotaStatusUsedRAMRequestMax"
  165. ResourceQuotaStatusUsedCPULimitAverageID MetricCollectorID = "ResourceQuotaStatusUsedCPULimitAverage"
  166. ResourceQuotaStatusUsedCPULimitMaxID MetricCollectorID = "ResourceQuotaStatusUsedCPULimitMax"
  167. ResourceQuotaStatusUsedRAMLimitAverageID MetricCollectorID = "ResourceQuotaStatusUsedRAMLimitAverage"
  168. ResourceQuotaStatusUsedRAMLimitMaxID MetricCollectorID = "ResourceQuotaStatusUsedRAMLimitMax"
  169. )
  170. // MetricCollector is a data structure that represents a specific MetricCollector metric instance that contains its own breakdown
  171. // of stored metrics by a specific label set.
  172. type MetricCollector struct {
  173. id MetricCollectorID // ie: RAMUsageAverage
  174. metricName string // ie: container_memory_working_set_bytes
  175. labels []string
  176. aggregatorFactory aggregator.MetricAggregatorFactory
  177. metrics map[uint64]aggregator.MetricAggregator // map[Hash(labelValues)] = aggregator
  178. filter func(map[string]string) bool
  179. }
  180. // NewMetricCollector creates a new MetricCollector instance with a unique identifier. The metric name is the specific
  181. // name of the collected metric that will be used to query the
  182. func NewMetricCollector(id MetricCollectorID, metricName string, labels []string, aggregatorFactory aggregator.MetricAggregatorFactory, fn func(map[string]string) bool) *MetricCollector {
  183. return &MetricCollector{
  184. id: id,
  185. metricName: metricName,
  186. labels: labels,
  187. aggregatorFactory: aggregatorFactory,
  188. metrics: make(map[uint64]aggregator.MetricAggregator),
  189. filter: fn,
  190. }
  191. }
  192. func (mi *MetricCollector) Update(labels map[string]string, value float64, timestamp time.Time, additionalInfo map[string]string) {
  193. if mi.filter != nil && !mi.filter(labels) {
  194. return
  195. }
  196. labelValues := make([]string, len(mi.labels))
  197. for i, key := range mi.labels {
  198. labelValues[i] = labels[key]
  199. }
  200. key := util.Hash(labelValues)
  201. if mi.metrics[key] == nil {
  202. mi.metrics[key] = mi.aggregatorFactory(labelValues)
  203. }
  204. mi.metrics[key].Update(value, timestamp, additionalInfo)
  205. }
  206. func (mi *MetricCollector) Get() []*aggregator.MetricResult {
  207. results := make([]*aggregator.MetricResult, 0, len(mi.metrics))
  208. for _, metric := range mi.metrics {
  209. labels := util.ToMap(mi.labels, metric.LabelValues())
  210. maps.Copy(labels, metric.AdditionInfo())
  211. mr := &aggregator.MetricResult{
  212. MetricLabels: labels,
  213. Values: metric.Value(),
  214. }
  215. results = append(results, mr)
  216. }
  217. return results
  218. }
  219. func (mi *MetricCollector) Labels() []string {
  220. return mi.labels
  221. }