-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtimer.go
More file actions
59 lines (49 loc) · 1.64 KB
/
timer.go
File metadata and controls
59 lines (49 loc) · 1.64 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
package monitoring
import (
"time"
"github.com/AliceO2Group/Control/common/logger/infologger"
)
type TimeResolution int
const (
Millisecond TimeResolution = iota
Nanosecond
)
// Timer function is meant to be used with defer statement to measure runtime of given function:
// defer Timer(&metric, Milliseconds)()
func Timer(metric *Metric, unit TimeResolution) func() {
return timer(metric, unit, false, false)
}
// Timer function is meant to be used with defer statement to measure runtime of given function:
// defer Timer(&metric, Milliseconds)()
// sends measured value as Send(metric)
func TimerSendSingle(metric *Metric, unit TimeResolution) func() {
return timer(metric, unit, true, false)
}
// Timer function is meant to be used with defer statement to measure runtime of given function:
// defer Timer(&metric, Milliseconds)()
// sends measured value as SendHistogrammable(metric)
func TimerSendHist(metric *Metric, unit TimeResolution) func() {
return timer(metric, unit, true, true)
}
func timer(metric *Metric, unit TimeResolution, send bool, sendHistogrammable bool) func() {
start := time.Now()
return func() {
dur := time.Since(start)
// we are setting default value as Nanoseconds
switch unit {
case Millisecond:
metric.SetFieldInt64("execution_time_ms", dur.Milliseconds())
case Nanosecond:
metric.SetFieldInt64("execution_time_ns", dur.Nanoseconds())
default:
log.WithField("level", infologger.IL_Devel).Warnf("trying to use unknown time resolution in monitoring.timer function [%d], skipping", unit)
}
if send {
if sendHistogrammable {
SendHistogrammable(metric)
} else {
Send(metric)
}
}
}
}