podmetrics.go 32 KB

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