Skip to content

Commit b419518

Browse files
authored
Add constructor to avoid lazy init (#11)
1 parent 99aa89d commit b419518

File tree

2 files changed

+47
-23
lines changed

2 files changed

+47
-23
lines changed

observer.go

+43-17
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,34 @@ type Config struct {
4545
FilterMessage bool
4646
}
4747

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+
}
5155

56+
// PreparedObserver keeps track of messages.
57+
type PreparedObserver struct {
5258
samplingInterval int64
5359
count uint32
5460
maxCardinality uint32
5561
maxSamples uint32
5662
distResolution int
5763
distRetentionPeriod int64
5864
entries sync.Map
59-
once sync.Once
6065
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
6176
}
6277

6378
type entry struct {
@@ -103,28 +118,28 @@ func (en *entry) push(now int64, sample Sample) {
103118
en.samples <- sample
104119
}
105120

106-
func (l *Observer) initialize() {
107-
l.samplingInterval = int64(l.SamplingInterval)
121+
func (l *PreparedObserver) initialize(cfg Config) {
122+
l.samplingInterval = int64(cfg.SamplingInterval)
108123
if l.samplingInterval == 0 {
109124
l.samplingInterval = int64(time.Millisecond) // 1ms sampling interval by default.
110125
}
111126

112-
l.maxCardinality = l.MaxCardinality
127+
l.maxCardinality = cfg.MaxCardinality
113128
if l.maxCardinality == 0 {
114129
l.maxCardinality = 100
115130
}
116131

117-
l.maxSamples = l.MaxSamples
132+
l.maxSamples = cfg.MaxSamples
118133
if l.maxSamples == 0 {
119134
l.maxSamples = 10
120135
}
121136

122-
l.distResolution = l.DistResolution
137+
l.distResolution = cfg.DistResolution
123138
if l.distResolution == 0 {
124139
l.distResolution = 100
125140
}
126141

127-
l.distRetentionPeriod = int64(l.DistRetentionPeriod)
142+
l.distRetentionPeriod = int64(cfg.DistRetentionPeriod)
128143
if l.distRetentionPeriod == 0 {
129144
l.distRetentionPeriod = int64(168 * time.Hour)
130145
}
@@ -142,12 +157,23 @@ func (l *Observer) initialize() {
142157
}
143158
l.other.distRetentionPeriod = l.distRetentionPeriod
144159
}
160+
161+
if cfg.FilterMessage {
162+
l.filterMessage = true
163+
}
145164
}
146165

147166
// ObserveMessage updates aggregated information about message.
148167
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+
}
150174

175+
// ObserveMessage updates aggregated information about message.
176+
func (l *PreparedObserver) ObserveMessage(msg string, data interface{}) {
151177
tn := time.Now()
152178
now := tn.UnixNano() / l.samplingInterval
153179
s := Sample{
@@ -156,7 +182,7 @@ func (l *Observer) ObserveMessage(msg string, data interface{}) {
156182
Time: tn,
157183
}
158184

159-
if l.Config.FilterMessage {
185+
if l.filterMessage {
160186
msg = string(filter.Dynamic([]byte(msg), 200))
161187
}
162188

@@ -193,7 +219,7 @@ func (l *Observer) ObserveMessage(msg string, data interface{}) {
193219
}
194220
}
195221

196-
func (l *Observer) exportEntry(en *entry, withSamples bool) Entry {
222+
func (l *PreparedObserver) exportEntry(en *entry, withSamples bool) Entry {
197223
if en == nil {
198224
return Entry{}
199225
}
@@ -256,7 +282,7 @@ type Bucket struct {
256282
}
257283

258284
// GetEntries returns a list of observed event entries without data samples.
259-
func (l *Observer) GetEntries() []Entry {
285+
func (l *PreparedObserver) GetEntries() []Entry {
260286
result := make([]Entry, 0, atomic.LoadUint32(&l.count))
261287

262288
l.entries.Range(func(key, value interface{}) bool {
@@ -269,7 +295,7 @@ func (l *Observer) GetEntries() []Entry {
269295
}
270296

271297
// GetEntriesWithSamples returns a list of observed event entries with data samples.
272-
func (l *Observer) GetEntriesWithSamples() []Entry {
298+
func (l *PreparedObserver) GetEntriesWithSamples() []Entry {
273299
result := make([]Entry, 0, atomic.LoadUint32(&l.count))
274300

275301
l.entries.Range(func(key, value interface{}) bool {
@@ -282,7 +308,7 @@ func (l *Observer) GetEntriesWithSamples() []Entry {
282308
}
283309

284310
// Find lookups entry by message.
285-
func (l *Observer) Find(msg string) Entry {
311+
func (l *PreparedObserver) Find(msg string) Entry {
286312
var e Entry
287313

288314
l.entries.Range(func(key, value interface{}) bool {
@@ -299,7 +325,7 @@ func (l *Observer) Find(msg string) Entry {
299325
}
300326

301327
// Other returns entry for other events.
302-
func (l *Observer) Other(withSamples bool) Entry {
328+
func (l *PreparedObserver) Other(withSamples bool) Entry {
303329
return l.exportEntry(l.other, withSamples)
304330
}
305331

observer_test.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func TestObserver_ObserveMessage(t *testing.T) {
14-
o := logz.Observer{}
14+
o := logz.NewObserver(logz.Config{})
1515

1616
o.ObserveMessage("test", 123)
1717
o.ObserveMessage("test", 456)
@@ -43,8 +43,7 @@ func TestObserver_ObserveMessage(t *testing.T) {
4343
}
4444

4545
func TestObserver_ObserveMessage_filter(t *testing.T) {
46-
o := logz.Observer{}
47-
o.Config.FilterMessage = true
46+
o := logz.NewObserver(logz.Config{FilterMessage: true})
4847

4948
o.ObserveMessage("test foo123", 123)
5049
o.ObserveMessage("test bar456", 456)
@@ -76,7 +75,7 @@ func TestObserver_ObserveMessage_filter(t *testing.T) {
7675
}
7776

7877
func BenchmarkObserver_ObserveMessage(b *testing.B) {
79-
o := logz.Observer{}
78+
o := logz.NewObserver(logz.Config{})
8079
wg := sync.WaitGroup{}
8180
concurrency := 50
8281

@@ -99,8 +98,7 @@ func BenchmarkObserver_ObserveMessage(b *testing.B) {
9998
}
10099

101100
func BenchmarkObserver_ObserveMessage_filter(b *testing.B) {
102-
o := logz.Observer{}
103-
o.Config.FilterMessage = true
101+
o := logz.NewObserver(logz.Config{FilterMessage: true})
104102
wg := sync.WaitGroup{}
105103
concurrency := 50
106104

0 commit comments

Comments
 (0)