|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package collectors |
| 18 | + |
| 19 | +import ( |
| 20 | + "time" |
| 21 | + |
| 22 | + "k8s.io/component-base/metrics" |
| 23 | + "k8s.io/klog" |
| 24 | + summary "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1" |
| 25 | + "k8s.io/kubernetes/pkg/kubelet/server/stats" |
| 26 | +) |
| 27 | + |
| 28 | +var ( |
| 29 | + nodeCPUUsageDesc = metrics.NewDesc("node_cpu_usage_seconds", |
| 30 | + "Cumulative cpu time consumed by the node in core-seconds", |
| 31 | + nil, |
| 32 | + nil, |
| 33 | + metrics.ALPHA, |
| 34 | + "") |
| 35 | + |
| 36 | + nodeMemoryUsageDesc = metrics.NewDesc("node_memory_working_set_bytes", |
| 37 | + "Current working set of the node in bytes", |
| 38 | + nil, |
| 39 | + nil, |
| 40 | + metrics.ALPHA, |
| 41 | + "") |
| 42 | + |
| 43 | + containerCPUUsageDesc = metrics.NewDesc("container_cpu_usage_seconds", |
| 44 | + "Cumulative cpu time consumed by the container in core-seconds", |
| 45 | + []string{"container", "pod", "namespace"}, |
| 46 | + nil, |
| 47 | + metrics.ALPHA, |
| 48 | + "") |
| 49 | + |
| 50 | + containerMemoryUsageDesc = metrics.NewDesc("container_memory_working_set_bytes", |
| 51 | + "Current working set of the container in bytes", |
| 52 | + []string{"container", "pod", "namespace"}, |
| 53 | + nil, |
| 54 | + metrics.ALPHA, |
| 55 | + "") |
| 56 | + |
| 57 | + resouceScrapeResultDesc = metrics.NewDesc("scrape_error", |
| 58 | + "1 if there was an error while getting container metrics, 0 otherwise", |
| 59 | + nil, |
| 60 | + nil, |
| 61 | + metrics.ALPHA, |
| 62 | + "") |
| 63 | +) |
| 64 | + |
| 65 | +// NewResourceMetricsCollector returns a metrics.StableCollector which exports resource metrics |
| 66 | +func NewResourceMetricsCollector(provider stats.SummaryProvider) metrics.StableCollector { |
| 67 | + return &resourceMetricsCollector{ |
| 68 | + provider: provider, |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +type resourceMetricsCollector struct { |
| 73 | + metrics.BaseStableCollector |
| 74 | + |
| 75 | + provider stats.SummaryProvider |
| 76 | +} |
| 77 | + |
| 78 | +// Check if resourceMetricsCollector implements necessary interface |
| 79 | +var _ metrics.StableCollector = &resourceMetricsCollector{} |
| 80 | + |
| 81 | +// DescribeWithStability implements metrics.StableCollector |
| 82 | +func (rc *resourceMetricsCollector) DescribeWithStability(ch chan<- *metrics.Desc) { |
| 83 | + ch <- nodeCPUUsageDesc |
| 84 | + ch <- nodeMemoryUsageDesc |
| 85 | + ch <- containerCPUUsageDesc |
| 86 | + ch <- containerMemoryUsageDesc |
| 87 | + ch <- resouceScrapeResultDesc |
| 88 | +} |
| 89 | + |
| 90 | +// CollectWithStability implements metrics.StableCollector |
| 91 | +// Since new containers are frequently created and removed, using the Gauge would |
| 92 | +// leak metric collectors for containers or pods that no longer exist. Instead, implement |
| 93 | +// custom collector in a way that only collects metrics for active containers. |
| 94 | +func (rc *resourceMetricsCollector) CollectWithStability(ch chan<- metrics.Metric) { |
| 95 | + var errorCount float64 |
| 96 | + defer func() { |
| 97 | + ch <- metrics.NewLazyConstMetric(resouceScrapeResultDesc, metrics.GaugeValue, errorCount) |
| 98 | + }() |
| 99 | + statsSummary, err := rc.provider.GetCPUAndMemoryStats() |
| 100 | + if err != nil { |
| 101 | + errorCount = 1 |
| 102 | + klog.Warningf("Error getting summary for resourceMetric prometheus endpoint: %v", err) |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + rc.collectNodeCPUMetrics(ch, statsSummary.Node) |
| 107 | + rc.collectNodeMemoryMetrics(ch, statsSummary.Node) |
| 108 | + |
| 109 | + for _, pod := range statsSummary.Pods { |
| 110 | + for _, container := range pod.Containers { |
| 111 | + rc.collectContainerCPUMetrics(ch, pod, container) |
| 112 | + rc.collectContainerMemoryMetrics(ch, pod, container) |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func (rc *resourceMetricsCollector) collectNodeCPUMetrics(ch chan<- metrics.Metric, s summary.NodeStats) { |
| 118 | + if s.CPU == nil { |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + ch <- metrics.NewLazyMetricWithTimestamp(s.CPU.Time.Time, |
| 123 | + metrics.NewLazyConstMetric(nodeCPUUsageDesc, metrics.GaugeValue, float64(*s.CPU.UsageCoreNanoSeconds)/float64(time.Second))) |
| 124 | +} |
| 125 | + |
| 126 | +func (rc *resourceMetricsCollector) collectNodeMemoryMetrics(ch chan<- metrics.Metric, s summary.NodeStats) { |
| 127 | + if s.Memory == nil { |
| 128 | + return |
| 129 | + } |
| 130 | + |
| 131 | + ch <- metrics.NewLazyMetricWithTimestamp(s.Memory.Time.Time, |
| 132 | + metrics.NewLazyConstMetric(nodeMemoryUsageDesc, metrics.GaugeValue, float64(*s.Memory.WorkingSetBytes))) |
| 133 | +} |
| 134 | + |
| 135 | +func (rc *resourceMetricsCollector) collectContainerCPUMetrics(ch chan<- metrics.Metric, pod summary.PodStats, s summary.ContainerStats) { |
| 136 | + if s.CPU == nil { |
| 137 | + return |
| 138 | + } |
| 139 | + |
| 140 | + ch <- metrics.NewLazyMetricWithTimestamp(s.CPU.Time.Time, |
| 141 | + metrics.NewLazyConstMetric(containerCPUUsageDesc, metrics.GaugeValue, |
| 142 | + float64(*s.CPU.UsageCoreNanoSeconds)/float64(time.Second), s.Name, pod.PodRef.Name, pod.PodRef.Namespace)) |
| 143 | +} |
| 144 | + |
| 145 | +func (rc *resourceMetricsCollector) collectContainerMemoryMetrics(ch chan<- metrics.Metric, pod summary.PodStats, s summary.ContainerStats) { |
| 146 | + if s.Memory == nil { |
| 147 | + return |
| 148 | + } |
| 149 | + |
| 150 | + ch <- metrics.NewLazyMetricWithTimestamp(s.Memory.Time.Time, |
| 151 | + metrics.NewLazyConstMetric(containerMemoryUsageDesc, metrics.GaugeValue, |
| 152 | + float64(*s.Memory.WorkingSetBytes), s.Name, pod.PodRef.Name, pod.PodRef.Namespace)) |
| 153 | +} |
0 commit comments