This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
logger.go
158 lines (131 loc) · 3.82 KB
/
logger.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package lunk
import (
"encoding/json"
"fmt"
"io"
"math/rand"
"sort"
"strconv"
"strings"
"sync"
"time"
)
// An EventLogger logs events and their metadata.
type EventLogger interface {
// Log adds the given event to the log stream.
Log(id EventID, e Event)
}
// NewJSONEventLogger returns an EventLogger which writes entries as streaming
// JSON to the given writer.
func NewJSONEventLogger(w io.Writer) EventLogger {
return jsonEventLogger{json.NewEncoder(w)}
}
// NewTextEventLogger returns an EventLogger which writes entries as single
// lines of attr="value" formatted text.
func NewTextEventLogger(w io.Writer) EventLogger {
return textEventLogger{w: w}
}
// A SamplingEventLogger logs a uniform sampling of events, if configured to do
// so.
type SamplingEventLogger struct {
l EventLogger
r *rand.Rand
rates map[string]float64
rootRates map[ID]float64
m *sync.Mutex
}
// NewSamplingEventLogger returns a new SamplingEventLogger, passing events
// through to the given EventLogger.
func NewSamplingEventLogger(l EventLogger) *SamplingEventLogger {
return &SamplingEventLogger{
l: l,
r: rand.New(rand.NewSource(time.Now().UnixNano())),
rates: make(map[string]float64),
rootRates: make(map[ID]float64),
m: new(sync.Mutex),
}
}
// SetRootSampleRate sets the sampling rate for all events with the given root
// ID. p should be between 0.0 (no events logged) and 1.0 (all events logged),
// inclusive.
func (l SamplingEventLogger) SetRootSampleRate(root ID, p float64) {
l.m.Lock()
defer l.m.Unlock()
l.rootRates[root] = p
}
// UnsetRootSampleRate removes any settings for events with the given root ID.
func (l SamplingEventLogger) UnsetRootSampleRate(root ID) {
l.m.Lock()
defer l.m.Unlock()
delete(l.rootRates, root)
}
// SetSchemaSampleRate sets the sampling rate for all events with the given
// schema. p should be between 0.0 (no events logged) and 1.0 (all events
// logged), inclusive.
func (l SamplingEventLogger) SetSchemaSampleRate(schema string, p float64) {
l.m.Lock()
defer l.m.Unlock()
l.rates[schema] = p
}
// UnsetSchemaSampleRate removes any settings for events with the given root ID.
func (l SamplingEventLogger) UnsetSchemaSampleRate(schema string) {
l.m.Lock()
defer l.m.Unlock()
delete(l.rates, schema)
}
// Log passes the event to the underlying EventLogger, probabilistically
// dropping some events.
func (l SamplingEventLogger) Log(id EventID, e Event) {
l.m.Lock()
defer l.m.Unlock()
r, ok := l.rootRates[id.Root]
if !ok {
r, ok = l.rates[e.Schema()]
}
if ok && r < l.r.Float64() {
return
}
l.l.Log(id, e)
}
type jsonEventLogger struct {
*json.Encoder
}
func (l jsonEventLogger) Log(id EventID, e Event) {
if err := l.Encode(NewEntry(id, e)); err != nil {
panic(err)
}
}
type textEventLogger struct {
w io.Writer
}
func (l textEventLogger) Log(id EventID, e Event) {
entry := NewEntry(id, e)
props := []string{
fmt.Sprintf("time=%s", strconv.Quote(entry.Time.Format(time.RFC3339))),
fmt.Sprintf("host=%s", strconv.Quote(entry.Host)),
fmt.Sprintf(`pid="%d"`, entry.PID),
fmt.Sprintf("deploy=%s", strconv.Quote(entry.Deploy)),
fmt.Sprintf("schema=%s", strconv.Quote(entry.Schema)),
fmt.Sprintf("id=%s", strconv.Quote(entry.ID.String())),
fmt.Sprintf("root=%s", strconv.Quote(entry.Root.String())),
}
if entry.Parent != 0 {
s := fmt.Sprintf("parent=%s", strconv.Quote(entry.Parent.String()))
props = append(props, s)
}
for _, k := range sortedKeys(entry.Properties) {
s := fmt.Sprintf("p:%s=%s", k, strconv.Quote(entry.Properties[k]))
props = append(props, s)
}
if _, err := fmt.Fprintln(l.w, strings.Join(props, " ")); err != nil {
panic(err)
}
}
func sortedKeys(m map[string]string) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}