-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.go
More file actions
123 lines (92 loc) · 2.61 KB
/
metrics.go
File metadata and controls
123 lines (92 loc) · 2.61 KB
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
/*
Copyright 2022-Present Couchbase, Inc.
Use of this software is governed by the Business Source License included in
the file licenses/BSL-Couchbase.txt. As of the Change Date specified in that
file, in accordance with the Business Source License, use of this software will
be governed by the Apache License, Version 2.0, included in the file
licenses/APL2.txt.
*/
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
corev1 "k8s.io/api/core/v1"
metricsv1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1"
)
const (
metricsFileBase = "metrics"
containerName = "certification"
)
func getMetrics(path string) ([]metricsv1beta1.PodMetrics, error) {
var metrics []metricsv1beta1.PodMetrics
samples, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
for _, sample := range samples {
metricsFile := filepath.Join(path, sample.Name(), metricsFileBase)
metricsJSON, err := os.ReadFile(metricsFile)
if err != nil {
return nil, err
}
var metric metricsv1beta1.PodMetrics
if err := json.Unmarshal(metricsJSON, &metric); err != nil {
return nil, err
}
metrics = append(metrics, metric)
}
return metrics, nil
}
func getContainerMetrics(metrics *metricsv1beta1.PodMetrics) (*metricsv1beta1.ContainerMetrics, error) {
for i := range metrics.Containers {
metric := metrics.Containers[i]
if metrics.Name == containerName {
return &metric, nil
}
}
return nil, fmt.Errorf("unable to find container metrics")
}
func getCPUMetric(metrics *metricsv1beta1.ContainerMetrics) (float64, error) {
metric, ok := metrics.Usage[corev1.ResourceCPU]
if !ok {
return 0.0, fmt.Errorf("unable to locate cpu metric")
}
return metric.AsApproximateFloat64(), nil
}
func getMemoryMetric(metrics *metricsv1beta1.ContainerMetrics) (float64, error) {
metric, ok := metrics.Usage[corev1.ResourceMemory]
if !ok {
return 0.0, fmt.Errorf("unable to locate memory metric")
}
return metric.AsApproximateFloat64() / (1 << 30), nil
}
func main() {
var path string
flag.StringVar(&path, "path", "", "Path to profile directory")
flag.Parse()
metrics, err := getMetrics(path)
if err != nil {
fmt.Println(err)
}
fmt.Println("Time,CPU,Memory (Gi)")
for i := range metrics {
podMetric := metrics[i]
containerMetric, err := getContainerMetrics(&podMetric)
if err != nil {
fmt.Println(err)
}
cpuMetric, err := getCPUMetric(containerMetric)
if err != nil {
fmt.Println(err)
}
memoryMetric, err := getMemoryMetric(containerMetric)
if err != nil {
fmt.Println(err)
}
fmt.Println(fmt.Sprintf("%s,%f,%f", podMetric.Timestamp, cpuMetric, memoryMetric))
}
}