This repository has been archived by the owner on Dec 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
pod_aggregator.go
136 lines (118 loc) · 3.88 KB
/
pod_aggregator.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
// 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.
// Copyright 2018-2019 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package processors
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/wavefronthq/wavefront-collector-for-kubernetes/internal/metrics"
)
var LabelsToPopulate = []metrics.LabelDescriptor{
metrics.LabelPodId,
metrics.LabelPodName,
metrics.LabelNamespaceName,
metrics.LabelPodNamespaceUID,
metrics.LabelHostname,
metrics.LabelHostID,
}
type PodAggregator struct {
skippedMetrics map[string]struct{}
}
func (aggregator *PodAggregator) Name() string {
return "pod_aggregator"
}
func (aggregator *PodAggregator) Process(batch *metrics.Batch) (*metrics.Batch, error) {
newPods := make(map[metrics.ResourceKey]*metrics.Set)
// If pod already has pod-level metrics, it no longer needs to aggregates its container's metrics.
requireAggregate := make(map[string]bool)
for key, metricSet := range batch.Sets {
if metricSetType, found := metricSet.Labels[metrics.LabelMetricSetType.Key]; !found || metricSetType != metrics.MetricSetTypePodContainer {
continue
}
// Aggregating containers
podName, found := metricSet.Labels[metrics.LabelPodName.Key]
ns, found2 := metricSet.Labels[metrics.LabelNamespaceName.Key]
if !found || !found2 {
log.Errorf("No namespace and/or pod info in container %s: %v", key, metricSet.Labels)
continue
}
podKey := metrics.PodKey(ns, podName)
pod, found := batch.Sets[podKey]
if !found {
pod, found = newPods[podKey]
if !found {
log.Infof("Pod not found adding %s", podKey)
pod = aggregator.podMetricSet(metricSet.Labels)
newPods[podKey] = pod
}
}
for metricName, metricValue := range metricSet.Values {
if _, found := aggregator.skippedMetrics[metricName]; found {
continue
}
aggregatedValue, found := pod.Values[metricName]
if !found {
requireAggregate[podKey.String()+metricName] = true
aggregatedValue = metricValue
} else {
if requireAggregate[podKey.String()+metricName] {
if aggregatedValue.ValueType != metricValue.ValueType {
log.Errorf("PodAggregator: inconsistent type in %s", metricName)
continue
}
switch aggregatedValue.ValueType {
case metrics.ValueInt64:
aggregatedValue.IntValue += metricValue.IntValue
case metrics.ValueFloat:
aggregatedValue.FloatValue += metricValue.FloatValue
default:
return nil, fmt.Errorf("PodAggregator: type not supported in %s", metricName)
}
}
}
pod.Values[metricName] = aggregatedValue
}
}
for key, val := range newPods {
batch.Sets[key] = val
}
return batch, nil
}
func (aggregator *PodAggregator) podMetricSet(labels map[string]string) *metrics.Set {
newLabels := map[string]string{
metrics.LabelMetricSetType.Key: metrics.MetricSetTypePod,
}
for _, l := range LabelsToPopulate {
if val, ok := labels[l.Key]; ok {
newLabels[l.Key] = val
}
}
return &metrics.Set{
Values: make(map[string]metrics.Value),
Labels: newLabels,
}
}
func NewPodAggregator() *PodAggregator {
skipped := make(map[string]struct{})
for _, metric := range metrics.StandardMetrics {
if metric.MetricDescriptor.Type == metrics.Cumulative ||
metric.MetricDescriptor.Type == metrics.Delta {
skipped[metric.MetricDescriptor.Name] = struct{}{}
}
}
return &PodAggregator{
skippedMetrics: skipped,
}
}