1 // Copyright 2017, OpenCensus Authors
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
22 "go.opencensus.io/internal/tagencoding"
23 "go.opencensus.io/tag"
26 type collector struct {
27 // signatures holds the aggregations values for each unique tag signature
28 // (values for all keys) to its aggregator.
29 signatures map[string]AggregationData
30 // Aggregation is the description of the aggregation to perform for this
35 func (c *collector) addSample(s string, v float64, attachments map[string]interface{}, t time.Time) {
36 aggregator, ok := c.signatures[s]
38 aggregator = c.a.newData()
39 c.signatures[s] = aggregator
41 aggregator.addSample(v, attachments, t)
44 // collectRows returns a snapshot of the collected Row values.
45 func (c *collector) collectedRows(keys []tag.Key) []*Row {
46 rows := make([]*Row, 0, len(c.signatures))
47 for sig, aggregator := range c.signatures {
48 tags := decodeTags([]byte(sig), keys)
49 row := &Row{Tags: tags, Data: aggregator.clone()}
50 rows = append(rows, row)
55 func (c *collector) clearRows() {
56 c.signatures = make(map[string]AggregationData)
59 // encodeWithKeys encodes the map by using values
60 // only associated with the keys provided.
61 func encodeWithKeys(m *tag.Map, keys []tag.Key) []byte {
62 vb := &tagencoding.Values{
63 Buffer: make([]byte, len(keys)),
65 for _, k := range keys {
67 vb.WriteValue([]byte(v))
72 // decodeTags decodes tags from the buffer and
73 // orders them by the keys.
74 func decodeTags(buf []byte, keys []tag.Key) []tag.Tag {
75 vb := &tagencoding.Values{Buffer: buf}
77 for _, k := range keys {
80 tags = append(tags, tag.Tag{Key: k, Value: string(v)})
84 sort.Slice(tags, func(i, j int) bool { return tags[i].Key.Name() < tags[j].Key.Name() })