Debian Codegraph @

zpages/bucket.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Copyright 2017, OpenCensus Authors
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17
18 package zpages // import "go.opentelemetry.io/contrib/zpages"
19
20 import (
21 "time"
22
23 sdktrace "go.opentelemetry.io/otel/sdk/trace"
24 )
25
26 const (
27 // defaultBucketCapacity is the default capacity for every bucket (latency or error based).
28 defaultBucketCapacity = 10
29 // samplePeriod is the minimum time between accepting spans in a single bucket.
30 samplePeriod = time.Second
31 )
32
33 // bucket is a container for a set of spans for latency buckets or errored spans.
34 type bucket struct {
35 nextTime time.Time // next time we can accept a span
36 buffer []sdktrace.ReadOnlySpan // circular buffer of spans
37 nextIndex int // location next ReadOnlySpan should be placed in buffer
38 overflow bool // whether the circular buffer has wrapped around
39 }
40
41 // newBucket returns a new bucket with the given capacity.
42 func newBucket(capacity uint) *bucket {
43 return &bucket{
44 buffer: make([]sdktrace.ReadOnlySpan, capacity),
45 }
46 }
47
48 // add adds a span to the bucket, if nextTime has been reached.
49 func (b *bucket) add(s sdktrace.ReadOnlySpan) {
50 if s.EndTime().Before(b.nextTime) {
51 return
52 }
53 if len(b.buffer) == 0 {
54 return
55 }
56 b.nextTime = s.EndTime().Add(samplePeriod)
57 b.buffer[b.nextIndex] = s
58 b.nextIndex++
59 if b.nextIndex == len(b.buffer) {
60 b.nextIndex = 0
61 b.overflow = true
62 }
63 }
64
65 // len returns the number of spans in the bucket.
66 func (b *bucket) len() int {
67 if b.overflow {
68 return len(b.buffer)
69 }
70 return b.nextIndex
71 }
72
73 // spans returns the spans in this bucket.
74 func (b *bucket) spans() []sdktrace.ReadOnlySpan {
75 return append([]sdktrace.ReadOnlySpan(nil), b.buffer[0:b.len()]...)
76 }