collector.go 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  1. package collector
  2. import (
  3. "github.com/opencost/opencost/core/pkg/source"
  4. "github.com/opencost/opencost/modules/collector-source/pkg/metric"
  5. "github.com/opencost/opencost/modules/collector-source/pkg/metric/aggregator"
  6. )
  7. // NewOpenCostMetricStore creates a new MetricStore which has registered all MetricCollector instances required
  8. // for OpenCost
  9. func NewOpenCostMetricStore() metric.MetricStore {
  10. memStore := metric.NewInMemoryMetricStore()
  11. // Register all the metrics
  12. memStore.Register(NewPVPricePerGiBHourMetricCollector())
  13. memStore.Register(NewPVUsedAverageMetricCollector())
  14. memStore.Register(NewPVUsedMaxMetricCollector())
  15. memStore.Register(NewPVCInfoMetricCollector())
  16. memStore.Register(NewPVActiveMinutesMetricCollector())
  17. memStore.Register(NewLocalStorageUsedActiveMinutesMetricCollector())
  18. memStore.Register(NewLocalStorageUsedAverageMetricCollector())
  19. memStore.Register(NewLocalStorageUsedMaxMetricCollector())
  20. memStore.Register(NewLocalStorageBytesMetricCollector())
  21. memStore.Register(NewLocalStorageActiveMinutesMetricCollector())
  22. memStore.Register(NewNodeCPUCoresCapacityMetricCollector())
  23. memStore.Register(NewNodeCPUCoresAllocatableMetricCollector())
  24. memStore.Register(NewNodeRAMBytesCapacityMetricCollector())
  25. memStore.Register(NewNodeRAMBytesAllocatableMetricCollector())
  26. memStore.Register(NewNodeGPUCountMetricCollector())
  27. memStore.Register(NewNodeLabelsMetricCollector())
  28. memStore.Register(NewNodeActiveMinutesMetricCollector())
  29. memStore.Register(NewNodeCPUModeTotalMetricCollector())
  30. memStore.Register(NewNodeRAMSystemUsageAverageMetricCollector())
  31. memStore.Register(NewNodeRAMUserUsageAverageMetricCollector())
  32. memStore.Register(NewLBPricePerHourMetricCollector())
  33. memStore.Register(NewLBActiveMinutesMetricCollector())
  34. memStore.Register(NewClusterManagementDurationMetricCollector())
  35. memStore.Register(NewClusterManagementPricePerHourMetricCollector())
  36. memStore.Register(NewPodActiveMinutesMetricCollector())
  37. memStore.Register(NewRAMBytesAllocatedMetricCollector())
  38. memStore.Register(NewRAMRequestsMetricCollector())
  39. memStore.Register(NewRAMUsageAverageMetricCollector())
  40. memStore.Register(NewRAMUsageMaxMetricCollector())
  41. memStore.Register(NewCPUCoresAllocatedMetricCollector())
  42. memStore.Register(NewCPURequestsMetricCollector())
  43. memStore.Register(NewCPUUsageAverageMetricCollector())
  44. memStore.Register(NewCPUUsageMaxMetricCollector())
  45. memStore.Register(NewGPUsRequestedMetricCollector())
  46. memStore.Register(NewGPUsUsageAverageMetricCollector())
  47. memStore.Register(NewGPUsUsageMaxMetricCollector())
  48. memStore.Register(NewGPUsAllocatedMetricCollector())
  49. memStore.Register(NewIsGPUSharedMetricCollector())
  50. memStore.Register(NewGPUInfoMetricCollector())
  51. memStore.Register(NewNodeCPUPricePerHourMetricCollector())
  52. memStore.Register(NewNodeRAMPricePerGiBHourMetricCollector())
  53. memStore.Register(NewNodeGPUPricePerHourMetricCollector())
  54. memStore.Register(NewNodeIsSpotMetricCollector())
  55. memStore.Register(NewPodPVCAllocationMetricCollector())
  56. memStore.Register(NewPVCBytesRequestedMetricCollector())
  57. memStore.Register(NewPVBytesMetricCollector())
  58. memStore.Register(NewPVInfoMetricCollector())
  59. memStore.Register(NewNetZoneGiBMetricCollector())
  60. memStore.Register(NewNetZonePricePerGiBMetricCollector())
  61. memStore.Register(NewNetRegionGiBMetricCollector())
  62. memStore.Register(NewNetRegionPricePerGiBMetricCollector())
  63. memStore.Register(NewNetInternetGiBMetricCollector())
  64. memStore.Register(NewNetInternetPricePerGiBMetricCollector())
  65. memStore.Register(NewNetInternetServiceGiBMetricCollector())
  66. memStore.Register(NewNetReceiveBytesMetricCollector())
  67. memStore.Register(NewNetZoneIngressGiBMetricCollector())
  68. memStore.Register(NewNetRegionIngressGiBMetricCollector())
  69. memStore.Register(NewNetInternetIngressGiBMetricCollector())
  70. memStore.Register(NewNetInternetServiceIngressGiBMetricCollector())
  71. memStore.Register(NewNetTransferBytesMetricCollector())
  72. memStore.Register(NewNamespaceLabelsMetricCollector())
  73. memStore.Register(NewNamespaceAnnotationsMetricCollector())
  74. memStore.Register(NewPodLabelsMetricCollector())
  75. memStore.Register(NewPodAnnotationsMetricCollector())
  76. memStore.Register(NewServiceLabelsMetricCollector())
  77. memStore.Register(NewDeploymentLabelsMetricCollector())
  78. memStore.Register(NewStatefulSetLabelsMetricCollector())
  79. memStore.Register(NewDaemonSetLabelsMetricCollector())
  80. memStore.Register(NewJobLabelsMetricCollector())
  81. memStore.Register(NewPodsWithReplicaSetOwnerMetricCollector())
  82. memStore.Register(NewReplicaSetsWithoutOwnersMetricCollector())
  83. memStore.Register(NewReplicaSetsWithRolloutMetricCollector())
  84. return memStore
  85. }
  86. // avg(
  87. // avg_over_time(
  88. // pv_hourly_cost{
  89. // <some_custom_filter>
  90. // }[1h]
  91. // )
  92. // ) by (cluster_id, persistentvolume, volumename, provider_id)
  93. func NewPVPricePerGiBHourMetricCollector() *metric.MetricCollector {
  94. return metric.NewMetricCollector(
  95. metric.PVPricePerGiBHourID,
  96. metric.PVHourlyCost,
  97. []string{
  98. source.VolumeNameLabel,
  99. source.PVLabel,
  100. source.ProviderIDLabel,
  101. },
  102. aggregator.AverageOverTime,
  103. nil,
  104. )
  105. }
  106. // avg(
  107. // avg_over_time(
  108. // kubelet_volume_stats_used_bytes{
  109. // <some_custom_filter>
  110. // }[1h]
  111. // )
  112. // ) by (cluster_id, persistentvolumeclaim, namespace)
  113. func NewPVUsedAverageMetricCollector() *metric.MetricCollector {
  114. return metric.NewMetricCollector(
  115. metric.PVUsedAverageID,
  116. metric.KubeletVolumeStatsUsedBytes,
  117. []string{
  118. source.NamespaceLabel,
  119. source.PVCLabel,
  120. },
  121. aggregator.AverageOverTime,
  122. nil,
  123. )
  124. }
  125. // max(
  126. // max_over_time(
  127. // kubelet_volume_stats_used_bytes{
  128. // <some_custom_filter>
  129. // }[1h]
  130. // )
  131. // ) by (cluster_id, persistentvolumeclaim, namespace)
  132. func NewPVUsedMaxMetricCollector() *metric.MetricCollector {
  133. return metric.NewMetricCollector(
  134. metric.PVUsedMaxID,
  135. metric.KubeletVolumeStatsUsedBytes,
  136. []string{
  137. source.NamespaceLabel,
  138. source.PVCLabel,
  139. },
  140. aggregator.MaxOverTime,
  141. nil,
  142. )
  143. }
  144. // avg(
  145. // kube_persistentvolumeclaim_info{
  146. // volumename != "",
  147. // <some_custom_filter>
  148. // }
  149. // ) by (persistentvolumeclaim, storageclass, volumename, namespace, cluster_id)[0:10m]
  150. func NewPVCInfoMetricCollector() *metric.MetricCollector {
  151. return metric.NewMetricCollector(
  152. metric.PVCInfoID,
  153. metric.KubePersistentVolumeClaimInfo,
  154. []string{
  155. source.NamespaceLabel,
  156. source.VolumeNameLabel,
  157. source.PVCLabel,
  158. source.StorageClassLabel,
  159. },
  160. aggregator.ActiveMinutes,
  161. func(labels map[string]string) bool {
  162. return labels[source.VolumeNameLabel] != ""
  163. },
  164. )
  165. }
  166. // avg(
  167. // kube_persistentvolume_capacity_bytes{
  168. // <some_custom_filter>
  169. // }
  170. // ) by (cluster_id, persistentvolume)[0:10m]
  171. func NewPVActiveMinutesMetricCollector() *metric.MetricCollector {
  172. return metric.NewMetricCollector(
  173. metric.PVActiveMinutesID,
  174. metric.KubePersistentVolumeCapacityBytes,
  175. []string{
  176. source.PVLabel,
  177. },
  178. aggregator.ActiveMinutes,
  179. nil,
  180. )
  181. }
  182. // sum_over_time(
  183. //
  184. // sum(
  185. // container_fs_usage_bytes{
  186. // device=~"/dev/(nvme|sda).*",
  187. // id="/",
  188. // <some_custom_filter>
  189. // }
  190. // ) by (instance, device, cluster_id)[%s:%dm]
  191. //
  192. // ) / 1024 / 1024 / 1024 * %f * %f`
  193. // NewLocalStorageUsedActiveMinutesMetricCollector does not have an associated query end point but is used in the results
  194. // of QueryLocalStorageUsedCost
  195. func NewLocalStorageUsedActiveMinutesMetricCollector() *metric.MetricCollector {
  196. return metric.NewMetricCollector(
  197. metric.LocalStorageUsedActiveMinutesID,
  198. metric.ContainerFSUsageBytes,
  199. []string{
  200. source.InstanceLabel,
  201. source.DeviceLabel,
  202. },
  203. aggregator.ActiveMinutes,
  204. nil, // filter not required here because only container root file system is being scraped
  205. )
  206. }
  207. // avg(
  208. // sum(
  209. // avg_over_time(
  210. // container_fs_usage_bytes{
  211. // device=~"/dev/(nvme|sda).*",
  212. // id="/",
  213. // <some_custom_filter>
  214. // }[1h]
  215. // )
  216. // ) by (instance, device, cluster_id, job)
  217. // ) by (instance, device, cluster_id)
  218. func NewLocalStorageUsedAverageMetricCollector() *metric.MetricCollector {
  219. return metric.NewMetricCollector(
  220. metric.LocalStorageUsedAverageID,
  221. metric.ContainerFSUsageBytes,
  222. []string{
  223. source.InstanceLabel,
  224. source.DeviceLabel,
  225. },
  226. aggregator.AverageOverTime,
  227. nil, // filter not required here because only container root file system is being scraped
  228. )
  229. }
  230. // max(
  231. //
  232. // sum(
  233. // max_over_time(
  234. // container_fs_usage_bytes{
  235. // device=~"/dev/(nvme|sda).*",
  236. // id="/",
  237. // <some_custom_filter>
  238. // }[1h]
  239. // )
  240. // ) by (instance, device, cluster_id, job)
  241. //
  242. // ) by (instance, device, cluster_id)
  243. func NewLocalStorageUsedMaxMetricCollector() *metric.MetricCollector {
  244. return metric.NewMetricCollector(
  245. metric.LocalStorageUsedMaxID,
  246. metric.ContainerFSUsageBytes,
  247. []string{
  248. source.InstanceLabel,
  249. source.DeviceLabel,
  250. },
  251. aggregator.MaxOverTime,
  252. nil, // filter not required here because only container root file system is being scraped
  253. )
  254. }
  255. // avg_over_time(
  256. //
  257. // sum(
  258. // container_fs_limit_bytes{
  259. // device=~"/dev/(nvme|sda).*",
  260. // id="/",
  261. // <some_custom_filter>
  262. // }
  263. // ) by (instance, device, cluster_id)[%s:%dm]
  264. //
  265. // )
  266. func NewLocalStorageBytesMetricCollector() *metric.MetricCollector {
  267. return metric.NewMetricCollector(
  268. metric.LocalStorageBytesID,
  269. metric.NodeFSCapacityBytes,
  270. []string{
  271. source.InstanceLabel,
  272. source.DeviceLabel,
  273. },
  274. aggregator.AverageOverTime,
  275. nil, // filter not required here because only node root file system is being scraped
  276. )
  277. }
  278. // count(
  279. //
  280. // node_total_hourly_cost{
  281. // <some_custom_filter>
  282. // }
  283. //
  284. // ) by (cluster_id, node, instance, provider_id)[%s:%dm]
  285. func NewLocalStorageActiveMinutesMetricCollector() *metric.MetricCollector {
  286. return metric.NewMetricCollector(
  287. metric.LocalStorageActiveMinutesID,
  288. metric.NodeTotalHourlyCost,
  289. []string{
  290. source.NodeLabel,
  291. source.ProviderIDLabel,
  292. },
  293. aggregator.ActiveMinutes,
  294. nil,
  295. )
  296. }
  297. // avg(
  298. //
  299. // avg_over_time(
  300. // kube_node_status_capacity_cpu_cores{
  301. // <some_custom_filter>
  302. // }[1h]
  303. // )
  304. //
  305. // ) by (cluster_id, node)
  306. func NewNodeCPUCoresCapacityMetricCollector() *metric.MetricCollector {
  307. return metric.NewMetricCollector(
  308. metric.NodeCPUCoresCapacityID,
  309. metric.KubeNodeStatusCapacityCPUCores,
  310. []string{
  311. source.NodeLabel,
  312. },
  313. aggregator.AverageOverTime,
  314. nil,
  315. )
  316. }
  317. // avg(
  318. // avg_over_time(
  319. // kube_node_status_allocatable_cpu_cores{
  320. // <some_custom_filter>
  321. // }[1h]
  322. // )
  323. // ) by (cluster_id, node)
  324. func NewNodeCPUCoresAllocatableMetricCollector() *metric.MetricCollector {
  325. return metric.NewMetricCollector(
  326. metric.NodeCPUCoresAllocatableID,
  327. metric.KubeNodeStatusAllocatableCPUCores,
  328. []string{
  329. source.NodeLabel,
  330. },
  331. aggregator.AverageOverTime,
  332. nil,
  333. )
  334. }
  335. // avg(
  336. // avg_over_time(
  337. // kube_node_status_capacity_memory_bytes{
  338. // <some_custom_filter>
  339. // }[1h]
  340. // )
  341. // ) by (cluster_id, node)
  342. func NewNodeRAMBytesCapacityMetricCollector() *metric.MetricCollector {
  343. return metric.NewMetricCollector(
  344. metric.NodeRAMBytesCapacityID,
  345. metric.KubeNodeStatusCapacityMemoryBytes,
  346. []string{
  347. source.NodeLabel,
  348. },
  349. aggregator.AverageOverTime,
  350. nil,
  351. )
  352. }
  353. // avg(
  354. // avg_over_time(
  355. // kube_node_status_allocatable_memory_bytes{
  356. // <some_custom_filter>
  357. // }[1h]
  358. // )
  359. // ) by (cluster_id, node)
  360. func NewNodeRAMBytesAllocatableMetricCollector() *metric.MetricCollector {
  361. return metric.NewMetricCollector(
  362. metric.NodeRAMBytesAllocatableID,
  363. metric.KubeNodeStatusAllocatableMemoryBytes,
  364. []string{
  365. source.NodeLabel,
  366. },
  367. aggregator.AverageOverTime,
  368. nil,
  369. )
  370. }
  371. // avg(
  372. // avg_over_time(
  373. // node_gpu_count{
  374. // <some_custom_filter>
  375. // }[1h]
  376. // )
  377. // ) by (cluster_id, node, provider_id)
  378. func NewNodeGPUCountMetricCollector() *metric.MetricCollector {
  379. return metric.NewMetricCollector(
  380. metric.NodeGPUCountID,
  381. metric.NodeGPUCount,
  382. []string{
  383. source.NodeLabel,
  384. source.ProviderIDLabel,
  385. },
  386. aggregator.AverageOverTime,
  387. nil,
  388. )
  389. }
  390. // avg_over_time(
  391. // kube_node_labels{
  392. // <some_custom_filter>
  393. // }[1h]
  394. // )
  395. func NewNodeLabelsMetricCollector() *metric.MetricCollector {
  396. return metric.NewMetricCollector(
  397. metric.NodeLabelsID,
  398. metric.KubeNodeLabels,
  399. []string{
  400. source.NodeLabel,
  401. },
  402. aggregator.Info,
  403. nil,
  404. )
  405. }
  406. // avg(
  407. // node_total_hourly_cost{
  408. // <some_custom_filter>
  409. // }
  410. // ) by (node, cluster_id, provider_id)[%s:%dm]
  411. func NewNodeActiveMinutesMetricCollector() *metric.MetricCollector {
  412. return metric.NewMetricCollector(
  413. metric.NodeActiveMinutesID,
  414. metric.NodeTotalHourlyCost,
  415. []string{
  416. source.NodeLabel,
  417. source.ProviderIDLabel,
  418. },
  419. aggregator.ActiveMinutes,
  420. nil,
  421. )
  422. }
  423. // sum(
  424. // rate(
  425. // node_cpu_seconds_total{
  426. // <some_custom_filter>
  427. // }[%s:%dm]
  428. // )
  429. // ) by (kubernetes_node, cluster_id, mode)
  430. func NewNodeCPUModeTotalMetricCollector() *metric.MetricCollector {
  431. return metric.NewMetricCollector(
  432. metric.NodeCPUModeTotalID,
  433. metric.NodeCPUSecondsTotal,
  434. []string{
  435. source.KubernetesNodeLabel,
  436. source.ModeLabel,
  437. },
  438. aggregator.Rate,
  439. nil,
  440. )
  441. }
  442. // avg(
  443. // avg_over_time(
  444. // container_memory_working_set_bytes{
  445. // container_name!="POD",
  446. // container_name!="",
  447. // namespace="kube-system",
  448. // <some_custom_filter>
  449. // }[%s:%dm]
  450. // )
  451. // ) by (instance, cluster_id)
  452. func NewNodeRAMSystemUsageAverageMetricCollector() *metric.MetricCollector {
  453. return metric.NewMetricCollector(
  454. metric.NodeRAMSystemUsageAverageID,
  455. metric.ContainerMemoryWorkingSetBytes,
  456. []string{
  457. source.InstanceLabel,
  458. },
  459. aggregator.AverageOverTime,
  460. func(labels map[string]string) bool {
  461. return labels[source.ContainerLabel] != "POD" && labels[source.ContainerLabel] != "" && labels[source.NamespaceLabel] == "kube-system"
  462. },
  463. )
  464. }
  465. // avg(
  466. // avg_over_time(
  467. // container_memory_working_set_bytes{
  468. // container_name!="POD",
  469. // container_name!="",
  470. // namespace!="kube-system",
  471. // <some_custom_filter>
  472. // }[%s:%dm]
  473. // )
  474. // ) by (instance, cluster_id)
  475. func NewNodeRAMUserUsageAverageMetricCollector() *metric.MetricCollector {
  476. return metric.NewMetricCollector(
  477. metric.NodeRAMUserUsageAverageID,
  478. metric.ContainerMemoryWorkingSetBytes,
  479. []string{
  480. source.InstanceLabel,
  481. },
  482. aggregator.AverageOverTime,
  483. func(labels map[string]string) bool {
  484. return labels[source.ContainerLabel] != "POD" && labels[source.ContainerLabel] != "" && labels[source.NamespaceLabel] != "kube-system"
  485. },
  486. )
  487. }
  488. // avg(
  489. // avg_over_time(
  490. // kubecost_load_balancer_cost{
  491. // <some_custom_filter>
  492. // }[1h]
  493. // )
  494. // ) by (namespace, service_name, ingress_ip, cluster_id)
  495. func NewLBPricePerHourMetricCollector() *metric.MetricCollector {
  496. return metric.NewMetricCollector(
  497. metric.LBPricePerHourID,
  498. metric.KubecostLoadBalancerCost,
  499. []string{
  500. source.NamespaceLabel,
  501. source.ServiceNameLabel,
  502. source.IngressIPLabel,
  503. },
  504. aggregator.AverageOverTime,
  505. nil,
  506. )
  507. }
  508. // avg(
  509. // kubecost_load_balancer_cost{
  510. // <some_custom_filter>
  511. // }
  512. // ) by (namespace, service_name, cluster_id, ingress_ip)[%s:%dm]
  513. func NewLBActiveMinutesMetricCollector() *metric.MetricCollector {
  514. return metric.NewMetricCollector(
  515. metric.LBActiveMinutesID,
  516. metric.KubecostLoadBalancerCost,
  517. []string{
  518. source.NamespaceLabel,
  519. source.ServiceNameLabel,
  520. source.IngressIPLabel,
  521. },
  522. aggregator.ActiveMinutes,
  523. nil,
  524. )
  525. }
  526. // avg(
  527. // kubecost_cluster_management_cost{
  528. // <some_custom_filter>
  529. // }
  530. // ) by (cluster_id, provisioner_name)[%s:%dm]
  531. func NewClusterManagementDurationMetricCollector() *metric.MetricCollector {
  532. return metric.NewMetricCollector(
  533. metric.ClusterManagementDurationID,
  534. metric.KubecostClusterManagementCost,
  535. []string{
  536. source.ProvisionerNameLabel,
  537. },
  538. aggregator.ActiveMinutes,
  539. nil,
  540. )
  541. }
  542. // avg(
  543. // avg_over_time(
  544. // kubecost_cluster_management_cost{
  545. // <some_custom_filter>
  546. // }[1h]
  547. // )
  548. // ) by (cluster_id, provisioner_name)
  549. func NewClusterManagementPricePerHourMetricCollector() *metric.MetricCollector {
  550. return metric.NewMetricCollector(
  551. metric.ClusterManagementPricePerHourID,
  552. metric.KubecostClusterManagementCost,
  553. []string{
  554. source.ProvisionerNameLabel,
  555. },
  556. aggregator.AverageOverTime,
  557. nil,
  558. )
  559. }
  560. // avg(
  561. // kube_pod_container_status_running{
  562. // <some_custom_filter>
  563. // } != 0
  564. // ) by (pod, namespace, uid, cluster_id)[%s:%s]
  565. func NewPodActiveMinutesMetricCollector() *metric.MetricCollector {
  566. return metric.NewMetricCollector(
  567. metric.PodActiveMinutesID,
  568. metric.KubePodContainerStatusRunning,
  569. []string{
  570. source.UIDLabel,
  571. source.NamespaceLabel,
  572. source.PodLabel,
  573. },
  574. aggregator.ActiveMinutes,
  575. nil,
  576. )
  577. }
  578. // avg(
  579. // avg_over_time(
  580. // container_memory_allocation_bytes{
  581. // container!="",
  582. // container!="POD",
  583. // node!="",
  584. // <some_custom_filter>
  585. // }[1h]
  586. // )
  587. // ) by (container, pod, namespace, node, cluster_id, provider_id)
  588. func NewRAMBytesAllocatedMetricCollector() *metric.MetricCollector {
  589. return metric.NewMetricCollector(
  590. metric.RAMBytesAllocatedID,
  591. metric.ContainerMemoryAllocationBytes,
  592. []string{
  593. source.NodeLabel,
  594. source.InstanceLabel,
  595. source.NamespaceLabel,
  596. source.PodLabel,
  597. source.ContainerLabel,
  598. },
  599. aggregator.AverageOverTime,
  600. func(labels map[string]string) bool {
  601. return labels[source.ContainerLabel] != "POD" && labels[source.ContainerLabel] != "" && labels[source.NodeLabel] != ""
  602. },
  603. )
  604. }
  605. // avg(
  606. // avg_over_time(
  607. // kube_pod_container_resource_requests{
  608. // resource="memory",
  609. // unit="byte",
  610. // container!="",
  611. // container!="POD",
  612. // node!="",
  613. // <some_custom_filter>
  614. // }[1h]
  615. // )
  616. //) by (container, pod, namespace, node, cluster_id)
  617. func NewRAMRequestsMetricCollector() *metric.MetricCollector {
  618. return metric.NewMetricCollector(
  619. metric.RAMRequestsID,
  620. metric.KubePodContainerResourceRequests,
  621. []string{
  622. source.NodeLabel,
  623. source.InstanceLabel,
  624. source.NamespaceLabel,
  625. source.PodLabel,
  626. source.ContainerLabel,
  627. },
  628. aggregator.AverageOverTime,
  629. func(labels map[string]string) bool {
  630. return labels[source.ResourceLabel] == "memory" && labels[source.UnitLabel] == "byte" && labels[source.ContainerLabel] != "POD" && labels[source.ContainerLabel] != "" && labels[source.NodeLabel] != ""
  631. },
  632. )
  633. }
  634. // avg(
  635. // avg_over_time(
  636. // container_memory_working_set_bytes{
  637. // container!="",
  638. // container!="POD",
  639. // <some_custom_filter>
  640. // }[1h]
  641. // )
  642. // ) by (container, pod, namespace, instance, cluster_id)
  643. func NewRAMUsageAverageMetricCollector() *metric.MetricCollector {
  644. return metric.NewMetricCollector(
  645. metric.RAMUsageAverageID,
  646. metric.ContainerMemoryWorkingSetBytes,
  647. []string{
  648. source.NodeLabel,
  649. source.InstanceLabel,
  650. source.NamespaceLabel,
  651. source.PodLabel,
  652. source.ContainerLabel,
  653. },
  654. aggregator.AverageOverTime,
  655. func(labels map[string]string) bool {
  656. return labels[source.ContainerLabel] != "POD" && labels[source.ContainerLabel] != ""
  657. },
  658. )
  659. }
  660. // max(
  661. // max_over_time(
  662. // container_memory_working_set_bytes{
  663. // container!="",
  664. // container_name!="POD",
  665. // container!="POD",
  666. // <some_custom_filter>
  667. // }[%s]
  668. // )
  669. // ) by (container_name, container, pod_name, pod, namespace, node, instance, %s)
  670. func NewRAMUsageMaxMetricCollector() *metric.MetricCollector {
  671. return metric.NewMetricCollector(
  672. metric.RAMUsageMaxID,
  673. metric.ContainerMemoryWorkingSetBytes,
  674. []string{
  675. source.NodeLabel,
  676. source.InstanceLabel,
  677. source.NamespaceLabel,
  678. source.PodLabel,
  679. source.ContainerLabel,
  680. },
  681. aggregator.MaxOverTime,
  682. func(labels map[string]string) bool {
  683. return labels[source.ContainerLabel] != "" && labels[source.ContainerLabel] != "POD" && labels[source.NodeLabel] != ""
  684. },
  685. )
  686. }
  687. // avg(
  688. // avg_over_time(
  689. // container_cpu_allocation{
  690. // container!="",
  691. // container!="POD",
  692. // node!="",
  693. // <some_custom_filter>
  694. // }[1h]
  695. // )
  696. // ) by (container, pod, namespace, node, cluster_id)
  697. func NewCPUCoresAllocatedMetricCollector() *metric.MetricCollector {
  698. return metric.NewMetricCollector(
  699. metric.CPUCoresAllocatedID,
  700. metric.ContainerCPUAllocation,
  701. []string{
  702. source.NodeLabel,
  703. source.InstanceLabel,
  704. source.NamespaceLabel,
  705. source.PodLabel,
  706. source.ContainerLabel,
  707. },
  708. aggregator.AverageOverTime,
  709. func(labels map[string]string) bool {
  710. return labels[source.ContainerLabel] != "POD" && labels[source.ContainerLabel] != "" && labels[source.NodeLabel] != ""
  711. },
  712. )
  713. }
  714. // avg(
  715. // avg_over_time(
  716. // kube_pod_container_resource_requests{
  717. // resource="cpu",
  718. // unit="core",
  719. // container!="",
  720. // container!="POD",
  721. // node!="",
  722. // <some_custom_filter>
  723. // }[1h]
  724. // )
  725. // ) by (container, pod, namespace, node, cluster_id)
  726. func NewCPURequestsMetricCollector() *metric.MetricCollector {
  727. return metric.NewMetricCollector(
  728. metric.CPURequestsID,
  729. metric.KubePodContainerResourceRequests,
  730. []string{
  731. source.NodeLabel,
  732. source.InstanceLabel,
  733. source.NamespaceLabel,
  734. source.PodLabel,
  735. source.ContainerLabel,
  736. },
  737. aggregator.AverageOverTime,
  738. func(labels map[string]string) bool {
  739. return labels[source.ResourceLabel] == "cpu" && labels[source.UnitLabel] == "core" && labels[source.ContainerLabel] != "POD" && labels[source.ContainerLabel] != "" && labels[source.NodeLabel] != ""
  740. },
  741. )
  742. }
  743. // avg(
  744. // rate(
  745. // container_cpu_usage_seconds_total{
  746. // container!="",
  747. // container_name!="POD",
  748. // container!="POD",
  749. // <some_custom_filter>
  750. // }[1h]
  751. // )
  752. // ) by (container_name, container, pod_name, pod, namespace, node, instance, cluster_id)
  753. func NewCPUUsageAverageMetricCollector() *metric.MetricCollector {
  754. return metric.NewMetricCollector(
  755. metric.CPUUsageAverageID,
  756. metric.ContainerCPUUsageSecondsTotal,
  757. []string{
  758. source.NodeLabel,
  759. source.InstanceLabel,
  760. source.NamespaceLabel,
  761. source.PodLabel,
  762. source.ContainerLabel,
  763. },
  764. aggregator.Rate,
  765. func(labels map[string]string) bool {
  766. return labels[source.ContainerLabel] != "" && labels[source.ContainerLabel] != "POD"
  767. },
  768. )
  769. }
  770. // max(
  771. //
  772. // max_over_time(
  773. // irate(
  774. // container_cpu_usage_seconds_total{
  775. // container!="POD",
  776. // container!="",
  777. // <some_custom_filter>
  778. // }[1h]
  779. // )[%s:%s]
  780. // )
  781. //
  782. // ) by (container, pod_name, pod, namespace, node, instance, cluster_id)
  783. func NewCPUUsageMaxMetricCollector() *metric.MetricCollector {
  784. return metric.NewMetricCollector(
  785. metric.CPUUsageMaxID,
  786. metric.ContainerCPUUsageSecondsTotal,
  787. []string{
  788. source.NodeLabel,
  789. source.InstanceLabel,
  790. source.NamespaceLabel,
  791. source.PodLabel,
  792. source.ContainerLabel,
  793. },
  794. aggregator.IRateMax,
  795. func(labels map[string]string) bool {
  796. return labels[source.ContainerLabel] != "" && labels[source.ContainerLabel] != "POD"
  797. },
  798. )
  799. }
  800. // avg(
  801. // avg_over_time(
  802. // kube_pod_container_resource_requests{
  803. // resource="nvidia_com_gpu",
  804. // container!="",
  805. // container!="POD",
  806. // node!="",
  807. // <some_custom_filter>
  808. // }[1h]
  809. // )
  810. // ) by (container, pod, namespace, node, cluster_id)
  811. func NewGPUsRequestedMetricCollector() *metric.MetricCollector {
  812. return metric.NewMetricCollector(
  813. metric.GPUsRequestedID,
  814. metric.KubePodContainerResourceRequests,
  815. []string{
  816. source.NamespaceLabel,
  817. source.PodLabel,
  818. source.ContainerLabel,
  819. },
  820. aggregator.AverageOverTime,
  821. func(labels map[string]string) bool {
  822. return labels[source.ResourceLabel] == "nvidia_com_gpu" && labels[source.ContainerLabel] != "POD" && labels[source.ContainerLabel] != "" && labels[source.NodeLabel] != ""
  823. },
  824. )
  825. }
  826. // avg(
  827. // avg_over_time(
  828. // DCGM_FI_PROF_GR_ENGINE_ACTIVE{
  829. // container!=""
  830. // }[1h]
  831. // )
  832. // ) by (container, pod, namespace, cluster_id)
  833. func NewGPUsUsageAverageMetricCollector() *metric.MetricCollector {
  834. return metric.NewMetricCollector(
  835. metric.GPUsUsageAverageID,
  836. metric.DCGMFIPROFGRENGINEACTIVE,
  837. []string{
  838. source.NamespaceLabel,
  839. source.PodLabel,
  840. source.ContainerLabel,
  841. },
  842. aggregator.AverageOverTime,
  843. func(labels map[string]string) bool {
  844. return labels[source.ContainerLabel] != ""
  845. },
  846. )
  847. }
  848. // max(
  849. // max_over_time(
  850. // DCGM_FI_PROF_GR_ENGINE_ACTIVE{
  851. // container!=""
  852. // }[1h]
  853. // )
  854. // ) by (container, pod, namespace, cluster_id)
  855. func NewGPUsUsageMaxMetricCollector() *metric.MetricCollector {
  856. return metric.NewMetricCollector(
  857. metric.GPUsUsageMaxID,
  858. metric.DCGMFIPROFGRENGINEACTIVE,
  859. []string{
  860. source.NamespaceLabel,
  861. source.PodLabel,
  862. source.ContainerLabel,
  863. },
  864. aggregator.MaxOverTime,
  865. func(labels map[string]string) bool {
  866. return labels[source.ContainerLabel] != ""
  867. },
  868. )
  869. }
  870. // avg(
  871. // avg_over_time(
  872. // container_gpu_allocation{
  873. // container!="",
  874. // container!="POD",
  875. // node!="",
  876. // <some_custom_filter>
  877. // }[1h]
  878. // )
  879. // ) by (container, pod, namespace, node, cluster_id)
  880. func NewGPUsAllocatedMetricCollector() *metric.MetricCollector {
  881. return metric.NewMetricCollector(
  882. metric.GPUsAllocatedID,
  883. metric.ContainerGPUAllocation,
  884. []string{
  885. source.NamespaceLabel,
  886. source.PodLabel,
  887. source.ContainerLabel,
  888. },
  889. aggregator.AverageOverTime,
  890. func(labels map[string]string) bool {
  891. return labels[source.ContainerLabel] != "" && labels[source.ContainerLabel] != "POD" && labels[source.NodeLabel] != ""
  892. },
  893. )
  894. }
  895. // avg(
  896. // avg_over_time(
  897. // kube_pod_container_resource_requests{
  898. // container!="",
  899. // node != "",
  900. // pod != "",
  901. // container!= "",
  902. // unit = "integer",
  903. // <some_custom_filter>
  904. // }[1h]
  905. // )
  906. // ) by (container, pod, namespace, node, resource, cluster_id)
  907. func NewIsGPUSharedMetricCollector() *metric.MetricCollector {
  908. return metric.NewMetricCollector(
  909. metric.IsGPUSharedID,
  910. metric.KubePodContainerResourceRequests,
  911. []string{
  912. source.NamespaceLabel,
  913. source.PodLabel,
  914. source.ContainerLabel,
  915. source.ResourceLabel,
  916. },
  917. aggregator.AverageOverTime,
  918. func(labels map[string]string) bool {
  919. return labels[source.ContainerLabel] != "" && labels[source.NodeLabel] != "" && labels[source.PodLabel] != "" && labels[source.UnitLabel] == "integer"
  920. },
  921. )
  922. }
  923. // avg(
  924. // avg_over_time(
  925. // DCGM_FI_DEV_DEC_UTIL{
  926. // container!="",
  927. // <some_custom_filter>
  928. // }[1h]
  929. // )
  930. // ) by (container, pod, namespace, device, modelName, UUID, cluster_id)
  931. func NewGPUInfoMetricCollector() *metric.MetricCollector {
  932. return metric.NewMetricCollector(
  933. metric.GPUInfoID,
  934. metric.DCGMFIDEVDECUTIL,
  935. []string{
  936. source.NamespaceLabel,
  937. source.PodLabel,
  938. source.ContainerLabel,
  939. source.DeviceLabel,
  940. source.ModelNameLabel,
  941. source.UUIDLabel,
  942. },
  943. aggregator.Info,
  944. func(labels map[string]string) bool {
  945. return labels[source.ContainerLabel] != ""
  946. },
  947. )
  948. }
  949. // avg(
  950. // avg_over_time(
  951. // node_cpu_hourly_cost{
  952. // <some_custom_filter>
  953. // }[1h]
  954. // )
  955. // ) by (node, cluster_id, instance_type, provider_id)
  956. func NewNodeCPUPricePerHourMetricCollector() *metric.MetricCollector {
  957. return metric.NewMetricCollector(
  958. metric.NodeCPUPricePerHourID,
  959. metric.NodeCPUHourlyCost,
  960. []string{
  961. source.NodeLabel,
  962. source.InstanceTypeLabel,
  963. source.ProviderIDLabel,
  964. },
  965. aggregator.AverageOverTime,
  966. nil,
  967. )
  968. }
  969. // avg(
  970. // avg_over_time(
  971. // node_ram_hourly_cost{
  972. // <some_custom_filter>
  973. // }[1h]
  974. // )
  975. // ) by (node, cluster_id, instance_type, provider_id)
  976. func NewNodeRAMPricePerGiBHourMetricCollector() *metric.MetricCollector {
  977. return metric.NewMetricCollector(
  978. metric.NodeRAMPricePerGiBHourID,
  979. metric.NodeRAMHourlyCost,
  980. []string{
  981. source.NodeLabel,
  982. source.InstanceTypeLabel,
  983. source.ProviderIDLabel,
  984. },
  985. aggregator.AverageOverTime,
  986. nil,
  987. )
  988. }
  989. // avg(
  990. // avg_over_time(
  991. // node_gpu_hourly_cost{
  992. // <some_custom_filter>
  993. // }[1h]
  994. // )
  995. // ) by (node, cluster_id, instance_type, provider_id)
  996. func NewNodeGPUPricePerHourMetricCollector() *metric.MetricCollector {
  997. return metric.NewMetricCollector(
  998. metric.NodeGPUPricePerHourID,
  999. metric.NodeGPUHourlyCost,
  1000. []string{
  1001. source.NodeLabel,
  1002. source.InstanceTypeLabel,
  1003. source.ProviderIDLabel,
  1004. },
  1005. aggregator.AverageOverTime,
  1006. nil,
  1007. )
  1008. }
  1009. // avg_over_time(
  1010. // kubecost_node_is_spot{
  1011. // <some_custom_filter>
  1012. // }[1h]
  1013. // )
  1014. func NewNodeIsSpotMetricCollector() *metric.MetricCollector {
  1015. return metric.NewMetricCollector(
  1016. metric.NodeIsSpotID,
  1017. metric.KubecostNodeIsSpot,
  1018. []string{
  1019. source.NodeLabel,
  1020. source.ProviderIDLabel,
  1021. },
  1022. aggregator.AverageOverTime,
  1023. nil,
  1024. )
  1025. }
  1026. // avg(
  1027. // avg_over_time(
  1028. // pod_pvc_allocation{
  1029. // <some_custom_filter>
  1030. // }[1h]
  1031. // )
  1032. // ) by (persistentvolume, persistentvolumeclaim, pod, namespace, cluster_id)
  1033. func NewPodPVCAllocationMetricCollector() *metric.MetricCollector {
  1034. return metric.NewMetricCollector(
  1035. metric.PodPVCAllocationID,
  1036. metric.PodPVCAllocation,
  1037. []string{
  1038. source.NamespaceLabel,
  1039. source.PodLabel,
  1040. source.PVLabel,
  1041. source.PVCLabel,
  1042. },
  1043. aggregator.AverageOverTime,
  1044. nil,
  1045. )
  1046. }
  1047. // avg(
  1048. // avg_over_time(
  1049. // kube_persistentvolumeclaim_resource_requests_storage_bytes{
  1050. // <some_custom_filter>
  1051. // }[1h]
  1052. // )
  1053. // ) by (persistentvolumeclaim, namespace, cluster_id)
  1054. func NewPVCBytesRequestedMetricCollector() *metric.MetricCollector {
  1055. return metric.NewMetricCollector(
  1056. metric.PVCBytesRequestedID,
  1057. metric.KubePersistentVolumeClaimResourceRequestsStorageBytes,
  1058. []string{
  1059. source.NamespaceLabel,
  1060. source.PVCLabel,
  1061. },
  1062. aggregator.AverageOverTime,
  1063. nil,
  1064. )
  1065. }
  1066. // avg(
  1067. // avg_over_time(
  1068. // kube_persistentvolume_capacity_bytes{
  1069. // <some_custom_filter>
  1070. // }[1h]
  1071. // )
  1072. // ) by (persistentvolume, cluster_id)
  1073. func NewPVBytesMetricCollector() *metric.MetricCollector {
  1074. return metric.NewMetricCollector(
  1075. metric.PVBytesID,
  1076. metric.KubePersistentVolumeCapacityBytes,
  1077. []string{
  1078. source.PVLabel,
  1079. },
  1080. aggregator.AverageOverTime,
  1081. nil,
  1082. )
  1083. }
  1084. // avg(
  1085. // avg_over_time(
  1086. // kubecost_pv_info{
  1087. // <some_custom_filter>
  1088. // }[1h]
  1089. // )
  1090. // ) by (cluster_id, storageclass, persistentvolume, provider_id)
  1091. func NewPVInfoMetricCollector() *metric.MetricCollector {
  1092. return metric.NewMetricCollector(
  1093. metric.PVInfoID,
  1094. metric.KubecostPVInfo,
  1095. []string{
  1096. source.PVLabel,
  1097. source.StorageClassLabel,
  1098. source.ProviderIDLabel,
  1099. },
  1100. aggregator.AverageOverTime,
  1101. nil,
  1102. )
  1103. }
  1104. // sum(
  1105. // increase(
  1106. // kubecost_pod_network_egress_bytes_total{
  1107. // internet="false",
  1108. // same_zone="false",
  1109. // same_region="true",
  1110. // <some_custom_filter>
  1111. // }[1h]
  1112. // )
  1113. // ) by (pod_name, namespace, cluster_id) / 1024 / 1024 / 1024
  1114. //
  1115. func NewNetZoneGiBMetricCollector() *metric.MetricCollector {
  1116. return metric.NewMetricCollector(
  1117. metric.NetZoneGiBID,
  1118. metric.KubecostPodNetworkEgressBytesTotal,
  1119. []string{
  1120. source.NamespaceLabel,
  1121. source.PodNameLabel,
  1122. },
  1123. aggregator.Increase,
  1124. func(labels map[string]string) bool {
  1125. return labels[source.InternetLabel] == "false" && labels[source.SameZoneLabel] == "false" && labels[source.SameRegionLabel] == "true"
  1126. },
  1127. )
  1128. }
  1129. // avg(
  1130. // avg_over_time(
  1131. // kubecost_network_zone_egress_cost{
  1132. // <some_custom_filter>
  1133. // }[1h]
  1134. // )
  1135. // ) by (cluster_id)
  1136. //
  1137. func NewNetZonePricePerGiBMetricCollector() *metric.MetricCollector {
  1138. return metric.NewMetricCollector(
  1139. metric.NetZonePricePerGiBID,
  1140. metric.KubecostNetworkZoneEgressCost,
  1141. []string{},
  1142. aggregator.AverageOverTime,
  1143. nil,
  1144. )
  1145. }
  1146. // sum(
  1147. // increase(
  1148. // kubecost_pod_network_egress_bytes_total{
  1149. // internet="false",
  1150. // same_zone="false",
  1151. // same_region="false",
  1152. // <some_custom_filter>
  1153. // }[1h]
  1154. // )
  1155. // ) by (pod_name, namespace, cluster_id) / 1024 / 1024 / 1024
  1156. func NewNetRegionGiBMetricCollector() *metric.MetricCollector {
  1157. return metric.NewMetricCollector(
  1158. metric.NetRegionGiBID,
  1159. metric.KubecostPodNetworkEgressBytesTotal,
  1160. []string{
  1161. source.NamespaceLabel,
  1162. source.PodNameLabel,
  1163. },
  1164. aggregator.Increase,
  1165. func(labels map[string]string) bool {
  1166. return labels[source.InternetLabel] == "false" && labels[source.SameZoneLabel] == "false" && labels[source.SameRegionLabel] == "false"
  1167. },
  1168. )
  1169. }
  1170. // avg(
  1171. // avg_over_time(
  1172. // kubecost_network_region_egress_cost{
  1173. // <some_custom_filter>
  1174. // }[1h]
  1175. // )
  1176. // ) by (cluster_id)
  1177. func NewNetRegionPricePerGiBMetricCollector() *metric.MetricCollector {
  1178. return metric.NewMetricCollector(
  1179. metric.NetRegionPricePerGiBID,
  1180. metric.KubecostNetworkRegionEgressCost,
  1181. []string{},
  1182. aggregator.AverageOverTime,
  1183. nil,
  1184. )
  1185. }
  1186. // sum(
  1187. // increase(
  1188. // kubecost_pod_network_egress_bytes_total{
  1189. // internet="true",
  1190. // <some_custom_filter>
  1191. // }[1h]
  1192. // )
  1193. // ) by (pod_name, namespace, cluster_id) / 1024 / 1024 / 1024
  1194. func NewNetInternetGiBMetricCollector() *metric.MetricCollector {
  1195. return metric.NewMetricCollector(
  1196. metric.NetInternetGiBID,
  1197. metric.KubecostPodNetworkEgressBytesTotal,
  1198. []string{
  1199. source.NamespaceLabel,
  1200. source.PodNameLabel,
  1201. },
  1202. aggregator.Increase,
  1203. func(labels map[string]string) bool {
  1204. return labels[source.InternetLabel] == "true"
  1205. },
  1206. )
  1207. }
  1208. // avg(
  1209. // avg_over_time(
  1210. // kubecost_network_internet_egress_cost{
  1211. // <some_custom_filter>
  1212. // }[1h]
  1213. // )
  1214. // ) by (cluster_id)
  1215. func NewNetInternetPricePerGiBMetricCollector() *metric.MetricCollector {
  1216. return metric.NewMetricCollector(
  1217. metric.NetInternetPricePerGiBID,
  1218. metric.KubecostNetworkInternetEgressCost,
  1219. []string{},
  1220. aggregator.AverageOverTime,
  1221. nil,
  1222. )
  1223. }
  1224. // sum(
  1225. // increase(
  1226. // kubecost_pod_network_egress_bytes_total{
  1227. // internet="true",
  1228. // <some_custom_filter>
  1229. // }[%s]
  1230. // )
  1231. // ) by (pod_name, namespace, service, %s) / 1024 / 1024 / 1024
  1232. func NewNetInternetServiceGiBMetricCollector() *metric.MetricCollector {
  1233. return metric.NewMetricCollector(
  1234. metric.NetInternetServiceGiBID,
  1235. metric.KubecostPodNetworkEgressBytesTotal,
  1236. []string{
  1237. source.NamespaceLabel,
  1238. source.PodNameLabel,
  1239. source.ServiceLabel,
  1240. },
  1241. aggregator.Increase,
  1242. func(labels map[string]string) bool {
  1243. return labels[source.InternetLabel] == "true"
  1244. },
  1245. )
  1246. }
  1247. // sum(
  1248. // increase(
  1249. // container_network_receive_bytes_total{
  1250. // pod!="",
  1251. // <some_custom_filter>
  1252. // }[1h]
  1253. // )
  1254. // ) by (pod_name, pod, namespace, cluster_id)
  1255. func NewNetReceiveBytesMetricCollector() *metric.MetricCollector {
  1256. return metric.NewMetricCollector(
  1257. metric.NetReceiveBytesID,
  1258. metric.ContainerNetworkReceiveBytesTotal,
  1259. []string{
  1260. source.NamespaceLabel,
  1261. source.PodLabel,
  1262. },
  1263. aggregator.Increase,
  1264. func(labels map[string]string) bool {
  1265. return labels[source.PodLabel] != ""
  1266. },
  1267. )
  1268. }
  1269. // sum(
  1270. // increase(
  1271. // kubecost_pod_network_ingress_bytes_total{
  1272. // internet="false",
  1273. // same_zone="false",
  1274. // same_region="true",
  1275. // <some_custom_filter>
  1276. // }[1h]
  1277. // )
  1278. // ) by (pod_name, namespace, cluster_id) / 1024 / 1024 / 1024
  1279. func NewNetZoneIngressGiBMetricCollector() *metric.MetricCollector {
  1280. return metric.NewMetricCollector(
  1281. metric.NetZoneIngressGiBID,
  1282. metric.KubecostPodNetworkIngressBytesTotal,
  1283. []string{
  1284. source.NamespaceLabel,
  1285. source.PodNameLabel,
  1286. },
  1287. aggregator.Increase,
  1288. func(labels map[string]string) bool {
  1289. return labels[source.InternetLabel] == "false" &&
  1290. labels[source.SameZoneLabel] == "false" &&
  1291. labels[source.SameRegionLabel] == "true"
  1292. },
  1293. )
  1294. }
  1295. // sum(
  1296. // increase(
  1297. // kubecost_pod_network_ingress_bytes_total{
  1298. // internet="false",
  1299. // same_zone="false",
  1300. // same_region="false",
  1301. // <some_custom_filter>
  1302. // }[1h]
  1303. // )
  1304. // ) by (pod_name, namespace, cluster_id) / 1024 / 1024 / 1024
  1305. func NewNetRegionIngressGiBMetricCollector() *metric.MetricCollector {
  1306. return metric.NewMetricCollector(
  1307. metric.NetRegionIngressGiBID,
  1308. metric.KubecostPodNetworkIngressBytesTotal,
  1309. []string{
  1310. source.NamespaceLabel,
  1311. source.PodNameLabel,
  1312. },
  1313. aggregator.Increase,
  1314. func(labels map[string]string) bool {
  1315. return labels[source.InternetLabel] == "false" &&
  1316. labels[source.SameZoneLabel] == "false" &&
  1317. labels[source.SameRegionLabel] == "false"
  1318. },
  1319. )
  1320. }
  1321. // sum(
  1322. // increase(
  1323. // kubecost_pod_network_ingress_bytes_total{
  1324. // internet="true",
  1325. // <some_custom_filter>
  1326. // }[1h]
  1327. // )
  1328. // ) by (pod_name, namespace, cluster_id) / 1024 / 1024 / 1024
  1329. func NewNetInternetIngressGiBMetricCollector() *metric.MetricCollector {
  1330. return metric.NewMetricCollector(
  1331. metric.NetInternetIngressGiBID,
  1332. metric.KubecostPodNetworkIngressBytesTotal,
  1333. []string{
  1334. source.NamespaceLabel,
  1335. source.PodNameLabel,
  1336. },
  1337. aggregator.Increase,
  1338. func(labels map[string]string) bool {
  1339. return labels[source.InternetLabel] == "true"
  1340. },
  1341. )
  1342. }
  1343. // `sum(
  1344. // increase(
  1345. // kubecost_pod_network_ingress_bytes_total{
  1346. // internet="true",
  1347. // <some_custom_filter>
  1348. // }[1h]
  1349. // )
  1350. // ) by (pod_name, namespace, service, cluster_id) / 1024 / 1024 / 1024
  1351. func NewNetInternetServiceIngressGiBMetricCollector() *metric.MetricCollector {
  1352. return metric.NewMetricCollector(
  1353. metric.NetInternetServiceIngressGiBID,
  1354. metric.KubecostPodNetworkIngressBytesTotal,
  1355. []string{
  1356. source.NamespaceLabel,
  1357. source.PodNameLabel,
  1358. source.ServiceLabel,
  1359. },
  1360. aggregator.Increase,
  1361. func(labels map[string]string) bool {
  1362. return labels[source.InternetLabel] == "true"
  1363. },
  1364. )
  1365. }
  1366. // sum(
  1367. // increase(
  1368. // container_network_transmit_bytes_total{
  1369. // pod!="",
  1370. // <some_custom_filter>
  1371. // }[1h]
  1372. // )
  1373. // ) by (pod_name, pod, namespace, cluster_id)
  1374. func NewNetTransferBytesMetricCollector() *metric.MetricCollector {
  1375. return metric.NewMetricCollector(
  1376. metric.NetTransferBytesID,
  1377. metric.ContainerNetworkTransmitBytesTotal,
  1378. []string{
  1379. source.NamespaceLabel,
  1380. source.PodLabel,
  1381. },
  1382. aggregator.Increase,
  1383. func(labels map[string]string) bool {
  1384. return labels[source.PodLabel] != ""
  1385. },
  1386. )
  1387. }
  1388. // avg_over_time(
  1389. // kube_namespace_labels{
  1390. // <some_custom_filter>
  1391. // }[1h]
  1392. // )
  1393. func NewNamespaceLabelsMetricCollector() *metric.MetricCollector {
  1394. return metric.NewMetricCollector(
  1395. metric.NamespaceLabelsID,
  1396. metric.KubeNamespaceLabels,
  1397. []string{
  1398. source.NamespaceLabel,
  1399. },
  1400. aggregator.Info,
  1401. nil,
  1402. )
  1403. }
  1404. // avg_over_time(
  1405. // kube_namespace_annotations{
  1406. // <some_custom_filter>
  1407. // }[1h]
  1408. // )
  1409. func NewNamespaceAnnotationsMetricCollector() *metric.MetricCollector {
  1410. return metric.NewMetricCollector(
  1411. metric.NamespaceAnnotationsID,
  1412. metric.KubeNamespaceAnnotations,
  1413. []string{
  1414. source.NamespaceLabel,
  1415. },
  1416. aggregator.Info,
  1417. nil,
  1418. )
  1419. }
  1420. // avg_over_time(
  1421. // kube_pod_labels{
  1422. // <some_custom_filter>
  1423. // }[1h]
  1424. // )
  1425. func NewPodLabelsMetricCollector() *metric.MetricCollector {
  1426. return metric.NewMetricCollector(
  1427. metric.PodLabelsID,
  1428. metric.KubePodLabels,
  1429. []string{
  1430. source.NamespaceLabel,
  1431. source.PodLabel,
  1432. },
  1433. aggregator.Info,
  1434. nil,
  1435. )
  1436. }
  1437. // avg_over_time(
  1438. // kube_pod_annotations{
  1439. // <some_custom_filter>
  1440. // }[1h]
  1441. // )
  1442. func NewPodAnnotationsMetricCollector() *metric.MetricCollector {
  1443. return metric.NewMetricCollector(
  1444. metric.PodAnnotationsID,
  1445. metric.KubePodAnnotations,
  1446. []string{
  1447. source.NamespaceLabel,
  1448. source.PodLabel,
  1449. },
  1450. aggregator.Info,
  1451. nil,
  1452. )
  1453. }
  1454. // avg_over_time(
  1455. // service_selector_labels{
  1456. // <some_custom_filter>
  1457. // }[1h]
  1458. // )
  1459. func NewServiceLabelsMetricCollector() *metric.MetricCollector {
  1460. return metric.NewMetricCollector(
  1461. metric.ServiceLabelsID,
  1462. metric.ServiceSelectorLabels,
  1463. []string{
  1464. source.NamespaceLabel,
  1465. source.ServiceLabel,
  1466. },
  1467. aggregator.Info,
  1468. nil,
  1469. )
  1470. }
  1471. // avg_over_time(
  1472. // deployment_match_labels{
  1473. // <some_custom_filter>
  1474. // }[1h]
  1475. // )
  1476. func NewDeploymentLabelsMetricCollector() *metric.MetricCollector {
  1477. return metric.NewMetricCollector(
  1478. metric.DeploymentLabelsID,
  1479. metric.DeploymentMatchLabels,
  1480. []string{
  1481. source.NamespaceLabel,
  1482. source.DeploymentLabel,
  1483. },
  1484. aggregator.Info,
  1485. nil,
  1486. )
  1487. }
  1488. // avg_over_time(
  1489. // statefulSet_match_labels{
  1490. // <some_custom_filter>
  1491. // }[1h]
  1492. // )
  1493. func NewStatefulSetLabelsMetricCollector() *metric.MetricCollector {
  1494. return metric.NewMetricCollector(
  1495. metric.StatefulSetLabelsID,
  1496. metric.StatefulSetMatchLabels,
  1497. []string{
  1498. source.NamespaceLabel,
  1499. source.StatefulSetLabel,
  1500. },
  1501. aggregator.Info,
  1502. nil,
  1503. )
  1504. }
  1505. // sum(
  1506. // avg_over_time(
  1507. // kube_pod_owner{
  1508. // owner_kind="DaemonSet",
  1509. // <some_custom_filter>
  1510. // }[1h]
  1511. // )
  1512. // ) by (pod, owner_name, namespace, cluster_id)
  1513. func NewDaemonSetLabelsMetricCollector() *metric.MetricCollector {
  1514. return metric.NewMetricCollector(
  1515. metric.DaemonSetLabelsID,
  1516. metric.KubePodOwner,
  1517. []string{
  1518. source.NamespaceLabel,
  1519. source.PodLabel,
  1520. source.OwnerNameLabel,
  1521. },
  1522. aggregator.Info,
  1523. func(labels map[string]string) bool {
  1524. return labels[source.OwnerKindLabel] == "DaemonSet"
  1525. },
  1526. )
  1527. }
  1528. // sum(
  1529. // avg_over_time(
  1530. // kube_pod_owner{
  1531. // owner_kind="Job",
  1532. // <some_custom_filter>
  1533. // }[1h]
  1534. // )
  1535. // ) by (pod, owner_name, namespace, cluster_id)
  1536. func NewJobLabelsMetricCollector() *metric.MetricCollector {
  1537. return metric.NewMetricCollector(
  1538. metric.JobLabelsID,
  1539. metric.KubePodOwner,
  1540. []string{
  1541. source.NamespaceLabel,
  1542. source.PodLabel,
  1543. source.OwnerNameLabel,
  1544. },
  1545. aggregator.Info,
  1546. func(labels map[string]string) bool {
  1547. return labels[source.OwnerKindLabel] == "Job"
  1548. },
  1549. )
  1550. }
  1551. // sum(
  1552. // avg_over_time(
  1553. // kube_pod_owner{
  1554. // owner_kind="ReplicaSet",
  1555. // <some_custom_filter>
  1556. // }[1h]
  1557. // )
  1558. // ) by (pod, owner_name, namespace, cluster_id)
  1559. func NewPodsWithReplicaSetOwnerMetricCollector() *metric.MetricCollector {
  1560. return metric.NewMetricCollector(
  1561. metric.PodsWithReplicaSetOwnerID,
  1562. metric.KubePodOwner,
  1563. []string{
  1564. source.NamespaceLabel,
  1565. source.PodLabel,
  1566. source.OwnerNameLabel,
  1567. },
  1568. aggregator.Info,
  1569. func(labels map[string]string) bool {
  1570. return labels[source.OwnerKindLabel] == "ReplicaSet"
  1571. },
  1572. )
  1573. }
  1574. // sum(
  1575. // avg_over_time(
  1576. // kube_replicaset_owner{
  1577. // owner_kind="<none>",
  1578. // owner_name="<none>",
  1579. // <some_custom_filter>
  1580. // }[1h]
  1581. // )
  1582. // ) by (replicaset, namespace, cluster_id)
  1583. func NewReplicaSetsWithoutOwnersMetricCollector() *metric.MetricCollector {
  1584. return metric.NewMetricCollector(
  1585. metric.ReplicaSetsWithoutOwnersID,
  1586. metric.KubeReplicasetOwner,
  1587. []string{
  1588. source.NamespaceLabel,
  1589. source.ReplicaSetLabel,
  1590. },
  1591. aggregator.Info,
  1592. func(labels map[string]string) bool {
  1593. return labels[source.OwnerKindLabel] == "<none>" && labels[source.OwnerNameLabel] == "<none>"
  1594. },
  1595. )
  1596. }
  1597. // sum(
  1598. // avg_over_time(
  1599. // kube_replicaset_owner{
  1600. // owner_kind="Rollout",
  1601. // <some_custom_filter>
  1602. // }[1h]
  1603. // )
  1604. // ) by (replicaset, namespace, owner_kind, owner_name, cluster_id)
  1605. func NewReplicaSetsWithRolloutMetricCollector() *metric.MetricCollector {
  1606. return metric.NewMetricCollector(
  1607. metric.ReplicaSetsWithRolloutID,
  1608. metric.KubeReplicasetOwner,
  1609. []string{
  1610. source.NamespaceLabel,
  1611. source.ReplicaSetLabel,
  1612. source.OwnerNameLabel,
  1613. source.OwnerKindLabel,
  1614. },
  1615. aggregator.Info,
  1616. func(labels map[string]string) bool {
  1617. return labels[source.OwnerKindLabel] == "Rollout"
  1618. },
  1619. )
  1620. }