forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics.go
86 lines (76 loc) · 1.66 KB
/
metrics.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
package metrics
import (
"encoding/json"
"fmt"
"time"
)
const (
marker_name string = "cluster_loader_marker"
)
type Metrics interface {
printLog() error
}
type BaseMetrics struct {
// To let the 3rd party know that this log entry is important
// TODO set this up by config file
Marker string `json:"marker"`
Name string `json:"name"`
Type string `json:"type"`
}
type TestDuration struct {
BaseMetrics
StartTime time.Time `json:"startTime"`
TestDuration time.Duration `json:"testDuration"`
}
func (td TestDuration) printLog() error {
b, err := json.Marshal(td)
fmt.Println(string(b))
return err
}
func (td TestDuration) MarshalJSON() ([]byte, error) {
type Alias TestDuration
return json.Marshal(&struct {
Alias
TestDuration string `json:"testDuration"`
}{
Alias: (Alias)(td),
TestDuration: td.TestDuration.String(),
})
}
func (td *TestDuration) UnmarshalJSON(b []byte) error {
var err error
type Alias TestDuration
s := &struct {
TestDuration string `json:"testDuration"`
*Alias
}{
Alias: (*Alias)(td),
}
if err := json.Unmarshal(b, &s); err != nil {
return err
}
td.TestDuration, err = time.ParseDuration(s.TestDuration)
if err != nil {
return err
}
return nil
}
func LogMetrics(metrics []Metrics) error {
for _, m := range metrics {
err := m.printLog()
if err != nil {
return err
}
}
return nil
}
func NewTestDuration(name string, startTime time.Time, testDuration time.Duration) TestDuration {
return TestDuration{
BaseMetrics: BaseMetrics{
Marker: marker_name,
Name: name,
Type: fmt.Sprintf("%T", (*TestDuration)(nil))[1:]},
StartTime: startTime,
TestDuration: testDuration,
}
}