...

Package metric

import "go.opentelemetry.io/otel/metric"
Overview
Index
Examples
Subdirectories

Overview ▾

Package metric provides the OpenTelemetry API used to measure metrics about source code operation.

This API is separate from its implementation so the instrumentation built from it is reusable. See go.opentelemetry.io/otel/sdk/metric for the official OpenTelemetry implementation of this API.

All measurements made with this package are made via instruments. These instruments are created by a Meter which itself is created by a MeterProvider. Applications need to accept a MeterProvider implementation as a starting point when instrumenting. This can be done directly, or by using the OpenTelemetry global MeterProvider via GetMeterProvider. Using an appropriately named Meter from the accepted MeterProvider, instrumentation can then be built from the Meter's instruments.

Instruments

Each instrument is designed to make measurements of a particular type. Broadly, all instruments fall into two overlapping logical categories: asynchronous or synchronous, and int64 or float64.

All synchronous instruments (Int64Counter, Int64UpDownCounter, Int64Histogram, Float64Counter, Float64UpDownCounter, and Float64Histogram) are used to measure the operation and performance of source code during the source code execution. These instruments only make measurements when the source code they instrument is run.

All asynchronous instruments (Int64ObservableCounter, Int64ObservableUpDownCounter, Int64ObservableGauge, Float64ObservableCounter, Float64ObservableUpDownCounter, and Float64ObservableGauge) are used to measure metrics outside of the execution of source code. They are said to make "observations" via a callback function called once every measurement collection cycle.

Each instrument is also grouped by the value type it measures. Either int64 or float64. The value being measured will dictate which instrument in these categories to use.

Outside of these two broad categories, instruments are described by the function they are designed to serve. All Counters (Int64Counter, Float64Counter, Int64ObservableCounter, and Float64ObservableCounter) are designed to measure values that never decrease in value, but instead only incrementally increase in value. UpDownCounters (Int64UpDownCounter, Float64UpDownCounter, Int64ObservableUpDownCounter, and Float64ObservableUpDownCounter) on the other hand, are designed to measure values that can increase and decrease. When more information needs to be conveyed about all the synchronous measurements made during a collection cycle, a Histogram (Int64Histogram and Float64Histogram) should be used. Finally, when just the most recent measurement needs to be conveyed about an asynchronous measurement, a Gauge (Int64ObservableGauge and Float64ObservableGauge) should be used.

See the OpenTelemetry documentation for more information about instruments and their intended use.

Measurements

Measurements are made by recording values and information about the values with an instrument. How these measurements are recorded depends on the instrument.

Measurements for synchronous instruments (Int64Counter, Int64UpDownCounter, Int64Histogram, Float64Counter, Float64UpDownCounter, and Float64Histogram) are recorded using the instrument methods directly. All counter instruments have an Add method that is used to measure an increment value, and all histogram instruments have a Record method to measure a data point.

Asynchronous instruments (Int64ObservableCounter, Int64ObservableUpDownCounter, Int64ObservableGauge, Float64ObservableCounter, Float64ObservableUpDownCounter, and Float64ObservableGauge) record measurements within a callback function. The callback is registered with the Meter which ensures the callback is called once per collection cycle. A callback can be registered two ways: during the instrument's creation using an option, or later using the RegisterCallback method of the Meter that created the instrument.

If the following criteria are met, an option (WithInt64Callback or WithFloat64Callback) can be used during the asynchronous instrument's creation to register a callback (Int64Callback or Float64Callback, respectively):

  • The measurement process is known when the instrument is created
  • Only that instrument will make a measurement within the callback
  • The callback never needs to be unregistered

If the criteria are not met, use the RegisterCallback method of the Meter that created the instrument to register a Callback.

API Implementations

This package does not conform to the standard Go versioning policy, all of its interfaces may have methods added to them without a package major version bump. This non-standard API evolution could surprise an uninformed implementation author. They could unknowingly build their implementation in a way that would result in a runtime panic for their users that update to the new API.

The API is designed to help inform an instrumentation author about this non-standard API evolution. It requires them to choose a default behavior for unimplemented interface methods. There are three behavior choices they can make:

  • Compilation failure
  • Panic
  • Default to another implementation

All interfaces in this API embed a corresponding interface from go.opentelemetry.io/otel/metric/embedded. If an author wants the default behavior of their implementations to be a compilation failure, signaling to their users they need to update to the latest version of that implementation, they need to embed the corresponding interface from go.opentelemetry.io/otel/metric/embedded in their implementation. For example,

import "go.opentelemetry.io/otel/metric/embedded"

type MeterProvider struct {
	embedded.MeterProvider
	// ...
}

If an author wants the default behavior of their implementations to a panic, they need to embed the API interface directly.

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

type MeterProvider struct {
	metric.MeterProvider
	// ...
}

This is not a recommended behavior as it could lead to publishing packages that contain runtime panics when users update other package that use newer versions of go.opentelemetry.io/otel/metric.

Finally, an author can embed another implementation in theirs. The embedded implementation will be used for methods not defined by the author. For example, an author who wants to default to silently dropping the call can use go.opentelemetry.io/otel/metric/noop:

import "go.opentelemetry.io/otel/metric/noop"

type MeterProvider struct {
	noop.MeterProvider
	// ...
}

It is strongly recommended that authors only embed go.opentelemetry.io/otel/metric/noop if they choose this default behavior. That implementation is the only one OpenTelemetry authors can guarantee will fully implement all the API interfaces when a user updates their API.

Index ▾

type AddConfig
    func NewAddConfig(opts []AddOption) AddConfig
    func (c AddConfig) Attributes() attribute.Set
type AddOption
type Callback
type Float64Callback
type Float64Counter
type Float64CounterConfig
    func NewFloat64CounterConfig(opts ...Float64CounterOption) Float64CounterConfig
    func (c Float64CounterConfig) Description() string
    func (c Float64CounterConfig) Unit() string
type Float64CounterOption
type Float64Histogram
type Float64HistogramConfig
    func NewFloat64HistogramConfig(opts ...Float64HistogramOption) Float64HistogramConfig
    func (c Float64HistogramConfig) Description() string
    func (c Float64HistogramConfig) ExplicitBucketBoundaries() []float64
    func (c Float64HistogramConfig) Unit() string
type Float64HistogramOption
type Float64Observable
type Float64ObservableCounter
type Float64ObservableCounterConfig
    func NewFloat64ObservableCounterConfig(opts ...Float64ObservableCounterOption) Float64ObservableCounterConfig
    func (c Float64ObservableCounterConfig) Callbacks() []Float64Callback
    func (c Float64ObservableCounterConfig) Description() string
    func (c Float64ObservableCounterConfig) Unit() string
type Float64ObservableCounterOption
type Float64ObservableGauge
type Float64ObservableGaugeConfig
    func NewFloat64ObservableGaugeConfig(opts ...Float64ObservableGaugeOption) Float64ObservableGaugeConfig
    func (c Float64ObservableGaugeConfig) Callbacks() []Float64Callback
    func (c Float64ObservableGaugeConfig) Description() string
    func (c Float64ObservableGaugeConfig) Unit() string
type Float64ObservableGaugeOption
type Float64ObservableOption
    func WithFloat64Callback(callback Float64Callback) Float64ObservableOption
type Float64ObservableUpDownCounter
type Float64ObservableUpDownCounterConfig
    func NewFloat64ObservableUpDownCounterConfig(opts ...Float64ObservableUpDownCounterOption) Float64ObservableUpDownCounterConfig
    func (c Float64ObservableUpDownCounterConfig) Callbacks() []Float64Callback
    func (c Float64ObservableUpDownCounterConfig) Description() string
    func (c Float64ObservableUpDownCounterConfig) Unit() string
type Float64ObservableUpDownCounterOption
type Float64Observer
type Float64UpDownCounter
type Float64UpDownCounterConfig
    func NewFloat64UpDownCounterConfig(opts ...Float64UpDownCounterOption) Float64UpDownCounterConfig
    func (c Float64UpDownCounterConfig) Description() string
    func (c Float64UpDownCounterConfig) Unit() string
type Float64UpDownCounterOption
type HistogramOption
    func WithExplicitBucketBoundaries(bounds ...float64) HistogramOption
type InstrumentOption
    func WithDescription(desc string) InstrumentOption
    func WithUnit(u string) InstrumentOption
type Int64Callback
type Int64Counter
type Int64CounterConfig
    func NewInt64CounterConfig(opts ...Int64CounterOption) Int64CounterConfig
    func (c Int64CounterConfig) Description() string
    func (c Int64CounterConfig) Unit() string
type Int64CounterOption
type Int64Histogram
type Int64HistogramConfig
    func NewInt64HistogramConfig(opts ...Int64HistogramOption) Int64HistogramConfig
    func (c Int64HistogramConfig) Description() string
    func (c Int64HistogramConfig) ExplicitBucketBoundaries() []float64
    func (c Int64HistogramConfig) Unit() string
type Int64HistogramOption
type Int64Observable
type Int64ObservableCounter
type Int64ObservableCounterConfig
    func NewInt64ObservableCounterConfig(opts ...Int64ObservableCounterOption) Int64ObservableCounterConfig
    func (c Int64ObservableCounterConfig) Callbacks() []Int64Callback
    func (c Int64ObservableCounterConfig) Description() string
    func (c Int64ObservableCounterConfig) Unit() string
type Int64ObservableCounterOption
type Int64ObservableGauge
type Int64ObservableGaugeConfig
    func NewInt64ObservableGaugeConfig(opts ...Int64ObservableGaugeOption) Int64ObservableGaugeConfig
    func (c Int64ObservableGaugeConfig) Callbacks() []Int64Callback
    func (c Int64ObservableGaugeConfig) Description() string
    func (c Int64ObservableGaugeConfig) Unit() string
type Int64ObservableGaugeOption
type Int64ObservableOption
    func WithInt64Callback(callback Int64Callback) Int64ObservableOption
type Int64ObservableUpDownCounter
type Int64ObservableUpDownCounterConfig
    func NewInt64ObservableUpDownCounterConfig(opts ...Int64ObservableUpDownCounterOption) Int64ObservableUpDownCounterConfig
    func (c Int64ObservableUpDownCounterConfig) Callbacks() []Int64Callback
    func (c Int64ObservableUpDownCounterConfig) Description() string
    func (c Int64ObservableUpDownCounterConfig) Unit() string
type Int64ObservableUpDownCounterOption
type Int64Observer
type Int64UpDownCounter
type Int64UpDownCounterConfig
    func NewInt64UpDownCounterConfig(opts ...Int64UpDownCounterOption) Int64UpDownCounterConfig
    func (c Int64UpDownCounterConfig) Description() string
    func (c Int64UpDownCounterConfig) Unit() string
type Int64UpDownCounterOption
type MeasurementOption
    func WithAttributeSet(attributes attribute.Set) MeasurementOption
    func WithAttributes(attributes ...attribute.KeyValue) MeasurementOption
type Meter
type MeterConfig
    func NewMeterConfig(opts ...MeterOption) MeterConfig
    func (cfg MeterConfig) InstrumentationAttributes() attribute.Set
    func (cfg MeterConfig) InstrumentationVersion() string
    func (cfg MeterConfig) SchemaURL() string
type MeterOption
    func WithInstrumentationAttributes(attr ...attribute.KeyValue) MeterOption
    func WithInstrumentationVersion(version string) MeterOption
    func WithSchemaURL(schemaURL string) MeterOption
type MeterProvider
type Observable
type ObserveConfig
    func NewObserveConfig(opts []ObserveOption) ObserveConfig
    func (c ObserveConfig) Attributes() attribute.Set
type ObserveOption
type Observer
type RecordConfig
    func NewRecordConfig(opts []RecordOption) RecordConfig
    func (c RecordConfig) Attributes() attribute.Set
type RecordOption
type Registration

Package files

asyncfloat64.go asyncint64.go config.go doc.go instrument.go meter.go syncfloat64.go syncint64.go

type AddConfig

AddConfig contains options for an addition measurement.

type AddConfig struct {
    // contains filtered or unexported fields
}

func NewAddConfig

func NewAddConfig(opts []AddOption) AddConfig

NewAddConfig returns a new AddConfig with all opts applied.

func (AddConfig) Attributes

func (c AddConfig) Attributes() attribute.Set

Attributes returns the configured attribute set.

type AddOption

AddOption applies options to an addition measurement. See MeasurementOption for other options that can be used as an AddOption.

type AddOption interface {
    // contains filtered or unexported methods
}

type Callback

Callback is a function registered with a Meter that makes observations for the set of instruments it is registered with. The Observer parameter is used to record measurement observations for these instruments.

The function needs to complete in a finite amount of time and the deadline of the passed context is expected to be honored.

The function needs to make unique observations across all registered Callbacks. Meaning, it should not report measurements for an instrument with the same attributes as another Callback will report.

The function needs to be concurrent safe.

type Callback func(context.Context, Observer) error

type Float64Callback

Float64Callback is a function registered with a Meter that makes observations for a Float64Observerable instrument it is registered with. Calls to the Float64Observer record measurement values for the Float64Observable.

The function needs to complete in a finite amount of time and the deadline of the passed context is expected to be honored.

The function needs to make unique observations across all registered Float64Callbacks. Meaning, it should not report measurements with the same attributes as another Float64Callbacks also registered for the same instrument.

The function needs to be concurrent safe.

type Float64Callback func(context.Context, Float64Observer) error

type Float64Counter

Float64Counter is an instrument that records increasing float64 values.

Warning: Methods may be added to this interface in minor releases. See package documentation on API implementation for information on how to set default behavior for unimplemented methods.

type Float64Counter interface {
    // Users of the interface can ignore this. This embedded type is only used
    // by implementations of this interface. See the "API Implementations"
    // section of the package documentation for more information.
    embedded.Float64Counter

    // Add records a change to the counter.
    //
    // Use the WithAttributeSet (or, if performance is not a concern,
    // the WithAttributes) option to include measurement attributes.
    Add(ctx context.Context, incr float64, options ...AddOption)
}

type Float64CounterConfig

Float64CounterConfig contains options for synchronous counter instruments that record int64 values.

type Float64CounterConfig struct {
    // contains filtered or unexported fields
}

func NewFloat64CounterConfig

func NewFloat64CounterConfig(opts ...Float64CounterOption) Float64CounterConfig

NewFloat64CounterConfig returns a new Float64CounterConfig with all opts applied.

func (Float64CounterConfig) Description

func (c Float64CounterConfig) Description() string

Description returns the configured description.

func (Float64CounterConfig) Unit

func (c Float64CounterConfig) Unit() string

Unit returns the configured unit.

type Float64CounterOption

Float64CounterOption applies options to a Float64CounterConfig. See InstrumentOption for other options that can be used as a Float64CounterOption.

type Float64CounterOption interface {
    // contains filtered or unexported methods
}

type Float64Histogram

Float64Histogram is an instrument that records a distribution of float64 values.

Warning: Methods may be added to this interface in minor releases. See package documentation on API implementation for information on how to set default behavior for unimplemented methods.

type Float64Histogram interface {
    // Users of the interface can ignore this. This embedded type is only used
    // by implementations of this interface. See the "API Implementations"
    // section of the package documentation for more information.
    embedded.Float64Histogram

    // Record adds an additional value to the distribution.
    //
    // Use the WithAttributeSet (or, if performance is not a concern,
    // the WithAttributes) option to include measurement attributes.
    Record(ctx context.Context, incr float64, options ...RecordOption)
}

type Float64HistogramConfig

Float64HistogramConfig contains options for synchronous counter instruments that record int64 values.

type Float64HistogramConfig struct {
    // contains filtered or unexported fields
}

func NewFloat64HistogramConfig

func NewFloat64HistogramConfig(opts ...Float64HistogramOption) Float64HistogramConfig

NewFloat64HistogramConfig returns a new Float64HistogramConfig with all opts applied.

func (Float64HistogramConfig) Description

func (c Float64HistogramConfig) Description() string

Description returns the configured description.

func (Float64HistogramConfig) ExplicitBucketBoundaries

func (c Float64HistogramConfig) ExplicitBucketBoundaries() []float64

ExplicitBucketBoundaries returns the configured explicit bucket boundaries.

func (Float64HistogramConfig) Unit

func (c Float64HistogramConfig) Unit() string

Unit returns the configured unit.

type Float64HistogramOption

Float64HistogramOption applies options to a Float64HistogramConfig. See InstrumentOption for other options that can be used as a Float64HistogramOption.

type Float64HistogramOption interface {
    // contains filtered or unexported methods
}

type Float64Observable

Float64Observable describes a set of instruments used asynchronously to record float64 measurements once per collection cycle. Observations of these instruments are only made within a callback.

Warning: Methods may be added to this interface in minor releases.

type Float64Observable interface {
    Observable
    // contains filtered or unexported methods
}

type Float64ObservableCounter

Float64ObservableCounter is an instrument used to asynchronously record increasing float64 measurements once per collection cycle. Observations are only made within a callback for this instrument. The value observed is assumed the to be the cumulative sum of the count.

Warning: Methods may be added to this interface in minor releases. See package documentation on API implementation for information on how to set default behavior for unimplemented methods.

type Float64ObservableCounter interface {
    // Users of the interface can ignore this. This embedded type is only used
    // by implementations of this interface. See the "API Implementations"
    // section of the package documentation for more information.
    embedded.Float64ObservableCounter

    Float64Observable
}

type Float64ObservableCounterConfig

Float64ObservableCounterConfig contains options for asynchronous counter instruments that record int64 values.

type Float64ObservableCounterConfig struct {
    // contains filtered or unexported fields
}

func NewFloat64ObservableCounterConfig

func NewFloat64ObservableCounterConfig(opts ...Float64ObservableCounterOption) Float64ObservableCounterConfig

NewFloat64ObservableCounterConfig returns a new Float64ObservableCounterConfig with all opts applied.

func (Float64ObservableCounterConfig) Callbacks

func (c Float64ObservableCounterConfig) Callbacks() []Float64Callback

Callbacks returns the configured callbacks.

func (Float64ObservableCounterConfig) Description

func (c Float64ObservableCounterConfig) Description() string

Description returns the configured description.

func (Float64ObservableCounterConfig) Unit

func (c Float64ObservableCounterConfig) Unit() string

Unit returns the configured unit.

type Float64ObservableCounterOption

Float64ObservableCounterOption applies options to a Float64ObservableCounterConfig. See Float64ObservableOption and InstrumentOption for other options that can be used as a Float64ObservableCounterOption.

type Float64ObservableCounterOption interface {
    // contains filtered or unexported methods
}

type Float64ObservableGauge

Float64ObservableGauge is an instrument used to asynchronously record instantaneous float64 measurements once per collection cycle. Observations are only made within a callback for this instrument.

Warning: Methods may be added to this interface in minor releases. See package documentation on API implementation for information on how to set default behavior for unimplemented methods.

type Float64ObservableGauge interface {
    // Users of the interface can ignore this. This embedded type is only used
    // by implementations of this interface. See the "API Implementations"
    // section of the package documentation for more information.
    embedded.Float64ObservableGauge

    Float64Observable
}

type Float64ObservableGaugeConfig

Float64ObservableGaugeConfig contains options for asynchronous counter instruments that record int64 values.

type Float64ObservableGaugeConfig struct {
    // contains filtered or unexported fields
}

func NewFloat64ObservableGaugeConfig

func NewFloat64ObservableGaugeConfig(opts ...Float64ObservableGaugeOption) Float64ObservableGaugeConfig

NewFloat64ObservableGaugeConfig returns a new Float64ObservableGaugeConfig with all opts applied.

func (Float64ObservableGaugeConfig) Callbacks

func (c Float64ObservableGaugeConfig) Callbacks() []Float64Callback

Callbacks returns the configured callbacks.

func (Float64ObservableGaugeConfig) Description

func (c Float64ObservableGaugeConfig) Description() string

Description returns the configured description.

func (Float64ObservableGaugeConfig) Unit

func (c Float64ObservableGaugeConfig) Unit() string

Unit returns the configured unit.

type Float64ObservableGaugeOption

Float64ObservableGaugeOption applies options to a Float64ObservableGaugeConfig. See Float64ObservableOption and InstrumentOption for other options that can be used as a Float64ObservableGaugeOption.

type Float64ObservableGaugeOption interface {
    // contains filtered or unexported methods
}

type Float64ObservableOption

Float64ObservableOption applies options to float64 Observer instruments.

type Float64ObservableOption interface {
    Float64ObservableCounterOption
    Float64ObservableUpDownCounterOption
    Float64ObservableGaugeOption
}

func WithFloat64Callback

func WithFloat64Callback(callback Float64Callback) Float64ObservableOption

WithFloat64Callback adds callback to be called for an instrument.

type Float64ObservableUpDownCounter

Float64ObservableUpDownCounter is an instrument used to asynchronously record float64 measurements once per collection cycle. Observations are only made within a callback for this instrument. The value observed is assumed the to be the cumulative sum of the count.

Warning: Methods may be added to this interface in minor releases. See package documentation on API implementation for information on how to set default behavior for unimplemented methods.

type Float64ObservableUpDownCounter interface {
    // Users of the interface can ignore this. This embedded type is only used
    // by implementations of this interface. See the "API Implementations"
    // section of the package documentation for more information.
    embedded.Float64ObservableUpDownCounter

    Float64Observable
}

type Float64ObservableUpDownCounterConfig

Float64ObservableUpDownCounterConfig contains options for asynchronous counter instruments that record int64 values.

type Float64ObservableUpDownCounterConfig struct {
    // contains filtered or unexported fields
}

func NewFloat64ObservableUpDownCounterConfig

func NewFloat64ObservableUpDownCounterConfig(opts ...Float64ObservableUpDownCounterOption) Float64ObservableUpDownCounterConfig

NewFloat64ObservableUpDownCounterConfig returns a new Float64ObservableUpDownCounterConfig with all opts applied.

func (Float64ObservableUpDownCounterConfig) Callbacks

func (c Float64ObservableUpDownCounterConfig) Callbacks() []Float64Callback

Callbacks returns the configured callbacks.

func (Float64ObservableUpDownCounterConfig) Description

func (c Float64ObservableUpDownCounterConfig) Description() string

Description returns the configured description.

func (Float64ObservableUpDownCounterConfig) Unit

func (c Float64ObservableUpDownCounterConfig) Unit() string

Unit returns the configured unit.

type Float64ObservableUpDownCounterOption

Float64ObservableUpDownCounterOption applies options to a Float64ObservableUpDownCounterConfig. See Float64ObservableOption and InstrumentOption for other options that can be used as a Float64ObservableUpDownCounterOption.

type Float64ObservableUpDownCounterOption interface {
    // contains filtered or unexported methods
}

type Float64Observer

Float64Observer is a recorder of float64 measurements.

Warning: Methods may be added to this interface in minor releases. See package documentation on API implementation for information on how to set default behavior for unimplemented methods.

type Float64Observer interface {
    // Users of the interface can ignore this. This embedded type is only used
    // by implementations of this interface. See the "API Implementations"
    // section of the package documentation for more information.
    embedded.Float64Observer

    // Observe records the float64 value.
    //
    // Use the WithAttributeSet (or, if performance is not a concern,
    // the WithAttributes) option to include measurement attributes.
    Observe(value float64, options ...ObserveOption)
}

type Float64UpDownCounter

Float64UpDownCounter is an instrument that records increasing or decreasing float64 values.

Warning: Methods may be added to this interface in minor releases. See package documentation on API implementation for information on how to set default behavior for unimplemented methods.

type Float64UpDownCounter interface {
    // Users of the interface can ignore this. This embedded type is only used
    // by implementations of this interface. See the "API Implementations"
    // section of the package documentation for more information.
    embedded.Float64UpDownCounter

    // Add records a change to the counter.
    //
    // Use the WithAttributeSet (or, if performance is not a concern,
    // the WithAttributes) option to include measurement attributes.
    Add(ctx context.Context, incr float64, options ...AddOption)
}

type Float64UpDownCounterConfig

Float64UpDownCounterConfig contains options for synchronous counter instruments that record int64 values.

type Float64UpDownCounterConfig struct {
    // contains filtered or unexported fields
}

func NewFloat64UpDownCounterConfig

func NewFloat64UpDownCounterConfig(opts ...Float64UpDownCounterOption) Float64UpDownCounterConfig

NewFloat64UpDownCounterConfig returns a new Float64UpDownCounterConfig with all opts applied.

func (Float64UpDownCounterConfig) Description

func (c Float64UpDownCounterConfig) Description() string

Description returns the configured description.

func (Float64UpDownCounterConfig) Unit

func (c Float64UpDownCounterConfig) Unit() string

Unit returns the configured unit.

type Float64UpDownCounterOption

Float64UpDownCounterOption applies options to a Float64UpDownCounterConfig. See InstrumentOption for other options that can be used as a Float64UpDownCounterOption.

type Float64UpDownCounterOption interface {
    // contains filtered or unexported methods
}

type HistogramOption

HistogramOption applies options to histogram instruments.

type HistogramOption interface {
    Int64HistogramOption
    Float64HistogramOption
}

func WithExplicitBucketBoundaries

func WithExplicitBucketBoundaries(bounds ...float64) HistogramOption

WithExplicitBucketBoundaries sets the instrument explicit bucket boundaries.

This option is considered "advisory", and may be ignored by API implementations.

type InstrumentOption

InstrumentOption applies options to all instruments.

type InstrumentOption interface {
    Int64CounterOption
    Int64UpDownCounterOption
    Int64HistogramOption
    Int64ObservableCounterOption
    Int64ObservableUpDownCounterOption
    Int64ObservableGaugeOption

    Float64CounterOption
    Float64UpDownCounterOption
    Float64HistogramOption
    Float64ObservableCounterOption
    Float64ObservableUpDownCounterOption
    Float64ObservableGaugeOption
}

func WithDescription

func WithDescription(desc string) InstrumentOption

WithDescription sets the instrument description.

func WithUnit

func WithUnit(u string) InstrumentOption

WithUnit sets the instrument unit.

The unit u should be defined using the appropriate [UCUM](https://ucum.org) case-sensitive code.

type Int64Callback

Int64Callback is a function registered with a Meter that makes observations for an Int64Observerable instrument it is registered with. Calls to the Int64Observer record measurement values for the Int64Observable.

The function needs to complete in a finite amount of time and the deadline of the passed context is expected to be honored.

The function needs to make unique observations across all registered Int64Callbacks. Meaning, it should not report measurements with the same attributes as another Int64Callbacks also registered for the same instrument.

The function needs to be concurrent safe.

type Int64Callback func(context.Context, Int64Observer) error

type Int64Counter

Int64Counter is an instrument that records increasing int64 values.

Warning: Methods may be added to this interface in minor releases. See package documentation on API implementation for information on how to set default behavior for unimplemented methods.

type Int64Counter interface {
    // Users of the interface can ignore this. This embedded type is only used
    // by implementations of this interface. See the "API Implementations"
    // section of the package documentation for more information.
    embedded.Int64Counter

    // Add records a change to the counter.
    //
    // Use the WithAttributeSet (or, if performance is not a concern,
    // the WithAttributes) option to include measurement attributes.
    Add(ctx context.Context, incr int64, options ...AddOption)
}

type Int64CounterConfig

Int64CounterConfig contains options for synchronous counter instruments that record int64 values.

type Int64CounterConfig struct {
    // contains filtered or unexported fields
}

func NewInt64CounterConfig

func NewInt64CounterConfig(opts ...Int64CounterOption) Int64CounterConfig

NewInt64CounterConfig returns a new Int64CounterConfig with all opts applied.

func (Int64CounterConfig) Description

func (c Int64CounterConfig) Description() string

Description returns the configured description.

func (Int64CounterConfig) Unit

func (c Int64CounterConfig) Unit() string

Unit returns the configured unit.

type Int64CounterOption

Int64CounterOption applies options to a Int64CounterConfig. See InstrumentOption for other options that can be used as an Int64CounterOption.

type Int64CounterOption interface {
    // contains filtered or unexported methods
}

type Int64Histogram

Int64Histogram is an instrument that records a distribution of int64 values.

Warning: Methods may be added to this interface in minor releases. See package documentation on API implementation for information on how to set default behavior for unimplemented methods.

type Int64Histogram interface {
    // Users of the interface can ignore this. This embedded type is only used
    // by implementations of this interface. See the "API Implementations"
    // section of the package documentation for more information.
    embedded.Int64Histogram

    // Record adds an additional value to the distribution.
    //
    // Use the WithAttributeSet (or, if performance is not a concern,
    // the WithAttributes) option to include measurement attributes.
    Record(ctx context.Context, incr int64, options ...RecordOption)
}

type Int64HistogramConfig

Int64HistogramConfig contains options for synchronous counter instruments that record int64 values.

type Int64HistogramConfig struct {
    // contains filtered or unexported fields
}

func NewInt64HistogramConfig

func NewInt64HistogramConfig(opts ...Int64HistogramOption) Int64HistogramConfig

NewInt64HistogramConfig returns a new Int64HistogramConfig with all opts applied.

func (Int64HistogramConfig) Description

func (c Int64HistogramConfig) Description() string

Description returns the configured description.

func (Int64HistogramConfig) ExplicitBucketBoundaries

func (c Int64HistogramConfig) ExplicitBucketBoundaries() []float64

ExplicitBucketBoundaries returns the configured explicit bucket boundaries.

func (Int64HistogramConfig) Unit

func (c Int64HistogramConfig) Unit() string

Unit returns the configured unit.

type Int64HistogramOption

Int64HistogramOption applies options to a Int64HistogramConfig. See InstrumentOption for other options that can be used as an Int64HistogramOption.

type Int64HistogramOption interface {
    // contains filtered or unexported methods
}

type Int64Observable

Int64Observable describes a set of instruments used asynchronously to record int64 measurements once per collection cycle. Observations of these instruments are only made within a callback.

Warning: Methods may be added to this interface in minor releases.

type Int64Observable interface {
    Observable
    // contains filtered or unexported methods
}

type Int64ObservableCounter

Int64ObservableCounter is an instrument used to asynchronously record increasing int64 measurements once per collection cycle. Observations are only made within a callback for this instrument. The value observed is assumed the to be the cumulative sum of the count.

Warning: Methods may be added to this interface in minor releases. See package documentation on API implementation for information on how to set default behavior for unimplemented methods.

type Int64ObservableCounter interface {
    // Users of the interface can ignore this. This embedded type is only used
    // by implementations of this interface. See the "API Implementations"
    // section of the package documentation for more information.
    embedded.Int64ObservableCounter

    Int64Observable
}

type Int64ObservableCounterConfig

Int64ObservableCounterConfig contains options for asynchronous counter instruments that record int64 values.

type Int64ObservableCounterConfig struct {
    // contains filtered or unexported fields
}

func NewInt64ObservableCounterConfig

func NewInt64ObservableCounterConfig(opts ...Int64ObservableCounterOption) Int64ObservableCounterConfig

NewInt64ObservableCounterConfig returns a new Int64ObservableCounterConfig with all opts applied.

func (Int64ObservableCounterConfig) Callbacks

func (c Int64ObservableCounterConfig) Callbacks() []Int64Callback

Callbacks returns the configured callbacks.

func (Int64ObservableCounterConfig) Description

func (c Int64ObservableCounterConfig) Description() string

Description returns the configured description.

func (Int64ObservableCounterConfig) Unit

func (c Int64ObservableCounterConfig) Unit() string

Unit returns the configured unit.

type Int64ObservableCounterOption

Int64ObservableCounterOption applies options to a Int64ObservableCounterConfig. See Int64ObservableOption and InstrumentOption for other options that can be used as an Int64ObservableCounterOption.

type Int64ObservableCounterOption interface {
    // contains filtered or unexported methods
}

type Int64ObservableGauge

Int64ObservableGauge is an instrument used to asynchronously record instantaneous int64 measurements once per collection cycle. Observations are only made within a callback for this instrument.

Warning: Methods may be added to this interface in minor releases. See package documentation on API implementation for information on how to set default behavior for unimplemented methods.

type Int64ObservableGauge interface {
    // Users of the interface can ignore this. This embedded type is only used
    // by implementations of this interface. See the "API Implementations"
    // section of the package documentation for more information.
    embedded.Int64ObservableGauge

    Int64Observable
}

type Int64ObservableGaugeConfig

Int64ObservableGaugeConfig contains options for asynchronous counter instruments that record int64 values.

type Int64ObservableGaugeConfig struct {
    // contains filtered or unexported fields
}

func NewInt64ObservableGaugeConfig

func NewInt64ObservableGaugeConfig(opts ...Int64ObservableGaugeOption) Int64ObservableGaugeConfig

NewInt64ObservableGaugeConfig returns a new Int64ObservableGaugeConfig with all opts applied.

func (Int64ObservableGaugeConfig) Callbacks

func (c Int64ObservableGaugeConfig) Callbacks() []Int64Callback

Callbacks returns the configured callbacks.

func (Int64ObservableGaugeConfig) Description

func (c Int64ObservableGaugeConfig) Description() string

Description returns the configured description.

func (Int64ObservableGaugeConfig) Unit

func (c Int64ObservableGaugeConfig) Unit() string

Unit returns the configured unit.

type Int64ObservableGaugeOption

Int64ObservableGaugeOption applies options to a Int64ObservableGaugeConfig. See Int64ObservableOption and InstrumentOption for other options that can be used as an Int64ObservableGaugeOption.

type Int64ObservableGaugeOption interface {
    // contains filtered or unexported methods
}

type Int64ObservableOption

Int64ObservableOption applies options to int64 Observer instruments.

type Int64ObservableOption interface {
    Int64ObservableCounterOption
    Int64ObservableUpDownCounterOption
    Int64ObservableGaugeOption
}

func WithInt64Callback

func WithInt64Callback(callback Int64Callback) Int64ObservableOption

WithInt64Callback adds callback to be called for an instrument.

type Int64ObservableUpDownCounter

Int64ObservableUpDownCounter is an instrument used to asynchronously record int64 measurements once per collection cycle. Observations are only made within a callback for this instrument. The value observed is assumed the to be the cumulative sum of the count.

Warning: Methods may be added to this interface in minor releases. See package documentation on API implementation for information on how to set default behavior for unimplemented methods.

type Int64ObservableUpDownCounter interface {
    // Users of the interface can ignore this. This embedded type is only used
    // by implementations of this interface. See the "API Implementations"
    // section of the package documentation for more information.
    embedded.Int64ObservableUpDownCounter

    Int64Observable
}

type Int64ObservableUpDownCounterConfig

Int64ObservableUpDownCounterConfig contains options for asynchronous counter instruments that record int64 values.

type Int64ObservableUpDownCounterConfig struct {
    // contains filtered or unexported fields
}

func NewInt64ObservableUpDownCounterConfig

func NewInt64ObservableUpDownCounterConfig(opts ...Int64ObservableUpDownCounterOption) Int64ObservableUpDownCounterConfig

NewInt64ObservableUpDownCounterConfig returns a new Int64ObservableUpDownCounterConfig with all opts applied.

func (Int64ObservableUpDownCounterConfig) Callbacks

func (c Int64ObservableUpDownCounterConfig) Callbacks() []Int64Callback

Callbacks returns the configured callbacks.

func (Int64ObservableUpDownCounterConfig) Description

func (c Int64ObservableUpDownCounterConfig) Description() string

Description returns the configured description.

func (Int64ObservableUpDownCounterConfig) Unit

func (c Int64ObservableUpDownCounterConfig) Unit() string

Unit returns the configured unit.

type Int64ObservableUpDownCounterOption

Int64ObservableUpDownCounterOption applies options to a Int64ObservableUpDownCounterConfig. See Int64ObservableOption and InstrumentOption for other options that can be used as an Int64ObservableUpDownCounterOption.

type Int64ObservableUpDownCounterOption interface {
    // contains filtered or unexported methods
}

type Int64Observer

Int64Observer is a recorder of int64 measurements.

Warning: Methods may be added to this interface in minor releases. See package documentation on API implementation for information on how to set default behavior for unimplemented methods.

type Int64Observer interface {
    // Users of the interface can ignore this. This embedded type is only used
    // by implementations of this interface. See the "API Implementations"
    // section of the package documentation for more information.
    embedded.Int64Observer

    // Observe records the int64 value.
    //
    // Use the WithAttributeSet (or, if performance is not a concern,
    // the WithAttributes) option to include measurement attributes.
    Observe(value int64, options ...ObserveOption)
}

type Int64UpDownCounter

Int64UpDownCounter is an instrument that records increasing or decreasing int64 values.

Warning: Methods may be added to this interface in minor releases. See package documentation on API implementation for information on how to set default behavior for unimplemented methods.

type Int64UpDownCounter interface {
    // Users of the interface can ignore this. This embedded type is only used
    // by implementations of this interface. See the "API Implementations"
    // section of the package documentation for more information.
    embedded.Int64UpDownCounter

    // Add records a change to the counter.
    //
    // Use the WithAttributeSet (or, if performance is not a concern,
    // the WithAttributes) option to include measurement attributes.
    Add(ctx context.Context, incr int64, options ...AddOption)
}

type Int64UpDownCounterConfig

Int64UpDownCounterConfig contains options for synchronous counter instruments that record int64 values.

type Int64UpDownCounterConfig struct {
    // contains filtered or unexported fields
}

func NewInt64UpDownCounterConfig

func NewInt64UpDownCounterConfig(opts ...Int64UpDownCounterOption) Int64UpDownCounterConfig

NewInt64UpDownCounterConfig returns a new Int64UpDownCounterConfig with all opts applied.

func (Int64UpDownCounterConfig) Description

func (c Int64UpDownCounterConfig) Description() string

Description returns the configured description.

func (Int64UpDownCounterConfig) Unit

func (c Int64UpDownCounterConfig) Unit() string

Unit returns the configured unit.

type Int64UpDownCounterOption

Int64UpDownCounterOption applies options to a Int64UpDownCounterConfig. See InstrumentOption for other options that can be used as an Int64UpDownCounterOption.

type Int64UpDownCounterOption interface {
    // contains filtered or unexported methods
}

type MeasurementOption

MeasurementOption applies options to all instrument measurement.

type MeasurementOption interface {
    AddOption
    RecordOption
    ObserveOption
}

func WithAttributeSet

func WithAttributeSet(attributes attribute.Set) MeasurementOption

WithAttributeSet sets the attribute Set associated with a measurement is made with.

If multiple WithAttributeSet or WithAttributes options are passed the attributes will be merged together in the order they are passed. Attributes with duplicate keys will use the last value passed.

func WithAttributes

func WithAttributes(attributes ...attribute.KeyValue) MeasurementOption

WithAttributes converts attributes into an attribute Set and sets the Set to be associated with a measurement. This is shorthand for:

cp := make([]attribute.KeyValue, len(attributes))
copy(cp, attributes)
WithAttributes(attribute.NewSet(cp...))

attribute.NewSet may modify the passed attributes so this will make a copy of attributes before creating a set in order to ensure this function is concurrent safe. This makes this option function less optimized in comparison to WithAttributeSet. Therefore, WithAttributeSet should be preferred for performance sensitive code.

See WithAttributeSet for information about how multiple WithAttributes are merged.

type Meter

Meter provides access to instrument instances for recording metrics.

Warning: Methods may be added to this interface in minor releases. See package documentation on API implementation for information on how to set default behavior for unimplemented methods.

type Meter interface {
    // Users of the interface can ignore this. This embedded type is only used
    // by implementations of this interface. See the "API Implementations"
    // section of the package documentation for more information.
    embedded.Meter

    // Int64Counter returns a new Int64Counter instrument identified by name
    // and configured with options. The instrument is used to synchronously
    // record increasing int64 measurements during a computational operation.
    Int64Counter(name string, options ...Int64CounterOption) (Int64Counter, error)
    // Int64UpDownCounter returns a new Int64UpDownCounter instrument
    // identified by name and configured with options. The instrument is used
    // to synchronously record int64 measurements during a computational
    // operation.
    Int64UpDownCounter(name string, options ...Int64UpDownCounterOption) (Int64UpDownCounter, error)
    // Int64Histogram returns a new Int64Histogram instrument identified by
    // name and configured with options. The instrument is used to
    // synchronously record the distribution of int64 measurements during a
    // computational operation.
    Int64Histogram(name string, options ...Int64HistogramOption) (Int64Histogram, error)
    // Int64ObservableCounter returns a new Int64ObservableCounter identified
    // by name and configured with options. The instrument is used to
    // asynchronously record increasing int64 measurements once per a
    // measurement collection cycle.
    //
    // Measurements for the returned instrument are made via a callback. Use
    // the WithInt64Callback option to register the callback here, or use the
    // RegisterCallback method of this Meter to register one later. See the
    // Measurements section of the package documentation for more information.
    Int64ObservableCounter(name string, options ...Int64ObservableCounterOption) (Int64ObservableCounter, error)
    // Int64ObservableUpDownCounter returns a new Int64ObservableUpDownCounter
    // instrument identified by name and configured with options. The
    // instrument is used to asynchronously record int64 measurements once per
    // a measurement collection cycle.
    //
    // Measurements for the returned instrument are made via a callback. Use
    // the WithInt64Callback option to register the callback here, or use the
    // RegisterCallback method of this Meter to register one later. See the
    // Measurements section of the package documentation for more information.
    Int64ObservableUpDownCounter(name string, options ...Int64ObservableUpDownCounterOption) (Int64ObservableUpDownCounter, error)
    // Int64ObservableGauge returns a new Int64ObservableGauge instrument
    // identified by name and configured with options. The instrument is used
    // to asynchronously record instantaneous int64 measurements once per a
    // measurement collection cycle.
    //
    // Measurements for the returned instrument are made via a callback. Use
    // the WithInt64Callback option to register the callback here, or use the
    // RegisterCallback method of this Meter to register one later. See the
    // Measurements section of the package documentation for more information.
    Int64ObservableGauge(name string, options ...Int64ObservableGaugeOption) (Int64ObservableGauge, error)

    // Float64Counter returns a new Float64Counter instrument identified by
    // name and configured with options. The instrument is used to
    // synchronously record increasing float64 measurements during a
    // computational operation.
    Float64Counter(name string, options ...Float64CounterOption) (Float64Counter, error)
    // Float64UpDownCounter returns a new Float64UpDownCounter instrument
    // identified by name and configured with options. The instrument is used
    // to synchronously record float64 measurements during a computational
    // operation.
    Float64UpDownCounter(name string, options ...Float64UpDownCounterOption) (Float64UpDownCounter, error)
    // Float64Histogram returns a new Float64Histogram instrument identified by
    // name and configured with options. The instrument is used to
    // synchronously record the distribution of float64 measurements during a
    // computational operation.
    Float64Histogram(name string, options ...Float64HistogramOption) (Float64Histogram, error)
    // Float64ObservableCounter returns a new Float64ObservableCounter
    // instrument identified by name and configured with options. The
    // instrument is used to asynchronously record increasing float64
    // measurements once per a measurement collection cycle.
    //
    // Measurements for the returned instrument are made via a callback. Use
    // the WithFloat64Callback option to register the callback here, or use the
    // RegisterCallback method of this Meter to register one later. See the
    // Measurements section of the package documentation for more information.
    Float64ObservableCounter(name string, options ...Float64ObservableCounterOption) (Float64ObservableCounter, error)
    // Float64ObservableUpDownCounter returns a new
    // Float64ObservableUpDownCounter instrument identified by name and
    // configured with options. The instrument is used to asynchronously record
    // float64 measurements once per a measurement collection cycle.
    //
    // Measurements for the returned instrument are made via a callback. Use
    // the WithFloat64Callback option to register the callback here, or use the
    // RegisterCallback method of this Meter to register one later. See the
    // Measurements section of the package documentation for more information.
    Float64ObservableUpDownCounter(name string, options ...Float64ObservableUpDownCounterOption) (Float64ObservableUpDownCounter, error)
    // Float64ObservableGauge returns a new Float64ObservableGauge instrument
    // identified by name and configured with options. The instrument is used
    // to asynchronously record instantaneous float64 measurements once per a
    // measurement collection cycle.
    //
    // Measurements for the returned instrument are made via a callback. Use
    // the WithFloat64Callback option to register the callback here, or use the
    // RegisterCallback method of this Meter to register one later. See the
    // Measurements section of the package documentation for more information.
    Float64ObservableGauge(name string, options ...Float64ObservableGaugeOption) (Float64ObservableGauge, error)

    // RegisterCallback registers f to be called during the collection of a
    // measurement cycle.
    //
    // If Unregister of the returned Registration is called, f needs to be
    // unregistered and not called during collection.
    //
    // The instruments f is registered with are the only instruments that f may
    // observe values for.
    //
    // If no instruments are passed, f should not be registered nor called
    // during collection.
    //
    // The function f needs to be concurrent safe.
    RegisterCallback(f Callback, instruments ...Observable) (Registration, error)
}

Example (Attributes)

You can add Attributes by using the [WithAttributeSet] and [WithAttributes] options. Here's how you might add the HTTP status code attribute to your recordings.

Code:

apiCounter, err := meter.Int64UpDownCounter(
    "api.finished.counter",
    metric.WithDescription("Number of finished API calls."),
    metric.WithUnit("{call}"),
)
if err != nil {
    panic(err)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    // do some work in an API call and set the response HTTP status code
    statusCode := http.StatusOK

    apiCounter.Add(r.Context(), 1,
        metric.WithAttributes(semconv.HTTPStatusCode(statusCode)))
})

Example (Counter)

Counters can be used to measure a non-negative, increasing value. Here's how you might report the number of calls for an HTTP handler.

Code:

apiCounter, err := meter.Int64Counter(
    "api.counter",
    metric.WithDescription("Number of API calls."),
    metric.WithUnit("{call}"),
)
if err != nil {
    panic(err)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    apiCounter.Add(r.Context(), 1)

    // do some work in an API call
})

Example (Histogram)

Histograms are used to measure a distribution of values over time. Here's how you might report a distribution of response times for an HTTP handler.

Code:

histogram, err := meter.Float64Histogram(
    "task.duration",
    metric.WithDescription("The duration of task execution."),
    metric.WithUnit("s"),
    metric.WithExplicitBucketBoundaries(.005, .01, .025, .05, .075, .1, .25, .5, .75, 1, 2.5, 5, 7.5, 10),
)
if err != nil {
    panic(err)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    start := time.Now()

    // do some work in an API call

    duration := time.Since(start)
    histogram.Record(r.Context(), duration.Seconds())
})

Example (ObservableCounter)

Observable counters can be used to measure an additive, non-negative, monotonically increasing value. Here's how you might report time since the application started.

Code:

start := time.Now()
if _, err := meter.Float64ObservableCounter(
    "uptime",
    metric.WithDescription("The duration since the application started."),
    metric.WithUnit("s"),
    metric.WithFloat64Callback(func(_ context.Context, o metric.Float64Observer) error {
        o.Observe(float64(time.Since(start).Seconds()))
        return nil
    }),
); err != nil {
    panic(err)
}

Example (ObservableGauge)

Observable Gauges should be used to measure non-additive values. Here's how you might report memory usage of the heap objects used in application.

Code:

if _, err := meter.Int64ObservableGauge(
    "memory.heap",
    metric.WithDescription(
        "Memory usage of the allocated heap objects.",
    ),
    metric.WithUnit("By"),
    metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error {
        var m runtime.MemStats
        runtime.ReadMemStats(&m)
        o.Observe(int64(m.HeapAlloc))
        return nil
    }),
); err != nil {
    panic(err)
}

Example (ObservableUpDownCounter)

Observable UpDown counters can increment and decrement, allowing you to measure an additive, non-negative, non-monotonically increasing cumulative value. Here's how you might report some database metrics.

Code:

// The function registers asynchronous metrics for the provided db.
// Make sure to unregister metric.Registration before closing the provided db.
_ = func(db *sql.DB, meter metric.Meter, poolName string) (metric.Registration, error) {
    max, err := meter.Int64ObservableUpDownCounter(
        "db.client.connections.max",
        metric.WithDescription("The maximum number of open connections allowed."),
        metric.WithUnit("{connection}"),
    )
    if err != nil {
        return nil, err
    }

    waitTime, err := meter.Int64ObservableUpDownCounter(
        "db.client.connections.wait_time",
        metric.WithDescription("The time it took to obtain an open connection from the pool."),
        metric.WithUnit("ms"),
    )
    if err != nil {
        return nil, err
    }

    reg, err := meter.RegisterCallback(
        func(_ context.Context, o metric.Observer) error {
            stats := db.Stats()
            o.ObserveInt64(max, int64(stats.MaxOpenConnections))
            o.ObserveInt64(waitTime, int64(stats.WaitDuration))
            return nil
        },
        max,
        waitTime,
    )
    if err != nil {
        return nil, err
    }
    return reg, nil
}

Example (Synchronous)

Code:

// Create a histogram using the global MeterProvider.
workDuration, err := meter.Int64Histogram(
    "workDuration",
    metric.WithUnit("ms"))
if err != nil {
    fmt.Println("Failed to register instrument")
    panic(err)
}

startTime := time.Now()
ctx := context.Background()
// Do work
// ...
workDuration.Record(ctx, time.Since(startTime).Milliseconds())

Example (UpDownCounter)

UpDown counters can increment and decrement, allowing you to observe a cumulative value that goes up or down. Here's how you might report the number of items of some collection.

Code:

var err error
itemsCounter, err := meter.Int64UpDownCounter(
    "items.counter",
    metric.WithDescription("Number of items."),
    metric.WithUnit("{item}"),
)
if err != nil {
    panic(err)
}

_ = func() {
    // code that adds an item to the collection
    itemsCounter.Add(context.Background(), 1)
}

_ = func() {
    // code that removes an item from the collection
    itemsCounter.Add(context.Background(), -1)
}

type MeterConfig

MeterConfig contains options for Meters.

type MeterConfig struct {
    // contains filtered or unexported fields
}

func NewMeterConfig

func NewMeterConfig(opts ...MeterOption) MeterConfig

NewMeterConfig creates a new MeterConfig and applies all the given options.

func (MeterConfig) InstrumentationAttributes

func (cfg MeterConfig) InstrumentationAttributes() attribute.Set

InstrumentationAttributes returns the attributes associated with the library providing instrumentation.

func (MeterConfig) InstrumentationVersion

func (cfg MeterConfig) InstrumentationVersion() string

InstrumentationVersion returns the version of the library providing instrumentation.

func (MeterConfig) SchemaURL

func (cfg MeterConfig) SchemaURL() string

SchemaURL is the schema_url of the library providing instrumentation.

type MeterOption

MeterOption is an interface for applying Meter options.

type MeterOption interface {
    // contains filtered or unexported methods
}

func WithInstrumentationAttributes

func WithInstrumentationAttributes(attr ...attribute.KeyValue) MeterOption

WithInstrumentationAttributes sets the instrumentation attributes.

The passed attributes will be de-duplicated.

func WithInstrumentationVersion

func WithInstrumentationVersion(version string) MeterOption

WithInstrumentationVersion sets the instrumentation version.

func WithSchemaURL

func WithSchemaURL(schemaURL string) MeterOption

WithSchemaURL sets the schema URL.

type MeterProvider

MeterProvider provides access to named Meter instances, for instrumenting an application or package.

Warning: Methods may be added to this interface in minor releases. See package documentation on API implementation for information on how to set default behavior for unimplemented methods.

type MeterProvider interface {
    // Users of the interface can ignore this. This embedded type is only used
    // by implementations of this interface. See the "API Implementations"
    // section of the package documentation for more information.
    embedded.MeterProvider

    // Meter returns a new Meter with the provided name and configuration.
    //
    // A Meter should be scoped at most to a single package. The name needs to
    // be unique so it does not collide with other names used by
    // an application, nor other applications. To achieve this, the import path
    // of the instrumentation package is recommended to be used as name.
    //
    // If the name is empty, then an implementation defined default name will
    // be used instead.
    Meter(name string, opts ...MeterOption) Meter
}

type Observable

Observable is used as a grouping mechanism for all instruments that are updated within a Callback.

type Observable interface {
    // contains filtered or unexported methods
}

type ObserveConfig

ObserveConfig contains options for an observed measurement.

type ObserveConfig struct {
    // contains filtered or unexported fields
}

func NewObserveConfig

func NewObserveConfig(opts []ObserveOption) ObserveConfig

NewObserveConfig returns a new ObserveConfig with all opts applied.

func (ObserveConfig) Attributes

func (c ObserveConfig) Attributes() attribute.Set

Attributes returns the configured attribute set.

type ObserveOption

ObserveOption applies options to an addition measurement. See MeasurementOption for other options that can be used as a ObserveOption.

type ObserveOption interface {
    // contains filtered or unexported methods
}

type Observer

Observer records measurements for multiple instruments in a Callback.

Warning: Methods may be added to this interface in minor releases. See package documentation on API implementation for information on how to set default behavior for unimplemented methods.

type Observer interface {
    // Users of the interface can ignore this. This embedded type is only used
    // by implementations of this interface. See the "API Implementations"
    // section of the package documentation for more information.
    embedded.Observer

    // ObserveFloat64 records the float64 value for obsrv.
    ObserveFloat64(obsrv Float64Observable, value float64, opts ...ObserveOption)
    // ObserveInt64 records the int64 value for obsrv.
    ObserveInt64(obsrv Int64Observable, value int64, opts ...ObserveOption)
}

type RecordConfig

RecordConfig contains options for a recorded measurement.

type RecordConfig struct {
    // contains filtered or unexported fields
}

func NewRecordConfig

func NewRecordConfig(opts []RecordOption) RecordConfig

NewRecordConfig returns a new RecordConfig with all opts applied.

func (RecordConfig) Attributes

func (c RecordConfig) Attributes() attribute.Set

Attributes returns the configured attribute set.

type RecordOption

RecordOption applies options to an addition measurement. See MeasurementOption for other options that can be used as a RecordOption.

type RecordOption interface {
    // contains filtered or unexported methods
}

type Registration

Registration is an token representing the unique registration of a callback for a set of instruments with a Meter.

Warning: Methods may be added to this interface in minor releases. See package documentation on API implementation for information on how to set default behavior for unimplemented methods.

type Registration interface {
    // Users of the interface can ignore this. This embedded type is only used
    // by implementations of this interface. See the "API Implementations"
    // section of the package documentation for more information.
    embedded.Registration

    // Unregister removes the callback registration from a Meter.
    //
    // This method needs to be idempotent and concurrent safe.
    Unregister() error
}

Subdirectories

Name Synopsis
..
embedded Package embedded provides interfaces embedded within the [OpenTelemetry metric API].
noop Package noop provides an implementation of the OpenTelemetry metric API that produces no telemetry and minimizes used computation resources.