| 87 | } |
| 88 | |
| 89 | func (s *BurstSampler) inc() uint32 { |
| 90 | now := TimestampFunc().UnixNano() |
| 91 | resetAt := atomic.LoadInt64(&s.resetAt) |
| 92 | var c uint32 |
| 93 | if now >= resetAt { |
| 94 | c = 1 |
| 95 | atomic.StoreUint32(&s.counter, c) |
| 96 | newResetAt := now + s.Period.Nanoseconds() |
| 97 | reset := atomic.CompareAndSwapInt64(&s.resetAt, resetAt, newResetAt) |
| 98 | if !reset { |
| 99 | // Lost the race with another goroutine trying to reset. |
| 100 | c = atomic.AddUint32(&s.counter, 1) |
| 101 | } |
| 102 | } else { |
| 103 | c = atomic.AddUint32(&s.counter, 1) |
| 104 | } |
| 105 | return c |
| 106 | } |
| 107 | |
| 108 | // LevelSampler applies a different sampler for each level. |
| 109 | type LevelSampler struct { |