-
Notifications
You must be signed in to change notification settings - Fork 52
/
writer_sink.go
134 lines (122 loc) · 3.06 KB
/
writer_sink.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
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
package health
import (
"bytes"
"fmt"
"io"
"sort"
"time"
)
// This sink writes bytes in a format that a human might like to read in a logfile
// This can be used to log to Stdout:
// .AddSink(&WriterSink{os.Stdout})
// And to a file:
// f, err := os.OpenFile(fname, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
// .AddSink(&WriterSink{f})
// And to syslog:
// w, err := syslog.New(LOG_INFO, "wat")
// .AddSink(&WriterSink{w})
type WriterSink struct {
io.Writer
}
func (s *WriterSink) EmitEvent(job string, event string, kvs map[string]string) {
var b bytes.Buffer
b.WriteRune('[')
b.WriteString(timestamp())
b.WriteString("]: job:")
b.WriteString(job)
b.WriteString(" event:")
b.WriteString(event)
writeMapConsistently(&b, kvs)
b.WriteRune('\n')
s.Writer.Write(b.Bytes())
}
func (s *WriterSink) EmitEventErr(job string, event string, inputErr error, kvs map[string]string) {
var b bytes.Buffer
b.WriteRune('[')
b.WriteString(timestamp())
b.WriteString("]: job:")
b.WriteString(job)
b.WriteString(" event:")
b.WriteString(event)
b.WriteString(" err:")
b.WriteString(inputErr.Error())
writeMapConsistently(&b, kvs)
b.WriteRune('\n')
s.Writer.Write(b.Bytes())
}
func (s *WriterSink) EmitTiming(job string, event string, nanos int64, kvs map[string]string) {
var b bytes.Buffer
b.WriteRune('[')
b.WriteString(timestamp())
b.WriteString("]: job:")
b.WriteString(job)
b.WriteString(" event:")
b.WriteString(event)
b.WriteString(" time:")
writeNanoseconds(&b, nanos)
writeMapConsistently(&b, kvs)
b.WriteRune('\n')
s.Writer.Write(b.Bytes())
}
func (s *WriterSink) EmitGauge(job string, event string, value float64, kvs map[string]string) {
var b bytes.Buffer
b.WriteRune('[')
b.WriteString(timestamp())
b.WriteString("]: job:")
b.WriteString(job)
b.WriteString(" event:")
b.WriteString(event)
b.WriteString(" gauge:")
fmt.Fprintf(&b, "%g", value)
writeMapConsistently(&b, kvs)
b.WriteRune('\n')
s.Writer.Write(b.Bytes())
}
func (s *WriterSink) EmitComplete(job string, status CompletionStatus, nanos int64, kvs map[string]string) {
var b bytes.Buffer
b.WriteRune('[')
b.WriteString(timestamp())
b.WriteString("]: job:")
b.WriteString(job)
b.WriteString(" status:")
b.WriteString(status.String())
b.WriteString(" time:")
writeNanoseconds(&b, nanos)
writeMapConsistently(&b, kvs)
b.WriteRune('\n')
s.Writer.Write(b.Bytes())
}
func timestamp() string {
return time.Now().UTC().Format(time.RFC3339Nano)
}
func writeMapConsistently(b *bytes.Buffer, kvs map[string]string) {
if kvs == nil {
return
}
keys := make([]string, 0, len(kvs))
for k := range kvs {
keys = append(keys, k)
}
sort.Strings(keys)
keysLenMinusOne := len(keys) - 1
b.WriteString(" kvs:[")
for i, k := range keys {
b.WriteString(k)
b.WriteRune(':')
b.WriteString(kvs[k])
if i != keysLenMinusOne {
b.WriteRune(' ')
}
}
b.WriteRune(']')
}
func writeNanoseconds(b *bytes.Buffer, nanos int64) {
switch {
case nanos > 2000000:
fmt.Fprintf(b, "%d ms", nanos/1000000)
case nanos > 2000:
fmt.Fprintf(b, "%d μs", nanos/1000)
default:
fmt.Fprintf(b, "%d ns", nanos)
}
}