podmetrics.go 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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, 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. ownerIsController bool
  928. ownerName string
  929. ownerKind string
  930. }
  931. // Creates a new KubePodOwnerMetric, implementation of prometheus.Metric
  932. func newKubePodOwnerMetric(fqname, namespace, pod, ownerName, ownerKind string, ownerIsController bool) KubePodOwnerMetric {
  933. return KubePodOwnerMetric{
  934. fqName: fqname,
  935. help: "kube_pod_owner Information about the Pod's owner",
  936. namespace: namespace,
  937. pod: pod,
  938. ownerName: ownerName,
  939. ownerKind: ownerKind,
  940. ownerIsController: ownerIsController,
  941. }
  942. }
  943. // Desc returns the descriptor for the Metric. This method idempotently
  944. // returns the same descriptor throughout the lifetime of the Metric.
  945. func (kpo KubePodOwnerMetric) Desc() *prometheus.Desc {
  946. l := prometheus.Labels{
  947. "namespace": kpo.namespace,
  948. "pod": kpo.pod,
  949. "owner_name": kpo.ownerName,
  950. "owner_kind": kpo.ownerKind,
  951. "owner_is_controller": fmt.Sprintf("%t", kpo.ownerIsController),
  952. }
  953. return prometheus.NewDesc(kpo.fqName, kpo.help, []string{}, l)
  954. }
  955. // Write encodes the Metric into a "Metric" Protocol Buffer data
  956. // transmission object.
  957. func (kpo KubePodOwnerMetric) Write(m *dto.Metric) error {
  958. v := float64(1.0)
  959. m.Gauge = &dto.Gauge{
  960. Value: &v,
  961. }
  962. m.Label = []*dto.LabelPair{
  963. {
  964. Name: toStringPtr("namespace"),
  965. Value: &kpo.namespace,
  966. },
  967. {
  968. Name: toStringPtr("pod"),
  969. Value: &kpo.pod,
  970. },
  971. {
  972. Name: toStringPtr("owner_name"),
  973. Value: &kpo.ownerName,
  974. },
  975. {
  976. Name: toStringPtr("owner_kind"),
  977. Value: &kpo.ownerKind,
  978. },
  979. {
  980. Name: toStringPtr("owner_is_controller"),
  981. Value: toStringPtr(fmt.Sprintf("%t", kpo.ownerIsController)),
  982. },
  983. }
  984. return nil
  985. }