blob: e3164186367d29a01c789acd8ef66a39f7d6fa4f [file] [log] [blame]
Don Newton7577f072020-01-06 12:41:11 -05001// Copyright (c) 2016 Uber Technologies, Inc.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21package zapcore
22
23import (
24 "time"
25
26 "go.uber.org/atomic"
27)
28
29const (
30 _numLevels = _maxLevel - _minLevel + 1
31 _countersPerLevel = 4096
32)
33
34type counter struct {
35 resetAt atomic.Int64
36 counter atomic.Uint64
37}
38
39type counters [_numLevels][_countersPerLevel]counter
40
41func newCounters() *counters {
42 return &counters{}
43}
44
45func (cs *counters) get(lvl Level, key string) *counter {
46 i := lvl - _minLevel
47 j := fnv32a(key) % _countersPerLevel
48 return &cs[i][j]
49}
50
51// fnv32a, adapted from "hash/fnv", but without a []byte(string) alloc
52func fnv32a(s string) uint32 {
53 const (
54 offset32 = 2166136261
55 prime32 = 16777619
56 )
57 hash := uint32(offset32)
58 for i := 0; i < len(s); i++ {
59 hash ^= uint32(s[i])
60 hash *= prime32
61 }
62 return hash
63}
64
65func (c *counter) IncCheckReset(t time.Time, tick time.Duration) uint64 {
66 tn := t.UnixNano()
67 resetAfter := c.resetAt.Load()
68 if resetAfter > tn {
69 return c.counter.Inc()
70 }
71
72 c.counter.Store(1)
73
74 newResetAfter := tn + tick.Nanoseconds()
75 if !c.resetAt.CAS(resetAfter, newResetAfter) {
76 // We raced with another goroutine trying to reset, and it also reset
77 // the counter to 1, so we need to reincrement the counter.
78 return c.counter.Inc()
79 }
80
81 return 1
82}
83
84type sampler struct {
85 Core
86
87 counts *counters
88 tick time.Duration
89 first, thereafter uint64
90}
91
92// NewSampler creates a Core that samples incoming entries, which caps the CPU
93// and I/O load of logging while attempting to preserve a representative subset
94// of your logs.
95//
96// Zap samples by logging the first N entries with a given level and message
97// each tick. If more Entries with the same level and message are seen during
98// the same interval, every Mth message is logged and the rest are dropped.
99//
100// Keep in mind that zap's sampling implementation is optimized for speed over
101// absolute precision; under load, each tick may be slightly over- or
102// under-sampled.
103func NewSampler(core Core, tick time.Duration, first, thereafter int) Core {
104 return &sampler{
105 Core: core,
106 tick: tick,
107 counts: newCounters(),
108 first: uint64(first),
109 thereafter: uint64(thereafter),
110 }
111}
112
113func (s *sampler) With(fields []Field) Core {
114 return &sampler{
115 Core: s.Core.With(fields),
116 tick: s.tick,
117 counts: s.counts,
118 first: s.first,
119 thereafter: s.thereafter,
120 }
121}
122
123func (s *sampler) Check(ent Entry, ce *CheckedEntry) *CheckedEntry {
124 if !s.Enabled(ent.Level) {
125 return ce
126 }
127
128 counter := s.counts.get(ent.Level, ent.Message)
129 n := counter.IncCheckReset(ent.Time, s.tick)
130 if n > s.first && (n-s.first)%s.thereafter != 0 {
131 return ce
132 }
133 return s.Core.Check(ent, ce)
134}