2020-10-09 04:58:56 +02:00
|
|
|
// Copyright The 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.
|
|
|
|
|
2020-11-12 17:28:32 +02:00
|
|
|
package metric // import "go.opentelemetry.io/otel/metric"
|
2020-10-09 04:58:56 +02:00
|
|
|
|
|
|
|
import (
|
2021-05-12 20:51:26 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/unit"
|
2020-10-09 04:58:56 +02:00
|
|
|
)
|
|
|
|
|
2020-10-17 18:48:21 +02:00
|
|
|
// InstrumentConfig contains options for metric instrument descriptors.
|
|
|
|
type InstrumentConfig struct {
|
2021-09-27 17:51:47 +02:00
|
|
|
description string
|
|
|
|
unit unit.Unit
|
2021-05-27 16:53:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Description describes the instrument in human-readable terms.
|
|
|
|
func (cfg InstrumentConfig) Description() string {
|
|
|
|
return cfg.description
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unit describes the measurement unit for a instrument.
|
|
|
|
func (cfg InstrumentConfig) Unit() unit.Unit {
|
|
|
|
return cfg.unit
|
|
|
|
}
|
|
|
|
|
2020-10-17 18:48:21 +02:00
|
|
|
// InstrumentOption is an interface for applying metric instrument options.
|
|
|
|
type InstrumentOption interface {
|
|
|
|
// ApplyMeter is used to set a InstrumentOption value of a
|
|
|
|
// InstrumentConfig.
|
2021-05-27 16:53:56 +02:00
|
|
|
applyInstrument(*InstrumentConfig)
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewInstrumentConfig creates a new InstrumentConfig
|
|
|
|
// and applies all the given options.
|
|
|
|
func NewInstrumentConfig(opts ...InstrumentOption) InstrumentConfig {
|
|
|
|
var config InstrumentConfig
|
|
|
|
for _, o := range opts {
|
2021-05-27 16:53:56 +02:00
|
|
|
o.applyInstrument(&config)
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2021-05-27 16:53:56 +02:00
|
|
|
type instrumentOptionFunc func(*InstrumentConfig)
|
2020-10-17 18:48:21 +02:00
|
|
|
|
2021-05-27 16:53:56 +02:00
|
|
|
func (fn instrumentOptionFunc) applyInstrument(cfg *InstrumentConfig) {
|
|
|
|
fn(cfg)
|
|
|
|
}
|
2020-10-17 18:48:21 +02:00
|
|
|
|
2021-05-27 16:53:56 +02:00
|
|
|
// WithDescription applies provided description.
|
|
|
|
func WithDescription(desc string) InstrumentOption {
|
|
|
|
return instrumentOptionFunc(func(cfg *InstrumentConfig) {
|
|
|
|
cfg.description = desc
|
|
|
|
})
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithUnit applies provided unit.
|
|
|
|
func WithUnit(unit unit.Unit) InstrumentOption {
|
2021-05-27 16:53:56 +02:00
|
|
|
return instrumentOptionFunc(func(cfg *InstrumentConfig) {
|
|
|
|
cfg.unit = unit
|
|
|
|
})
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MeterConfig contains options for Meters.
|
|
|
|
type MeterConfig struct {
|
2021-05-27 16:53:56 +02:00
|
|
|
instrumentationVersion string
|
2021-09-27 17:51:47 +02:00
|
|
|
schemaURL string
|
2021-05-27 16:53:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// InstrumentationVersion is the version of the library providing instrumentation.
|
|
|
|
func (cfg MeterConfig) InstrumentationVersion() string {
|
|
|
|
return cfg.instrumentationVersion
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
// SchemaURL is the schema_url of the library providing instrumentation.
|
|
|
|
func (cfg MeterConfig) SchemaURL() string {
|
|
|
|
return cfg.schemaURL
|
|
|
|
}
|
|
|
|
|
2020-10-17 18:48:21 +02:00
|
|
|
// MeterOption is an interface for applying Meter options.
|
|
|
|
type MeterOption interface {
|
|
|
|
// ApplyMeter is used to set a MeterOption value of a MeterConfig.
|
2021-05-27 16:53:56 +02:00
|
|
|
applyMeter(*MeterConfig)
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMeterConfig creates a new MeterConfig and applies
|
|
|
|
// all the given options.
|
|
|
|
func NewMeterConfig(opts ...MeterOption) MeterConfig {
|
|
|
|
var config MeterConfig
|
|
|
|
for _, o := range opts {
|
2021-05-27 16:53:56 +02:00
|
|
|
o.applyMeter(&config)
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
type meterOptionFunc func(*MeterConfig)
|
2020-10-17 18:48:21 +02:00
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
func (fn meterOptionFunc) applyMeter(cfg *MeterConfig) {
|
|
|
|
fn(cfg)
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
// WithInstrumentationVersion sets the instrumentation version.
|
|
|
|
func WithInstrumentationVersion(version string) MeterOption {
|
|
|
|
return meterOptionFunc(func(config *MeterConfig) {
|
|
|
|
config.instrumentationVersion = version
|
|
|
|
})
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 17:51:47 +02:00
|
|
|
// WithSchemaURL sets the schema URL.
|
|
|
|
func WithSchemaURL(schemaURL string) MeterOption {
|
|
|
|
return meterOptionFunc(func(config *MeterConfig) {
|
|
|
|
config.schemaURL = schemaURL
|
|
|
|
})
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|