x
v0.69.0
Documentation
pkg.go.dev ↗Package x contains experimental metric options and extension interfaces.
Functions
WithDefaultAttributes
func WithDefaultAttributes(keys ...attribute.Key) metric.InstrumentOption
WithDefaultAttributes returns a metric.InstrumentOption that specifies default attribute keys. The implementation should treat keys that are not included in the passed keys as opt-in, and they should be filtered out by default. Users of go.opentelemetry.io/otel/sdk/metric can enable these attributes by using the AttributeFilter of a View to include them.
WithUnsafeAttributes
func WithUnsafeAttributes(kvs ...attribute.KeyValue) metric.MeasurementOption
WithUnsafeAttributes returns a metric.MeasurementOption that stores the raw attributes and associates them with a measurement without making a copy. The caller must not modify the attributes slice after passing it to this function. This is a work-in-progress, and does not yet have better performance than metric.WithAttributeSet.
Attributes passed via WithUnsafeAttributes are merged with attributes from standard options (e.g., metric.WithAttributes). If duplicate keys exist, attributes from WithUnsafeAttributes take precedence and will override standard attributes, regardless of the order they are passed.
Types
Finisher
type Finisher interface {
// Finish marks the series identified by attrs as finished. Its final
// aggregate is eligible for one collection before the series state is
// released.
//
// Implementations must not retain or modify attrs.
//
// Finish is safe to call concurrently with measurement and collection.
Finish(ctx context.Context, attrs ...attribute.KeyValue)
}
Finisher is implemented by synchronous instruments that support ending the lifetime of a series identified by exact attributes.
The attributes passed to Finisher.Finish are matched as a complete collection, not as a subset. Attribute order does not affect identity, and duplicate keys use last-value-wins semantics. Calling Finish without attributes identifies only the series recorded without attributes; it does not select every series.
Finishing an unknown, already-finished, dropped, or unsupported series is a no-op. Finishing attributes mapped to a shared cardinality-overflow series is also a no-op.
Finisher is an optional interface. Callers should use a type assertion to determine whether an instrument supports it. Instruments obtained from the global MeterProvider before a delegate is registered cannot acquire this optional method later. Callers that require this capability should obtain their instruments directly from a configured MeterProvider.
Example
{
var counter metric.Int64Counter = noop.Int64Counter{}
ctx := context.Background()
attrs := []attribute.KeyValue{
attribute.String("service.name", "checkout"),
attribute.Int("service.port", 8080),
}
counter.Add(ctx, 1, metric.WithAttributes(attrs...))
if finisher, ok := counter.(x.Finisher); ok {
finisher.Finish(ctx, attrs...)
}
}
Float64CounterBinder
type Float64CounterBinder interface {
// Bind returns a Float64Counter bound to the provided attributes.
// Measurements recorded on the returned counter will use the bound attributes.
//
// Bound instruments still accept attribute options (e.g., [metric.WithAttributes]
// or [metric.WithAttributeSet]) when recording measurements, but passing
// attributes at record time can negate the performance benefits of binding.
//
// Implementations of this method need to be safe for a user to call
// concurrently.
//
// Implementations must not retain the provided slice; the caller may
// modify or reuse it after Bind returns.
Bind(attrs ...attribute.KeyValue) metric.Float64Counter
}
Float64CounterBinder is an interface for instruments that support binding attributes ahead of time.
Float64GaugeBinder
type Float64GaugeBinder interface {
// Bind returns a Float64Gauge bound to the provided attributes.
// Measurements recorded on the returned gauge will use the bound attributes.
//
// Bound instruments still accept attribute options (e.g., [metric.WithAttributes]
// or [metric.WithAttributeSet]) when recording measurements, but passing
// attributes at record time can negate the performance benefits of binding.
//
// Implementations of this method need to be safe for a user to call
// concurrently.
//
// Implementations must not retain the provided slice; the caller may
// modify or reuse it after Bind returns.
Bind(attrs ...attribute.KeyValue) metric.Float64Gauge
}
Float64GaugeBinder is an interface for instruments that support binding attributes ahead of time.
Float64HistogramBinder
type Float64HistogramBinder interface {
// Bind returns a Float64Histogram bound to the provided attributes.
// Measurements recorded on the returned histogram will use the bound attributes.
//
// Bound instruments still accept attribute options (e.g., [metric.WithAttributes]
// or [metric.WithAttributeSet]) when recording measurements, but passing
// attributes at record time can negate the performance benefits of binding.
//
// Implementations of this method need to be safe for a user to call
// concurrently.
//
// Implementations must not retain the provided slice; the caller may
// modify or reuse it after Bind returns.
Bind(attrs ...attribute.KeyValue) metric.Float64Histogram
}
Float64HistogramBinder is an interface for instruments that support binding attributes ahead of time.
Float64UpDownCounterBinder
type Float64UpDownCounterBinder interface {
// Bind returns a Float64UpDownCounter bound to the provided attributes.
// Measurements recorded on the returned counter will use the bound attributes.
//
// Bound instruments still accept attribute options (e.g., [metric.WithAttributes]
// or [metric.WithAttributeSet]) when recording measurements, but passing
// attributes at record time can negate the performance benefits of binding.
//
// Implementations of this method need to be safe for a user to call
// concurrently.
//
// Implementations must not retain the provided slice; the caller may
// modify or reuse it after Bind returns.
Bind(attrs ...attribute.KeyValue) metric.Float64UpDownCounter
}
Float64UpDownCounterBinder is an interface for instruments that support binding attributes ahead of time.
Int64CounterBinder
type Int64CounterBinder interface {
// Bind returns an Int64Counter bound to the provided attributes.
// Measurements recorded on the returned counter will use the bound attributes.
//
// Bound instruments still accept attribute options (e.g., [metric.WithAttributes]
// or [metric.WithAttributeSet]) when recording measurements, but passing
// attributes at record time can negate the performance benefits of binding.
//
// Implementations of this method need to be safe for a user to call
// concurrently.
//
// Implementations must not retain the provided slice; the caller may
// modify or reuse it after Bind returns.
Bind(attrs ...attribute.KeyValue) metric.Int64Counter
}
Int64CounterBinder is an interface for instruments that support binding attributes ahead of time.
Example
{
var counter metric.Int64Counter = noop.Int64Counter{}
attrs := []attribute.KeyValue{
attribute.String("handler", "orders"),
}
var opts []metric.AddOption
if b, ok := counter.(x.Int64CounterBinder); ok {
counter = b.Bind(attrs...)
} else {
opts = append(opts, metric.WithAttributeSet(attribute.NewSet(attrs...)))
}
http.HandleFunc("/orders", func(_ http.ResponseWriter, r *http.Request) {
counter.Add(r.Context(), 1, opts...)
})
}
Int64GaugeBinder
type Int64GaugeBinder interface {
// Bind returns an Int64Gauge bound to the provided attributes.
// Measurements recorded on the returned gauge will use the bound attributes.
//
// Bound instruments still accept attribute options (e.g., [metric.WithAttributes]
// or [metric.WithAttributeSet]) when recording measurements, but passing
// attributes at record time can negate the performance benefits of binding.
//
// Implementations of this method need to be safe for a user to call
// concurrently.
//
// Implementations must not retain the provided slice; the caller may
// modify or reuse it after Bind returns.
Bind(attrs ...attribute.KeyValue) metric.Int64Gauge
}
Int64GaugeBinder is an interface for instruments that support binding attributes ahead of time.
Int64HistogramBinder
type Int64HistogramBinder interface {
// Bind returns an Int64Histogram bound to the provided attributes.
// Measurements recorded on the returned histogram will use the bound attributes.
//
// Bound instruments still accept attribute options (e.g., [metric.WithAttributes]
// or [metric.WithAttributeSet]) when recording measurements, but passing
// attributes at record time can negate the performance benefits of binding.
//
// Implementations of this method need to be safe for a user to call
// concurrently.
//
// Implementations must not retain the provided slice; the caller may
// modify or reuse it after Bind returns.
Bind(attrs ...attribute.KeyValue) metric.Int64Histogram
}
Int64HistogramBinder is an interface for instruments that support binding attributes ahead of time.
Int64UpDownCounterBinder
type Int64UpDownCounterBinder interface {
// Bind returns an Int64UpDownCounter bound to the provided attributes.
// Measurements recorded on the returned counter will use the bound attributes.
//
// Bound instruments still accept attribute options (e.g., [metric.WithAttributes]
// or [metric.WithAttributeSet]) when recording measurements, but passing
// attributes at record time can negate the performance benefits of binding.
//
// Implementations of this method need to be safe for a user to call
// concurrently.
//
// Implementations must not retain the provided slice; the caller may
// modify or reuse it after Bind returns.
Bind(attrs ...attribute.KeyValue) metric.Int64UpDownCounter
}
Int64UpDownCounterBinder is an interface for instruments that support binding attributes ahead of time.
Settable
type Settable[T any] interface {
Set(T)
}
Settable is an optional interface that Options can implement to allow reuse without additional allocations.
Example usage with sync.Pool:
var optionPool = sync.Pool{
New: func() any {
return metric.WithAttributeSet(*attribute.EmptySet())
},
}
func record(ctx context.Context, counter metric.Int64Counter, set attribute.Set) {
opt := optionPool.Get().(metric.MeasurementOption)
defer optionPool.Put(opt)
if r, ok := opt.(x.Settable[attribute.Set]); ok {
r.Set(set)
} else {
opt = metric.WithAttributeSet(set)
}
counter.Add(ctx, 1, opt)
}
WARNING: It is the user's responsibility to ensure that the option is not concurrently set while being passed to the API or used by another goroutine.