-
Notifications
You must be signed in to change notification settings - Fork 32
/
clock.go
53 lines (46 loc) · 1.54 KB
/
clock.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
package stats
import "time"
// The Clock type can be used to report statistics on durations.
//
// Clocks are useful to measure the duration taken by sequential execution steps
// and therefore aren't safe to be used concurrently by multiple goroutines.
type Clock struct {
name string
first time.Time
last time.Time
tags []Tag
eng *Engine
}
// Stamp reports the time difference between now and the last time the method
// was called (or since the clock was created).
//
// The metric produced by this method call will have a "stamp" tag set to name.
func (c *Clock) Stamp(name string) {
c.StampAt(name, time.Now())
}
// StampAt reports the time difference between now and the last time the method
// was called (or since the clock was created).
//
// The metric produced by this method call will have a "stamp" tag set to name.
func (c *Clock) StampAt(name string, now time.Time) {
c.observe(name, now.Sub(c.last))
c.last = now
}
// Stop reports the time difference between now and the time the clock was created at.
//
// The metric produced by this method call will have a "stamp" tag set to
// "total".
func (c *Clock) Stop() {
c.StopAt(time.Now())
}
// StopAt reports the time difference between now and the time the clock was created at.
//
// The metric produced by this method call will have a "stamp" tag set to
// "total".
func (c *Clock) StopAt(now time.Time) {
c.observe("total", now.Sub(c.first))
}
func (c *Clock) observe(stamp string, d time.Duration) {
tags := append(c.tags, Tag{"stamp", stamp})
c.eng.Observe(c.name, d, tags...)
}