podman
130 строк · 3.3 Кб
1// Copyright 2017, OpenCensus Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package trace
16
17import (
18"time"
19)
20
21// samplePeriod is the minimum time between accepting spans in a single bucket.
22const samplePeriod = time.Second
23
24// defaultLatencies contains the default latency bucket bounds.
25// TODO: consider defaults, make configurable
26var defaultLatencies = [...]time.Duration{
2710 * time.Microsecond,
28100 * time.Microsecond,
29time.Millisecond,
3010 * time.Millisecond,
31100 * time.Millisecond,
32time.Second,
3310 * time.Second,
34time.Minute,
35}
36
37// bucket is a container for a set of spans for a particular error code or latency range.
38type bucket struct {
39nextTime time.Time // next time we can accept a span
40buffer []*SpanData // circular buffer of spans
41nextIndex int // location next SpanData should be placed in buffer
42overflow bool // whether the circular buffer has wrapped around
43}
44
45func makeBucket(bufferSize int) bucket {
46return bucket{
47buffer: make([]*SpanData, bufferSize),
48}
49}
50
51// add adds a span to the bucket, if nextTime has been reached.
52func (b *bucket) add(s *SpanData) {
53if s.EndTime.Before(b.nextTime) {
54return
55}
56if len(b.buffer) == 0 {
57return
58}
59b.nextTime = s.EndTime.Add(samplePeriod)
60b.buffer[b.nextIndex] = s
61b.nextIndex++
62if b.nextIndex == len(b.buffer) {
63b.nextIndex = 0
64b.overflow = true
65}
66}
67
68// size returns the number of spans in the bucket.
69func (b *bucket) size() int {
70if b.overflow {
71return len(b.buffer)
72}
73return b.nextIndex
74}
75
76// span returns the ith span in the bucket.
77func (b *bucket) span(i int) *SpanData {
78if !b.overflow {
79return b.buffer[i]
80}
81if i < len(b.buffer)-b.nextIndex {
82return b.buffer[b.nextIndex+i]
83}
84return b.buffer[b.nextIndex+i-len(b.buffer)]
85}
86
87// resize changes the size of the bucket to n, keeping up to n existing spans.
88func (b *bucket) resize(n int) {
89cur := b.size()
90newBuffer := make([]*SpanData, n)
91if cur < n {
92for i := 0; i < cur; i++ {
93newBuffer[i] = b.span(i)
94}
95b.buffer = newBuffer
96b.nextIndex = cur
97b.overflow = false
98return
99}
100for i := 0; i < n; i++ {
101newBuffer[i] = b.span(i + cur - n)
102}
103b.buffer = newBuffer
104b.nextIndex = 0
105b.overflow = true
106}
107
108// latencyBucket returns the appropriate bucket number for a given latency.
109func latencyBucket(latency time.Duration) int {
110i := 0
111for i < len(defaultLatencies) && latency >= defaultLatencies[i] {
112i++
113}
114return i
115}
116
117// latencyBucketBounds returns the lower and upper bounds for a latency bucket
118// number.
119//
120// The lower bound is inclusive, the upper bound is exclusive (except for the
121// last bucket.)
122func latencyBucketBounds(index int) (lower time.Duration, upper time.Duration) {
123if index == 0 {
124return 0, defaultLatencies[index]
125}
126if index == len(defaultLatencies) {
127return defaultLatencies[index-1], 1<<63 - 1
128}
129return defaultLatencies[index-1], defaultLatencies[index]
130}
131