2021-08-09 07:19:24 +00:00
|
|
|
// Copyright The 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 attribute // import "go.opentelemetry.io/otel/attribute"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2023-02-01 17:06:36 +00:00
|
|
|
// Encoder is a mechanism for serializing an attribute set into a specific
|
|
|
|
// string representation that supports caching, to avoid repeated
|
|
|
|
// serialization. An example could be an exporter encoding the attribute
|
|
|
|
// set into a wire representation.
|
2021-08-09 07:19:24 +00:00
|
|
|
Encoder interface {
|
2023-02-01 17:06:36 +00:00
|
|
|
// Encode returns the serialized encoding of the attribute set using
|
|
|
|
// its Iterator. This result may be cached by a attribute.Set.
|
2021-08-09 07:19:24 +00:00
|
|
|
Encode(iterator Iterator) string
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// ID returns a value that is unique for each class of attribute
|
|
|
|
// encoder. Attribute encoders allocate these using `NewEncoderID`.
|
2021-08-09 07:19:24 +00:00
|
|
|
ID() EncoderID
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncoderID is used to identify distinct Encoder
|
|
|
|
// implementations, for caching encoded results.
|
|
|
|
EncoderID struct {
|
|
|
|
value uint64
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// defaultAttrEncoder uses a sync.Pool of buffers to reduce the number of
|
|
|
|
// allocations used in encoding attributes. This implementation encodes a
|
|
|
|
// comma-separated list of key=value, with '/'-escaping of '=', ',', and
|
|
|
|
// '\'.
|
|
|
|
defaultAttrEncoder struct {
|
|
|
|
// pool is a pool of attribute set builders. The buffers in this pool
|
|
|
|
// grow to a size that most attribute encodings will not allocate new
|
|
|
|
// memory.
|
2021-08-09 07:19:24 +00:00
|
|
|
pool sync.Pool // *bytes.Buffer
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// escapeChar is used to ensure uniqueness of the attribute 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 as a conventional choice.
|
2021-08-09 07:19:24 +00:00
|
|
|
const escapeChar = '\\'
|
|
|
|
|
|
|
|
var (
|
2023-02-01 17:06:36 +00:00
|
|
|
_ Encoder = &defaultAttrEncoder{}
|
2021-08-09 07:19:24 +00:00
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// encoderIDCounter is for generating IDs for other attribute encoders.
|
2021-08-09 07:19:24 +00:00
|
|
|
encoderIDCounter uint64
|
|
|
|
|
|
|
|
defaultEncoderOnce sync.Once
|
|
|
|
defaultEncoderID = NewEncoderID()
|
2023-02-01 17:06:36 +00:00
|
|
|
defaultEncoderInstance *defaultAttrEncoder
|
2021-08-09 07:19:24 +00:00
|
|
|
)
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// NewEncoderID returns a unique attribute encoder ID. It should be called
|
|
|
|
// once per each type of attribute encoder. Preferably in init() or in var
|
|
|
|
// definition.
|
2021-08-09 07:19:24 +00:00
|
|
|
func NewEncoderID() EncoderID {
|
|
|
|
return EncoderID{value: atomic.AddUint64(&encoderIDCounter, 1)}
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// DefaultEncoder returns an attribute encoder that encodes attributes in such
|
|
|
|
// a way that each escaped attribute's key is followed by an equal sign and
|
|
|
|
// then by an escaped attribute's value. All key-value pairs are separated by
|
|
|
|
// a comma.
|
2021-08-09 07:19:24 +00:00
|
|
|
//
|
2023-02-01 17:06:36 +00:00
|
|
|
// Escaping is done by prepending a backslash before either a backslash, equal
|
|
|
|
// sign or a comma.
|
2021-08-09 07:19:24 +00:00
|
|
|
func DefaultEncoder() Encoder {
|
|
|
|
defaultEncoderOnce.Do(func() {
|
2023-02-01 17:06:36 +00:00
|
|
|
defaultEncoderInstance = &defaultAttrEncoder{
|
2021-08-09 07:19:24 +00:00
|
|
|
pool: sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return &bytes.Buffer{}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return defaultEncoderInstance
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// Encode is a part of an implementation of the AttributeEncoder interface.
|
|
|
|
func (d *defaultAttrEncoder) Encode(iter Iterator) string {
|
2021-08-09 07:19:24 +00:00
|
|
|
buf := d.pool.Get().(*bytes.Buffer)
|
|
|
|
defer d.pool.Put(buf)
|
|
|
|
buf.Reset()
|
|
|
|
|
|
|
|
for iter.Next() {
|
2023-02-01 17:06:36 +00:00
|
|
|
i, keyValue := iter.IndexedAttribute()
|
2021-08-09 07:19:24 +00:00
|
|
|
if i > 0 {
|
|
|
|
_, _ = buf.WriteRune(',')
|
|
|
|
}
|
|
|
|
copyAndEscape(buf, string(keyValue.Key))
|
|
|
|
|
|
|
|
_, _ = buf.WriteRune('=')
|
|
|
|
|
|
|
|
if keyValue.Value.Type() == STRING {
|
|
|
|
copyAndEscape(buf, keyValue.Value.AsString())
|
|
|
|
} else {
|
|
|
|
_, _ = buf.WriteString(keyValue.Value.Emit())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// ID is a part of an implementation of the AttributeEncoder interface.
|
|
|
|
func (*defaultAttrEncoder) ID() EncoderID {
|
2021-08-09 07:19:24 +00:00
|
|
|
return defaultEncoderID
|
|
|
|
}
|
|
|
|
|
|
|
|
// copyAndEscape escapes `=`, `,` and its own escape character (`\`),
|
|
|
|
// making the default encoding unique.
|
|
|
|
func copyAndEscape(buf *bytes.Buffer, val string) {
|
|
|
|
for _, ch := range val {
|
|
|
|
switch ch {
|
|
|
|
case '=', ',', escapeChar:
|
2023-02-01 17:06:36 +00:00
|
|
|
_, _ = buf.WriteRune(escapeChar)
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
2023-02-01 17:06:36 +00:00
|
|
|
_, _ = buf.WriteRune(ch)
|
2021-08-09 07:19:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid returns true if this encoder ID was allocated by
|
|
|
|
// `NewEncoderID`. Invalid encoder IDs will not be cached.
|
|
|
|
func (id EncoderID) Valid() bool {
|
|
|
|
return id.value != 0
|
|
|
|
}
|