1 // Copyright (c) 2016 Uber Technologies, Inc.
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:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
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
30 _numLevels = _maxLevel - _minLevel + 1
31 _countersPerLevel = 4096
39 type counters [_numLevels][_countersPerLevel]counter
41 func newCounters() *counters {
45 func (cs *counters) get(lvl Level, key string) *counter {
47 j := fnv32a(key) % _countersPerLevel
51 // fnv32a, adapted from "hash/fnv", but without a []byte(string) alloc
52 func fnv32a(s string) uint32 {
57 hash := uint32(offset32)
58 for i := 0; i < len(s); i++ {
65 func (c *counter) IncCheckReset(t time.Time, tick time.Duration) uint64 {
67 resetAfter := c.resetAt.Load()
69 return c.counter.Inc()
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()
89 first, thereafter uint64
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
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.
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
103 func NewSampler(core Core, tick time.Duration, first, thereafter int) Core {
107 counts: newCounters(),
108 first: uint64(first),
109 thereafter: uint64(thereafter),
113 func (s *sampler) With(fields []Field) Core {
115 Core: s.Core.With(fields),
119 thereafter: s.thereafter,
123 func (s *sampler) Check(ent Entry, ce *CheckedEntry) *CheckedEntry {
124 if !s.Enabled(ent.Level) {
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 {
133 return s.Core.Check(ent, ce)