prometheus_resource_metrics.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/klog"
  17. stats "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
  18. "github.com/prometheus/client_golang/prometheus"
  19. )
  20. // NodeResourceMetric describes a metric for the node
  21. type NodeResourceMetric struct {
  22. Name string
  23. Description string
  24. ValueFn func(stats.NodeStats) (*float64, time.Time)
  25. }
  26. func (n *NodeResourceMetric) desc() *prometheus.Desc {
  27. return prometheus.NewDesc(n.Name, n.Description, []string{}, nil)
  28. }
  29. // ContainerResourceMetric describes a metric for containers
  30. type ContainerResourceMetric struct {
  31. Name string
  32. Description string
  33. ValueFn func(stats.ContainerStats) (*float64, time.Time)
  34. }
  35. func (n *ContainerResourceMetric) desc() *prometheus.Desc {
  36. return prometheus.NewDesc(n.Name, n.Description, []string{"container", "pod", "namespace"}, nil)
  37. }
  38. // ResourceMetricsConfig specifies which metrics to collect and export
  39. type ResourceMetricsConfig struct {
  40. NodeMetrics []NodeResourceMetric
  41. ContainerMetrics []ContainerResourceMetric
  42. }
  43. // NewPrometheusResourceMetricCollector returns a prometheus.Collector which exports resource metrics
  44. func NewPrometheusResourceMetricCollector(provider SummaryProvider, config ResourceMetricsConfig) prometheus.Collector {
  45. return &resourceMetricCollector{
  46. provider: provider,
  47. config: config,
  48. errors: prometheus.NewGauge(prometheus.GaugeOpts{
  49. Name: "scrape_error",
  50. Help: "1 if there was an error while getting container metrics, 0 otherwise",
  51. }),
  52. }
  53. }
  54. type resourceMetricCollector struct {
  55. provider SummaryProvider
  56. config ResourceMetricsConfig
  57. errors prometheus.Gauge
  58. }
  59. var _ prometheus.Collector = &resourceMetricCollector{}
  60. // Describe implements prometheus.Collector
  61. func (rc *resourceMetricCollector) Describe(ch chan<- *prometheus.Desc) {
  62. rc.errors.Describe(ch)
  63. for _, metric := range rc.config.NodeMetrics {
  64. ch <- metric.desc()
  65. }
  66. for _, metric := range rc.config.ContainerMetrics {
  67. ch <- metric.desc()
  68. }
  69. }
  70. // Collect implements prometheus.Collector
  71. // Since new containers are frequently created and removed, using the prometheus.Gauge Collector would
  72. // leak metric collectors for containers or pods that no longer exist. Instead, implement
  73. // prometheus.Collector in a way that only collects metrics for active containers.
  74. func (rc *resourceMetricCollector) Collect(ch chan<- prometheus.Metric) {
  75. rc.errors.Set(0)
  76. defer rc.errors.Collect(ch)
  77. summary, err := rc.provider.GetCPUAndMemoryStats()
  78. if err != nil {
  79. rc.errors.Set(1)
  80. klog.Warningf("Error getting summary for resourceMetric prometheus endpoint: %v", err)
  81. return
  82. }
  83. for _, metric := range rc.config.NodeMetrics {
  84. if value, timestamp := metric.ValueFn(summary.Node); value != nil {
  85. ch <- prometheus.NewMetricWithTimestamp(timestamp,
  86. prometheus.MustNewConstMetric(metric.desc(), prometheus.GaugeValue, *value))
  87. }
  88. }
  89. for _, pod := range summary.Pods {
  90. for _, container := range pod.Containers {
  91. for _, metric := range rc.config.ContainerMetrics {
  92. if value, timestamp := metric.ValueFn(container); value != nil {
  93. ch <- prometheus.NewMetricWithTimestamp(timestamp,
  94. prometheus.MustNewConstMetric(metric.desc(), prometheus.GaugeValue, *value, container.Name, pod.PodRef.Name, pod.PodRef.Namespace))
  95. }
  96. }
  97. }
  98. }
  99. }