mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-01-12 02:28:07 +02:00
a01f63bec4
* Do not expose a slice of labels in export.Record This is really an inconvenient implementation detail leak - we may want to store labels in a different way. Replace it with an iterator - it does not force us to use slice of key values as a storage in the long run. * Add Len to LabelIterator It may come in handy in several situations, where we don't have access to export.Labels object, but only to the label iterator. * Use reflect value label iterator for the fixed labels * add reset operation to iterator Makes my life easier when writing a benchmark. Might also be an alternative to cloning the iterator. * Add benchmarks for iterators * Add import comment * Add clone operation to label iterator * Move iterator tests to a separate package * Add tests for cloning iterators * Pass label iterator to export labels * Use non-addressable array reflect values By not using the value created by `reflect.New()`, but rather by `reflect.ValueOf()`, we get a non-addressable array in the value, which does not infer an allocation cost when getting an element from the array. * Drop zero iterator This can be substituted by a reflect value iterator that goes over a value with a zero-sized array. * Add a simple iterator that implements label iterator In the long run this will completely replace the LabelIterator interface. * Replace reflect value iterator with simple iterator * Pass label storage to new export labels, not label iterator * Drop label iterator interface, rename storage iterator to label iterator * Drop clone operation from iterator It's a leftover from interface times and now it's pointless - the iterator is a simple struct, so cloning it is a simple copy. * Drop Reset from label iterator The sole existence of Reset was actually for benchmarking convenience. Now we can just copy the iterator cheaply, so a need for Reset is no more. * Drop noop iterator tests * Move back iterator tests to export package * Eagerly get the reflect value of ordered labels So we won't get into problems when several goroutines want to iterate the same labels at the same time. Not sure if this would be a big deal, since every goroutine would compute the same reflect.Value, but concurrent write to the same memory is bad anyway. And it doesn't cost us any extra allocations anyway. * Replace NewSliceLabelIterator() with a method of LabelSlice * Add some documentation * Documentation fixes
87 lines
2.4 KiB
Go
87 lines
2.4 KiB
Go
// Copyright 2019, OpenTelemetry Authors
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package metric
|
|
|
|
import (
|
|
"bytes"
|
|
"sync"
|
|
|
|
"go.opentelemetry.io/otel/api/core"
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
|
)
|
|
|
|
// escapeChar is used to ensure uniqueness of the label encoding where
|
|
// keys or values contain either '=' or ','. Since there is no parser
|
|
// needed for this encoding and its only requirement is to be unique,
|
|
// this choice is arbitrary. Users will see these in some exporters
|
|
// (e.g., stdout), so the backslash ('\') is used a conventional choice.
|
|
const escapeChar = '\\'
|
|
|
|
type defaultLabelEncoder struct {
|
|
// pool is a pool of labelset builders. The buffers in this
|
|
// pool grow to a size that most label encodings will not
|
|
// allocate new memory. This pool reduces the number of
|
|
// allocations per new LabelSet to 3, typically, as seen in
|
|
// the benchmarks. (It should be 2--one for the LabelSet
|
|
// object and one for the buffer.String() here--see the extra
|
|
// allocation in the call to sort.Stable).
|
|
pool sync.Pool // *bytes.Buffer
|
|
}
|
|
|
|
var _ export.LabelEncoder = &defaultLabelEncoder{}
|
|
|
|
func NewDefaultLabelEncoder() export.LabelEncoder {
|
|
return &defaultLabelEncoder{
|
|
pool: sync.Pool{
|
|
New: func() interface{} {
|
|
return &bytes.Buffer{}
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (d *defaultLabelEncoder) Encode(iter export.LabelIterator) string {
|
|
buf := d.pool.Get().(*bytes.Buffer)
|
|
defer d.pool.Put(buf)
|
|
buf.Reset()
|
|
|
|
for iter.Next() {
|
|
i, kv := iter.IndexedLabel()
|
|
if i > 0 {
|
|
_, _ = buf.WriteRune(',')
|
|
}
|
|
copyAndEscape(buf, string(kv.Key))
|
|
|
|
_, _ = buf.WriteRune('=')
|
|
|
|
if kv.Value.Type() == core.STRING {
|
|
copyAndEscape(buf, kv.Value.AsString())
|
|
} else {
|
|
_, _ = buf.WriteString(kv.Value.Emit())
|
|
}
|
|
}
|
|
return buf.String()
|
|
}
|
|
|
|
func copyAndEscape(buf *bytes.Buffer, val string) {
|
|
for _, ch := range val {
|
|
switch ch {
|
|
case '=', ',', escapeChar:
|
|
buf.WriteRune(escapeChar)
|
|
}
|
|
buf.WriteRune(ch)
|
|
}
|
|
}
|