Attributes are key-value pairs that provide additional context to telemetry data (spans, metrics, logs).
import "go.opentelemetry.io/otel/attribute"Attributes enable:
import "go.opentelemetry.io/otel/attribute"
// Create attributes
attrs := []attribute.KeyValue{
attribute.String("user.id", "12345"),
attribute.Int("status.code", 200),
attribute.Bool("cache.hit", true),
attribute.Float64("duration", 0.123),
}
// Use with spans
span.SetAttributes(attrs...)
// Use with metrics
counter.Add(ctx, 1, metric.WithAttributes(attrs...))type Key string
// Create KeyValues
func (k Key) Bool(v bool) KeyValue
func (k Key) Int(v int) KeyValue
func (k Key) Int64(v int64) KeyValue
func (k Key) Float64(v float64) KeyValue
func (k Key) String(v string) KeyValue
func (k Key) BoolSlice(v []bool) KeyValue
func (k Key) IntSlice(v []int) KeyValue
func (k Key) Int64Slice(v []int64) KeyValue
func (k Key) Float64Slice(v []float64) KeyValue
func (k Key) StringSlice(v []string) KeyValue
// Utility
func (k Key) Defined() booltype KeyValue struct {
Key Key
Value Value
}
// Constructors
func Bool(k string, v bool) KeyValue
func Int(k string, v int) KeyValue
func Int64(k string, v int64) KeyValue
func Float64(k string, v float64) KeyValue
func String(k, v string) KeyValue
func Stringer(k string, v fmt.Stringer) KeyValue
func BoolSlice(k string, v []bool) KeyValue
func IntSlice(k string, v []int) KeyValue
func Int64Slice(k string, v []int64) KeyValue
func Float64Slice(k string, v []float64) KeyValue
func StringSlice(k string, v []string) KeyValue
// Validation
func (kv KeyValue) Valid() booltype Value struct {
// Has unexported fields.
}
// Constructors
func BoolValue(v bool) Value
func IntValue(v int) Value
func Int64Value(v int64) Value
func Float64Value(v float64) Value
func StringValue(v string) Value
func BoolSliceValue(v []bool) Value
func IntSliceValue(v []int) Value
func Int64SliceValue(v []int64) Value
func Float64SliceValue(v []float64) Value
func StringSliceValue(v []string) Value
// Getters
func (v Value) AsBool() bool
func (v Value) AsInt64() int64
func (v Value) AsFloat64() float64
func (v Value) AsString() string
func (v Value) AsBoolSlice() []bool
func (v Value) AsInt64Slice() []int64
func (v Value) AsFloat64Slice() []float64
func (v Value) AsStringSlice() []string
func (v Value) AsInterface() any
// Utility
func (v Value) Type() Type
func (v Value) Emit() string
func (v Value) MarshalJSON() ([]byte, error)type Type int
const (
INVALID Type = iota
BOOL
INT64
FLOAT64
STRING
BOOLSLICE
INT64SLICE
FLOAT64SLICE
STRINGSLICE
)
func (i Type) String() stringtype Set struct {
// Has unexported fields.
}
// Create sets
func NewSet(kvs ...KeyValue) Set
func NewSetWithFiltered(kvs []KeyValue, filter Filter) (Set, []KeyValue)
func EmptySet() *Set
// Methods
func (l *Set) Get(idx int) (KeyValue, bool)
func (l *Set) Value(k Key) (Value, bool)
func (l *Set) HasValue(k Key) bool
func (l *Set) Iter() Iterator
func (l *Set) ToSlice() []KeyValue
func (l *Set) Len() int
func (l *Set) Filter(re Filter) (Set, []KeyValue)
func (l *Set) Equals(o *Set) bool
func (l *Set) Equivalent() Distinct
func (l *Set) Encoded(encoder Encoder) string
func (l *Set) MarshalJSON() ([]byte, error)
func (l Set) MarshalLog() anytype Distinct struct {
// Has unexported fields.
}
func (d Distinct) Valid() booltype Filter func(KeyValue) bool
// Create filters
func NewAllowKeysFilter(keys ...Key) Filter
func NewDenyKeysFilter(keys ...Key) FilterExample:
// Allow only specific keys
filter := attribute.NewAllowKeysFilter(
attribute.Key("user.id"),
attribute.Key("session.id"),
)
// Apply filter
filtered, removed := set.Filter(filter)type Iterator struct {
// Has unexported fields.
}
func (i *Iterator) Next() bool
func (i *Iterator) Attribute() KeyValue
func (i *Iterator) IndexedAttribute() (int, KeyValue)
func (i *Iterator) Len() int
func (i *Iterator) ToSlice() []KeyValuetype MergeIterator struct {
// Has unexported fields.
}
func NewMergeIterator(s1, s2 *Set) MergeIterator
func (m *MergeIterator) Next() bool
func (m *MergeIterator) Attribute() KeyValuetype Encoder interface {
// Encode returns the serialized encoding of the attribute set
Encode(iterator Iterator) string
// ID returns a value that is unique for each class of attribute encoder
ID() EncoderID
}
// Default encoder
func DefaultEncoder() Encodertype EncoderID struct {
// Has unexported fields.
}
func NewEncoderID() EncoderID
func (id EncoderID) Valid() boolpackage main
import (
"fmt"
"go.opentelemetry.io/otel/attribute"
)
func main() {
// Create attributes
attrs := []attribute.KeyValue{
attribute.String("service.name", "api"),
attribute.String("service.version", "1.0.0"),
attribute.Int("http.status_code", 200),
attribute.Float64("http.duration", 0.123),
attribute.Bool("cache.hit", true),
attribute.StringSlice("tags", []string{"prod", "us-west"}),
}
// Create set
set := attribute.NewSet(attrs...)
// Query attributes
if value, ok := set.Value(attribute.Key("http.status_code")); ok {
fmt.Printf("Status: %d\n", value.AsInt64())
}
// Check existence
if set.HasValue(attribute.Key("cache.hit")) {
fmt.Println("Cache hit attribute exists")
}
// Iterate
iter := set.Iter()
for iter.Next() {
kv := iter.Attribute()
fmt.Printf("%s = %v\n", kv.Key, kv.Value.AsInterface())
}
// Filter
filter := attribute.NewAllowKeysFilter(
attribute.Key("service.name"),
attribute.Key("service.version"),
)
filtered, _ := set.Filter(filter)
fmt.Printf("Filtered set has %d attributes\n", filtered.Len())
// Encode
encoded := set.Encoded(attribute.DefaultEncoder())
fmt.Printf("Encoded: %s\n", encoded)
}