@@ -45,19 +45,34 @@ type Config struct {
45
45
FilterMessage bool
46
46
}
47
47
48
- // Observer keeps track of messages.
49
- type Observer struct {
50
- Config
48
+ // NewObserver creates PreparedObserver.
49
+ func NewObserver (cfg Config ) * PreparedObserver {
50
+ o := PreparedObserver {}
51
+ o .initialize (cfg )
52
+
53
+ return & o
54
+ }
51
55
56
+ // PreparedObserver keeps track of messages.
57
+ type PreparedObserver struct {
52
58
samplingInterval int64
53
59
count uint32
54
60
maxCardinality uint32
55
61
maxSamples uint32
56
62
distResolution int
57
63
distRetentionPeriod int64
58
64
entries sync.Map
59
- once sync.Once
60
65
other * entry
66
+ filterMessage bool
67
+ }
68
+
69
+ // Observer keeps track of messages.
70
+ // Deprecated: use NewObserver() to avoid lazy init.
71
+ type Observer struct {
72
+ Config
73
+ PreparedObserver
74
+
75
+ once sync.Once
61
76
}
62
77
63
78
type entry struct {
@@ -103,28 +118,28 @@ func (en *entry) push(now int64, sample Sample) {
103
118
en .samples <- sample
104
119
}
105
120
106
- func (l * Observer ) initialize () {
107
- l .samplingInterval = int64 (l .SamplingInterval )
121
+ func (l * PreparedObserver ) initialize (cfg Config ) {
122
+ l .samplingInterval = int64 (cfg .SamplingInterval )
108
123
if l .samplingInterval == 0 {
109
124
l .samplingInterval = int64 (time .Millisecond ) // 1ms sampling interval by default.
110
125
}
111
126
112
- l .maxCardinality = l .MaxCardinality
127
+ l .maxCardinality = cfg .MaxCardinality
113
128
if l .maxCardinality == 0 {
114
129
l .maxCardinality = 100
115
130
}
116
131
117
- l .maxSamples = l .MaxSamples
132
+ l .maxSamples = cfg .MaxSamples
118
133
if l .maxSamples == 0 {
119
134
l .maxSamples = 10
120
135
}
121
136
122
- l .distResolution = l .DistResolution
137
+ l .distResolution = cfg .DistResolution
123
138
if l .distResolution == 0 {
124
139
l .distResolution = 100
125
140
}
126
141
127
- l .distRetentionPeriod = int64 (l .DistRetentionPeriod )
142
+ l .distRetentionPeriod = int64 (cfg .DistRetentionPeriod )
128
143
if l .distRetentionPeriod == 0 {
129
144
l .distRetentionPeriod = int64 (168 * time .Hour )
130
145
}
@@ -142,12 +157,23 @@ func (l *Observer) initialize() {
142
157
}
143
158
l .other .distRetentionPeriod = l .distRetentionPeriod
144
159
}
160
+
161
+ if cfg .FilterMessage {
162
+ l .filterMessage = true
163
+ }
145
164
}
146
165
147
166
// ObserveMessage updates aggregated information about message.
148
167
func (l * Observer ) ObserveMessage (msg string , data interface {}) {
149
- l .once .Do (l .initialize )
168
+ l .once .Do (func () {
169
+ l .initialize (l .Config )
170
+ })
171
+
172
+ l .PreparedObserver .ObserveMessage (msg , data )
173
+ }
150
174
175
+ // ObserveMessage updates aggregated information about message.
176
+ func (l * PreparedObserver ) ObserveMessage (msg string , data interface {}) {
151
177
tn := time .Now ()
152
178
now := tn .UnixNano () / l .samplingInterval
153
179
s := Sample {
@@ -156,7 +182,7 @@ func (l *Observer) ObserveMessage(msg string, data interface{}) {
156
182
Time : tn ,
157
183
}
158
184
159
- if l .Config . FilterMessage {
185
+ if l .filterMessage {
160
186
msg = string (filter .Dynamic ([]byte (msg ), 200 ))
161
187
}
162
188
@@ -193,7 +219,7 @@ func (l *Observer) ObserveMessage(msg string, data interface{}) {
193
219
}
194
220
}
195
221
196
- func (l * Observer ) exportEntry (en * entry , withSamples bool ) Entry {
222
+ func (l * PreparedObserver ) exportEntry (en * entry , withSamples bool ) Entry {
197
223
if en == nil {
198
224
return Entry {}
199
225
}
@@ -256,7 +282,7 @@ type Bucket struct {
256
282
}
257
283
258
284
// GetEntries returns a list of observed event entries without data samples.
259
- func (l * Observer ) GetEntries () []Entry {
285
+ func (l * PreparedObserver ) GetEntries () []Entry {
260
286
result := make ([]Entry , 0 , atomic .LoadUint32 (& l .count ))
261
287
262
288
l .entries .Range (func (key , value interface {}) bool {
@@ -269,7 +295,7 @@ func (l *Observer) GetEntries() []Entry {
269
295
}
270
296
271
297
// GetEntriesWithSamples returns a list of observed event entries with data samples.
272
- func (l * Observer ) GetEntriesWithSamples () []Entry {
298
+ func (l * PreparedObserver ) GetEntriesWithSamples () []Entry {
273
299
result := make ([]Entry , 0 , atomic .LoadUint32 (& l .count ))
274
300
275
301
l .entries .Range (func (key , value interface {}) bool {
@@ -282,7 +308,7 @@ func (l *Observer) GetEntriesWithSamples() []Entry {
282
308
}
283
309
284
310
// Find lookups entry by message.
285
- func (l * Observer ) Find (msg string ) Entry {
311
+ func (l * PreparedObserver ) Find (msg string ) Entry {
286
312
var e Entry
287
313
288
314
l .entries .Range (func (key , value interface {}) bool {
@@ -299,7 +325,7 @@ func (l *Observer) Find(msg string) Entry {
299
325
}
300
326
301
327
// Other returns entry for other events.
302
- func (l * Observer ) Other (withSamples bool ) Entry {
328
+ func (l * PreparedObserver ) Other (withSamples bool ) Entry {
303
329
return l .exportEntry (l .other , withSamples )
304
330
}
305
331
0 commit comments