/
instrumentation.go
96 lines (76 loc) · 1.9 KB
/
instrumentation.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
package main
import (
"encoding/json"
"os"
"path"
"sync"
"time"
"github.com/rebuy-de/rebuy-go-sdk/v3/pkg/typeutil"
"github.com/sirupsen/logrus"
)
type Instrumentation struct {
l *sync.Mutex
Durations struct {
Steps *DurationMap `json:"steps,omitempty"`
Testing *DurationMap `json:"test,omitempty"`
Building *DurationMap `json:"build,omitempty"`
Artifacts *DurationMap `json:"artifacts,omitempty"`
Upload *DurationMap `json:"upload,omitempty"`
} `json:",omitempty"`
Sizes map[string]typeutil.JSONBytes `json:",omitempty"`
}
func NewInstrumentation() *Instrumentation {
inst := new(Instrumentation)
inst.l = new(sync.Mutex)
inst.Durations.Steps = NewDurationMap()
inst.Durations.Testing = NewDurationMap()
inst.Durations.Building = NewDurationMap()
inst.Durations.Artifacts = NewDurationMap()
inst.Durations.Upload = NewDurationMap()
return inst
}
func (i *Instrumentation) ReadSize(name string) {
fi, err := os.Stat(path.Join("dist", name))
if err != nil {
logrus.WithError(err).Errorf("Failed to get size of %s", name)
return
}
i.l.Lock()
defer i.l.Unlock()
if i.Sizes == nil {
i.Sizes = map[string]typeutil.JSONBytes{}
}
i.Sizes[name] = typeutil.JSONBytes{
Size: fi.Size(),
}
}
func Stopwatch(target *typeutil.JSONDuration) func() {
start := time.Now()
return func() {
target.Duration = time.Since(start).Truncate(time.Millisecond)
}
}
type DurationMap struct {
m map[string]typeutil.JSONDuration
l *sync.Mutex
}
func NewDurationMap() *DurationMap {
return &DurationMap{
m: map[string]typeutil.JSONDuration{},
l: new(sync.Mutex),
}
}
func (m *DurationMap) MarshalJSON() ([]byte, error) {
return json.Marshal(m.m)
}
func (m *DurationMap) Stopwatch(name string) func() {
start := time.Now()
return func() {
d := time.Since(start).Truncate(time.Millisecond)
m.l.Lock()
defer m.l.Unlock()
m.m[name] = typeutil.JSONDuration{
Duration: d,
}
}
}