]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/go.opencensus.io/stats/view/collector.go
Upgrade to 0.12
[github/fretlink/terraform-provider-statuscake.git] / vendor / go.opencensus.io / stats / view / collector.go
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 //
15
16 package view
17
18 import (
19 "sort"
20
21 "go.opencensus.io/exemplar"
22
23 "go.opencensus.io/internal/tagencoding"
24 "go.opencensus.io/tag"
25 )
26
27 type collector struct {
28 // signatures holds the aggregations values for each unique tag signature
29 // (values for all keys) to its aggregator.
30 signatures map[string]AggregationData
31 // Aggregation is the description of the aggregation to perform for this
32 // view.
33 a *Aggregation
34 }
35
36 func (c *collector) addSample(s string, e *exemplar.Exemplar) {
37 aggregator, ok := c.signatures[s]
38 if !ok {
39 aggregator = c.a.newData()
40 c.signatures[s] = aggregator
41 }
42 aggregator.addSample(e)
43 }
44
45 // collectRows returns a snapshot of the collected Row values.
46 func (c *collector) collectedRows(keys []tag.Key) []*Row {
47 rows := make([]*Row, 0, len(c.signatures))
48 for sig, aggregator := range c.signatures {
49 tags := decodeTags([]byte(sig), keys)
50 row := &Row{Tags: tags, Data: aggregator.clone()}
51 rows = append(rows, row)
52 }
53 return rows
54 }
55
56 func (c *collector) clearRows() {
57 c.signatures = make(map[string]AggregationData)
58 }
59
60 // encodeWithKeys encodes the map by using values
61 // only associated with the keys provided.
62 func encodeWithKeys(m *tag.Map, keys []tag.Key) []byte {
63 vb := &tagencoding.Values{
64 Buffer: make([]byte, len(keys)),
65 }
66 for _, k := range keys {
67 v, _ := m.Value(k)
68 vb.WriteValue([]byte(v))
69 }
70 return vb.Bytes()
71 }
72
73 // decodeTags decodes tags from the buffer and
74 // orders them by the keys.
75 func decodeTags(buf []byte, keys []tag.Key) []tag.Tag {
76 vb := &tagencoding.Values{Buffer: buf}
77 var tags []tag.Tag
78 for _, k := range keys {
79 v := vb.ReadValue()
80 if v != nil {
81 tags = append(tags, tag.Tag{Key: k, Value: string(v)})
82 }
83 }
84 vb.ReadIndex = 0
85 sort.Slice(tags, func(i, j int) bool { return tags[i].Key.Name() < tags[j].Key.Name() })
86 return tags
87 }