-
Notifications
You must be signed in to change notification settings - Fork 4
/
metric_helper.go
74 lines (63 loc) · 1.65 KB
/
metric_helper.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
package json
import (
"bytes"
"encoding/binary"
"io"
"time"
)
const metricTimeMarker = byte(0)
const metricTimeMarkSize = 9 // SIZE(uint64) + SIZE(metricTimeMarker)
func AppendMetricTimeMark(buf []byte, reportedAt time.Time) []byte {
if reportedAt.IsZero() {
return buf
}
pos := len(buf)
if pos > 0 && buf[pos-1] == metricTimeMarker {
panic("illegal state")
}
buf = append(buf, make([]byte, metricTimeMarkSize)...)
binary.LittleEndian.PutUint64(buf[pos:pos+8], uint64(reportedAt.UnixNano()))
buf[pos+metricTimeMarkSize-1] = metricTimeMarker
return buf
}
func CutMetricTimeMarkAndEol(dst *[]byte, eol []byte, now time.Time) (bool, time.Duration) {
buf := *dst
pos := len(buf)
switch {
case pos < (metricTimeMarkSize + len(eol)):
return false, 0
case buf[pos-1] != metricTimeMarker:
return false, 0
case len(eol) == 0:
//
case !bytes.HasSuffix(buf[:pos-metricTimeMarkSize], eol):
return false, 0
}
timeMark := int64(binary.LittleEndian.Uint64(buf[pos-metricTimeMarkSize:]))
if now.IsZero() {
*dst = buf[:pos-metricTimeMarkSize]
return false, 0
}
*dst = buf[:pos-metricTimeMarkSize-len(eol)]
timeMark = now.UnixNano() - timeMark
return true, time.Duration(timeMark)
}
var _ io.Writer = MetricTimeWriter{}
type MetricTimeWriter struct {
Writer io.Writer
Eol []byte
ReportFn func(time.Duration)
AppendFn func([]byte, time.Duration) []byte
}
func (w MetricTimeWriter) Write(p []byte) (n int, err error) {
if ok, d := CutMetricTimeMarkAndEol(&p, w.Eol, time.Now()); ok {
if w.ReportFn != nil {
w.ReportFn(d)
}
if w.AppendFn != nil {
p = w.AppendFn(p, d)
}
p = append(p, w.Eol...)
}
return w.Writer.Write(p)
}