/
metrics_scraper.go
214 lines (181 loc) · 6.47 KB
/
metrics_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
// Copyright 2020-2023 Politecnico di Torino
//
// 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 instmetrics
import (
"context"
"fmt"
"sync"
"time"
"github.com/go-logr/logr"
criapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
"k8s.io/utils/trace"
clctx "github.com/netgroup-polito/CrownLabs/operators/pkg/context"
"github.com/netgroup-polito/CrownLabs/operators/pkg/forge"
"github.com/netgroup-polito/CrownLabs/operators/pkg/utils"
)
// StatsScraper interface.
type StatsScraper interface {
getStats(ctx context.Context, containers []*criapi.Container) ([]ContainerStats, error)
}
// ContainerStats used to compute metrics.
type ContainerStats struct {
// Timestamp in nanoseconds at which the information were collected
CPUTimestamp int64
// Cumulative CPU usage (sum across all cores) since object creation.
UsageCoreNanoSeconds uint64
// Total memory in use.
MemoryUsageInBytes uint64
// UsedBytes represents the bytes used for images on the filesystem.
// This may differ from the total bytes used on the filesystem and may not
// equal CapacityBytes - AvailableBytes.
DiskUsageInBytes uint64
container *criapi.Container
runningContainer bool
}
// CustomMetrics stores desired metrics of a container.
type CustomMetrics struct {
CPUPerc float32 `json:"cpu"`
MemBytes uint64 `json:"mem"`
DiskBytes uint64 `json:"disk"`
}
func (c CustomMetrics) String() string {
return fmt.Sprintf("[CPU] %v \t[MEM] %v \t[DISK] %v", c.CPUPerc, c.MemBytes, c.DiskBytes)
}
// MetricsScraper interface.
type MetricsScraper struct {
Log logr.Logger
UpdatePeriod time.Duration
RuntimeClient *RemoteRuntimeServiceClient
// Containers ignored while retrieving metrics.
ignoredContainerNames []string
// Stats used to evaluate resource usages changes
// <key=podName, val=ContainerStats>
oldStats map[string]*ContainerStats
// Stats used to evaluate resource usages changes
// <key=podName, val=CustomMetrics>
cachedMetrics map[string]*CustomMetrics
cachedMetricsMutex sync.RWMutex
scraper StatsScraper
}
// Start scraping metrics.
func (ms *MetricsScraper) Start(ctx context.Context) {
ms.ignoredContainerNames = []string{
forge.XVncName,
forge.WebsockifyName,
forge.ContentDownloaderName,
}
ms.oldStats = map[string]*ContainerStats{}
// Periodically scrape updated metrics for watched containers.
ticker := time.NewTicker(ms.UpdatePeriod)
ms.Log.Info("MetricsScraper started", "updatePeriod", ms.UpdatePeriod)
for range ticker.C {
ctx, cancel := context.WithTimeout(ctx, ms.UpdatePeriod)
if err := ms.fillCachedMetrics(ctx); err != nil {
ms.Log.Error(err, "Error retrieving containerStats", "timeout", ms.UpdatePeriod)
}
// Operation running the context completed
cancel()
}
}
func (ms *MetricsScraper) fillCachedMetrics(ctx context.Context) error {
log := ms.Log.WithName("fill-cached-metrics")
// Update containerIDs list.
containerIDs, err := ms.findApplicationContainerIDs(ctx)
if err != nil {
log.Error(err, "Error updating containerIDs list")
return err
}
tracer := trace.New("Started scraping metrics")
defer tracer.Log()
ctx = clctx.LoggerIntoContext(ctx, log)
containerStatsList, err := ms.scraper.getStats(ctx, containerIDs)
if err != nil {
return err
}
for i, containerStats := range containerStatsList {
podName := containerStats.container.GetLabels()["io.kubernetes.pod.name"]
// If no stats are found for containerID, Pod may no longer be running,
// related cache information must be removed.
if !containerStats.runningContainer {
log.V(2).Info("Removing containerId related cached metrics", "ID", containerStats.container.Id)
ms.removeCachedMetric(podName)
continue
}
metrics := &CustomMetrics{}
metrics.MemBytes = containerStats.MemoryUsageInBytes
metrics.DiskBytes = containerStats.DiskUsageInBytes
if oldCPU, ok := ms.oldStats[podName]; ok {
duration := containerStats.CPUTimestamp - oldCPU.CPUTimestamp
newNs := containerStats.UsageCoreNanoSeconds
oldNs := oldCPU.UsageCoreNanoSeconds
cpuPerc := float32(newNs-oldNs) * 100 / float32(duration)
metrics.CPUPerc = cpuPerc
}
ms.oldStats[podName] = &containerStatsList[i]
ms.cachedMetricsMutex.Lock()
ms.cachedMetrics[podName] = metrics
ms.cachedMetricsMutex.Unlock()
}
return nil
}
// findApplicationContainerIDs retrieves a list of running application containers.
func (ms *MetricsScraper) findApplicationContainerIDs(ctx context.Context) ([]*criapi.Container, error) {
log := ms.Log.WithName("find-application-container-ids")
// get instance pod names.
ctxp := clctx.LoggerIntoContext(ctx, log.WithName("list-pods"))
podFilter := &criapi.PodSandboxFilter{
State: &criapi.PodSandboxStateValue{
State: criapi.PodSandboxState_SANDBOX_READY,
},
}
pods, err := ms.RuntimeClient.ListPodSandbox(ctxp, podFilter)
if err != nil {
return nil, err
}
instancePodIDs := []string{}
for _, pod := range pods {
if _, ok := pod.Labels["crownlabs.polito.it/instance"]; !ok {
continue
}
if _, ok := pod.Labels["kubevirt.io"]; ok {
continue
}
instancePodIDs = append(instancePodIDs, pod.Id)
}
// get application container ids.
ctxc := clctx.LoggerIntoContext(ctx, log.WithName("list-containers"))
containerFilter := &criapi.ContainerFilter{
State: &criapi.ContainerStateValue{
State: criapi.ContainerState_CONTAINER_RUNNING,
},
}
containers, err := ms.RuntimeClient.ListContainers(ctxc, containerFilter)
if err != nil {
return nil, err
}
watchedContainers := []*criapi.Container{}
for _, container := range containers {
if utils.Contains(instancePodIDs, container.PodSandboxId) && !utils.Contains(ms.ignoredContainerNames, container.Metadata.Name) {
watchedContainers = append(watchedContainers, container)
}
}
log.V(3).Info("application containers found ", "containers", containers)
return watchedContainers, nil
}
func (ms *MetricsScraper) removeCachedMetric(podName string) {
delete(ms.oldStats, podName)
ms.cachedMetricsMutex.Lock()
delete(ms.cachedMetrics, podName)
ms.cachedMetricsMutex.Unlock()
}