2019-06-14 22:09:41 +02:00
|
|
|
// Copyright 2019, OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/api/core"
|
|
|
|
"go.opentelemetry.io/otel/api/unit"
|
2019-06-14 20:37:05 +02:00
|
|
|
)
|
|
|
|
|
2019-10-31 08:35:02 +02:00
|
|
|
// Provider supports named Meter instances.
|
|
|
|
type Provider interface {
|
2019-11-26 19:54:05 +02:00
|
|
|
// Meter gets a named Meter interface. If the name is an
|
2019-10-31 08:35:02 +02:00
|
|
|
// empty string, the provider uses a default name.
|
2019-11-26 19:54:05 +02:00
|
|
|
Meter(name string) Meter
|
2019-10-31 08:35:02 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
// LabelSet is an implementation-level interface that represents a
|
|
|
|
// []core.KeyValue for use as pre-defined labels in the metrics API.
|
2019-10-09 00:45:49 +02:00
|
|
|
type LabelSet interface {
|
|
|
|
}
|
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
// Options contains some options for metrics of any kind.
|
|
|
|
type Options struct {
|
|
|
|
// Description is an optional field describing the metric
|
|
|
|
// instrument.
|
|
|
|
Description string
|
|
|
|
// Unit is an optional field describing the metric instrument.
|
|
|
|
Unit unit.Unit
|
|
|
|
// Keys are recommended keys determined in the handles
|
|
|
|
// obtained for the metric.
|
|
|
|
Keys []core.Key
|
|
|
|
// Alternate defines the property of metric value dependent on
|
|
|
|
// a metric type.
|
|
|
|
//
|
|
|
|
// - for Counter, true implies that the metric is an up-down
|
|
|
|
// Counter
|
|
|
|
//
|
|
|
|
// - for Gauge, true implies that the metric is a
|
|
|
|
// non-descending Gauge
|
|
|
|
//
|
|
|
|
// - for Measure, true implies that the metric supports
|
|
|
|
// positive and negative values
|
|
|
|
Alternate bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// CounterOptionApplier is an interface for applying metric options
|
|
|
|
// that are valid only for counter metrics.
|
|
|
|
type CounterOptionApplier interface {
|
|
|
|
// ApplyCounterOption is used to make some general or
|
|
|
|
// counter-specific changes in the Options.
|
|
|
|
ApplyCounterOption(*Options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GaugeOptionApplier is an interface for applying metric options that
|
|
|
|
// are valid only for gauge metrics.
|
|
|
|
type GaugeOptionApplier interface {
|
|
|
|
// ApplyGaugeOption is used to make some general or
|
|
|
|
// gauge-specific changes in the Options.
|
|
|
|
ApplyGaugeOption(*Options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MeasureOptionApplier is an interface for applying metric options
|
|
|
|
// that are valid only for measure metrics.
|
|
|
|
type MeasureOptionApplier interface {
|
|
|
|
// ApplyMeasureOption is used to make some general or
|
|
|
|
// measure-specific changes in the Options.
|
|
|
|
ApplyMeasureOption(*Options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Measurement is used for reporting a batch of metric
|
|
|
|
// values. Instances of this type should be created by instruments
|
2019-10-29 22:27:22 +02:00
|
|
|
// (e.g., Int64Counter.Measurement()).
|
2019-10-23 08:29:24 +02:00
|
|
|
type Measurement struct {
|
2020-01-06 20:08:40 +02:00
|
|
|
// number needs to be aligned for 64-bit atomic operations.
|
2019-10-29 22:27:22 +02:00
|
|
|
number core.Number
|
2020-01-06 20:08:40 +02:00
|
|
|
instrument InstrumentImpl
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
|
|
|
|
2019-10-29 22:27:22 +02:00
|
|
|
// Instrument returns the instrument that created this measurement.
|
|
|
|
// This returns an implementation-level object for use by the SDK,
|
|
|
|
// users should not refer to this.
|
|
|
|
func (m Measurement) InstrumentImpl() InstrumentImpl {
|
2019-10-23 08:29:24 +02:00
|
|
|
return m.instrument
|
|
|
|
}
|
2019-10-15 18:28:36 +02:00
|
|
|
|
2019-10-29 22:27:22 +02:00
|
|
|
// Number returns a number recorded in this measurement.
|
|
|
|
func (m Measurement) Number() core.Number {
|
|
|
|
return m.number
|
2019-10-15 18:28:36 +02:00
|
|
|
}
|
|
|
|
|
2019-10-09 00:45:49 +02:00
|
|
|
// Meter is an interface to the metrics portion of the OpenTelemetry SDK.
|
2019-07-12 00:28:38 +02:00
|
|
|
type Meter interface {
|
2019-10-23 08:29:24 +02:00
|
|
|
// Labels returns a reference to a set of labels that cannot
|
|
|
|
// be read by the application.
|
2019-10-29 22:27:22 +02:00
|
|
|
Labels(...core.KeyValue) LabelSet
|
2019-10-23 08:29:24 +02:00
|
|
|
|
|
|
|
// NewInt64Counter creates a new integral counter with a given
|
|
|
|
// name and customized with passed options.
|
|
|
|
NewInt64Counter(name string, cos ...CounterOptionApplier) Int64Counter
|
|
|
|
// NewFloat64Counter creates a new floating point counter with
|
|
|
|
// a given name and customized with passed options.
|
|
|
|
NewFloat64Counter(name string, cos ...CounterOptionApplier) Float64Counter
|
|
|
|
// NewInt64Gauge creates a new integral gauge with a given
|
|
|
|
// name and customized with passed options.
|
|
|
|
NewInt64Gauge(name string, gos ...GaugeOptionApplier) Int64Gauge
|
|
|
|
// NewFloat64Gauge creates a new floating point gauge with a
|
|
|
|
// given name and customized with passed options.
|
|
|
|
NewFloat64Gauge(name string, gos ...GaugeOptionApplier) Float64Gauge
|
|
|
|
// NewInt64Measure creates a new integral measure with a given
|
|
|
|
// name and customized with passed options.
|
|
|
|
NewInt64Measure(name string, mos ...MeasureOptionApplier) Int64Measure
|
|
|
|
// NewFloat64Measure creates a new floating point measure with
|
|
|
|
// a given name and customized with passed options.
|
|
|
|
NewFloat64Measure(name string, mos ...MeasureOptionApplier) Float64Measure
|
2019-10-09 00:45:49 +02:00
|
|
|
|
|
|
|
// RecordBatch atomically records a batch of measurements.
|
|
|
|
RecordBatch(context.Context, LabelSet, ...Measurement)
|
2019-10-23 08:29:24 +02:00
|
|
|
}
|
2019-10-09 00:45:49 +02:00
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
// Option supports specifying the various metric options.
|
|
|
|
type Option func(*Options)
|
2019-10-09 00:45:49 +02:00
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
// OptionApplier is an interface for applying metric options that are
|
|
|
|
// valid for all the kinds of metrics.
|
|
|
|
type OptionApplier interface {
|
|
|
|
CounterOptionApplier
|
|
|
|
GaugeOptionApplier
|
|
|
|
MeasureOptionApplier
|
|
|
|
// ApplyOption is used to make some general changes in the
|
|
|
|
// Options.
|
|
|
|
ApplyOption(*Options)
|
2019-07-12 00:28:38 +02:00
|
|
|
}
|
2019-07-01 21:12:35 +02:00
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
// CounterGaugeOptionApplier is an interface for applying metric
|
|
|
|
// options that are valid for counter or gauge metrics.
|
|
|
|
type CounterGaugeOptionApplier interface {
|
|
|
|
CounterOptionApplier
|
|
|
|
GaugeOptionApplier
|
2019-07-12 00:28:38 +02:00
|
|
|
}
|
2019-07-01 21:12:35 +02:00
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
type optionWrapper struct {
|
|
|
|
F Option
|
2019-10-09 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
type counterOptionWrapper struct {
|
|
|
|
F Option
|
2019-10-09 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
type gaugeOptionWrapper struct {
|
|
|
|
F Option
|
2019-10-09 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
type measureOptionWrapper struct {
|
|
|
|
F Option
|
2019-10-09 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
type counterGaugeOptionWrapper struct {
|
|
|
|
FC Option
|
|
|
|
FG Option
|
2019-10-09 00:45:49 +02:00
|
|
|
}
|
2019-07-01 21:12:35 +02:00
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
var (
|
|
|
|
_ OptionApplier = optionWrapper{}
|
|
|
|
_ CounterOptionApplier = counterOptionWrapper{}
|
|
|
|
_ GaugeOptionApplier = gaugeOptionWrapper{}
|
|
|
|
_ MeasureOptionApplier = measureOptionWrapper{}
|
|
|
|
)
|
2019-07-01 21:12:35 +02:00
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
func (o optionWrapper) ApplyCounterOption(opts *Options) {
|
|
|
|
o.ApplyOption(opts)
|
2019-10-09 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
func (o optionWrapper) ApplyGaugeOption(opts *Options) {
|
|
|
|
o.ApplyOption(opts)
|
2019-10-09 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
func (o optionWrapper) ApplyMeasureOption(opts *Options) {
|
|
|
|
o.ApplyOption(opts)
|
2019-10-09 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
func (o optionWrapper) ApplyOption(opts *Options) {
|
|
|
|
o.F(opts)
|
2019-10-09 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
func (o counterOptionWrapper) ApplyCounterOption(opts *Options) {
|
|
|
|
o.F(opts)
|
|
|
|
}
|
2019-10-09 00:45:49 +02:00
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
func (o gaugeOptionWrapper) ApplyGaugeOption(opts *Options) {
|
|
|
|
o.F(opts)
|
2019-10-09 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
func (o measureOptionWrapper) ApplyMeasureOption(opts *Options) {
|
|
|
|
o.F(opts)
|
2019-10-09 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
func (o counterGaugeOptionWrapper) ApplyCounterOption(opts *Options) {
|
|
|
|
o.FC(opts)
|
2019-10-09 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
func (o counterGaugeOptionWrapper) ApplyGaugeOption(opts *Options) {
|
|
|
|
o.FG(opts)
|
2019-10-09 00:45:49 +02:00
|
|
|
}
|
2019-06-14 20:37:05 +02:00
|
|
|
|
|
|
|
// WithDescription applies provided description.
|
2019-10-09 00:45:49 +02:00
|
|
|
func WithDescription(desc string) OptionApplier {
|
|
|
|
return optionWrapper{
|
2019-10-23 08:29:24 +02:00
|
|
|
F: func(opts *Options) {
|
|
|
|
opts.Description = desc
|
2019-10-09 00:45:49 +02:00
|
|
|
},
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithUnit applies provided unit.
|
2019-10-09 00:45:49 +02:00
|
|
|
func WithUnit(unit unit.Unit) OptionApplier {
|
|
|
|
return optionWrapper{
|
2019-10-23 08:29:24 +02:00
|
|
|
F: func(opts *Options) {
|
|
|
|
opts.Unit = unit
|
2019-10-09 00:45:49 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
// WithKeys applies recommended label keys. Multiple `WithKeys`
|
|
|
|
// options accumulate.
|
2019-10-09 00:45:49 +02:00
|
|
|
func WithKeys(keys ...core.Key) OptionApplier {
|
|
|
|
return optionWrapper{
|
2019-10-23 08:29:24 +02:00
|
|
|
F: func(opts *Options) {
|
|
|
|
opts.Keys = append(opts.Keys, keys...)
|
2019-10-09 00:45:49 +02:00
|
|
|
},
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
// WithMonotonic sets whether a counter or a gauge is not permitted to
|
|
|
|
// go down.
|
|
|
|
func WithMonotonic(monotonic bool) CounterGaugeOptionApplier {
|
|
|
|
return counterGaugeOptionWrapper{
|
|
|
|
FC: func(opts *Options) {
|
|
|
|
opts.Alternate = !monotonic
|
2019-10-09 00:45:49 +02:00
|
|
|
},
|
2019-10-23 08:29:24 +02:00
|
|
|
FG: func(opts *Options) {
|
|
|
|
opts.Alternate = monotonic
|
2019-10-09 00:45:49 +02:00
|
|
|
},
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-09 00:45:49 +02:00
|
|
|
|
2019-10-23 08:29:24 +02:00
|
|
|
// WithAbsolute sets whether a measure is not permitted to be
|
|
|
|
// negative.
|
|
|
|
func WithAbsolute(absolute bool) MeasureOptionApplier {
|
2019-10-09 00:45:49 +02:00
|
|
|
return measureOptionWrapper{
|
2019-10-23 08:29:24 +02:00
|
|
|
F: func(opts *Options) {
|
|
|
|
opts.Alternate = !absolute
|
2019-10-09 00:45:49 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|