podmetrics.go 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. package metrics
  2. import (
  3. "fmt"
  4. "github.com/kubecost/cost-model/pkg/clustercache"
  5. "github.com/kubecost/cost-model/pkg/log"
  6. "github.com/kubecost/cost-model/pkg/prom"
  7. "github.com/prometheus/client_golang/prometheus"
  8. dto "github.com/prometheus/client_model/go"
  9. v1 "k8s.io/api/core/v1"
  10. )
  11. //--------------------------------------------------------------------------
  12. // KubecostPodCollector
  13. //--------------------------------------------------------------------------
  14. // KubecostPodCollector is a prometheus collector that emits pod metrics
  15. type KubecostPodCollector struct {
  16. KubeClusterCache clustercache.ClusterCache
  17. }
  18. // Describe sends the super-set of all possible descriptors of metrics
  19. // collected by this Collector.
  20. func (kpmc KubecostPodCollector) Describe(ch chan<- *prometheus.Desc) {
  21. ch <- prometheus.NewDesc("kube_pod_annotations", "All annotations for each pod prefix with annotation_", []string{}, nil)
  22. }
  23. // Collect is called by the Prometheus registry when collecting metrics.
  24. func (kpmc KubecostPodCollector) Collect(ch chan<- prometheus.Metric) {
  25. pods := kpmc.KubeClusterCache.GetAllPods()
  26. for _, pod := range pods {
  27. podName := pod.GetName()
  28. podNS := pod.GetNamespace()
  29. // Pod Annotations
  30. labels, values := prom.KubeAnnotationsToLabels(pod.Annotations)
  31. if len(labels) > 0 {
  32. ch <- newPodAnnotationMetric("kube_pod_annotations", podNS, podName, labels, values)
  33. }
  34. }
  35. }
  36. //--------------------------------------------------------------------------
  37. // KubePodCollector
  38. //--------------------------------------------------------------------------
  39. // KubePodMetricCollector is a prometheus collector that emits pod metrics
  40. type KubePodCollector struct {
  41. KubeClusterCache clustercache.ClusterCache
  42. }
  43. // Describe sends the super-set of all possible descriptors of metrics
  44. // collected by this Collector.
  45. func (kpmc KubePodCollector) Describe(ch chan<- *prometheus.Desc) {
  46. ch <- prometheus.NewDesc("kube_pod_labels", "All labels for each pod prefixed with label_", []string{}, nil)
  47. ch <- prometheus.NewDesc("kube_pod_owner", "Information about the Pod's owner", []string{}, nil)
  48. ch <- prometheus.NewDesc("kube_pod_container_status_running", "Describes whether the container is currently in running state", []string{}, nil)
  49. ch <- prometheus.NewDesc("kube_pod_container_status_terminated_reason", "Describes the reason the container is currently in terminated state.", []string{}, nil)
  50. ch <- prometheus.NewDesc("kube_pod_container_status_restarts_total", "The number of container restarts per container.", []string{}, nil)
  51. ch <- prometheus.NewDesc("kube_pod_container_resource_requests", "The number of requested resource by a container", []string{}, nil)
  52. ch <- prometheus.NewDesc("kube_pod_container_resource_limits", "The number of requested limit resource by a container.", []string{}, nil)
  53. ch <- prometheus.NewDesc("kube_pod_container_resource_limits_cpu_cores", "The number of requested limit cpu core resource by a container.", []string{}, nil)
  54. ch <- prometheus.NewDesc("kube_pod_container_resource_limits_memory_bytes", "The number of requested limit memory resource by a container.", []string{}, nil)
  55. ch <- prometheus.NewDesc("kube_pod_status_phase", "The pods current phase.", []string{}, nil)
  56. }
  57. // Collect is called by the Prometheus registry when collecting metrics.
  58. func (kpmc KubePodCollector) Collect(ch chan<- prometheus.Metric) {
  59. pods := kpmc.KubeClusterCache.GetAllPods()
  60. for _, pod := range pods {
  61. podName := pod.GetName()
  62. podNS := pod.GetNamespace()
  63. podUID := string(pod.GetUID())
  64. node := pod.Spec.NodeName
  65. phase := pod.Status.Phase
  66. // Pod Status Phase
  67. if phase != "" {
  68. phases := []struct {
  69. v bool
  70. n string
  71. }{
  72. {phase == v1.PodPending, string(v1.PodPending)},
  73. {phase == v1.PodSucceeded, string(v1.PodSucceeded)},
  74. {phase == v1.PodFailed, string(v1.PodFailed)},
  75. {phase == v1.PodUnknown, string(v1.PodUnknown)},
  76. {phase == v1.PodRunning, string(v1.PodRunning)},
  77. }
  78. for _, p := range phases {
  79. ch <- newKubePodStatusPhaseMetric("kube_pod_status_phase", podNS, podName, podUID, p.n, boolFloat64(p.v))
  80. }
  81. }
  82. // Pod Labels
  83. labelNames, labelValues := prom.KubePrependQualifierToLabels(pod.GetLabels(), "label_")
  84. ch <- newKubePodLabelsMetric("kube_pod_labels", podNS, podName, podUID, labelNames, labelValues)
  85. // Owner References
  86. for _, owner := range pod.OwnerReferences {
  87. ch <- newKubePodOwnerMetric("kube_pod_owner", podNS, podName, owner.Name, owner.Kind, owner.Controller != nil)
  88. }
  89. // Container Status
  90. for _, status := range pod.Status.ContainerStatuses {
  91. ch <- newKubePodContainerStatusRestartsTotalMetric("kube_pod_container_status_restarts_total", podNS, podName, podUID, status.Name, float64(status.RestartCount))
  92. if status.State.Running != nil {
  93. ch <- newKubePodContainerStatusRunningMetric("kube_pod_container_status_running", podNS, podName, podUID, status.Name)
  94. }
  95. if status.State.Terminated != nil {
  96. ch <- newKubePodContainerStatusTerminatedReasonMetric(
  97. "kube_pod_container_status_terminated_reason",
  98. podNS,
  99. podName,
  100. podUID,
  101. status.Name,
  102. status.State.Terminated.Reason)
  103. }
  104. }
  105. for _, container := range pod.Spec.Containers {
  106. // Requests
  107. for resourceName, quantity := range container.Resources.Requests {
  108. resource, unit, value := toResourceUnitValue(resourceName, quantity)
  109. // failed to parse the resource type
  110. if resource == "" {
  111. log.DedupedWarningf(5, "Failed to parse resource units and quantity for resource: %s", resourceName)
  112. continue
  113. }
  114. ch <- newKubePodContainerResourceRequestsMetric(
  115. "kube_pod_container_resource_requests",
  116. podNS,
  117. podName,
  118. podUID,
  119. container.Name,
  120. node,
  121. resource,
  122. unit,
  123. value)
  124. }
  125. // Limits
  126. for resourceName, quantity := range container.Resources.Limits {
  127. resource, unit, value := toResourceUnitValue(resourceName, quantity)
  128. // failed to parse the resource type
  129. if resource == "" {
  130. log.DedupedWarningf(5, "Failed to parse resource units and quantity for resource: %s", resourceName)
  131. continue
  132. }
  133. // KSM v1 Emission
  134. if resource == "cpu" {
  135. ch <- newKubePodContainerResourceLimitsCPUCoresMetric(
  136. "kube_pod_container_resource_limits_cpu_cores",
  137. podNS,
  138. podName,
  139. podUID,
  140. container.Name,
  141. node,
  142. value)
  143. }
  144. if resource == "memory" {
  145. ch <- newKubePodContainerResourceLimitsMemoryBytesMetric(
  146. "kube_pod_container_resource_limits_memory_bytes",
  147. podNS,
  148. podName,
  149. podUID,
  150. container.Name,
  151. node,
  152. value)
  153. }
  154. ch <- newKubePodContainerResourceLimitsMetric(
  155. "kube_pod_container_resource_limits",
  156. podNS,
  157. podName,
  158. podUID,
  159. container.Name,
  160. node,
  161. resource,
  162. unit,
  163. value)
  164. }
  165. }
  166. }
  167. }
  168. //--------------------------------------------------------------------------
  169. // PodAnnotationsMetric
  170. //--------------------------------------------------------------------------
  171. // PodAnnotationsMetric is a prometheus.Metric used to encode namespace annotations
  172. type PodAnnotationsMetric struct {
  173. fqName string
  174. help string
  175. namespace string
  176. pod string
  177. labelNames []string
  178. labelValues []string
  179. }
  180. // Creates a new PodAnnotationsMetric, implementation of prometheus.Metric
  181. func newPodAnnotationMetric(fqname, namespace, pod string, labelNames, labelValues []string) PodAnnotationsMetric {
  182. return PodAnnotationsMetric{
  183. fqName: fqname,
  184. help: "kube_pod_annotations Pod Annotations",
  185. namespace: namespace,
  186. pod: pod,
  187. labelNames: labelNames,
  188. labelValues: labelValues,
  189. }
  190. }
  191. // Desc returns the descriptor for the Metric. This method idempotently
  192. // returns the same descriptor throughout the lifetime of the Metric.
  193. func (pam PodAnnotationsMetric) Desc() *prometheus.Desc {
  194. l := prometheus.Labels{
  195. "namespace": pam.namespace,
  196. "pod": pam.pod,
  197. }
  198. return prometheus.NewDesc(pam.fqName, pam.help, []string{}, l)
  199. }
  200. // Write encodes the Metric into a "Metric" Protocol Buffer data
  201. // transmission object.
  202. func (pam PodAnnotationsMetric) Write(m *dto.Metric) error {
  203. h := float64(1)
  204. m.Gauge = &dto.Gauge{
  205. Value: &h,
  206. }
  207. var labels []*dto.LabelPair
  208. for i := range pam.labelNames {
  209. labels = append(labels, &dto.LabelPair{
  210. Name: &pam.labelNames[i],
  211. Value: &pam.labelValues[i],
  212. })
  213. }
  214. labels = append(labels,
  215. &dto.LabelPair{
  216. Name: toStringPtr("namespace"),
  217. Value: &pam.namespace,
  218. },
  219. &dto.LabelPair{
  220. Name: toStringPtr("pod"),
  221. Value: &pam.pod,
  222. })
  223. m.Label = labels
  224. return nil
  225. }
  226. //--------------------------------------------------------------------------
  227. // KubePodLabelsMetric
  228. //--------------------------------------------------------------------------
  229. // KubePodLabelsMetric is a prometheus.Metric used to encode
  230. // a duplicate of the deprecated kube-state-metrics metric
  231. // kube_pod_labels
  232. type KubePodLabelsMetric struct {
  233. fqName string
  234. help string
  235. pod string
  236. namespace string
  237. uid string
  238. labelNames []string
  239. labelValues []string
  240. }
  241. // Creates a new KubePodLabelsMetric, implementation of prometheus.Metric
  242. func newKubePodLabelsMetric(fqname, namespace, pod, uid string, labelNames []string, labelValues []string) KubePodLabelsMetric {
  243. return KubePodLabelsMetric{
  244. fqName: fqname,
  245. help: "kube_pod_labels all labels for each pod prefixed with label_",
  246. pod: pod,
  247. namespace: namespace,
  248. uid: uid,
  249. labelNames: labelNames,
  250. labelValues: labelValues,
  251. }
  252. }
  253. // Desc returns the descriptor for the Metric. This method idempotently
  254. // returns the same descriptor throughout the lifetime of the Metric.
  255. func (nam KubePodLabelsMetric) Desc() *prometheus.Desc {
  256. l := prometheus.Labels{
  257. "namespace": nam.namespace,
  258. "pod": nam.pod,
  259. "uid": nam.uid,
  260. }
  261. return prometheus.NewDesc(nam.fqName, nam.help, nam.labelNames, l)
  262. }
  263. // Write encodes the Metric into a "Metric" Protocol Buffer data
  264. // transmission object.
  265. func (nam KubePodLabelsMetric) Write(m *dto.Metric) error {
  266. h := float64(1)
  267. m.Gauge = &dto.Gauge{
  268. Value: &h,
  269. }
  270. var labels []*dto.LabelPair
  271. for i := range nam.labelNames {
  272. labels = append(labels, &dto.LabelPair{
  273. Name: &nam.labelNames[i],
  274. Value: &nam.labelValues[i],
  275. })
  276. }
  277. labels = append(labels,
  278. &dto.LabelPair{
  279. Name: toStringPtr("pod"),
  280. Value: &nam.pod,
  281. },
  282. &dto.LabelPair{
  283. Name: toStringPtr("namespace"),
  284. Value: &nam.namespace,
  285. },
  286. &dto.LabelPair{
  287. Name: toStringPtr("uid"),
  288. Value: &nam.uid,
  289. },
  290. )
  291. m.Label = labels
  292. return nil
  293. }
  294. //--------------------------------------------------------------------------
  295. // KubePodContainerStatusRestartsTotalMetric
  296. //--------------------------------------------------------------------------
  297. // KubePodContainerStatusRestartsTotalMetric is a prometheus.Metric emitting container restarts metrics.
  298. type KubePodContainerStatusRestartsTotalMetric struct {
  299. fqName string
  300. help string
  301. pod string
  302. namespace string
  303. container string
  304. uid string
  305. value float64
  306. }
  307. // Creates a new KubePodContainerStatusRestartsTotalMetric, implementation of prometheus.Metric
  308. func newKubePodContainerStatusRestartsTotalMetric(fqname, namespace, pod, uid, container string, value float64) KubePodContainerStatusRestartsTotalMetric {
  309. return KubePodContainerStatusRestartsTotalMetric{
  310. fqName: fqname,
  311. help: "kube_pod_container_status_restarts_total total container restarts",
  312. pod: pod,
  313. namespace: namespace,
  314. uid: uid,
  315. container: container,
  316. value: value,
  317. }
  318. }
  319. // Desc returns the descriptor for the Metric. This method idempotently
  320. // returns the same descriptor throughout the lifetime of the Metric.
  321. func (kpcs KubePodContainerStatusRestartsTotalMetric) Desc() *prometheus.Desc {
  322. l := prometheus.Labels{
  323. "namespace": kpcs.namespace,
  324. "pod": kpcs.pod,
  325. "uid": kpcs.uid,
  326. "container": kpcs.container,
  327. }
  328. return prometheus.NewDesc(kpcs.fqName, kpcs.help, []string{}, l)
  329. }
  330. // Write encodes the Metric into a "Metric" Protocol Buffer data transmission object.
  331. func (kpcs KubePodContainerStatusRestartsTotalMetric) Write(m *dto.Metric) error {
  332. m.Counter = &dto.Counter{
  333. Value: &kpcs.value,
  334. }
  335. var labels []*dto.LabelPair
  336. labels = append(labels,
  337. &dto.LabelPair{
  338. Name: toStringPtr("namespace"),
  339. Value: &kpcs.namespace,
  340. },
  341. &dto.LabelPair{
  342. Name: toStringPtr("pod"),
  343. Value: &kpcs.pod,
  344. },
  345. &dto.LabelPair{
  346. Name: toStringPtr("container"),
  347. Value: &kpcs.container,
  348. },
  349. &dto.LabelPair{
  350. Name: toStringPtr("uid"),
  351. Value: &kpcs.uid,
  352. },
  353. )
  354. m.Label = labels
  355. return nil
  356. }
  357. //--------------------------------------------------------------------------
  358. // KubePodContainerStatusTerminatedReasonMetric
  359. //--------------------------------------------------------------------------
  360. // KubePodContainerStatusTerminatedReasonMetric is a prometheus.Metric emitting container termination reasons.
  361. type KubePodContainerStatusTerminatedReasonMetric struct {
  362. fqName string
  363. help string
  364. pod string
  365. namespace string
  366. container string
  367. uid string
  368. reason string
  369. }
  370. // Creates a new KubePodContainerStatusRestartsTotalMetric, implementation of prometheus.Metric
  371. func newKubePodContainerStatusTerminatedReasonMetric(fqname, namespace, pod, uid, container, reason string) KubePodContainerStatusTerminatedReasonMetric {
  372. return KubePodContainerStatusTerminatedReasonMetric{
  373. fqName: fqname,
  374. help: "kube_pod_container_status_terminated_reason Describes the reason the container is currently in terminated state.",
  375. pod: pod,
  376. namespace: namespace,
  377. uid: uid,
  378. container: container,
  379. reason: reason,
  380. }
  381. }
  382. // Desc returns the descriptor for the Metric. This method idempotently
  383. // returns the same descriptor throughout the lifetime of the Metric.
  384. func (kpcs KubePodContainerStatusTerminatedReasonMetric) Desc() *prometheus.Desc {
  385. l := prometheus.Labels{
  386. "namespace": kpcs.namespace,
  387. "pod": kpcs.pod,
  388. "uid": kpcs.uid,
  389. "container": kpcs.container,
  390. "reason": kpcs.reason,
  391. }
  392. return prometheus.NewDesc(kpcs.fqName, kpcs.help, []string{}, l)
  393. }
  394. // Write encodes the Metric into a "Metric" Protocol Buffer data transmission object.
  395. func (kpcs KubePodContainerStatusTerminatedReasonMetric) Write(m *dto.Metric) error {
  396. h := float64(1)
  397. m.Gauge = &dto.Gauge{
  398. Value: &h,
  399. }
  400. var labels []*dto.LabelPair
  401. labels = append(labels,
  402. &dto.LabelPair{
  403. Name: toStringPtr("namespace"),
  404. Value: &kpcs.namespace,
  405. },
  406. &dto.LabelPair{
  407. Name: toStringPtr("pod"),
  408. Value: &kpcs.pod,
  409. },
  410. &dto.LabelPair{
  411. Name: toStringPtr("container"),
  412. Value: &kpcs.container,
  413. },
  414. &dto.LabelPair{
  415. Name: toStringPtr("uid"),
  416. Value: &kpcs.uid,
  417. },
  418. &dto.LabelPair{
  419. Name: toStringPtr("reason"),
  420. Value: &kpcs.reason,
  421. },
  422. )
  423. m.Label = labels
  424. return nil
  425. }
  426. //--------------------------------------------------------------------------
  427. // KubePodStatusPhaseMetric
  428. //--------------------------------------------------------------------------
  429. // KubePodStatusPhaseMetric is a prometheus.Metric emitting all phases for a pod
  430. type KubePodStatusPhaseMetric struct {
  431. fqName string
  432. help string
  433. pod string
  434. namespace string
  435. uid string
  436. phase string
  437. value float64
  438. }
  439. // Creates a new KubePodContainerStatusRestartsTotalMetric, implementation of prometheus.Metric
  440. func newKubePodStatusPhaseMetric(fqname, namespace, pod, uid, phase string, value float64) KubePodStatusPhaseMetric {
  441. return KubePodStatusPhaseMetric{
  442. fqName: fqname,
  443. help: "kube_pod_container_status_terminated_reason Describes the reason the container is currently in terminated state.",
  444. pod: pod,
  445. namespace: namespace,
  446. uid: uid,
  447. phase: phase,
  448. value: value,
  449. }
  450. }
  451. // Desc returns the descriptor for the Metric. This method idempotently
  452. // returns the same descriptor throughout the lifetime of the Metric.
  453. func (kpcs KubePodStatusPhaseMetric) Desc() *prometheus.Desc {
  454. l := prometheus.Labels{
  455. "namespace": kpcs.namespace,
  456. "pod": kpcs.pod,
  457. "uid": kpcs.uid,
  458. "phase": kpcs.phase,
  459. }
  460. return prometheus.NewDesc(kpcs.fqName, kpcs.help, []string{}, l)
  461. }
  462. // Write encodes the Metric into a "Metric" Protocol Buffer data transmission object.
  463. func (kpcs KubePodStatusPhaseMetric) Write(m *dto.Metric) error {
  464. m.Gauge = &dto.Gauge{
  465. Value: &kpcs.value,
  466. }
  467. var labels []*dto.LabelPair
  468. labels = append(labels,
  469. &dto.LabelPair{
  470. Name: toStringPtr("namespace"),
  471. Value: &kpcs.namespace,
  472. },
  473. &dto.LabelPair{
  474. Name: toStringPtr("pod"),
  475. Value: &kpcs.pod,
  476. },
  477. &dto.LabelPair{
  478. Name: toStringPtr("uid"),
  479. Value: &kpcs.uid,
  480. },
  481. &dto.LabelPair{
  482. Name: toStringPtr("phase"),
  483. Value: &kpcs.phase,
  484. },
  485. )
  486. m.Label = labels
  487. return nil
  488. }
  489. //--------------------------------------------------------------------------
  490. // KubePodContainerStatusRunningMetric
  491. //--------------------------------------------------------------------------
  492. // KubePodLabelsMetric is a prometheus.Metric used to encode
  493. // a duplicate of the deprecated kube-state-metrics metric
  494. // kube_pod_labels
  495. type KubePodContainerStatusRunningMetric struct {
  496. fqName string
  497. help string
  498. pod string
  499. namespace string
  500. container string
  501. uid string
  502. }
  503. // Creates a new KubePodContainerStatusRunningMetric, implementation of prometheus.Metric
  504. func newKubePodContainerStatusRunningMetric(fqname, namespace, pod, uid, container string) KubePodContainerStatusRunningMetric {
  505. return KubePodContainerStatusRunningMetric{
  506. fqName: fqname,
  507. help: "kube_pod_container_status_running pods container status",
  508. pod: pod,
  509. namespace: namespace,
  510. uid: uid,
  511. container: container,
  512. }
  513. }
  514. // Desc returns the descriptor for the Metric. This method idempotently
  515. // returns the same descriptor throughout the lifetime of the Metric.
  516. func (kpcs KubePodContainerStatusRunningMetric) Desc() *prometheus.Desc {
  517. l := prometheus.Labels{
  518. "namespace": kpcs.namespace,
  519. "pod": kpcs.pod,
  520. "uid": kpcs.uid,
  521. "container": kpcs.container,
  522. }
  523. return prometheus.NewDesc(kpcs.fqName, kpcs.help, []string{}, l)
  524. }
  525. // Write encodes the Metric into a "Metric" Protocol Buffer data
  526. // transmission object.
  527. func (kpcs KubePodContainerStatusRunningMetric) Write(m *dto.Metric) error {
  528. h := float64(1)
  529. m.Gauge = &dto.Gauge{
  530. Value: &h,
  531. }
  532. var labels []*dto.LabelPair
  533. labels = append(labels,
  534. &dto.LabelPair{
  535. Name: toStringPtr("namespace"),
  536. Value: &kpcs.namespace,
  537. },
  538. &dto.LabelPair{
  539. Name: toStringPtr("pod"),
  540. Value: &kpcs.pod,
  541. },
  542. &dto.LabelPair{
  543. Name: toStringPtr("container"),
  544. Value: &kpcs.container,
  545. },
  546. &dto.LabelPair{
  547. Name: toStringPtr("uid"),
  548. Value: &kpcs.uid,
  549. },
  550. )
  551. m.Label = labels
  552. return nil
  553. }
  554. //--------------------------------------------------------------------------
  555. // KubePodContainerResourceRequestMetric
  556. //--------------------------------------------------------------------------
  557. // KubePodContainerResourceRequestsMetric is a prometheus.Metric
  558. type KubePodContainerResourceRequestsMetric struct {
  559. fqName string
  560. help string
  561. pod string
  562. namespace string
  563. container string
  564. uid string
  565. resource string
  566. unit string
  567. node string
  568. value float64
  569. }
  570. // Creates a new newKubePodContainerResourceRequestsMetric, implementation of prometheus.Metric
  571. func newKubePodContainerResourceRequestsMetric(fqname, namespace, pod, uid, container, node, resource, unit string, value float64) KubePodContainerResourceRequestsMetric {
  572. return KubePodContainerResourceRequestsMetric{
  573. fqName: fqname,
  574. help: "kube_pod_container_resource_requests pods container resource requests",
  575. pod: pod,
  576. namespace: namespace,
  577. uid: uid,
  578. container: container,
  579. node: node,
  580. resource: resource,
  581. unit: unit,
  582. value: value,
  583. }
  584. }
  585. // Desc returns the descriptor for the Metric. This method idempotently
  586. // returns the same descriptor throughout the lifetime of the Metric.
  587. func (kpcrr KubePodContainerResourceRequestsMetric) Desc() *prometheus.Desc {
  588. l := prometheus.Labels{
  589. "namespace": kpcrr.namespace,
  590. "pod": kpcrr.pod,
  591. "uid": kpcrr.uid,
  592. "container": kpcrr.container,
  593. "node": kpcrr.node,
  594. "resource": kpcrr.resource,
  595. "unit": kpcrr.unit,
  596. }
  597. return prometheus.NewDesc(kpcrr.fqName, kpcrr.help, []string{}, l)
  598. }
  599. // Write encodes the Metric into a "Metric" Protocol Buffer data
  600. // transmission object.
  601. func (kpcrr KubePodContainerResourceRequestsMetric) Write(m *dto.Metric) error {
  602. m.Gauge = &dto.Gauge{
  603. Value: &kpcrr.value,
  604. }
  605. m.Label = []*dto.LabelPair{
  606. {
  607. Name: toStringPtr("namespace"),
  608. Value: &kpcrr.namespace,
  609. },
  610. {
  611. Name: toStringPtr("pod"),
  612. Value: &kpcrr.pod,
  613. },
  614. {
  615. Name: toStringPtr("container"),
  616. Value: &kpcrr.container,
  617. },
  618. {
  619. Name: toStringPtr("uid"),
  620. Value: &kpcrr.uid,
  621. },
  622. {
  623. Name: toStringPtr("node"),
  624. Value: &kpcrr.node,
  625. },
  626. {
  627. Name: toStringPtr("resource"),
  628. Value: &kpcrr.resource,
  629. },
  630. {
  631. Name: toStringPtr("unit"),
  632. Value: &kpcrr.unit,
  633. },
  634. }
  635. return nil
  636. }
  637. //--------------------------------------------------------------------------
  638. // KubePodContainerResourceLimitsMetric
  639. //--------------------------------------------------------------------------
  640. // KubePodContainerResourceLimitsMetric is a prometheus.Metric
  641. type KubePodContainerResourceLimitsMetric struct {
  642. fqName string
  643. help string
  644. pod string
  645. namespace string
  646. container string
  647. uid string
  648. resource string
  649. unit string
  650. node string
  651. value float64
  652. }
  653. // Creates a new KubePodContainerResourceLimitsMetric, implementation of prometheus.Metric
  654. func newKubePodContainerResourceLimitsMetric(fqname, namespace, pod, uid, container, node, resource, unit string, value float64) KubePodContainerResourceLimitsMetric {
  655. return KubePodContainerResourceLimitsMetric{
  656. fqName: fqname,
  657. help: "kube_pod_container_resource_limits pods container resource limits",
  658. pod: pod,
  659. namespace: namespace,
  660. uid: uid,
  661. container: container,
  662. node: node,
  663. resource: resource,
  664. unit: unit,
  665. value: value,
  666. }
  667. }
  668. // Desc returns the descriptor for the Metric. This method idempotently
  669. // returns the same descriptor throughout the lifetime of the Metric.
  670. func (kpcrr KubePodContainerResourceLimitsMetric) Desc() *prometheus.Desc {
  671. l := prometheus.Labels{
  672. "namespace": kpcrr.namespace,
  673. "pod": kpcrr.pod,
  674. "uid": kpcrr.uid,
  675. "container": kpcrr.container,
  676. "node": kpcrr.node,
  677. "resource": kpcrr.resource,
  678. "unit": kpcrr.unit,
  679. }
  680. return prometheus.NewDesc(kpcrr.fqName, kpcrr.help, []string{}, l)
  681. }
  682. // Write encodes the Metric into a "Metric" Protocol Buffer data
  683. // transmission object.
  684. func (kpcrr KubePodContainerResourceLimitsMetric) Write(m *dto.Metric) error {
  685. m.Gauge = &dto.Gauge{
  686. Value: &kpcrr.value,
  687. }
  688. m.Label = []*dto.LabelPair{
  689. {
  690. Name: toStringPtr("namespace"),
  691. Value: &kpcrr.namespace,
  692. },
  693. {
  694. Name: toStringPtr("pod"),
  695. Value: &kpcrr.pod,
  696. },
  697. {
  698. Name: toStringPtr("container"),
  699. Value: &kpcrr.container,
  700. },
  701. {
  702. Name: toStringPtr("uid"),
  703. Value: &kpcrr.uid,
  704. },
  705. {
  706. Name: toStringPtr("node"),
  707. Value: &kpcrr.node,
  708. },
  709. {
  710. Name: toStringPtr("resource"),
  711. Value: &kpcrr.resource,
  712. },
  713. {
  714. Name: toStringPtr("unit"),
  715. Value: &kpcrr.unit,
  716. },
  717. }
  718. return nil
  719. }
  720. //--------------------------------------------------------------------------
  721. // KubePodContainerResourceLimitsCPUCoresMetric (KSM v1)
  722. //--------------------------------------------------------------------------
  723. // KubePodContainerResourceLimitsCPUCoresMetric is a prometheus.Metric
  724. type KubePodContainerResourceLimitsCPUCoresMetric struct {
  725. fqName string
  726. help string
  727. pod string
  728. namespace string
  729. container string
  730. uid string
  731. node string
  732. value float64
  733. }
  734. // Creates a new KubePodContainerResourceLimitsMetric, implementation of prometheus.Metric
  735. func newKubePodContainerResourceLimitsCPUCoresMetric(fqname, namespace, pod, uid, container, node string, value float64) KubePodContainerResourceLimitsCPUCoresMetric {
  736. return KubePodContainerResourceLimitsCPUCoresMetric{
  737. fqName: fqname,
  738. help: "kube_pod_container_resource_limits_cpu_cores pods container cpu cores resource limits",
  739. pod: pod,
  740. namespace: namespace,
  741. uid: uid,
  742. container: container,
  743. node: node,
  744. value: value,
  745. }
  746. }
  747. // Desc returns the descriptor for the Metric. This method idempotently
  748. // returns the same descriptor throughout the lifetime of the Metric.
  749. func (kpcrr KubePodContainerResourceLimitsCPUCoresMetric) Desc() *prometheus.Desc {
  750. l := prometheus.Labels{
  751. "namespace": kpcrr.namespace,
  752. "pod": kpcrr.pod,
  753. "uid": kpcrr.uid,
  754. "container": kpcrr.container,
  755. "node": kpcrr.node,
  756. }
  757. return prometheus.NewDesc(kpcrr.fqName, kpcrr.help, []string{}, l)
  758. }
  759. // Write encodes the Metric into a "Metric" Protocol Buffer data
  760. // transmission object.
  761. func (kpcrr KubePodContainerResourceLimitsCPUCoresMetric) Write(m *dto.Metric) error {
  762. m.Gauge = &dto.Gauge{
  763. Value: &kpcrr.value,
  764. }
  765. m.Label = []*dto.LabelPair{
  766. {
  767. Name: toStringPtr("namespace"),
  768. Value: &kpcrr.namespace,
  769. },
  770. {
  771. Name: toStringPtr("pod"),
  772. Value: &kpcrr.pod,
  773. },
  774. {
  775. Name: toStringPtr("container"),
  776. Value: &kpcrr.container,
  777. },
  778. {
  779. Name: toStringPtr("uid"),
  780. Value: &kpcrr.uid,
  781. },
  782. {
  783. Name: toStringPtr("node"),
  784. Value: &kpcrr.node,
  785. },
  786. }
  787. return nil
  788. }
  789. //--------------------------------------------------------------------------
  790. // KubePodContainerResourceLimitsMemoryBytesMetric (KSM v1)
  791. //--------------------------------------------------------------------------
  792. // KubePodContainerResourceLimitsMemoryBytesMetric is a prometheus.Metric
  793. type KubePodContainerResourceLimitsMemoryBytesMetric struct {
  794. fqName string
  795. help string
  796. pod string
  797. namespace string
  798. container string
  799. uid string
  800. node string
  801. value float64
  802. }
  803. // Creates a new KubePodContainerResourceLimitsMemoryBytesMetric, implementation of prometheus.Metric
  804. func newKubePodContainerResourceLimitsMemoryBytesMetric(fqname, namespace, pod, uid, container, node string, value float64) KubePodContainerResourceLimitsMemoryBytesMetric {
  805. return KubePodContainerResourceLimitsMemoryBytesMetric{
  806. fqName: fqname,
  807. help: "kube_pod_container_resource_limits_memory_bytes pods container memory bytes resource limits",
  808. pod: pod,
  809. namespace: namespace,
  810. uid: uid,
  811. container: container,
  812. node: node,
  813. value: value,
  814. }
  815. }
  816. // Desc returns the descriptor for the Metric. This method idempotently
  817. // returns the same descriptor throughout the lifetime of the Metric.
  818. func (kpcrr KubePodContainerResourceLimitsMemoryBytesMetric) Desc() *prometheus.Desc {
  819. l := prometheus.Labels{
  820. "namespace": kpcrr.namespace,
  821. "pod": kpcrr.pod,
  822. "uid": kpcrr.uid,
  823. "container": kpcrr.container,
  824. "node": kpcrr.node,
  825. }
  826. return prometheus.NewDesc(kpcrr.fqName, kpcrr.help, []string{}, l)
  827. }
  828. // Write encodes the Metric into a "Metric" Protocol Buffer data
  829. // transmission object.
  830. func (kpcrr KubePodContainerResourceLimitsMemoryBytesMetric) Write(m *dto.Metric) error {
  831. m.Gauge = &dto.Gauge{
  832. Value: &kpcrr.value,
  833. }
  834. m.Label = []*dto.LabelPair{
  835. {
  836. Name: toStringPtr("namespace"),
  837. Value: &kpcrr.namespace,
  838. },
  839. {
  840. Name: toStringPtr("pod"),
  841. Value: &kpcrr.pod,
  842. },
  843. {
  844. Name: toStringPtr("container"),
  845. Value: &kpcrr.container,
  846. },
  847. {
  848. Name: toStringPtr("uid"),
  849. Value: &kpcrr.uid,
  850. },
  851. {
  852. Name: toStringPtr("node"),
  853. Value: &kpcrr.node,
  854. },
  855. }
  856. return nil
  857. }
  858. //--------------------------------------------------------------------------
  859. // KubePodOwnerMetric
  860. //--------------------------------------------------------------------------
  861. // KubePodOwnerMetric is a prometheus.Metric
  862. type KubePodOwnerMetric struct {
  863. fqName string
  864. help string
  865. namespace string
  866. pod string
  867. ownerIsController bool
  868. ownerName string
  869. ownerKind string
  870. }
  871. // Creates a new KubePodOwnerMetric, implementation of prometheus.Metric
  872. func newKubePodOwnerMetric(fqname, namespace, pod, ownerName, ownerKind string, ownerIsController bool) KubePodOwnerMetric {
  873. return KubePodOwnerMetric{
  874. fqName: fqname,
  875. help: "kube_pod_owner Information about the Pod's owner",
  876. namespace: namespace,
  877. pod: pod,
  878. ownerName: ownerName,
  879. ownerKind: ownerKind,
  880. ownerIsController: ownerIsController,
  881. }
  882. }
  883. // Desc returns the descriptor for the Metric. This method idempotently
  884. // returns the same descriptor throughout the lifetime of the Metric.
  885. func (kpo KubePodOwnerMetric) Desc() *prometheus.Desc {
  886. l := prometheus.Labels{
  887. "namespace": kpo.namespace,
  888. "pod": kpo.pod,
  889. "owner_name": kpo.ownerName,
  890. "owner_kind": kpo.ownerKind,
  891. "owner_is_controller": fmt.Sprintf("%t", kpo.ownerIsController),
  892. }
  893. return prometheus.NewDesc(kpo.fqName, kpo.help, []string{}, l)
  894. }
  895. // Write encodes the Metric into a "Metric" Protocol Buffer data
  896. // transmission object.
  897. func (kpo KubePodOwnerMetric) Write(m *dto.Metric) error {
  898. v := float64(1.0)
  899. m.Gauge = &dto.Gauge{
  900. Value: &v,
  901. }
  902. m.Label = []*dto.LabelPair{
  903. {
  904. Name: toStringPtr("namespace"),
  905. Value: &kpo.namespace,
  906. },
  907. {
  908. Name: toStringPtr("pod"),
  909. Value: &kpo.pod,
  910. },
  911. {
  912. Name: toStringPtr("owner_name"),
  913. Value: &kpo.ownerName,
  914. },
  915. {
  916. Name: toStringPtr("owner_kind"),
  917. Value: &kpo.ownerKind,
  918. },
  919. {
  920. Name: toStringPtr("owner_is_controller"),
  921. Value: toStringPtr(fmt.Sprintf("%t", kpo.ownerIsController)),
  922. },
  923. }
  924. return nil
  925. }