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.
16 // Package tagencoding contains the tag encoding
17 // used interally by the stats collector.
18 package tagencoding // import "go.opencensus.io/internal/tagencoding"
20 // Values represent the encoded buffer for the values.
27 func (vb *Values) growIfRequired(expected int) {
28 if len(vb.Buffer)-vb.WriteIndex < expected {
29 tmp := make([]byte, 2*(len(vb.Buffer)+1)+expected)
35 // WriteValue is the helper method to encode Values from map[Key][]byte.
36 func (vb *Values) WriteValue(v []byte) {
37 length := len(v) & 0xff
38 vb.growIfRequired(1 + length)
40 // writing length of v
41 vb.Buffer[vb.WriteIndex] = byte(length)
45 // No value was encoded for this key
50 copy(vb.Buffer[vb.WriteIndex:], v[:length])
51 vb.WriteIndex += length
54 // ReadValue is the helper method to decode Values to a map[Key][]byte.
55 func (vb *Values) ReadValue() []byte {
57 length := int(vb.Buffer[vb.ReadIndex])
60 // No value was encoded for this key
65 v := make([]byte, length)
66 endIdx := vb.ReadIndex + length
67 copy(v, vb.Buffer[vb.ReadIndex:endIdx])
72 // Bytes returns a reference to already written bytes in the Buffer.
73 func (vb *Values) Bytes() []byte {
74 return vb.Buffer[:vb.WriteIndex]