-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker.go
More file actions
274 lines (232 loc) · 5.4 KB
/
tracker.go
File metadata and controls
274 lines (232 loc) · 5.4 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package tracker
import (
"encoding/json"
"errors"
"fmt"
"github.com/olekukonko/tablewriter"
"io"
"log"
"runtime"
"time"
)
var (
msgFormat = "function:[%s]|sinceStart:[%s]|duration:[%s]|"
)
// Renderer track trace must implement Render() , but should not be aware of the output.
// In this package are implemented two Renderer`s - table render and json render,
// but you can use other.
type Renderer interface {
Render(metadata MetaData, opt *Options)
}
// the track is
type Track struct {
Data MetaData `json:"trackedData,omitempty"`
Loggable bool
callerSkip int
messageFormat string
options *Options
Renderer
}
// contains meta information about current function
type MetaData []Meta
type Meta struct {
Name string `json:"name"`
Start time.Time `json:"start"`
Dur time.Duration `json:"dur"`
StartDif time.Duration `json:"start_dif"`
Err error `json:"error"`
}
// leverage of options for build info
// withErrors - will add an errors fields in output if error sent into Update()
// withName - will add a name of calling function
// withSinceStart - will add duration of since creation instance of Track
// withDuration - will add a duration since previous call Update()
// withTrack - will add a string which visualize the called function duration
type Options struct {
withErrors,
withName,
withSinceStart,
withDuration,
withTrack,
withLink bool
}
func (t *Track) SetMessageFormat(s string) {
msgFormat = s
}
func New(callerSkip int) *Track {
t := Track{
callerSkip: callerSkip,
}
t.Data = append(t.Data, Meta{
Start: time.Now(),
Name: trace(t.callerSkip),
})
if t.Loggable {
fmt.Println(t.Data[0].info())
}
return &t
}
// Track.Update() append elem into t.Data which contain the invoke time ,
// duration since of previous invoke, name of function who call Update()
func (t *Track) Update(err error) error {
if len(t.Data) < 1 {
return errors.New("at first need to invoke New(int)")
}
meta := Meta{
Name: trace(t.callerSkip),
Start: time.Now(),
Dur: t.Data[len(t.Data)-1].Since(),
StartDif: t.Data[0].Since(),
Err: err,
}
t.Data = append(t.Data, meta)
if t.Loggable {
fmt.Println(meta.info())
}
return nil
}
type RenderOptions struct {
Divider int
}
type TableRender struct {
Out io.Writer
Options *RenderOptions
}
type JSONRender struct {
Out io.Writer
Options *RenderOptions
}
// returns max duration of []Track.Data elems
func (m MetaData) MaxDuration() time.Duration {
var max time.Duration
for _, v := range m {
if max < v.Dur {
max = v.Dur
}
}
return max
}
func (m MetaData) MinDuration() time.Duration {
var min time.Duration
// the first elem will always be with the smallest duration
// because it create on start, skip it
for i, e := range m[1:] {
if i == 0 || e.Dur < min {
min = e.Dur
}
}
return min
}
func (iter Meta) Since() time.Duration {
return time.Since(iter.Start)
}
func (iter Meta) info() string {
return fmt.Sprintf(msgFormat, iter.Name, iter.StartDif, iter.Dur, )
}
func (tbr TableRender) Render(data MetaData, opt *Options) {
headers := make([]string, 0, len(data))
headers = createHeaders(headers, opt)
table := tablewriter.NewWriter(tbr.Out)
table.SetHeader(headers)
for i, v := range data {
var timeLine string
if v.Err == nil {
v.Err = errors.New("")
}
step := int(data.MaxDuration()) / tbr.Options.Divider
for k := 0; k < int(data[i].Dur); k += step {
timeLine = timeLine + "*"
}
row := createRow(opt, data[i], timeLine)
table.Append(row)
}
table.Render()
}
func (jsr JSONRender) Render(data MetaData, opt *Options) {
payload, err := json.MarshalIndent(data, "", " ")
if err != nil {
log.Printf("err:%s; error marshaling data", err.Error(), )
}
_, err = jsr.Out.Write(payload)
if err != nil {
log.Printf("err:%s; error marshaling data", err.Error(), )
}
}
func createHeaders(s []string, opt *Options) []string {
if opt.withName {
s = append(s, "func.name")
}
if opt.withSinceStart {
s = append(s, "since.start")
}
if opt.withDuration {
s = append(s, "duration")
}
if opt.withErrors {
s = append(s, "errors")
}
if opt.withTrack {
s = append(s, "track")
}
return s
}
func createRow(opt *Options, meta Meta, timeLine string) []string {
s := make([]string, 0, 5)
if opt.withName {
s = append(s, meta.Name)
}
if opt.withSinceStart {
s = append(s, meta.StartDif.String())
}
if opt.withDuration {
s = append(s, meta.Dur.String())
}
if opt.withErrors {
if meta.Err == nil {
s = append(s, "")
} else {
s = append(s, meta.Err.Error())
}
}
if opt.withTrack {
s = append(s, timeLine)
}
return s
}
func (t *Track) Configure() *Options {
t.options = new(Options)
return t.options
}
func (o *Options) WithErrors() *Options {
o.withErrors = true
return o
}
func (o *Options) WithName() *Options {
o.withName = true
return o
}
func (o *Options) WithSinceStart() *Options {
o.withSinceStart = true
return o
}
func (o *Options) WithDuration() *Options {
o.withDuration = true
return o
}
func (o *Options) WithTrack() *Options {
o.withTrack = true
return o
}
func (t *Track) SetRenderer(render Renderer) {
t.Renderer = render
}
func (t *Track) Render() {
t.Renderer.Render(t.Data, t.options)
}
//returns the name of the function in which it is called
func trace(skip int) string {
pc := make([]uintptr, skip)
runtime.Callers(skip, pc)
f := runtime.FuncForPC(pc[0])
return f.Name()
}