/
human_readable_formatter.go
137 lines (110 loc) · 4.25 KB
/
human_readable_formatter.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
// Copyright 2019 Preferred Networks, Inc.
//
// 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 metrics
import (
"fmt"
"github.com/pfnet-research/k8s-cluster-simulator/pkg/node"
"github.com/pfnet-research/k8s-cluster-simulator/pkg/pod"
"github.com/pfnet-research/k8s-cluster-simulator/pkg/queue"
)
// HumanReadableFormatter is a Foramtter that formats metrics in a human-readable style.
type HumanReadableFormatter struct{}
// Format implements Formatter interface.
// Returns error if the given metrics does not have valid structure.
func (h *HumanReadableFormatter) Format(metrics *Metrics) (string, error) {
if err := validateMetrics(metrics); err != nil {
return "", err
}
// Clock
clk := (*metrics)[ClockKey].(string)
str := "Metrics " + clk + "\n"
// Nodes
str += " Nodes\n"
nodesMet := (*metrics)[NodesMetricsKey].(map[string]node.Metrics)
str += h.formatNodesMetrics(nodesMet)
// Pods
str += " Pods\n"
podsMet := (*metrics)[PodsMetricsKey].(map[string]pod.Metrics)
str += h.formatPodsMetrics(podsMet)
// Queue
str += " Queue\n"
queueMet := (*metrics)[QueueMetricsKey].(queue.Metrics)
str += h.formatQueueMetrics(queueMet)
return str, nil
}
func validateMetrics(metrics *Metrics) error {
keys := []string{ClockKey, NodesMetricsKey, PodsMetricsKey, QueueMetricsKey}
for _, key := range keys {
if _, ok := (*metrics)[key]; !ok {
return fmt.Errorf("No key %q in metrics", key)
}
}
if _, ok := (*metrics)[ClockKey].(string); !ok {
return fmt.Errorf("Type assertion failed: %q field of metrics is not string", ClockKey)
}
if _, ok := (*metrics)[NodesMetricsKey].(map[string]node.Metrics); !ok {
return fmt.Errorf("Type assertion failed: %q field of metrics is not map[string]node.Metrics", NodesMetricsKey)
}
if _, ok := (*metrics)[PodsMetricsKey].(map[string]pod.Metrics); !ok {
return fmt.Errorf("Type assertion failed: %q field of metrics is not map[string]pod.Metrics", PodsMetricsKey)
}
if _, ok := (*metrics)[QueueMetricsKey].(queue.Metrics); !ok {
return fmt.Errorf("Type assertion failed: %q field of metrics is not queue.Metrics", QueueMetricsKey)
}
return nil
}
func (h *HumanReadableFormatter) formatNodesMetrics(metrics map[string]node.Metrics) string {
str := ""
for name, met := range metrics {
str += fmt.Sprintf(" %s: Pods %d(%d)/%d", name, met.RunningPodsNum, met.TerminatingPodsNum, met.Allocatable.Pods().Value())
for rsrc, alloc := range met.Allocatable {
if rsrc == "pods" {
continue
}
usage := met.TotalResourceUsage[rsrc]
req := met.TotalResourceRequest[rsrc]
if rsrc == "memory" {
d := int64(1 << 20)
str += fmt.Sprintf(", memMB %d/%d/%d", usage.Value()/d, req.Value()/d, alloc.Value()/d)
} else {
str += fmt.Sprintf(", %s %d/%d/%d", rsrc, usage.Value(), req.Value(), alloc.Value())
}
}
str += fmt.Sprintf(", Failed %d\n", met.FailedPodsNum)
}
return str
}
func (h *HumanReadableFormatter) formatPodsMetrics(metrics map[string]pod.Metrics) string {
str := ""
for name, met := range metrics {
str += fmt.Sprintf(" %s: prio %d, bound at %s on %s, status %s, elapsed %d s",
name, met.Priority, met.BoundAt.ToRFC3339(), met.Node, met.Status, met.ExecutedSeconds)
for rsrc, req := range met.ResourceRequest {
lim := met.ResourceLimit[rsrc] // !ok -> usage == 0
usage := met.ResourceUsage[rsrc]
if rsrc == "memory" {
d := int64(1 << 20)
str += fmt.Sprintf(", memMB %d/%d/%d", usage.Value()/d, req.Value()/d, lim.Value()/d)
} else {
str += fmt.Sprintf(", %s %d/%d/%d", rsrc, usage.Value(), req.Value(), lim.Value())
}
}
str += "\n"
}
return str
}
func (h *HumanReadableFormatter) formatQueueMetrics(metrics queue.Metrics) string {
return fmt.Sprintf(" PendingPods %d\n", metrics.PendingPodsNum)
}
var _ = Formatter(&HumanReadableFormatter{})