-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
scraper.go
223 lines (196 loc) · 7.31 KB
/
scraper.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package kubeletstatsreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kubeletstatsreceiver"
import (
"context"
"fmt"
"sync"
"time"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/scraperhelper"
"go.uber.org/zap"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kubeletstatsreceiver/internal/kubelet"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kubeletstatsreceiver/internal/metadata"
)
type scraperOptions struct {
collectionInterval time.Duration
extraMetadataLabels []kubelet.MetadataLabel
metricGroupsToCollect map[kubelet.MetricGroup]bool
k8sAPIClient kubernetes.Interface
}
type kubletScraper struct {
statsProvider *kubelet.StatsProvider
metadataProvider *kubelet.MetadataProvider
logger *zap.Logger
extraMetadataLabels []kubelet.MetadataLabel
metricGroupsToCollect map[kubelet.MetricGroup]bool
k8sAPIClient kubernetes.Interface
cachedVolumeSource map[string]v1.PersistentVolumeSource
mbs *metadata.MetricsBuilders
needsResources bool
nodeInformer cache.SharedInformer
stopCh chan struct{}
m sync.RWMutex
// A struct that keeps Node's resource capacities
nodeLimits *kubelet.NodeCapacity
}
func newKubletScraper(
restClient kubelet.RestClient,
set receiver.Settings,
rOptions *scraperOptions,
metricsConfig metadata.MetricsBuilderConfig,
nodeName string,
) (scraperhelper.Scraper, error) {
ks := &kubletScraper{
statsProvider: kubelet.NewStatsProvider(restClient),
metadataProvider: kubelet.NewMetadataProvider(restClient),
logger: set.Logger,
extraMetadataLabels: rOptions.extraMetadataLabels,
metricGroupsToCollect: rOptions.metricGroupsToCollect,
k8sAPIClient: rOptions.k8sAPIClient,
cachedVolumeSource: make(map[string]v1.PersistentVolumeSource),
mbs: &metadata.MetricsBuilders{
NodeMetricsBuilder: metadata.NewMetricsBuilder(metricsConfig, set),
PodMetricsBuilder: metadata.NewMetricsBuilder(metricsConfig, set),
ContainerMetricsBuilder: metadata.NewMetricsBuilder(metricsConfig, set),
OtherMetricsBuilder: metadata.NewMetricsBuilder(metricsConfig, set),
},
needsResources: metricsConfig.Metrics.K8sPodCPULimitUtilization.Enabled ||
metricsConfig.Metrics.K8sPodCPURequestUtilization.Enabled ||
metricsConfig.Metrics.K8sContainerCPULimitUtilization.Enabled ||
metricsConfig.Metrics.K8sContainerCPURequestUtilization.Enabled ||
metricsConfig.Metrics.K8sPodMemoryLimitUtilization.Enabled ||
metricsConfig.Metrics.K8sPodMemoryRequestUtilization.Enabled ||
metricsConfig.Metrics.K8sContainerMemoryLimitUtilization.Enabled ||
metricsConfig.Metrics.K8sContainerMemoryRequestUtilization.Enabled,
stopCh: make(chan struct{}),
nodeLimits: &kubelet.NodeCapacity{},
}
if metricsConfig.Metrics.K8sContainerCPUNodeUtilization.Enabled ||
metricsConfig.Metrics.K8sPodCPUNodeUtilization.Enabled ||
metricsConfig.Metrics.K8sContainerMemoryNodeUtilization.Enabled ||
metricsConfig.Metrics.K8sPodMemoryNodeUtilization.Enabled {
ks.nodeInformer = k8sconfig.NewNodeSharedInformer(rOptions.k8sAPIClient, nodeName, 5*time.Minute)
}
return scraperhelper.NewScraper(
metadata.Type,
ks.scrape,
scraperhelper.WithStart(ks.start),
scraperhelper.WithShutdown(ks.shutdown),
)
}
func (r *kubletScraper) scrape(context.Context) (pmetric.Metrics, error) {
summary, err := r.statsProvider.StatsSummary()
if err != nil {
r.logger.Error("call to /stats/summary endpoint failed", zap.Error(err))
return pmetric.Metrics{}, err
}
var podsMetadata *v1.PodList
// fetch metadata only when extra metadata labels are needed
if len(r.extraMetadataLabels) > 0 || r.needsResources {
podsMetadata, err = r.metadataProvider.Pods()
if err != nil {
r.logger.Error("call to /pods endpoint failed", zap.Error(err))
return pmetric.Metrics{}, err
}
}
var node kubelet.NodeCapacity
if r.nodeInformer != nil {
node = r.node()
}
metaD := kubelet.NewMetadata(r.extraMetadataLabels, podsMetadata, node, r.detailedPVCLabelsSetter())
mds := kubelet.MetricsData(r.logger, summary, metaD, r.metricGroupsToCollect, r.mbs)
md := pmetric.NewMetrics()
for i := range mds {
mds[i].ResourceMetrics().MoveAndAppendTo(md.ResourceMetrics())
}
return md, nil
}
func (r *kubletScraper) detailedPVCLabelsSetter() func(rb *metadata.ResourceBuilder, volCacheID, volumeClaim, namespace string) error {
return func(rb *metadata.ResourceBuilder, volCacheID, volumeClaim, namespace string) error {
if r.k8sAPIClient == nil {
return nil
}
if _, ok := r.cachedVolumeSource[volCacheID]; !ok {
ctx := context.Background()
pvc, err := r.k8sAPIClient.CoreV1().PersistentVolumeClaims(namespace).Get(ctx, volumeClaim, metav1.GetOptions{})
if err != nil {
return err
}
volName := pvc.Spec.VolumeName
if volName == "" {
return fmt.Errorf("PersistentVolumeClaim %s does not have a volume name", pvc.Name)
}
pv, err := r.k8sAPIClient.CoreV1().PersistentVolumes().Get(ctx, volName, metav1.GetOptions{})
if err != nil {
return err
}
// Cache collected source.
r.cachedVolumeSource[volCacheID] = pv.Spec.PersistentVolumeSource
}
kubelet.SetPersistentVolumeLabels(rb, r.cachedVolumeSource[volCacheID])
return nil
}
}
func (r *kubletScraper) node() kubelet.NodeCapacity {
r.m.RLock()
defer r.m.RUnlock()
return *r.nodeLimits
}
func (r *kubletScraper) start(_ context.Context, _ component.Host) error {
if r.nodeInformer != nil {
_, err := r.nodeInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: r.handleNodeAdd,
UpdateFunc: r.handleNodeUpdate,
})
if err != nil {
r.logger.Error("error adding event handler to node informer", zap.Error(err))
}
go r.nodeInformer.Run(r.stopCh)
}
return nil
}
func (r *kubletScraper) shutdown(_ context.Context) error {
r.logger.Debug("executing close")
if r.stopCh != nil {
close(r.stopCh)
}
return nil
}
func (r *kubletScraper) handleNodeAdd(obj any) {
if node, ok := obj.(*v1.Node); ok {
r.addOrUpdateNode(node)
} else {
r.logger.Error("object received was not of type v1.Node", zap.Any("received", obj))
}
}
func (r *kubletScraper) handleNodeUpdate(_, newNode any) {
if node, ok := newNode.(*v1.Node); ok {
r.addOrUpdateNode(node)
} else {
r.logger.Error("object received was not of type v1.Node", zap.Any("received", newNode))
}
}
func (r *kubletScraper) addOrUpdateNode(node *v1.Node) {
r.m.Lock()
defer r.m.Unlock()
if cpu, ok := node.Status.Capacity["cpu"]; ok {
if q, err := resource.ParseQuantity(cpu.String()); err == nil {
r.nodeLimits.CPUCapacity = float64(q.MilliValue()) / 1000
}
}
if memory, ok := node.Status.Capacity["memory"]; ok {
// ie: 32564740Ki
if q, err := resource.ParseQuantity(memory.String()); err == nil {
r.nodeLimits.MemoryCapacity = float64(q.Value())
}
}
}