Documentation ¶
Overview ¶
Package metric provides an implementation of the metrics part of the OpenTelemetry API.
This package is currently in a pre-GA phase. Backwards incompatible changes may be introduced in subsequent minor version releases as we work to track the evolving OpenTelemetry specification and user feedback.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Callback ¶ added in v0.35.0
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 measurment 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 Meter ¶
type Meter interface { // Int64Counter returns a new 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 ...instrument.Int64Option) (instrument.Int64Counter, error) // Int64UpDownCounter returns a new 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 ...instrument.Int64Option) (instrument.Int64UpDownCounter, error) // Int64Histogram returns a new 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 ...instrument.Int64Option) (instrument.Int64Histogram, error) // Int64ObservableCounter returns a new instrument identified by name and // configured with options. The instrument is used to asynchronously record // increasing int64 measurements once per a measurement collection cycle. Int64ObservableCounter(name string, options ...instrument.Int64ObserverOption) (instrument.Int64ObservableCounter, error) // Int64ObservableUpDownCounter returns a new instrument identified by name // and configured with options. The instrument is used to asynchronously // record int64 measurements once per a measurement collection cycle. Int64ObservableUpDownCounter(name string, options ...instrument.Int64ObserverOption) (instrument.Int64ObservableUpDownCounter, error) // Int64ObservableGauge returns a new instrument identified by name and // configured with options. The instrument is used to asynchronously record // instantaneous int64 measurements once per a measurement collection // cycle. Int64ObservableGauge(name string, options ...instrument.Int64ObserverOption) (instrument.Int64ObservableGauge, error) // Float64Counter returns a new 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 ...instrument.Float64Option) (instrument.Float64Counter, error) // Float64UpDownCounter returns a new 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 ...instrument.Float64Option) (instrument.Float64UpDownCounter, error) // Float64Histogram returns a new 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 ...instrument.Float64Option) (instrument.Float64Histogram, error) // Float64ObservableCounter returns a new instrument identified by name and // configured with options. The instrument is used to asynchronously record // increasing float64 measurements once per a measurement collection cycle. Float64ObservableCounter(name string, options ...instrument.Float64ObserverOption) (instrument.Float64ObservableCounter, error) // Float64ObservableUpDownCounter returns a new instrument identified by // name and configured with options. The instrument is used to // asynchronously record float64 measurements once per a measurement // collection cycle. Float64ObservableUpDownCounter(name string, options ...instrument.Float64ObserverOption) (instrument.Float64ObservableUpDownCounter, error) // Float64ObservableGauge returns a new instrument identified by name and // configured with options. The instrument is used to asynchronously record // instantaneous float64 measurements once per a measurement collection // cycle. Float64ObservableGauge(name string, options ...instrument.Float64ObserverOption) (instrument.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. RegisterCallback(f Callback, instruments ...instrument.Asynchronous) (Registration, error) }
Meter provides access to instrument instances for recording metrics.
Warning: methods may be added to this interface in minor releases.
Example (Asynchronous_multiple) ¶
package main import ( "context" "fmt" "runtime" "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/metric/instrument" ) func main() { meterProvider := metric.NewNoopMeterProvider() meter := meterProvider.Meter("go.opentelemetry.io/otel/metric#MultiAsyncExample") // This is just a sample of memory stats to record from the Memstats heapAlloc, _ := meter.Int64ObservableUpDownCounter("heapAllocs") gcCount, _ := meter.Int64ObservableCounter("gcCount") gcPause, _ := meter.Float64Histogram("gcPause") _, err := meter.RegisterCallback( func(ctx context.Context, o metric.Observer) error { memStats := &runtime.MemStats{} // This call does work runtime.ReadMemStats(memStats) o.ObserveInt64(heapAlloc, int64(memStats.HeapAlloc)) o.ObserveInt64(gcCount, int64(memStats.NumGC)) // This function synchronously records the pauses computeGCPauses(ctx, gcPause, memStats.PauseNs[:]) return nil }, heapAlloc, gcCount, ) if err != nil { fmt.Println("Failed to register callback") panic(err) } } // This is just an example, see the the contrib runtime instrumentation for real implementation. func computeGCPauses(ctx context.Context, recorder instrument.Float64Histogram, pauseBuff []uint64) {}
Output:
Example (Asynchronous_single) ¶
package main import ( "context" "fmt" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/metric/instrument" "go.opentelemetry.io/otel/metric/unit" ) func main() { // In a library or program this would be provided by otel.GetMeterProvider(). meterProvider := metric.NewNoopMeterProvider() meter := meterProvider.Meter("go.opentelemetry.io/otel/metric#AsyncExample") _, err := meter.Int64ObservableGauge( "DiskUsage", instrument.WithUnit(unit.Bytes), instrument.WithInt64Callback(func(_ context.Context, obsrv instrument.Int64Observer) error { // Do the real work here to get the real disk usage. For example, // // usage, err := GetDiskUsage(diskID) // if err != nil { // if retryable(err) { // // Retry the usage measurement. // } else { // return err // } // } // // For demonstration purpose, a static value is used here. usage := 75000 obsrv.Observe(int64(usage), attribute.Int("disk.id", 3)) return nil }), ) if err != nil { fmt.Println("failed to register instrument") panic(err) } }
Output:
Example (Synchronous) ¶
package main import ( "context" "fmt" "time" "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/metric/instrument" "go.opentelemetry.io/otel/metric/unit" ) func main() { // In a library or program this would be provided by otel.GetMeterProvider(). meterProvider := metric.NewNoopMeterProvider() workDuration, err := meterProvider.Meter("go.opentelemetry.io/otel/metric#SyncExample").Int64Histogram( "workDuration", instrument.WithUnit(unit.Milliseconds)) 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()) }
Output:
func NewNoopMeter ¶ added in v0.31.0
func NewNoopMeter() Meter
NewNoopMeter creates a Meter that does not record any metrics.
type MeterConfig ¶
type MeterConfig struct {
// contains filtered or unexported fields
}
MeterConfig contains options for Meters.
func NewMeterConfig ¶
func NewMeterConfig(opts ...MeterOption) MeterConfig
NewMeterConfig creates a new MeterConfig and applies all the given options.
func (MeterConfig) InstrumentationVersion ¶
func (cfg MeterConfig) InstrumentationVersion() string
InstrumentationVersion is the version of the library providing instrumentation.
func (MeterConfig) SchemaURL ¶ added in v0.24.0
func (cfg MeterConfig) SchemaURL() string
SchemaURL is the schema_url of the library providing instrumentation.
type MeterOption ¶
type MeterOption interface {
// contains filtered or unexported methods
}
MeterOption is an interface for applying Meter options.
func WithInstrumentationVersion ¶
func WithInstrumentationVersion(version string) MeterOption
WithInstrumentationVersion sets the instrumentation version.
func WithSchemaURL ¶ added in v0.24.0
func WithSchemaURL(schemaURL string) MeterOption
WithSchemaURL sets the schema URL.
type MeterProvider ¶
type MeterProvider interface { // Meter creates an instance of a `Meter` interface. The instrumentationName // must be the name of the library providing instrumentation. This name may // be the same as the instrumented code only if that code provides built-in // instrumentation. If the instrumentationName is empty, then a // implementation defined default name will be used instead. Meter(instrumentationName string, opts ...MeterOption) Meter }
MeterProvider provides access to named Meter instances, for instrumenting an application or library.
Warning: methods may be added to this interface in minor releases.
func NewNoopMeterProvider ¶ added in v0.24.0
func NewNoopMeterProvider() MeterProvider
NewNoopMeterProvider creates a MeterProvider that does not record any metrics.
type Observer ¶ added in v0.35.0
type Observer interface { // ObserveFloat64 records the float64 value with attributes for obsrv. ObserveFloat64(obsrv instrument.Float64Observable, value float64, attributes ...attribute.KeyValue) // ObserveInt64 records the int64 value with attributes for obsrv. ObserveInt64(obsrv instrument.Int64Observable, value int64, attributes ...attribute.KeyValue) }
Observer records measurements for multiple instruments in a Callback.
type Registration ¶ added in v0.35.0
type Registration interface { // Unregister removes the callback registration from a Meter. // // This method needs to be idempotent and concurrent safe. Unregister() error }
Registration is an token representing the unique registration of a callback for a set of instruments with a Meter.