deploymentmetrics.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. package metrics
  2. import (
  3. "github.com/opencost/opencost/core/pkg/clustercache"
  4. "github.com/opencost/opencost/core/pkg/util/promutil"
  5. "github.com/prometheus/client_golang/prometheus"
  6. dto "github.com/prometheus/client_model/go"
  7. )
  8. //--------------------------------------------------------------------------
  9. // KubecostDeploymentCollector
  10. //--------------------------------------------------------------------------
  11. // KubecostDeploymentCollector is a prometheus collector that generates kubecost
  12. // specific deployment metrics.
  13. type KubecostDeploymentCollector struct {
  14. KubeClusterCache clustercache.ClusterCache
  15. metricsConfig MetricsConfig
  16. }
  17. // Describe sends the super-set of all possible descriptors of metrics
  18. // collected by this Collector.
  19. func (kdc KubecostDeploymentCollector) Describe(ch chan<- *prometheus.Desc) {
  20. disabledMetrics := kdc.metricsConfig.GetDisabledMetricsMap()
  21. if _, disabled := disabledMetrics["deployment_match_labels"]; disabled {
  22. return
  23. }
  24. ch <- prometheus.NewDesc("deployment_match_labels", "deployment match labels", []string{}, nil)
  25. }
  26. // Collect is called by the Prometheus registry when collecting metrics.
  27. func (kdc KubecostDeploymentCollector) Collect(ch chan<- prometheus.Metric) {
  28. disabledMetrics := kdc.metricsConfig.GetDisabledMetricsMap()
  29. if _, disabled := disabledMetrics["deployment_match_labels"]; disabled {
  30. return
  31. }
  32. ds := kdc.KubeClusterCache.GetAllDeployments()
  33. for _, deployment := range ds {
  34. deploymentName := deployment.Name
  35. deploymentNS := deployment.Namespace
  36. deploymentUID := string(deployment.UID)
  37. labels, values := promutil.KubeLabelsToLabels(promutil.SanitizeLabels(deployment.MatchLabels))
  38. if len(labels) > 0 {
  39. m := newDeploymentMatchLabelsMetric(deploymentName, deploymentNS, "deployment_match_labels", labels, values, deploymentUID)
  40. ch <- m
  41. }
  42. }
  43. }
  44. //--------------------------------------------------------------------------
  45. // DeploymentMatchLabelsMetric
  46. //--------------------------------------------------------------------------
  47. // DeploymentMatchLabelsMetric is a prometheus.Metric used to encode deployment match labels
  48. type DeploymentMatchLabelsMetric struct {
  49. fqName string
  50. help string
  51. labelNames []string
  52. labelValues []string
  53. deploymentName string
  54. namespace string
  55. uid string
  56. }
  57. // Creates a new DeploymentMatchLabelsMetric, implementation of prometheus.Metric
  58. func newDeploymentMatchLabelsMetric(name, namespace, fqname string, labelNames, labelvalues []string, uid string) DeploymentMatchLabelsMetric {
  59. return DeploymentMatchLabelsMetric{
  60. fqName: fqname,
  61. labelNames: labelNames,
  62. labelValues: labelvalues,
  63. help: "deployment_match_labels Deployment Match Labels",
  64. deploymentName: name,
  65. namespace: namespace,
  66. uid: uid,
  67. }
  68. }
  69. // Desc returns the descriptor for the Metric. This method idempotently
  70. // returns the same descriptor throughout the lifetime of the Metric.
  71. func (dmlm DeploymentMatchLabelsMetric) Desc() *prometheus.Desc {
  72. l := prometheus.Labels{
  73. "deployment": dmlm.deploymentName,
  74. "namespace": dmlm.namespace,
  75. "uid": dmlm.uid,
  76. }
  77. return prometheus.NewDesc(dmlm.fqName, dmlm.help, dmlm.labelNames, l)
  78. }
  79. // Write encodes the Metric into a "Metric" Protocol Buffer data
  80. // transmission object.
  81. func (dmlm DeploymentMatchLabelsMetric) Write(m *dto.Metric) error {
  82. h := float64(1)
  83. m.Gauge = &dto.Gauge{
  84. Value: &h,
  85. }
  86. var labels []*dto.LabelPair
  87. for i := range dmlm.labelNames {
  88. labels = append(labels, &dto.LabelPair{
  89. Name: &dmlm.labelNames[i],
  90. Value: &dmlm.labelValues[i],
  91. })
  92. }
  93. labels = append(labels, &dto.LabelPair{
  94. Name: toStringPtr("namespace"),
  95. Value: &dmlm.namespace,
  96. })
  97. labels = append(labels, &dto.LabelPair{
  98. Name: toStringPtr("deployment"),
  99. Value: &dmlm.deploymentName,
  100. })
  101. labels = append(labels, &dto.LabelPair{
  102. Name: toStringPtr("uid"),
  103. Value: &dmlm.uid,
  104. })
  105. m.Label = labels
  106. return nil
  107. }
  108. //--------------------------------------------------------------------------
  109. // KubeDeploymentCollector
  110. //--------------------------------------------------------------------------
  111. // KubeDeploymentCollector is a prometheus collector that generates
  112. type KubeDeploymentCollector struct {
  113. KubeClusterCache clustercache.ClusterCache
  114. metricsConfig MetricsConfig
  115. }
  116. // Describe sends the super-set of all possible descriptors of metrics
  117. // collected by this Collector.
  118. func (kdc KubeDeploymentCollector) Describe(ch chan<- *prometheus.Desc) {
  119. disabledMetrics := kdc.metricsConfig.GetDisabledMetricsMap()
  120. if _, disabled := disabledMetrics["kube_deployment_spec_replicas"]; !disabled {
  121. ch <- prometheus.NewDesc("kube_deployment_spec_replicas", "Number of desired pods for a deployment.", []string{}, nil)
  122. }
  123. if _, disabled := disabledMetrics["kube_deployment_status_replicas_available"]; !disabled {
  124. ch <- prometheus.NewDesc("kube_deployment_status_replicas_available", "The number of available replicas per deployment.", []string{}, nil)
  125. }
  126. }
  127. // Collect is called by the Prometheus registry when collecting metrics.
  128. func (kdc KubeDeploymentCollector) Collect(ch chan<- prometheus.Metric) {
  129. deployments := kdc.KubeClusterCache.GetAllDeployments()
  130. disabledMetrics := kdc.metricsConfig.GetDisabledMetricsMap()
  131. for _, deployment := range deployments {
  132. deploymentName := deployment.Name
  133. deploymentNS := deployment.Namespace
  134. deploymentUID := string(deployment.UID)
  135. // Replicas Defined
  136. var replicas int32
  137. if deployment.SpecReplicas == nil {
  138. replicas = 1 // defaults to 1, documented on the 'Replicas' field
  139. } else {
  140. replicas = *deployment.SpecReplicas
  141. }
  142. if _, disabled := disabledMetrics["kube_deployment_spec_replicas"]; !disabled {
  143. ch <- newKubeDeploymentReplicasMetric("kube_deployment_spec_replicas", deploymentName, deploymentNS, replicas, deploymentUID)
  144. }
  145. if _, disabled := disabledMetrics["kube_deployment_status_replicas_available"]; !disabled {
  146. // Replicas Available
  147. ch <- newKubeDeploymentStatusAvailableReplicasMetric(
  148. "kube_deployment_status_replicas_available",
  149. deploymentName,
  150. deploymentNS,
  151. deployment.StatusAvailableReplicas,
  152. deploymentUID)
  153. }
  154. }
  155. }
  156. //--------------------------------------------------------------------------
  157. // KubeDeploymentReplicasMetric
  158. //--------------------------------------------------------------------------
  159. // KubeDeploymentReplicasMetric is a prometheus.Metric used to encode deployment match labels
  160. type KubeDeploymentReplicasMetric struct {
  161. fqName string
  162. help string
  163. deployment string
  164. namespace string
  165. replicas float64
  166. uid string
  167. }
  168. // Creates a new DeploymentMatchLabelsMetric, implementation of prometheus.Metric
  169. func newKubeDeploymentReplicasMetric(fqname, deployment, namespace string, replicas int32, uid string) KubeDeploymentReplicasMetric {
  170. return KubeDeploymentReplicasMetric{
  171. fqName: fqname,
  172. help: "kube_deployment_spec_replicas Number of desired pods for a deployment.",
  173. deployment: deployment,
  174. namespace: namespace,
  175. replicas: float64(replicas),
  176. uid: uid,
  177. }
  178. }
  179. // Desc returns the descriptor for the Metric. This method idempotently
  180. // returns the same descriptor throughout the lifetime of the Metric.
  181. func (kdr KubeDeploymentReplicasMetric) Desc() *prometheus.Desc {
  182. l := prometheus.Labels{
  183. "deployment": kdr.deployment,
  184. "namespace": kdr.namespace,
  185. "uid": kdr.uid,
  186. }
  187. return prometheus.NewDesc(kdr.fqName, kdr.help, []string{}, l)
  188. }
  189. // Write encodes the Metric into a "Metric" Protocol Buffer data
  190. // transmission object.
  191. func (kdr KubeDeploymentReplicasMetric) Write(m *dto.Metric) error {
  192. m.Gauge = &dto.Gauge{
  193. Value: &kdr.replicas,
  194. }
  195. m.Label = []*dto.LabelPair{
  196. {
  197. Name: toStringPtr("namespace"),
  198. Value: &kdr.namespace,
  199. },
  200. {
  201. Name: toStringPtr("deployment"),
  202. Value: &kdr.deployment,
  203. },
  204. {
  205. Name: toStringPtr("uid"),
  206. Value: &kdr.uid,
  207. },
  208. }
  209. return nil
  210. }
  211. //--------------------------------------------------------------------------
  212. // KubeDeploymentStatusAvailableReplicasMetric
  213. //--------------------------------------------------------------------------
  214. // KubeDeploymentStatusAvailableReplicasMetric is a prometheus.Metric used to encode deployment match labels
  215. type KubeDeploymentStatusAvailableReplicasMetric struct {
  216. fqName string
  217. help string
  218. deployment string
  219. namespace string
  220. replicasAvailable float64
  221. uid string
  222. }
  223. // Creates a new DeploymentMatchLabelsMetric, implementation of prometheus.Metric
  224. func newKubeDeploymentStatusAvailableReplicasMetric(fqname, deployment, namespace string, replicasAvailable int32, uid string) KubeDeploymentStatusAvailableReplicasMetric {
  225. return KubeDeploymentStatusAvailableReplicasMetric{
  226. fqName: fqname,
  227. help: "kube_deployment_status_replicas_available The number of available replicas per deployment.",
  228. deployment: deployment,
  229. namespace: namespace,
  230. replicasAvailable: float64(replicasAvailable),
  231. uid: uid,
  232. }
  233. }
  234. // Desc returns the descriptor for the Metric. This method idempotently
  235. // returns the same descriptor throughout the lifetime of the Metric.
  236. func (kdr KubeDeploymentStatusAvailableReplicasMetric) Desc() *prometheus.Desc {
  237. l := prometheus.Labels{
  238. "deployment": kdr.deployment,
  239. "namespace": kdr.namespace,
  240. "uid": kdr.uid,
  241. }
  242. return prometheus.NewDesc(kdr.fqName, kdr.help, []string{}, l)
  243. }
  244. // Write encodes the Metric into a "Metric" Protocol Buffer data
  245. // transmission object.
  246. func (kdr KubeDeploymentStatusAvailableReplicasMetric) Write(m *dto.Metric) error {
  247. m.Gauge = &dto.Gauge{
  248. Value: &kdr.replicasAvailable,
  249. }
  250. m.Label = []*dto.LabelPair{
  251. {
  252. Name: toStringPtr("namespace"),
  253. Value: &kdr.namespace,
  254. },
  255. {
  256. Name: toStringPtr("deployment"),
  257. Value: &kdr.deployment,
  258. },
  259. {
  260. Name: toStringPtr("uid"),
  261. Value: &kdr.uid,
  262. },
  263. }
  264. return nil
  265. }