This repository has been archived by the owner on Dec 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
summary.go
465 lines (405 loc) · 15.4 KB
/
summary.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package summary
import (
"fmt"
"net/url"
"time"
. "k8s.io/heapster/metrics/core"
"k8s.io/heapster/metrics/sources/kubelet"
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/apimachinery/pkg/labels"
kube_client "k8s.io/client-go/kubernetes"
v1listers "k8s.io/client-go/listers/core/v1"
kube_api "k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/heapster/metrics/util"
stats "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
)
var (
summaryRequestLatency = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: "heapster",
Subsystem: "kubelet_summary",
Name: "request_duration_microseconds",
Help: "The Kubelet summary request latencies in microseconds.",
},
[]string{"node"},
)
)
// Prefix used for the LabelResourceID for volume metrics.
const VolumeResourcePrefix = "Volume:"
func init() {
prometheus.MustRegister(summaryRequestLatency)
}
type NodeInfo struct {
kubelet.Host
NodeName string
HostName string
HostID string
KubeletVersion string
}
// Kubelet-provided metrics for pod and system container.
type summaryMetricsSource struct {
node NodeInfo
kubeletClient *kubelet.KubeletClient
}
func NewSummaryMetricsSource(node NodeInfo, client *kubelet.KubeletClient) MetricsSource {
return &summaryMetricsSource{
node: node,
kubeletClient: client,
}
}
func (this *summaryMetricsSource) Name() string {
return this.String()
}
func (this *summaryMetricsSource) String() string {
return fmt.Sprintf("kubelet_summary:%s:%d", this.node.IP, this.node.Port)
}
func (this *summaryMetricsSource) ScrapeMetrics(start, end time.Time) (*DataBatch, error) {
result := &DataBatch{
Timestamp: time.Now(),
MetricSets: map[string]*MetricSet{},
}
summary, err := func() (*stats.Summary, error) {
startTime := time.Now()
defer summaryRequestLatency.WithLabelValues(this.node.HostName).Observe(float64(time.Since(startTime)))
return this.kubeletClient.GetSummary(this.node.Host)
}()
if err != nil {
return nil, err
}
result.MetricSets = this.decodeSummary(summary)
return result, err
}
const (
RootFsKey = "/"
LogsKey = "logs"
)
// For backwards compatibility, map summary system names into original names.
// TODO: Migrate to the new system names and remove this.
var systemNameMap = map[string]string{
stats.SystemContainerRuntime: "docker-daemon",
stats.SystemContainerMisc: "system",
}
// decodeSummary translates the kubelet stats.Summary API into the flattened heapster MetricSet API.
func (this *summaryMetricsSource) decodeSummary(summary *stats.Summary) map[string]*MetricSet {
glog.V(9).Infof("Begin summary decode")
result := map[string]*MetricSet{}
labels := map[string]string{
LabelNodename.Key: this.node.NodeName,
LabelHostname.Key: this.node.HostName,
LabelHostID.Key: this.node.HostID,
}
this.decodeNodeStats(result, labels, &summary.Node)
for _, pod := range summary.Pods {
this.decodePodStats(result, labels, &pod)
}
glog.V(9).Infof("End summary decode")
return result
}
// Convenience method for labels deep copy.
func (this *summaryMetricsSource) cloneLabels(labels map[string]string) map[string]string {
clone := make(map[string]string, len(labels))
for k, v := range labels {
clone[k] = v
}
return clone
}
func (this *summaryMetricsSource) decodeNodeStats(metrics map[string]*MetricSet, labels map[string]string, node *stats.NodeStats) {
glog.V(9).Infof("Decoding node stats for node %s...", node.NodeName)
nodeMetrics := &MetricSet{
Labels: this.cloneLabels(labels),
MetricValues: map[string]MetricValue{},
LabeledMetrics: []LabeledMetric{},
CollectionStartTime: node.StartTime.Time,
ScrapeTime: this.getScrapeTime(node.CPU, node.Memory, node.Network),
}
nodeMetrics.Labels[LabelMetricSetType.Key] = MetricSetTypeNode
this.decodeUptime(nodeMetrics, node.StartTime.Time)
this.decodeCPUStats(nodeMetrics, node.CPU)
this.decodeMemoryStats(nodeMetrics, node.Memory)
this.decodeNetworkStats(nodeMetrics, node.Network)
this.decodeFsStats(nodeMetrics, RootFsKey, node.Fs)
metrics[NodeKey(node.NodeName)] = nodeMetrics
for _, container := range node.SystemContainers {
key := NodeContainerKey(node.NodeName, this.getSystemContainerName(&container))
containerMetrics := this.decodeContainerStats(labels, &container, true)
containerMetrics.Labels[LabelMetricSetType.Key] = MetricSetTypeSystemContainer
metrics[key] = containerMetrics
}
}
func (this *summaryMetricsSource) decodePodStats(metrics map[string]*MetricSet, nodeLabels map[string]string, pod *stats.PodStats) {
glog.V(9).Infof("Decoding pod stats for pod %s/%s (%s)...", pod.PodRef.Namespace, pod.PodRef.Name, pod.PodRef.UID)
podMetrics := &MetricSet{
Labels: this.cloneLabels(nodeLabels),
MetricValues: map[string]MetricValue{},
LabeledMetrics: []LabeledMetric{},
CollectionStartTime: pod.StartTime.Time,
ScrapeTime: this.getScrapeTime(nil, nil, pod.Network),
}
ref := pod.PodRef
podMetrics.Labels[LabelMetricSetType.Key] = MetricSetTypePod
podMetrics.Labels[LabelPodId.Key] = ref.UID
podMetrics.Labels[LabelPodName.Key] = ref.Name
podMetrics.Labels[LabelNamespaceName.Key] = ref.Namespace
this.decodeUptime(podMetrics, pod.StartTime.Time)
this.decodeNetworkStats(podMetrics, pod.Network)
for _, vol := range pod.VolumeStats {
this.decodeFsStats(podMetrics, VolumeResourcePrefix+vol.Name, &vol.FsStats)
}
metrics[PodKey(ref.Namespace, ref.Name)] = podMetrics
for _, container := range pod.Containers {
key := PodContainerKey(ref.Namespace, ref.Name, container.Name)
// This check ensures that we are not replacing metrics of running container with metrics of terminated one if
// there are two exactly same containers reported by kubelet.
if _, exist := metrics[key]; exist {
glog.V(2).Infof("Metrics reported from two containers with the same key: %v. Create time of "+
"containers are %v and %v. Metrics from the older container are going to be dropped.", key,
container.StartTime.Time, metrics[key].CollectionStartTime)
if container.StartTime.Time.Before(metrics[key].CollectionStartTime) {
continue
}
}
metrics[key] = this.decodeContainerStats(podMetrics.Labels, &container, false)
}
}
func (this *summaryMetricsSource) decodeContainerStats(podLabels map[string]string, container *stats.ContainerStats, isSystemContainer bool) *MetricSet {
glog.V(9).Infof("Decoding container stats stats for container %s...", container.Name)
containerMetrics := &MetricSet{
Labels: this.cloneLabels(podLabels),
MetricValues: map[string]MetricValue{},
LabeledMetrics: []LabeledMetric{},
CollectionStartTime: container.StartTime.Time,
ScrapeTime: this.getScrapeTime(container.CPU, container.Memory, nil),
}
containerMetrics.Labels[LabelMetricSetType.Key] = MetricSetTypePodContainer
if isSystemContainer {
containerMetrics.Labels[LabelContainerName.Key] = this.getSystemContainerName(container)
} else {
containerMetrics.Labels[LabelContainerName.Key] = container.Name
}
this.decodeUptime(containerMetrics, container.StartTime.Time)
this.decodeCPUStats(containerMetrics, container.CPU)
this.decodeMemoryStats(containerMetrics, container.Memory)
this.decodeAcceleratorStats(containerMetrics, container.Accelerators)
this.decodeFsStats(containerMetrics, RootFsKey, container.Rootfs)
this.decodeFsStats(containerMetrics, LogsKey, container.Logs)
this.decodeUserDefinedMetrics(containerMetrics, container.UserDefinedMetrics)
return containerMetrics
}
func (this *summaryMetricsSource) decodeUptime(metrics *MetricSet, startTime time.Time) {
if startTime.IsZero() {
glog.V(9).Infof("missing start time!")
return
}
uptime := uint64(time.Since(startTime).Nanoseconds() / time.Millisecond.Nanoseconds())
this.addIntMetric(metrics, &MetricUptime, &uptime)
}
func (this *summaryMetricsSource) decodeCPUStats(metrics *MetricSet, cpu *stats.CPUStats) {
if cpu == nil {
glog.V(9).Infof("missing cpu usage metric!")
return
}
this.addIntMetric(metrics, &MetricCpuUsage, cpu.UsageCoreNanoSeconds)
}
func (this *summaryMetricsSource) decodeMemoryStats(metrics *MetricSet, memory *stats.MemoryStats) {
if memory == nil {
glog.V(9).Infof("missing memory metrics!")
return
}
this.addIntMetric(metrics, &MetricMemoryUsage, memory.UsageBytes)
this.addIntMetric(metrics, &MetricMemoryWorkingSet, memory.WorkingSetBytes)
this.addIntMetric(metrics, &MetricMemoryRSS, memory.RSSBytes)
this.addIntMetric(metrics, &MetricMemoryPageFaults, memory.PageFaults)
this.addIntMetric(metrics, &MetricMemoryMajorPageFaults, memory.MajorPageFaults)
}
func (this *summaryMetricsSource) decodeAcceleratorStats(metrics *MetricSet, accelerators []stats.AcceleratorStats) {
for _, accelerator := range accelerators {
acceleratorLabels := map[string]string{
LabelAcceleratorMake.Key: accelerator.Make,
LabelAcceleratorModel.Key: accelerator.Model,
LabelAcceleratorID.Key: accelerator.ID,
}
this.addLabeledIntMetric(metrics, &MetricAcceleratorMemoryTotal, acceleratorLabels, &accelerator.MemoryTotal)
this.addLabeledIntMetric(metrics, &MetricAcceleratorMemoryUsed, acceleratorLabels, &accelerator.MemoryUsed)
this.addLabeledIntMetric(metrics, &MetricAcceleratorDutyCycle, acceleratorLabels, &accelerator.DutyCycle)
}
}
func (this *summaryMetricsSource) decodeNetworkStats(metrics *MetricSet, network *stats.NetworkStats) {
if network == nil {
glog.V(9).Infof("missing network metrics!")
return
}
this.addIntMetric(metrics, &MetricNetworkRx, network.RxBytes)
this.addIntMetric(metrics, &MetricNetworkRxErrors, network.RxErrors)
this.addIntMetric(metrics, &MetricNetworkTx, network.TxBytes)
this.addIntMetric(metrics, &MetricNetworkTxErrors, network.TxErrors)
}
func (this *summaryMetricsSource) decodeFsStats(metrics *MetricSet, fsKey string, fs *stats.FsStats) {
if fs == nil {
glog.V(9).Infof("missing fs metrics!")
return
}
fsLabels := map[string]string{LabelResourceID.Key: fsKey}
this.addLabeledIntMetric(metrics, &MetricFilesystemUsage, fsLabels, fs.UsedBytes)
this.addLabeledIntMetric(metrics, &MetricFilesystemLimit, fsLabels, fs.CapacityBytes)
this.addLabeledIntMetric(metrics, &MetricFilesystemAvailable, fsLabels, fs.AvailableBytes)
this.addLabeledIntMetric(metrics, &MetricFilesystemInodes, fsLabels, fs.Inodes)
this.addLabeledIntMetric(metrics, &MetricFilesystemInodesFree, fsLabels, fs.InodesFree)
}
func (this *summaryMetricsSource) decodeUserDefinedMetrics(metrics *MetricSet, udm []stats.UserDefinedMetric) {
for _, metric := range udm {
mv := MetricValue{}
switch metric.Type {
case stats.MetricGauge:
mv.MetricType = MetricGauge
case stats.MetricCumulative:
mv.MetricType = MetricCumulative
case stats.MetricDelta:
mv.MetricType = MetricDelta
default:
glog.V(4).Infof("Skipping %s: unknown custom metric type: %v", metric.Name, metric.Type)
continue
}
// TODO: Handle double-precision values.
mv.ValueType = ValueFloat
mv.FloatValue = metric.Value
metrics.MetricValues[CustomMetricPrefix+metric.Name] = mv
}
}
func (this *summaryMetricsSource) getScrapeTime(cpu *stats.CPUStats, memory *stats.MemoryStats, network *stats.NetworkStats) time.Time {
// Assume CPU, memory and network scrape times are the same.
switch {
case cpu != nil && !cpu.Time.IsZero():
return cpu.Time.Time
case memory != nil && !memory.Time.IsZero():
return memory.Time.Time
case network != nil && !network.Time.IsZero():
return network.Time.Time
default:
return time.Time{}
}
}
// addIntMetric is a convenience method for adding the metric and value to the metric set.
func (this *summaryMetricsSource) addIntMetric(metrics *MetricSet, metric *Metric, value *uint64) {
if value == nil {
glog.V(9).Infof("skipping metric %s because the value was nil", metric.Name)
return
}
val := MetricValue{
ValueType: ValueInt64,
MetricType: metric.Type,
IntValue: int64(*value),
}
metrics.MetricValues[metric.Name] = val
}
// addLabeledIntMetric is a convenience method for adding the labeled metric and value to the metric set.
func (this *summaryMetricsSource) addLabeledIntMetric(metrics *MetricSet, metric *Metric, labels map[string]string, value *uint64) {
if value == nil {
glog.V(9).Infof("skipping labeled metric %s (%v) because the value was nil", metric.Name, labels)
return
}
val := LabeledMetric{
Name: metric.Name,
Labels: labels,
MetricValue: MetricValue{
ValueType: ValueInt64,
MetricType: metric.Type,
IntValue: int64(*value),
},
}
metrics.LabeledMetrics = append(metrics.LabeledMetrics, val)
}
// Translate system container names to the legacy names for backwards compatibility.
func (this *summaryMetricsSource) getSystemContainerName(c *stats.ContainerStats) string {
if legacyName, ok := systemNameMap[c.Name]; ok {
return legacyName
}
return c.Name
}
// TODO: The summaryProvider duplicates a lot of code from kubeletProvider, and should be refactored.
type summaryProvider struct {
nodeLister v1listers.NodeLister
reflector *cache.Reflector
kubeletClient *kubelet.KubeletClient
hostIDAnnotation string
}
func (this *summaryProvider) GetMetricsSources() []MetricsSource {
sources := []MetricsSource{}
nodes, err := this.nodeLister.List(labels.Everything())
if err != nil {
glog.Errorf("error while listing nodes: %v", err)
return sources
}
for _, node := range nodes {
info, err := this.getNodeInfo(node)
if err != nil {
glog.Errorf("%v", err)
continue
}
sources = append(sources, NewSummaryMetricsSource(info, this.kubeletClient))
}
return sources
}
func (this *summaryProvider) getNodeInfo(node *kube_api.Node) (NodeInfo, error) {
hostname, ip, err := kubelet.GetNodeHostnameAndIP(node)
if err != nil {
return NodeInfo{}, err
}
if hostname == "" {
hostname = node.Name
}
hostID := node.Spec.ExternalID
if hostID == "" && this.hostIDAnnotation != "" {
hostID = node.Annotations[this.hostIDAnnotation]
}
info := NodeInfo{
NodeName: node.Name,
HostName: hostname,
HostID: hostID,
Host: kubelet.Host{
IP: ip,
Port: this.kubeletClient.GetPort(),
},
KubeletVersion: node.Status.NodeInfo.KubeletVersion,
}
return info, nil
}
func NewSummaryProvider(uri *url.URL) (MetricsSourceProvider, error) {
opts := uri.Query()
hostIDAnnotation := ""
if len(opts["host_id_annotation"]) > 0 {
hostIDAnnotation = opts["host_id_annotation"][0]
}
// create clients
kubeConfig, kubeletConfig, err := kubelet.GetKubeConfigs(uri)
if err != nil {
return nil, err
}
kubeClient := kube_client.NewForConfigOrDie(kubeConfig)
kubeletClient, err := kubelet.NewKubeletClient(kubeletConfig)
if err != nil {
return nil, err
}
// watch nodes
nodeLister, reflector, _ := util.GetNodeLister(kubeClient)
return &summaryProvider{
nodeLister: nodeLister,
reflector: reflector,
kubeletClient: kubeletClient,
hostIDAnnotation: hostIDAnnotation,
}, nil
}