deploymentmetrics.go 9.2 KB

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