-
Notifications
You must be signed in to change notification settings - Fork 180
/
fs_extractor.go
96 lines (77 loc) · 2.5 KB
/
fs_extractor.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
package extractors
import (
"log"
"regexp"
"time"
cinfo "github.com/google/cadvisor/info/v1"
. "github.com/aws/private-amazon-cloudwatch-agent-staging/internal/containerinsightscommon"
)
const (
allowList = "^tmpfs$|^/dev/|^overlay$"
)
type FileSystemMetricExtractor struct {
allowListRegexP *regexp.Regexp
}
// TODO(pingleig): it is always false for container using containerd https://github.com/aws/amazon-cloudwatch-agent/issues/192
func (f *FileSystemMetricExtractor) HasValue(info *cinfo.ContainerInfo) bool {
return info.Spec.HasFilesystem
}
func (f *FileSystemMetricExtractor) GetValue(info *cinfo.ContainerInfo, containerType string) []*CAdvisorMetric {
var metrics []*CAdvisorMetric
if containerType == TypePod || containerType == TypeInfraContainer {
return metrics
}
containerType = getFSMetricType(containerType)
stats := GetStats(info)
for _, v := range stats.Filesystem {
metric := newCadvisorMetric(containerType)
if v.Device == "" {
continue
}
if f.allowListRegexP != nil && !f.allowListRegexP.MatchString(v.Device) {
continue
}
metric.tags[DiskDev] = v.Device
metric.tags[FSType] = v.Type
metric.fields[MetricName(containerType, FSUsage)] = v.Usage
metric.fields[MetricName(containerType, FSCapacity)] = v.Limit
metric.fields[MetricName(containerType, FSAvailable)] = v.Available
if v.Limit != 0 {
metric.fields[MetricName(containerType, FSUtilization)] = float64(v.Usage) / float64(v.Limit) * 100
}
if v.HasInodes {
metric.fields[MetricName(containerType, FSInodes)] = v.Inodes
metric.fields[MetricName(containerType, FSInodesfree)] = v.InodesFree
}
metric.cgroupPath = info.Name
metrics = append(metrics, metric)
}
return metrics
}
func (f *FileSystemMetricExtractor) CleanUp(now time.Time) {
}
func NewFileSystemMetricExtractor() *FileSystemMetricExtractor {
fse := &FileSystemMetricExtractor{}
if p, err := regexp.Compile(allowList); err == nil {
fse.allowListRegexP = p
} else {
log.Printf("E! NewFileSystemMetricExtractor set regex failed: %v", err)
}
return fse
}
func getFSMetricType(containerType string) string {
metricType := ""
switch containerType {
case TypeNode:
metricType = TypeNodeFS
case TypeInstance:
metricType = TypeInstanceFS
case TypeContainer:
metricType = TypeContainerFS
default:
log.Printf("W! fs_extractor: fs metric extractor is parsing unexpected containerType %s", containerType)
}
return metricType
}