2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-06-14 22:09:41 +02:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-06-14 20:37:05 +02:00
|
|
|
package metric
|
|
|
|
|
|
|
|
import (
|
2019-07-12 00:28:38 +02:00
|
|
|
"context"
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
"go.opentelemetry.io/otel/api/kv"
|
2019-06-14 20:37:05 +02:00
|
|
|
)
|
|
|
|
|
2020-05-14 22:10:48 +02:00
|
|
|
// The file is organized as follows:
|
|
|
|
//
|
|
|
|
// - Provider interface
|
|
|
|
// - Meter struct
|
|
|
|
// - RecordBatch
|
|
|
|
// - BatchObserver
|
|
|
|
// - Synchronous instrument constructors (2 x int64,float64)
|
|
|
|
// - Asynchronous instrument constructors (1 x int64,float64)
|
|
|
|
// - Batch asynchronous constructors (1 x int64,float64)
|
|
|
|
// - Internals
|
|
|
|
|
2019-10-31 08:35:02 +02:00
|
|
|
// Provider supports named Meter instances.
|
|
|
|
type Provider interface {
|
2020-06-12 18:11:17 +02:00
|
|
|
// Meter creates an implementation of the 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
|
2019-10-31 08:35:02 +02:00
|
|
|
}
|
|
|
|
|
2020-05-12 05:29:06 +02:00
|
|
|
// Meter is the OpenTelemetry metric API, based on a `MeterImpl`
|
|
|
|
// implementation and the `Meter` library name.
|
|
|
|
//
|
|
|
|
// An uninitialized Meter is a no-op implementation.
|
|
|
|
type Meter struct {
|
2020-06-12 18:11:17 +02:00
|
|
|
impl MeterImpl
|
|
|
|
name, version string
|
2020-05-12 05:29:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RecordBatch atomically records a batch of measurements.
|
2020-05-14 01:06:03 +02:00
|
|
|
func (m Meter) RecordBatch(ctx context.Context, ls []kv.KeyValue, ms ...Measurement) {
|
2020-05-12 05:29:06 +02:00
|
|
|
if m.impl == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.impl.RecordBatch(ctx, ls, ms...)
|
|
|
|
}
|
|
|
|
|
2020-05-14 22:10:48 +02:00
|
|
|
// NewBatchObserver creates a new BatchObserver that supports
|
|
|
|
// making batches of observations for multiple instruments.
|
|
|
|
func (m Meter) NewBatchObserver(callback BatchObserverCallback) BatchObserver {
|
|
|
|
return BatchObserver{
|
|
|
|
meter: m,
|
|
|
|
runner: newBatchAsyncRunner(callback),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 05:29:06 +02:00
|
|
|
// NewInt64Counter creates a new integer Counter instrument with the
|
|
|
|
// given name, customized with options. May return an error if the
|
|
|
|
// name is invalid (e.g., empty) or improperly registered (e.g.,
|
|
|
|
// duplicate registration).
|
2020-06-12 18:11:17 +02:00
|
|
|
func (m Meter) NewInt64Counter(name string, options ...InstrumentOption) (Int64Counter, error) {
|
2020-05-12 05:29:06 +02:00
|
|
|
return wrapInt64CounterInstrument(
|
|
|
|
m.newSync(name, CounterKind, Int64NumberKind, options))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFloat64Counter creates a new floating point Counter with the
|
|
|
|
// given name, customized with options. May return an error if the
|
|
|
|
// name is invalid (e.g., empty) or improperly registered (e.g.,
|
|
|
|
// duplicate registration).
|
2020-06-12 18:11:17 +02:00
|
|
|
func (m Meter) NewFloat64Counter(name string, options ...InstrumentOption) (Float64Counter, error) {
|
2020-05-12 05:29:06 +02:00
|
|
|
return wrapFloat64CounterInstrument(
|
|
|
|
m.newSync(name, CounterKind, Float64NumberKind, options))
|
|
|
|
}
|
|
|
|
|
2020-05-19 19:00:22 +02:00
|
|
|
// NewInt64UpDownCounter creates a new integer UpDownCounter instrument with the
|
|
|
|
// given name, customized with options. May return an error if the
|
|
|
|
// name is invalid (e.g., empty) or improperly registered (e.g.,
|
|
|
|
// duplicate registration).
|
2020-06-12 18:11:17 +02:00
|
|
|
func (m Meter) NewInt64UpDownCounter(name string, options ...InstrumentOption) (Int64UpDownCounter, error) {
|
2020-05-19 19:00:22 +02:00
|
|
|
return wrapInt64UpDownCounterInstrument(
|
|
|
|
m.newSync(name, UpDownCounterKind, Int64NumberKind, options))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFloat64UpDownCounter creates a new floating point UpDownCounter with the
|
|
|
|
// given name, customized with options. May return an error if the
|
|
|
|
// name is invalid (e.g., empty) or improperly registered (e.g.,
|
|
|
|
// duplicate registration).
|
2020-06-12 18:11:17 +02:00
|
|
|
func (m Meter) NewFloat64UpDownCounter(name string, options ...InstrumentOption) (Float64UpDownCounter, error) {
|
2020-05-19 19:00:22 +02:00
|
|
|
return wrapFloat64UpDownCounterInstrument(
|
|
|
|
m.newSync(name, UpDownCounterKind, Float64NumberKind, options))
|
|
|
|
}
|
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
// NewInt64ValueRecorder creates a new integer ValueRecorder instrument with the
|
2020-05-12 05:29:06 +02:00
|
|
|
// given name, customized with options. May return an error if the
|
|
|
|
// name is invalid (e.g., empty) or improperly registered (e.g.,
|
|
|
|
// duplicate registration).
|
2020-06-12 18:11:17 +02:00
|
|
|
func (m Meter) NewInt64ValueRecorder(name string, opts ...InstrumentOption) (Int64ValueRecorder, error) {
|
2020-05-16 07:11:12 +02:00
|
|
|
return wrapInt64ValueRecorderInstrument(
|
|
|
|
m.newSync(name, ValueRecorderKind, Int64NumberKind, opts))
|
2020-05-12 05:29:06 +02:00
|
|
|
}
|
|
|
|
|
2020-05-16 07:11:12 +02:00
|
|
|
// NewFloat64ValueRecorder creates a new floating point ValueRecorder with the
|
2020-05-12 05:29:06 +02:00
|
|
|
// given name, customized with options. May return an error if the
|
|
|
|
// name is invalid (e.g., empty) or improperly registered (e.g.,
|
|
|
|
// duplicate registration).
|
2020-06-12 18:11:17 +02:00
|
|
|
func (m Meter) NewFloat64ValueRecorder(name string, opts ...InstrumentOption) (Float64ValueRecorder, error) {
|
2020-05-16 07:11:12 +02:00
|
|
|
return wrapFloat64ValueRecorderInstrument(
|
|
|
|
m.newSync(name, ValueRecorderKind, Float64NumberKind, opts))
|
2020-05-12 05:29:06 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 00:42:14 +02:00
|
|
|
// NewInt64ValueObserver creates a new integer ValueObserver instrument
|
2020-05-12 05:29:06 +02:00
|
|
|
// with the given name, running a given callback, and customized with
|
|
|
|
// options. May return an error if the name is invalid (e.g., empty)
|
|
|
|
// or improperly registered (e.g., duplicate registration).
|
2020-06-12 18:11:17 +02:00
|
|
|
func (m Meter) NewInt64ValueObserver(name string, callback Int64ObserverCallback, opts ...InstrumentOption) (Int64ValueObserver, error) {
|
2020-05-12 05:29:06 +02:00
|
|
|
if callback == nil {
|
2020-05-18 20:03:43 +02:00
|
|
|
return wrapInt64ValueObserverInstrument(NoopAsync{}, nil)
|
2020-05-12 05:29:06 +02:00
|
|
|
}
|
2020-05-18 20:03:43 +02:00
|
|
|
return wrapInt64ValueObserverInstrument(
|
|
|
|
m.newAsync(name, ValueObserverKind, Int64NumberKind, opts,
|
2020-05-14 01:27:52 +02:00
|
|
|
newInt64AsyncRunner(callback)))
|
2020-05-12 05:29:06 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 00:42:14 +02:00
|
|
|
// NewFloat64ValueObserver creates a new floating point ValueObserver with
|
2020-05-12 05:29:06 +02:00
|
|
|
// the given name, running a given callback, and customized with
|
|
|
|
// options. May return an error if the name is invalid (e.g., empty)
|
|
|
|
// or improperly registered (e.g., duplicate registration).
|
2020-06-12 18:11:17 +02:00
|
|
|
func (m Meter) NewFloat64ValueObserver(name string, callback Float64ObserverCallback, opts ...InstrumentOption) (Float64ValueObserver, error) {
|
2020-05-12 05:29:06 +02:00
|
|
|
if callback == nil {
|
2020-05-18 20:03:43 +02:00
|
|
|
return wrapFloat64ValueObserverInstrument(NoopAsync{}, nil)
|
2020-05-12 05:29:06 +02:00
|
|
|
}
|
2020-05-18 20:03:43 +02:00
|
|
|
return wrapFloat64ValueObserverInstrument(
|
|
|
|
m.newAsync(name, ValueObserverKind, Float64NumberKind, opts,
|
2020-05-14 01:27:52 +02:00
|
|
|
newFloat64AsyncRunner(callback)))
|
2020-05-12 05:29:06 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 00:42:14 +02:00
|
|
|
// NewInt64SumObserver creates a new integer SumObserver instrument
|
2020-05-19 20:49:24 +02:00
|
|
|
// with the given name, running a given callback, and customized with
|
|
|
|
// options. May return an error if the name is invalid (e.g., empty)
|
|
|
|
// or improperly registered (e.g., duplicate registration).
|
2020-06-12 18:11:17 +02:00
|
|
|
func (m Meter) NewInt64SumObserver(name string, callback Int64ObserverCallback, opts ...InstrumentOption) (Int64SumObserver, error) {
|
2020-05-19 20:49:24 +02:00
|
|
|
if callback == nil {
|
|
|
|
return wrapInt64SumObserverInstrument(NoopAsync{}, nil)
|
|
|
|
}
|
|
|
|
return wrapInt64SumObserverInstrument(
|
|
|
|
m.newAsync(name, SumObserverKind, Int64NumberKind, opts,
|
|
|
|
newInt64AsyncRunner(callback)))
|
|
|
|
}
|
|
|
|
|
2020-05-22 00:42:14 +02:00
|
|
|
// NewFloat64SumObserver creates a new floating point SumObserver with
|
2020-05-19 20:49:24 +02:00
|
|
|
// the given name, running a given callback, and customized with
|
|
|
|
// options. May return an error if the name is invalid (e.g., empty)
|
|
|
|
// or improperly registered (e.g., duplicate registration).
|
2020-06-12 18:11:17 +02:00
|
|
|
func (m Meter) NewFloat64SumObserver(name string, callback Float64ObserverCallback, opts ...InstrumentOption) (Float64SumObserver, error) {
|
2020-05-19 20:49:24 +02:00
|
|
|
if callback == nil {
|
|
|
|
return wrapFloat64SumObserverInstrument(NoopAsync{}, nil)
|
|
|
|
}
|
|
|
|
return wrapFloat64SumObserverInstrument(
|
|
|
|
m.newAsync(name, SumObserverKind, Float64NumberKind, opts,
|
|
|
|
newFloat64AsyncRunner(callback)))
|
|
|
|
}
|
|
|
|
|
2020-05-22 00:42:14 +02:00
|
|
|
// NewInt64UpDownSumObserver creates a new integer UpDownSumObserver instrument
|
2020-05-20 19:19:51 +02:00
|
|
|
// with the given name, running a given callback, and customized with
|
|
|
|
// options. May return an error if the name is invalid (e.g., empty)
|
|
|
|
// or improperly registered (e.g., duplicate registration).
|
2020-06-12 18:11:17 +02:00
|
|
|
func (m Meter) NewInt64UpDownSumObserver(name string, callback Int64ObserverCallback, opts ...InstrumentOption) (Int64UpDownSumObserver, error) {
|
2020-05-20 19:19:51 +02:00
|
|
|
if callback == nil {
|
|
|
|
return wrapInt64UpDownSumObserverInstrument(NoopAsync{}, nil)
|
|
|
|
}
|
|
|
|
return wrapInt64UpDownSumObserverInstrument(
|
|
|
|
m.newAsync(name, UpDownSumObserverKind, Int64NumberKind, opts,
|
|
|
|
newInt64AsyncRunner(callback)))
|
|
|
|
}
|
|
|
|
|
2020-05-22 00:42:14 +02:00
|
|
|
// NewFloat64UpDownSumObserver creates a new floating point UpDownSumObserver with
|
2020-05-20 19:19:51 +02:00
|
|
|
// the given name, running a given callback, and customized with
|
|
|
|
// options. May return an error if the name is invalid (e.g., empty)
|
|
|
|
// or improperly registered (e.g., duplicate registration).
|
2020-06-12 18:11:17 +02:00
|
|
|
func (m Meter) NewFloat64UpDownSumObserver(name string, callback Float64ObserverCallback, opts ...InstrumentOption) (Float64UpDownSumObserver, error) {
|
2020-05-20 19:19:51 +02:00
|
|
|
if callback == nil {
|
|
|
|
return wrapFloat64UpDownSumObserverInstrument(NoopAsync{}, nil)
|
|
|
|
}
|
|
|
|
return wrapFloat64UpDownSumObserverInstrument(
|
|
|
|
m.newAsync(name, UpDownSumObserverKind, Float64NumberKind, opts,
|
|
|
|
newFloat64AsyncRunner(callback)))
|
|
|
|
}
|
|
|
|
|
2020-05-22 00:42:14 +02:00
|
|
|
// NewInt64ValueObserver creates a new integer ValueObserver instrument
|
2020-05-14 22:10:48 +02:00
|
|
|
// with the given name, running in a batch callback, and customized with
|
|
|
|
// options. May return an error if the name is invalid (e.g., empty)
|
|
|
|
// or improperly registered (e.g., duplicate registration).
|
2020-06-12 18:11:17 +02:00
|
|
|
func (b BatchObserver) NewInt64ValueObserver(name string, opts ...InstrumentOption) (Int64ValueObserver, error) {
|
2020-05-14 22:10:48 +02:00
|
|
|
if b.runner == nil {
|
2020-05-18 20:03:43 +02:00
|
|
|
return wrapInt64ValueObserverInstrument(NoopAsync{}, nil)
|
2020-05-14 01:27:52 +02:00
|
|
|
}
|
2020-05-18 20:03:43 +02:00
|
|
|
return wrapInt64ValueObserverInstrument(
|
|
|
|
b.meter.newAsync(name, ValueObserverKind, Int64NumberKind, opts, b.runner))
|
2019-10-23 08:29:24 +02:00
|
|
|
}
|
2019-10-09 00:45:49 +02:00
|
|
|
|
2020-05-22 00:42:14 +02:00
|
|
|
// NewFloat64ValueObserver creates a new floating point ValueObserver with
|
2020-05-14 22:10:48 +02:00
|
|
|
// the given name, running in a batch callback, and customized with
|
|
|
|
// options. May return an error if the name is invalid (e.g., empty)
|
|
|
|
// or improperly registered (e.g., duplicate registration).
|
2020-06-12 18:11:17 +02:00
|
|
|
func (b BatchObserver) NewFloat64ValueObserver(name string, opts ...InstrumentOption) (Float64ValueObserver, error) {
|
2020-05-14 22:10:48 +02:00
|
|
|
if b.runner == nil {
|
2020-05-18 20:03:43 +02:00
|
|
|
return wrapFloat64ValueObserverInstrument(NoopAsync{}, nil)
|
2020-05-14 22:10:48 +02:00
|
|
|
}
|
2020-05-18 20:03:43 +02:00
|
|
|
return wrapFloat64ValueObserverInstrument(
|
|
|
|
b.meter.newAsync(name, ValueObserverKind, Float64NumberKind, opts,
|
2020-05-14 22:10:48 +02:00
|
|
|
b.runner))
|
|
|
|
}
|
|
|
|
|
2020-05-22 00:42:14 +02:00
|
|
|
// NewInt64SumObserver creates a new integer SumObserver instrument
|
2020-05-19 20:49:24 +02:00
|
|
|
// with the given name, running in a batch callback, and customized with
|
|
|
|
// options. May return an error if the name is invalid (e.g., empty)
|
|
|
|
// or improperly registered (e.g., duplicate registration).
|
2020-06-12 18:11:17 +02:00
|
|
|
func (b BatchObserver) NewInt64SumObserver(name string, opts ...InstrumentOption) (Int64SumObserver, error) {
|
2020-05-19 20:49:24 +02:00
|
|
|
if b.runner == nil {
|
|
|
|
return wrapInt64SumObserverInstrument(NoopAsync{}, nil)
|
|
|
|
}
|
|
|
|
return wrapInt64SumObserverInstrument(
|
|
|
|
b.meter.newAsync(name, SumObserverKind, Int64NumberKind, opts, b.runner))
|
|
|
|
}
|
|
|
|
|
2020-05-22 00:42:14 +02:00
|
|
|
// NewFloat64SumObserver creates a new floating point SumObserver with
|
2020-05-19 20:49:24 +02:00
|
|
|
// the given name, running in a batch callback, and customized with
|
|
|
|
// options. May return an error if the name is invalid (e.g., empty)
|
|
|
|
// or improperly registered (e.g., duplicate registration).
|
2020-06-12 18:11:17 +02:00
|
|
|
func (b BatchObserver) NewFloat64SumObserver(name string, opts ...InstrumentOption) (Float64SumObserver, error) {
|
2020-05-19 20:49:24 +02:00
|
|
|
if b.runner == nil {
|
|
|
|
return wrapFloat64SumObserverInstrument(NoopAsync{}, nil)
|
|
|
|
}
|
|
|
|
return wrapFloat64SumObserverInstrument(
|
|
|
|
b.meter.newAsync(name, SumObserverKind, Float64NumberKind, opts,
|
|
|
|
b.runner))
|
|
|
|
}
|
|
|
|
|
2020-05-22 00:42:14 +02:00
|
|
|
// NewInt64UpDownSumObserver creates a new integer UpDownSumObserver instrument
|
2020-05-20 19:19:51 +02:00
|
|
|
// with the given name, running in a batch callback, and customized with
|
|
|
|
// options. May return an error if the name is invalid (e.g., empty)
|
|
|
|
// or improperly registered (e.g., duplicate registration).
|
2020-06-12 18:11:17 +02:00
|
|
|
func (b BatchObserver) NewInt64UpDownSumObserver(name string, opts ...InstrumentOption) (Int64UpDownSumObserver, error) {
|
2020-05-20 19:19:51 +02:00
|
|
|
if b.runner == nil {
|
|
|
|
return wrapInt64UpDownSumObserverInstrument(NoopAsync{}, nil)
|
|
|
|
}
|
|
|
|
return wrapInt64UpDownSumObserverInstrument(
|
|
|
|
b.meter.newAsync(name, UpDownSumObserverKind, Int64NumberKind, opts, b.runner))
|
|
|
|
}
|
|
|
|
|
2020-05-22 00:42:14 +02:00
|
|
|
// NewFloat64UpDownSumObserver creates a new floating point UpDownSumObserver with
|
2020-05-20 19:19:51 +02:00
|
|
|
// the given name, running in a batch callback, and customized with
|
|
|
|
// options. May return an error if the name is invalid (e.g., empty)
|
|
|
|
// or improperly registered (e.g., duplicate registration).
|
2020-06-12 18:11:17 +02:00
|
|
|
func (b BatchObserver) NewFloat64UpDownSumObserver(name string, opts ...InstrumentOption) (Float64UpDownSumObserver, error) {
|
2020-05-20 19:19:51 +02:00
|
|
|
if b.runner == nil {
|
|
|
|
return wrapFloat64UpDownSumObserverInstrument(NoopAsync{}, nil)
|
|
|
|
}
|
|
|
|
return wrapFloat64UpDownSumObserverInstrument(
|
|
|
|
b.meter.newAsync(name, UpDownSumObserverKind, Float64NumberKind, opts,
|
|
|
|
b.runner))
|
|
|
|
}
|
|
|
|
|
2020-05-14 22:10:48 +02:00
|
|
|
// MeterImpl returns the underlying MeterImpl of this Meter.
|
|
|
|
func (m Meter) MeterImpl() MeterImpl {
|
|
|
|
return m.impl
|
|
|
|
}
|
|
|
|
|
|
|
|
// newAsync constructs one new asynchronous instrument.
|
|
|
|
func (m Meter) newAsync(
|
|
|
|
name string,
|
|
|
|
mkind Kind,
|
|
|
|
nkind NumberKind,
|
2020-06-12 18:11:17 +02:00
|
|
|
opts []InstrumentOption,
|
2020-05-14 22:10:48 +02:00
|
|
|
runner AsyncRunner,
|
|
|
|
) (
|
|
|
|
AsyncImpl,
|
|
|
|
error,
|
|
|
|
) {
|
|
|
|
if m.impl == nil {
|
|
|
|
return NoopAsync{}, nil
|
|
|
|
}
|
|
|
|
desc := NewDescriptor(name, mkind, nkind, opts...)
|
2020-06-12 18:11:17 +02:00
|
|
|
desc.config.InstrumentationName = m.name
|
|
|
|
desc.config.InstrumentationVersion = m.version
|
2020-05-14 22:10:48 +02:00
|
|
|
return m.impl.NewAsyncInstrument(desc, runner)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newSync constructs one new synchronous instrument.
|
|
|
|
func (m Meter) newSync(
|
|
|
|
name string,
|
|
|
|
metricKind Kind,
|
|
|
|
numberKind NumberKind,
|
2020-06-12 18:11:17 +02:00
|
|
|
opts []InstrumentOption,
|
2020-05-14 22:10:48 +02:00
|
|
|
) (
|
|
|
|
SyncImpl,
|
|
|
|
error,
|
|
|
|
) {
|
|
|
|
if m.impl == nil {
|
|
|
|
return NoopSync{}, nil
|
|
|
|
}
|
|
|
|
desc := NewDescriptor(name, metricKind, numberKind, opts...)
|
2020-06-12 18:11:17 +02:00
|
|
|
desc.config.InstrumentationName = m.name
|
|
|
|
desc.config.InstrumentationVersion = m.version
|
2020-05-14 22:10:48 +02:00
|
|
|
return m.impl.NewSyncInstrument(desc)
|
2020-03-24 19:54:08 +02:00
|
|
|
}
|