2
0

metrics.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. package costmodel
  2. import (
  3. "math"
  4. "strconv"
  5. "strings"
  6. "sync"
  7. "time"
  8. "github.com/opencost/opencost/pkg/cloud/models"
  9. "github.com/opencost/opencost/pkg/clustercache"
  10. "github.com/opencost/opencost/pkg/costmodel/clusters"
  11. "github.com/opencost/opencost/pkg/env"
  12. "github.com/opencost/opencost/pkg/errors"
  13. "github.com/opencost/opencost/pkg/log"
  14. "github.com/opencost/opencost/pkg/metrics"
  15. "github.com/opencost/opencost/pkg/prom"
  16. "github.com/opencost/opencost/pkg/util"
  17. "github.com/opencost/opencost/pkg/util/atomic"
  18. promclient "github.com/prometheus/client_golang/api"
  19. "github.com/prometheus/client_golang/prometheus"
  20. dto "github.com/prometheus/client_model/go"
  21. v1 "k8s.io/api/core/v1"
  22. )
  23. //--------------------------------------------------------------------------
  24. // ClusterInfoCollector
  25. //--------------------------------------------------------------------------
  26. // ClusterInfoCollector is a prometheus collector that generates ClusterInfoMetrics
  27. type ClusterInfoCollector struct {
  28. ClusterInfo clusters.ClusterInfoProvider
  29. metricsConfig metrics.MetricsConfig
  30. }
  31. // Describe sends the super-set of all possible descriptors of metrics
  32. // collected by this Collector.
  33. func (cic ClusterInfoCollector) Describe(ch chan<- *prometheus.Desc) {
  34. disabledMetrics := cic.metricsConfig.GetDisabledMetricsMap()
  35. if _, disabled := disabledMetrics["kubecost_cluster_info"]; disabled {
  36. return
  37. }
  38. ch <- prometheus.NewDesc("kubecost_cluster_info", "Kubecost Cluster Info", []string{}, nil)
  39. }
  40. // Collect is called by the Prometheus registry when collecting metrics.
  41. func (cic ClusterInfoCollector) Collect(ch chan<- prometheus.Metric) {
  42. disabledMetrics := cic.metricsConfig.GetDisabledMetricsMap()
  43. if _, disabled := disabledMetrics["kubecost_cluster_info"]; disabled {
  44. return
  45. }
  46. clusterInfo := cic.ClusterInfo.GetClusterInfo()
  47. labels := prom.MapToLabels(clusterInfo)
  48. m := newClusterInfoMetric("kubecost_cluster_info", labels)
  49. ch <- m
  50. }
  51. //--------------------------------------------------------------------------
  52. // ClusterInfoMetric
  53. //--------------------------------------------------------------------------
  54. // ClusterInfoMetric is a prometheus.Metric used to encode the local cluster info
  55. type ClusterInfoMetric struct {
  56. fqName string
  57. help string
  58. labels map[string]string
  59. }
  60. // Creates a new ClusterInfoMetric, implementation of prometheus.Metric
  61. func newClusterInfoMetric(fqName string, labels map[string]string) ClusterInfoMetric {
  62. return ClusterInfoMetric{
  63. fqName: fqName,
  64. labels: labels,
  65. help: "kubecost_cluster_info ClusterInfo",
  66. }
  67. }
  68. // Desc returns the descriptor for the Metric. This method idempotently
  69. // returns the same descriptor throughout the lifetime of the Metric.
  70. func (cim ClusterInfoMetric) Desc() *prometheus.Desc {
  71. l := prometheus.Labels{}
  72. return prometheus.NewDesc(cim.fqName, cim.help, prom.LabelNamesFrom(cim.labels), l)
  73. }
  74. // Write encodes the Metric into a "Metric" Protocol Buffer data
  75. // transmission object.
  76. func (cim ClusterInfoMetric) Write(m *dto.Metric) error {
  77. h := float64(1)
  78. m.Gauge = &dto.Gauge{
  79. Value: &h,
  80. }
  81. var labels []*dto.LabelPair
  82. for k, v := range cim.labels {
  83. labels = append(labels, &dto.LabelPair{
  84. Name: toStringPtr(k),
  85. Value: toStringPtr(v),
  86. })
  87. }
  88. m.Label = labels
  89. return nil
  90. }
  91. // returns a pointer to the string provided
  92. func toStringPtr(s string) *string { return &s }
  93. //--------------------------------------------------------------------------
  94. // Cost Model Metrics Initialization
  95. //--------------------------------------------------------------------------
  96. // Only allow the metrics to be instantiated and registered once
  97. var metricsInit sync.Once
  98. var (
  99. cpuGv *prometheus.GaugeVec
  100. ramGv *prometheus.GaugeVec
  101. gpuGv *prometheus.GaugeVec
  102. gpuCountGv *prometheus.GaugeVec
  103. pvGv *prometheus.GaugeVec
  104. spotGv *prometheus.GaugeVec
  105. totalGv *prometheus.GaugeVec
  106. ramAllocGv *prometheus.GaugeVec
  107. cpuAllocGv *prometheus.GaugeVec
  108. gpuAllocGv *prometheus.GaugeVec
  109. pvAllocGv *prometheus.GaugeVec
  110. networkZoneEgressCostG prometheus.Gauge
  111. networkRegionEgressCostG prometheus.Gauge
  112. networkInternetEgressCostG prometheus.Gauge
  113. clusterManagementCostGv *prometheus.GaugeVec
  114. lbCostGv *prometheus.GaugeVec
  115. )
  116. // initCostModelMetrics uses a sync.Once to ensure that these metrics are only created once
  117. func initCostModelMetrics(clusterCache clustercache.ClusterCache, provider models.Provider, clusterInfo clusters.ClusterInfoProvider, metricsConfig *metrics.MetricsConfig) {
  118. disabledMetrics := metricsConfig.GetDisabledMetricsMap()
  119. var toRegisterGV []*prometheus.GaugeVec
  120. var toRegisterGauge []prometheus.Gauge
  121. metricsInit.Do(func() {
  122. cpuGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  123. Name: "node_cpu_hourly_cost",
  124. Help: "node_cpu_hourly_cost hourly cost for each cpu on this node",
  125. }, []string{"instance", "node", "instance_type", "region", "provider_id", "arch"})
  126. if _, disabled := disabledMetrics["node_cpu_hourly_cost"]; !disabled {
  127. toRegisterGV = append(toRegisterGV, cpuGv)
  128. }
  129. ramGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  130. Name: "node_ram_hourly_cost",
  131. Help: "node_ram_hourly_cost hourly cost for each gb of ram on this node",
  132. }, []string{"instance", "node", "instance_type", "region", "provider_id", "arch"})
  133. if _, disabled := disabledMetrics["node_ram_hourly_cost"]; !disabled {
  134. toRegisterGV = append(toRegisterGV, ramGv)
  135. }
  136. gpuGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  137. Name: "node_gpu_hourly_cost",
  138. Help: "node_gpu_hourly_cost hourly cost for each gpu on this node",
  139. }, []string{"instance", "node", "instance_type", "region", "provider_id", "arch"})
  140. if _, disabled := disabledMetrics["node_gpu_hourly_cost"]; !disabled {
  141. toRegisterGV = append(toRegisterGV, gpuGv)
  142. }
  143. gpuCountGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  144. Name: "node_gpu_count",
  145. Help: "node_gpu_count count of gpu on this node",
  146. }, []string{"instance", "node", "instance_type", "region", "provider_id", "arch"})
  147. if _, disabled := disabledMetrics["node_gpu_count"]; !disabled {
  148. toRegisterGV = append(toRegisterGV, gpuCountGv)
  149. }
  150. pvGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  151. Name: "pv_hourly_cost",
  152. Help: "pv_hourly_cost Cost per GB per hour on a persistent disk",
  153. }, []string{"volumename", "persistentvolume", "provider_id"})
  154. if _, disabled := disabledMetrics["pv_hourly_cost"]; !disabled {
  155. toRegisterGV = append(toRegisterGV, pvGv)
  156. }
  157. spotGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  158. Name: "kubecost_node_is_spot",
  159. Help: "kubecost_node_is_spot Cloud provider info about node preemptibility",
  160. }, []string{"instance", "node", "instance_type", "region", "provider_id", "arch"})
  161. if _, disabled := disabledMetrics["kubecost_node_is_spot"]; !disabled {
  162. toRegisterGV = append(toRegisterGV, spotGv)
  163. }
  164. totalGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  165. Name: "node_total_hourly_cost",
  166. Help: "node_total_hourly_cost Total node cost per hour",
  167. }, []string{"instance", "node", "instance_type", "region", "provider_id", "arch"})
  168. if _, disabled := disabledMetrics["node_total_hourly_cost"]; !disabled {
  169. toRegisterGV = append(toRegisterGV, totalGv)
  170. }
  171. ramAllocGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  172. Name: "container_memory_allocation_bytes",
  173. Help: "container_memory_allocation_bytes Bytes of RAM used",
  174. }, []string{"namespace", "pod", "container", "instance", "node"})
  175. if _, disabled := disabledMetrics["container_memory_allocation_bytes"]; !disabled {
  176. toRegisterGV = append(toRegisterGV, ramAllocGv)
  177. }
  178. cpuAllocGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  179. Name: "container_cpu_allocation",
  180. Help: "container_cpu_allocation Percent of a single CPU used in a minute",
  181. }, []string{"namespace", "pod", "container", "instance", "node"})
  182. if _, disabled := disabledMetrics["container_cpu_allocation"]; !disabled {
  183. toRegisterGV = append(toRegisterGV, cpuAllocGv)
  184. }
  185. gpuAllocGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  186. Name: "container_gpu_allocation",
  187. Help: "container_gpu_allocation GPU used",
  188. }, []string{"namespace", "pod", "container", "instance", "node"})
  189. if _, disabled := disabledMetrics["container_gpu_allocation"]; !disabled {
  190. toRegisterGV = append(toRegisterGV, gpuAllocGv)
  191. }
  192. pvAllocGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  193. Name: "pod_pvc_allocation",
  194. Help: "pod_pvc_allocation Bytes used by a PVC attached to a pod",
  195. }, []string{"namespace", "pod", "persistentvolumeclaim", "persistentvolume"})
  196. if _, disabled := disabledMetrics["pod_pvc_allocation"]; !disabled {
  197. toRegisterGV = append(toRegisterGV, pvAllocGv)
  198. }
  199. networkZoneEgressCostG = prometheus.NewGauge(prometheus.GaugeOpts{
  200. Name: "kubecost_network_zone_egress_cost",
  201. Help: "kubecost_network_zone_egress_cost Total cost per GB egress across zones",
  202. })
  203. if _, disabled := disabledMetrics["kubecost_network_zone_egress_cost"]; !disabled {
  204. toRegisterGauge = append(toRegisterGauge, networkZoneEgressCostG)
  205. }
  206. networkRegionEgressCostG = prometheus.NewGauge(prometheus.GaugeOpts{
  207. Name: "kubecost_network_region_egress_cost",
  208. Help: "kubecost_network_region_egress_cost Total cost per GB egress across regions",
  209. })
  210. if _, disabled := disabledMetrics["kubecost_network_region_egress_cost"]; !disabled {
  211. toRegisterGauge = append(toRegisterGauge, networkRegionEgressCostG)
  212. }
  213. networkInternetEgressCostG = prometheus.NewGauge(prometheus.GaugeOpts{
  214. Name: "kubecost_network_internet_egress_cost",
  215. Help: "kubecost_network_internet_egress_cost Total cost per GB of internet egress.",
  216. })
  217. if _, disabled := disabledMetrics["kubecost_network_internet_egress_cost"]; !disabled {
  218. toRegisterGauge = append(toRegisterGauge, networkInternetEgressCostG)
  219. }
  220. clusterManagementCostGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  221. Name: "kubecost_cluster_management_cost",
  222. Help: "kubecost_cluster_management_cost Hourly cost paid as a cluster management fee.",
  223. }, []string{"provisioner_name"})
  224. if _, disabled := disabledMetrics["kubecost_cluster_management_cost"]; !disabled {
  225. toRegisterGV = append(toRegisterGV, clusterManagementCostGv)
  226. }
  227. lbCostGv = prometheus.NewGaugeVec(prometheus.GaugeOpts{ // no differentiation between ELB and ALB right now
  228. Name: "kubecost_load_balancer_cost",
  229. Help: "kubecost_load_balancer_cost Hourly cost of load balancer",
  230. }, []string{"ingress_ip", "namespace", "service_name"}) // assumes one ingress IP per load balancer
  231. if _, disabled := disabledMetrics["kubecost_load_balancer_cost"]; !disabled {
  232. toRegisterGV = append(toRegisterGV, lbCostGv)
  233. }
  234. // Register cost-model metrics for emission
  235. for _, gv := range toRegisterGV {
  236. prometheus.MustRegister(gv)
  237. }
  238. for _, g := range toRegisterGauge {
  239. prometheus.MustRegister(g)
  240. }
  241. // General Metric Collectors
  242. prometheus.MustRegister(ClusterInfoCollector{
  243. ClusterInfo: clusterInfo,
  244. metricsConfig: *metricsConfig,
  245. })
  246. })
  247. }
  248. //--------------------------------------------------------------------------
  249. // CostModelMetricsEmitter
  250. //--------------------------------------------------------------------------
  251. // CostModelMetricsEmitter emits all cost-model specific metrics calculated by
  252. // the CostModel.ComputeCostData() method.
  253. type CostModelMetricsEmitter struct {
  254. PrometheusClient promclient.Client
  255. KubeClusterCache clustercache.ClusterCache
  256. CloudProvider models.Provider
  257. Model *CostModel
  258. // Metrics
  259. CPUPriceRecorder *prometheus.GaugeVec
  260. RAMPriceRecorder *prometheus.GaugeVec
  261. PersistentVolumePriceRecorder *prometheus.GaugeVec
  262. GPUPriceRecorder *prometheus.GaugeVec
  263. GPUCountRecorder *prometheus.GaugeVec
  264. PVAllocationRecorder *prometheus.GaugeVec
  265. NodeSpotRecorder *prometheus.GaugeVec
  266. NodeTotalPriceRecorder *prometheus.GaugeVec
  267. RAMAllocationRecorder *prometheus.GaugeVec
  268. CPUAllocationRecorder *prometheus.GaugeVec
  269. GPUAllocationRecorder *prometheus.GaugeVec
  270. ClusterManagementCostRecorder *prometheus.GaugeVec
  271. LBCostRecorder *prometheus.GaugeVec
  272. NetworkZoneEgressRecorder prometheus.Gauge
  273. NetworkRegionEgressRecorder prometheus.Gauge
  274. NetworkInternetEgressRecorder prometheus.Gauge
  275. // Concurrent Flow Control - Manages the run state of the metric emitter
  276. runState atomic.AtomicRunState
  277. }
  278. // NewCostModelMetricsEmitter creates a new cost-model metrics emitter. Use Start() to begin metric emission.
  279. func NewCostModelMetricsEmitter(promClient promclient.Client, clusterCache clustercache.ClusterCache, provider models.Provider, clusterInfo clusters.ClusterInfoProvider, model *CostModel) *CostModelMetricsEmitter {
  280. // Get metric configurations, if any
  281. metricsConfig, err := metrics.GetMetricsConfig()
  282. if err != nil {
  283. log.Infof("Failed to get metrics config before init: %s", err)
  284. }
  285. if len(metricsConfig.DisabledMetrics) > 0 {
  286. log.Infof("Starting metrics init with disabled metrics: %v", metricsConfig.DisabledMetrics)
  287. }
  288. // init will only actually execute once to register the custom gauges
  289. initCostModelMetrics(clusterCache, provider, clusterInfo, metricsConfig)
  290. metrics.InitKubeMetrics(clusterCache, metricsConfig, &metrics.KubeMetricsOpts{
  291. EmitKubecostControllerMetrics: true,
  292. EmitNamespaceAnnotations: env.IsEmitNamespaceAnnotationsMetric(),
  293. EmitPodAnnotations: env.IsEmitPodAnnotationsMetric(),
  294. EmitKubeStateMetrics: env.IsEmitKsmV1Metrics(),
  295. EmitKubeStateMetricsV1Only: env.IsEmitKsmV1MetricsOnly(),
  296. })
  297. metrics.InitOpencostTelemetry(metricsConfig)
  298. return &CostModelMetricsEmitter{
  299. PrometheusClient: promClient,
  300. KubeClusterCache: clusterCache,
  301. CloudProvider: provider,
  302. Model: model,
  303. CPUPriceRecorder: cpuGv,
  304. RAMPriceRecorder: ramGv,
  305. GPUPriceRecorder: gpuGv,
  306. GPUCountRecorder: gpuCountGv,
  307. PersistentVolumePriceRecorder: pvGv,
  308. NodeSpotRecorder: spotGv,
  309. NodeTotalPriceRecorder: totalGv,
  310. RAMAllocationRecorder: ramAllocGv,
  311. CPUAllocationRecorder: cpuAllocGv,
  312. GPUAllocationRecorder: gpuAllocGv,
  313. PVAllocationRecorder: pvAllocGv,
  314. NetworkZoneEgressRecorder: networkZoneEgressCostG,
  315. NetworkRegionEgressRecorder: networkRegionEgressCostG,
  316. NetworkInternetEgressRecorder: networkInternetEgressCostG,
  317. ClusterManagementCostRecorder: clusterManagementCostGv,
  318. LBCostRecorder: lbCostGv,
  319. }
  320. }
  321. // IsRunning returns true if metric recording is running.
  322. func (cmme *CostModelMetricsEmitter) IsRunning() bool {
  323. return cmme.runState.IsRunning()
  324. }
  325. // NodeCostAverages tracks a running average of a node's cost attributes.
  326. // The averages are used to detect and discard spurrious outliers.
  327. type NodeCostAverages struct {
  328. CpuCostAverage float64
  329. RamCostAverage float64
  330. NumCpuDataPoints float64
  331. NumRamDataPoints float64
  332. }
  333. // StartCostModelMetricRecording starts the go routine that emits metrics used to determine
  334. // cluster costs.
  335. func (cmme *CostModelMetricsEmitter) Start() bool {
  336. // wait for a reset to prevent a race between start and stop calls
  337. cmme.runState.WaitForReset()
  338. // Check to see if we're already recording, and atomically advance the run state to start if we're not
  339. if !cmme.runState.Start() {
  340. log.Errorf("Attempted to start cost model metric recording when it's already running.")
  341. return false
  342. }
  343. go func() {
  344. defer errors.HandlePanic()
  345. containerSeen := make(map[string]bool)
  346. nodeSeen := make(map[string]bool)
  347. loadBalancerSeen := make(map[string]bool)
  348. pvSeen := make(map[string]bool)
  349. pvcSeen := make(map[string]bool)
  350. nodeCostAverages := make(map[string]NodeCostAverages)
  351. getKeyFromLabelStrings := func(labels ...string) string {
  352. return strings.Join(labels, ",")
  353. }
  354. getLabelStringsFromKey := func(key string) []string {
  355. return strings.Split(key, ",")
  356. }
  357. var defaultRegion string = ""
  358. nodeList := cmme.KubeClusterCache.GetAllNodes()
  359. if len(nodeList) > 0 {
  360. var ok bool
  361. defaultRegion, ok = util.GetRegion(nodeList[0].Labels)
  362. if !ok {
  363. log.DedupedWarningf(5, "Failed to read default region from labels on node %s", nodeList[0].Name)
  364. }
  365. }
  366. for {
  367. log.Debugf("Recording prices...")
  368. podlist := cmme.KubeClusterCache.GetAllPods()
  369. podStatus := make(map[string]v1.PodPhase)
  370. for _, pod := range podlist {
  371. podStatus[pod.Name] = pod.Status.Phase
  372. }
  373. cfg, _ := cmme.CloudProvider.GetConfig()
  374. provisioner, clusterManagementCost, err := cmme.CloudProvider.ClusterManagementPricing()
  375. if err != nil {
  376. log.Errorf("Error getting cluster management cost %s", err.Error())
  377. }
  378. cmme.ClusterManagementCostRecorder.WithLabelValues(provisioner).Set(clusterManagementCost)
  379. // Record network pricing at global scope
  380. networkCosts, err := cmme.CloudProvider.NetworkPricing()
  381. if err != nil {
  382. log.Debugf("Failed to retrieve network costs: %s", err.Error())
  383. } else {
  384. cmme.NetworkZoneEgressRecorder.Set(networkCosts.ZoneNetworkEgressCost)
  385. cmme.NetworkRegionEgressRecorder.Set(networkCosts.RegionNetworkEgressCost)
  386. cmme.NetworkInternetEgressRecorder.Set(networkCosts.InternetNetworkEgressCost)
  387. }
  388. // TODO: Pass PrometheusClient and CloudProvider into CostModel on instantiation so this isn't so awkward
  389. data, err := cmme.Model.ComputeCostData(cmme.PrometheusClient, cmme.CloudProvider, "2m", "", "")
  390. if err != nil {
  391. // For an error collection, we'll just log the length of the errors (ComputeCostData already logs the
  392. // actual errors)
  393. if prom.IsErrorCollection(err) {
  394. if ec, ok := err.(prom.QueryErrorCollection); ok {
  395. log.Errorf("Error in price recording: %d errors occurred", len(ec.Errors()))
  396. }
  397. } else {
  398. log.Errorf("Error in price recording: " + err.Error())
  399. }
  400. // zero the for loop so the time.Sleep will still work
  401. data = map[string]*CostData{}
  402. }
  403. // TODO: Pass CloudProvider into CostModel on instantiation so this isn't so awkward
  404. nodes, err := cmme.Model.GetNodeCost(cmme.CloudProvider)
  405. if err != nil {
  406. log.Warnf("Error getting Node cost: %s", err)
  407. }
  408. for nodeName, node := range nodes {
  409. // Emit costs, guarding against NaN inputs for custom pricing.
  410. cpuCost, _ := strconv.ParseFloat(node.VCPUCost, 64)
  411. if math.IsNaN(cpuCost) || math.IsInf(cpuCost, 0) {
  412. cpuCost, _ = strconv.ParseFloat(cfg.CPU, 64)
  413. if math.IsNaN(cpuCost) || math.IsInf(cpuCost, 0) {
  414. cpuCost = 0
  415. }
  416. }
  417. cpu, _ := strconv.ParseFloat(node.VCPU, 64)
  418. if math.IsNaN(cpu) || math.IsInf(cpu, 0) {
  419. cpu = 1 // Assume 1 CPU
  420. }
  421. ramCost, _ := strconv.ParseFloat(node.RAMCost, 64)
  422. if math.IsNaN(ramCost) || math.IsInf(ramCost, 0) {
  423. ramCost, _ = strconv.ParseFloat(cfg.RAM, 64)
  424. if math.IsNaN(ramCost) || math.IsInf(ramCost, 0) {
  425. ramCost = 0
  426. }
  427. }
  428. ram, _ := strconv.ParseFloat(node.RAMBytes, 64)
  429. if math.IsNaN(ram) || math.IsInf(ram, 0) {
  430. ram = 0
  431. }
  432. gpu, _ := strconv.ParseFloat(node.GPU, 64)
  433. if math.IsNaN(gpu) || math.IsInf(gpu, 0) {
  434. gpu = 0
  435. }
  436. gpuCost, _ := strconv.ParseFloat(node.GPUCost, 64)
  437. if math.IsNaN(gpuCost) || math.IsInf(gpuCost, 0) {
  438. gpuCost, _ = strconv.ParseFloat(cfg.GPU, 64)
  439. if math.IsNaN(gpuCost) || math.IsInf(gpuCost, 0) {
  440. gpuCost = 0
  441. }
  442. }
  443. nodeType := node.InstanceType
  444. nodeRegion := node.Region
  445. totalCost := cpu*cpuCost + ramCost*(ram/1024/1024/1024) + gpu*gpuCost
  446. labelKey := getKeyFromLabelStrings(nodeName, nodeName, nodeType, nodeRegion, node.ProviderID, node.ArchType)
  447. avgCosts, ok := nodeCostAverages[labelKey]
  448. // initialize average cost tracking for this node if there is none
  449. if !ok {
  450. avgCosts = NodeCostAverages{
  451. CpuCostAverage: cpuCost,
  452. RamCostAverage: ramCost,
  453. NumCpuDataPoints: 1,
  454. NumRamDataPoints: 1,
  455. }
  456. nodeCostAverages[labelKey] = avgCosts
  457. }
  458. cmme.GPUCountRecorder.WithLabelValues(nodeName, nodeName, nodeType, nodeRegion, node.ProviderID, node.ArchType).Set(gpu)
  459. cmme.GPUPriceRecorder.WithLabelValues(nodeName, nodeName, nodeType, nodeRegion, node.ProviderID, node.ArchType).Set(gpuCost)
  460. const outlierFactor float64 = 30
  461. // don't record cpuCost, ramCost, or gpuCost in the case of wild outliers
  462. // k8s api sometimes causes cost spikes as described here:
  463. // https://github.com/opencost/opencost/issues/927
  464. cpuOutlierCutoff := outlierFactor * avgCosts.CpuCostAverage
  465. if cpuCost < cpuOutlierCutoff {
  466. cmme.CPUPriceRecorder.WithLabelValues(nodeName, nodeName, nodeType, nodeRegion, node.ProviderID, node.ArchType).Set(cpuCost)
  467. avgCosts.CpuCostAverage = (avgCosts.CpuCostAverage*avgCosts.NumCpuDataPoints + cpuCost) / (avgCosts.NumCpuDataPoints + 1)
  468. avgCosts.NumCpuDataPoints += 1
  469. } else {
  470. log.Debugf("CPU cost outlier detected; skipping data point: %s had %f as cost, which is above %f.", nodeName, cpuCost, cpuOutlierCutoff)
  471. }
  472. ramOutlierCutoff := outlierFactor * avgCosts.RamCostAverage
  473. if ramCost < ramOutlierCutoff {
  474. cmme.RAMPriceRecorder.WithLabelValues(nodeName, nodeName, nodeType, nodeRegion, node.ProviderID, node.ArchType).Set(ramCost)
  475. avgCosts.RamCostAverage = (avgCosts.RamCostAverage*avgCosts.NumRamDataPoints + ramCost) / (avgCosts.NumRamDataPoints + 1)
  476. avgCosts.NumRamDataPoints += 1
  477. } else {
  478. log.Debugf("RAM cost outlier detected; skipping data point: %s had %f as cost, which is above %f.", nodeName, ramCost, ramOutlierCutoff)
  479. }
  480. // skip redording totalCost if any constituent costs were outliers
  481. if cpuCost < cpuOutlierCutoff && ramCost < ramOutlierCutoff {
  482. cmme.NodeTotalPriceRecorder.WithLabelValues(nodeName, nodeName, nodeType, nodeRegion, node.ProviderID, node.ArchType).Set(totalCost)
  483. } else {
  484. log.Debugf("CPU and RAM outlier detected, not recording node %s total cost %f", nodeName, totalCost)
  485. }
  486. nodeCostAverages[labelKey] = avgCosts
  487. if node.IsSpot() {
  488. cmme.NodeSpotRecorder.WithLabelValues(nodeName, nodeName, nodeType, nodeRegion, node.ProviderID, node.ArchType).Set(1.0)
  489. } else {
  490. cmme.NodeSpotRecorder.WithLabelValues(nodeName, nodeName, nodeType, nodeRegion, node.ProviderID, node.ArchType).Set(0.0)
  491. }
  492. nodeSeen[labelKey] = true
  493. }
  494. // TODO: Pass CloudProvider into CostModel on instantiation so this isn't so awkward
  495. loadBalancers, err := cmme.Model.GetLBCost(cmme.CloudProvider)
  496. if err != nil {
  497. log.Warnf("Error getting LoadBalancer cost: %s", err)
  498. }
  499. for lbKey, lb := range loadBalancers {
  500. // TODO: parse (if necessary) and calculate cost associated with loadBalancer based on dynamic cloud prices fetched into each lb struct on GetLBCost() call
  501. namespace := lbKey.Namespace
  502. serviceName := lbKey.Service
  503. ingressIP := ""
  504. if len(lb.IngressIPAddresses) > 0 {
  505. ingressIP = lb.IngressIPAddresses[0] // assumes one ingress IP per load balancer
  506. }
  507. cmme.LBCostRecorder.WithLabelValues(ingressIP, namespace, serviceName).Set(lb.Cost)
  508. labelKey := getKeyFromLabelStrings(ingressIP, namespace, serviceName)
  509. loadBalancerSeen[labelKey] = true
  510. }
  511. for _, costs := range data {
  512. nodeName := costs.NodeName
  513. namespace := costs.Namespace
  514. podName := costs.PodName
  515. containerName := costs.Name
  516. if costs.PVCData != nil {
  517. for _, pvc := range costs.PVCData {
  518. if pvc.Volume != nil {
  519. timesClaimed := pvc.TimesClaimed
  520. if timesClaimed == 0 {
  521. timesClaimed = 1 // unallocated PVs are unclaimed but have a full allocation
  522. }
  523. cmme.PVAllocationRecorder.WithLabelValues(namespace, podName, pvc.Claim, pvc.VolumeName).Set(pvc.Values[0].Value / float64(timesClaimed))
  524. labelKey := getKeyFromLabelStrings(namespace, podName, pvc.Claim, pvc.VolumeName)
  525. pvcSeen[labelKey] = true
  526. }
  527. }
  528. }
  529. if len(costs.RAMAllocation) > 0 {
  530. cmme.RAMAllocationRecorder.WithLabelValues(namespace, podName, containerName, nodeName, nodeName).Set(costs.RAMAllocation[0].Value)
  531. }
  532. if len(costs.CPUAllocation) > 0 {
  533. cmme.CPUAllocationRecorder.WithLabelValues(namespace, podName, containerName, nodeName, nodeName).Set(costs.CPUAllocation[0].Value)
  534. }
  535. if len(costs.GPUReq) > 0 {
  536. // allocation here is set to the request because shared GPU usage not yet supported.
  537. cmme.GPUAllocationRecorder.WithLabelValues(namespace, podName, containerName, nodeName, nodeName).Set(costs.GPUReq[0].Value)
  538. }
  539. labelKey := getKeyFromLabelStrings(namespace, podName, containerName, nodeName, nodeName)
  540. if podStatus[podName] == v1.PodRunning { // Only report data for current pods
  541. containerSeen[labelKey] = true
  542. } else {
  543. containerSeen[labelKey] = false
  544. }
  545. }
  546. storageClasses := cmme.KubeClusterCache.GetAllStorageClasses()
  547. storageClassMap := make(map[string]map[string]string)
  548. for _, storageClass := range storageClasses {
  549. params := storageClass.Parameters
  550. storageClassMap[storageClass.ObjectMeta.Name] = params
  551. if storageClass.GetAnnotations()["storageclass.kubernetes.io/is-default-class"] == "true" || storageClass.GetAnnotations()["storageclass.beta.kubernetes.io/is-default-class"] == "true" {
  552. storageClassMap["default"] = params
  553. storageClassMap[""] = params
  554. }
  555. }
  556. pvs := cmme.KubeClusterCache.GetAllPersistentVolumes()
  557. for _, pv := range pvs {
  558. // Omit pv_hourly_cost if the volume status is failed
  559. if pv.Status.Phase == v1.VolumeFailed {
  560. continue
  561. }
  562. parameters, ok := storageClassMap[pv.Spec.StorageClassName]
  563. if !ok {
  564. log.Debugf("Unable to find parameters for storage class \"%s\". Pv \"%s\" might have an empty or invalid storageClassName.", pv.Spec.StorageClassName, pv.Name)
  565. }
  566. var region string
  567. if r, ok := util.GetRegion(pv.Labels); ok {
  568. region = r
  569. } else {
  570. region = defaultRegion
  571. }
  572. cacPv := &models.PV{
  573. Class: pv.Spec.StorageClassName,
  574. Region: region,
  575. Parameters: parameters,
  576. }
  577. // TODO: GetPVCost should be a method in CostModel?
  578. GetPVCost(cacPv, pv, cmme.CloudProvider, region)
  579. c, _ := strconv.ParseFloat(cacPv.Cost, 64)
  580. cmme.PersistentVolumePriceRecorder.WithLabelValues(pv.Name, pv.Name, cacPv.ProviderID).Set(c)
  581. labelKey := getKeyFromLabelStrings(pv.Name, pv.Name, cacPv.ProviderID)
  582. pvSeen[labelKey] = true
  583. }
  584. // Remove metrics on Nodes/LoadBalancers/Containers/PVs that no
  585. // longer exist
  586. for labelString, seen := range nodeSeen {
  587. if !seen {
  588. log.Debugf("Removing metrics for %s, no data observed recently", labelString)
  589. labels := getLabelStringsFromKey(labelString)
  590. ok := cmme.NodeTotalPriceRecorder.DeleteLabelValues(labels...)
  591. if ok {
  592. log.Debugf("No data observed for node with labels %v, removed from totalprice", labels)
  593. } else {
  594. log.Warnf("Failed to remove label set %v from metric node_total_hourly_cost. Failure to remove stale metrics may result in inaccurate data.", labels)
  595. }
  596. ok = cmme.NodeSpotRecorder.DeleteLabelValues(labels...)
  597. if ok {
  598. log.Debugf("No data observed for node with labels %v, removed from spot records", labels)
  599. } else {
  600. log.Warnf("Failed to remove label set %v from metric kubecost_node_is_spot. Failure to remove stale metrics may result in inaccurate data.", labels)
  601. }
  602. ok = cmme.CPUPriceRecorder.DeleteLabelValues(labels...)
  603. if ok {
  604. log.Debugf("No data observed for node with labels %v, removed from cpuprice", labels)
  605. } else {
  606. log.Warnf("Failed to remove label set %v from metric node_cpu_hourly_cost. Failure to remove stale metrics may result in inaccurate data.", labels)
  607. }
  608. ok = cmme.GPUPriceRecorder.DeleteLabelValues(labels...)
  609. if ok {
  610. log.Debugf("No data observed for node with labels %v, removed from gpuprice", labels)
  611. } else {
  612. log.Warnf("Failed to remove label set %v from metric node_gpu_hourly_cost. Failure to remove stale metrics may result in inaccurate data.", labels)
  613. }
  614. ok = cmme.GPUCountRecorder.DeleteLabelValues(labels...)
  615. if ok {
  616. log.Debugf("No data observed for node with labels %v, removed from gpucount", labels)
  617. } else {
  618. log.Warnf("Failed to remove label set %v from metric node_gpu_count. Failure to remove stale metrics may result in inaccurate data.", labels)
  619. }
  620. ok = cmme.RAMPriceRecorder.DeleteLabelValues(labels...)
  621. if ok {
  622. log.Debugf("No data observed for node with labels %v, removed from ramprice", labels)
  623. } else {
  624. log.Warnf("Failed to remove label set %v from metric node_ram_hourly_cost. Failure to remove stale metrics may result in inaccurate data.", labels)
  625. }
  626. delete(nodeSeen, labelString)
  627. delete(nodeCostAverages, labelString)
  628. } else {
  629. nodeSeen[labelString] = false
  630. }
  631. }
  632. for labelString, seen := range loadBalancerSeen {
  633. if !seen {
  634. labels := getLabelStringsFromKey(labelString)
  635. ok := cmme.LBCostRecorder.DeleteLabelValues(labels...)
  636. if !ok {
  637. log.Warnf("Failed to remove label set %v from metric kubecost_load_balancer_cost. Failure to remove stale metrics may result in inaccurate data.", labels)
  638. }
  639. delete(loadBalancerSeen, labelString)
  640. } else {
  641. loadBalancerSeen[labelString] = false
  642. }
  643. }
  644. for labelString, seen := range containerSeen {
  645. if !seen {
  646. labels := getLabelStringsFromKey(labelString)
  647. if len(labels) >= 2 && labels[1] != unmountedPVsContainer { // special "pod" to contain the unmounted PVs - does not have RAM/CPU/...
  648. ok := cmme.RAMAllocationRecorder.DeleteLabelValues(labels...)
  649. if !ok {
  650. log.Warnf("Failed to remove label set %v from metric container_memory_allocation_bytes. Failure to remove stale metrics may result in inaccurate data.", labels)
  651. }
  652. ok = cmme.CPUAllocationRecorder.DeleteLabelValues(labels...)
  653. if !ok {
  654. log.Warnf("Failed to remove label set %v from metric container_cpu_allocation. Failure to remove stale metrics may result in inaccurate data.", labels)
  655. }
  656. ok = cmme.GPUAllocationRecorder.DeleteLabelValues(labels...)
  657. if !ok {
  658. log.Warnf("Failed to remove label set %v from metric container_gpu_allocation. Failure to remove stale metrics may result in inaccurate data.", labels)
  659. }
  660. } else {
  661. log.Debugf("Did not try to delete RAM/CPU/GPU for fake '%s' container: %v", unmountedPVsContainer, labels)
  662. }
  663. delete(containerSeen, labelString)
  664. } else {
  665. containerSeen[labelString] = false
  666. }
  667. }
  668. for labelString, seen := range pvSeen {
  669. if !seen {
  670. labels := getLabelStringsFromKey(labelString)
  671. ok := cmme.PersistentVolumePriceRecorder.DeleteLabelValues(labels...)
  672. if !ok {
  673. log.Warnf("Failed to remove label set %v from metric pv_hourly_cost. Failure to remove stale metrics may result in inaccurate data.", labels)
  674. }
  675. delete(pvSeen, labelString)
  676. } else {
  677. pvSeen[labelString] = false
  678. }
  679. }
  680. for labelString, seen := range pvcSeen {
  681. if !seen {
  682. labels := getLabelStringsFromKey(labelString)
  683. ok := cmme.PVAllocationRecorder.DeleteLabelValues(labels...)
  684. if !ok {
  685. log.Warnf("Failed to remove label set %v from metric pod_pvc_allocation. Failure to remove stale metrics may result in inaccurate data.", labels)
  686. }
  687. delete(pvcSeen, labelString)
  688. } else {
  689. pvcSeen[labelString] = false
  690. }
  691. }
  692. select {
  693. case <-time.After(time.Minute):
  694. case <-cmme.runState.OnStop():
  695. cmme.runState.Reset()
  696. return
  697. }
  698. }
  699. }()
  700. return true
  701. }
  702. // Stop halts the metrics emission loop after the current emission is completed
  703. // or if the emission is paused.
  704. func (cmme *CostModelMetricsEmitter) Stop() {
  705. cmme.runState.Stop()
  706. }