podmetrics.go 32 KB

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