prometheus_resource_metrics.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package stats
  14. import (
  15. "time"
  16. "k8s.io/component-base/metrics"
  17. "k8s.io/klog"
  18. stats "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
  19. )
  20. // NodeResourceMetric describes a metric for the node
  21. type NodeResourceMetric struct {
  22. Desc *metrics.Desc
  23. ValueFn func(stats.NodeStats) (*float64, time.Time)
  24. }
  25. func (n *NodeResourceMetric) desc() *metrics.Desc {
  26. return n.Desc
  27. }
  28. // ContainerResourceMetric describes a metric for containers
  29. type ContainerResourceMetric struct {
  30. Desc *metrics.Desc
  31. ValueFn func(stats.ContainerStats) (*float64, time.Time)
  32. }
  33. func (n *ContainerResourceMetric) desc() *metrics.Desc {
  34. return n.Desc
  35. }
  36. // ResourceMetricsConfig specifies which metrics to collect and export
  37. type ResourceMetricsConfig struct {
  38. NodeMetrics []NodeResourceMetric
  39. ContainerMetrics []ContainerResourceMetric
  40. }
  41. // NewPrometheusResourceMetricCollector returns a metrics.StableCollector which exports resource metrics
  42. func NewPrometheusResourceMetricCollector(provider SummaryProvider, config ResourceMetricsConfig) metrics.StableCollector {
  43. return &resourceMetricCollector{
  44. provider: provider,
  45. config: config,
  46. errors: metrics.NewDesc("scrape_error",
  47. "1 if there was an error while getting container metrics, 0 otherwise",
  48. nil,
  49. nil,
  50. metrics.ALPHA,
  51. "1.18.0"),
  52. }
  53. }
  54. type resourceMetricCollector struct {
  55. metrics.BaseStableCollector
  56. provider SummaryProvider
  57. config ResourceMetricsConfig
  58. errors *metrics.Desc
  59. }
  60. var _ metrics.StableCollector = &resourceMetricCollector{}
  61. // DescribeWithStability implements metrics.StableCollector
  62. func (rc *resourceMetricCollector) DescribeWithStability(ch chan<- *metrics.Desc) {
  63. ch <- rc.errors
  64. for _, metric := range rc.config.NodeMetrics {
  65. ch <- metric.desc()
  66. }
  67. for _, metric := range rc.config.ContainerMetrics {
  68. ch <- metric.desc()
  69. }
  70. }
  71. // CollectWithStability implements metrics.StableCollector
  72. // Since new containers are frequently created and removed, using the Gauge would
  73. // leak metric collectors for containers or pods that no longer exist. Instead, implement
  74. // custom collector in a way that only collects metrics for active containers.
  75. func (rc *resourceMetricCollector) CollectWithStability(ch chan<- metrics.Metric) {
  76. var errorCount float64
  77. defer func() {
  78. ch <- metrics.NewLazyConstMetric(rc.errors, metrics.GaugeValue, errorCount)
  79. }()
  80. summary, err := rc.provider.GetCPUAndMemoryStats()
  81. if err != nil {
  82. errorCount = 1
  83. klog.Warningf("Error getting summary for resourceMetric prometheus endpoint: %v", err)
  84. return
  85. }
  86. for _, metric := range rc.config.NodeMetrics {
  87. if value, timestamp := metric.ValueFn(summary.Node); value != nil {
  88. ch <- metrics.NewLazyMetricWithTimestamp(timestamp,
  89. metrics.NewLazyConstMetric(metric.desc(), metrics.GaugeValue, *value))
  90. }
  91. }
  92. for _, pod := range summary.Pods {
  93. for _, container := range pod.Containers {
  94. for _, metric := range rc.config.ContainerMetrics {
  95. if value, timestamp := metric.ValueFn(container); value != nil {
  96. ch <- metrics.NewLazyMetricWithTimestamp(timestamp,
  97. metrics.NewLazyConstMetric(metric.desc(), metrics.GaugeValue, *value, container.Name, pod.PodRef.Name, pod.PodRef.Namespace))
  98. }
  99. }
  100. }
  101. }
  102. }