or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

attributes.mdbridges.mdcontext-propagation.mdexporters-other.mdexporters-otlp.mdindex.mdlogging.mdmetrics.mdsdk-log.mdsdk-metric.mdsdk-resource.mdsdk-trace.mdsemantic-conventions.mdtracing.md
tile.json

attributes.mddocs/

Attributes

Attributes are key-value pairs that provide additional context to telemetry data (spans, metrics, logs).

Package Import

import "go.opentelemetry.io/otel/attribute"

Overview

Attributes enable:

  • Type-safe key-value pairs
  • Efficient attribute sets
  • Filtering and encoding
  • Semantic conventions support

Quick Start

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...))

Key and KeyValue

Key Type

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() bool

KeyValue Type

type 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() bool

Value Types

Value

type 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 Type int

const (
	INVALID      Type = iota
	BOOL
	INT64
	FLOAT64
	STRING
	BOOLSLICE
	INT64SLICE
	FLOAT64SLICE
	STRINGSLICE
)

func (i Type) String() string

Attribute Sets

Set Type

type 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() any

Distinct

type Distinct struct {
	// Has unexported fields.
}

func (d Distinct) Valid() bool

Filtering

Filter

type Filter func(KeyValue) bool

// Create filters
func NewAllowKeysFilter(keys ...Key) Filter
func NewDenyKeysFilter(keys ...Key) Filter

Example:

// Allow only specific keys
filter := attribute.NewAllowKeysFilter(
	attribute.Key("user.id"),
	attribute.Key("session.id"),
)

// Apply filter
filtered, removed := set.Filter(filter)

Iteration

Iterator

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() []KeyValue

MergeIterator

type MergeIterator struct {
	// Has unexported fields.
}

func NewMergeIterator(s1, s2 *Set) MergeIterator

func (m *MergeIterator) Next() bool
func (m *MergeIterator) Attribute() KeyValue

Encoding

Encoder

type 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() Encoder

EncoderID

type EncoderID struct {
	// Has unexported fields.
}

func NewEncoderID() EncoderID
func (id EncoderID) Valid() bool

Complete Example

package 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)
}

Related Documentation

  • Semantic Conventions: Standard attribute names
  • Tracing: Using attributes with spans
  • Metrics: Using attributes with metrics