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.
|
2021-12-14 23:15:53 +02:00
|
|
|
func (cfg *InstrumentConfig) Description() string {
|
2021-05-27 16:53:56 +02:00
|
|
|
return cfg.description
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unit describes the measurement unit for a instrument.
|
2021-12-14 23:15:53 +02:00
|
|
|
func (cfg *InstrumentConfig) Unit() unit.Unit {
|
2021-05-27 16:53:56 +02:00
|
|
|
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.
|
2022-02-01 23:51:23 +02:00
|
|
|
applyInstrument(InstrumentConfig) 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 {
|
2022-02-01 23:51:23 +02:00
|
|
|
config = o.applyInstrument(config)
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2022-02-01 23:51:23 +02:00
|
|
|
type instrumentOptionFunc func(InstrumentConfig) InstrumentConfig
|
2020-10-17 18:48:21 +02:00
|
|
|
|
2022-02-01 23:51:23 +02:00
|
|
|
func (fn instrumentOptionFunc) applyInstrument(cfg InstrumentConfig) InstrumentConfig {
|
|
|
|
return fn(cfg)
|
2021-05-27 16:53:56 +02:00
|
|
|
}
|
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 {
|
2022-02-01 23:51:23 +02:00
|
|
|
return instrumentOptionFunc(func(cfg InstrumentConfig) InstrumentConfig {
|
2021-05-27 16:53:56 +02:00
|
|
|
cfg.description = desc
|
2022-02-01 23:51:23 +02:00
|
|
|
return cfg
|
2021-05-27 16:53:56 +02:00
|
|
|
})
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithUnit applies provided unit.
|
|
|
|
func WithUnit(unit unit.Unit) InstrumentOption {
|
2022-02-01 23:51:23 +02:00
|
|
|
return instrumentOptionFunc(func(cfg InstrumentConfig) InstrumentConfig {
|
2021-05-27 16:53:56 +02:00
|
|
|
cfg.unit = unit
|
2022-02-01 23:51:23 +02:00
|
|
|
return cfg
|
2021-05-27 16:53:56 +02:00
|
|
|
})
|
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.
|
2021-12-14 23:15:53 +02:00
|
|
|
func (cfg *MeterConfig) InstrumentationVersion() string {
|
2021-05-27 16:53:56 +02:00
|
|
|
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.
|
2021-12-14 23:15:53 +02:00
|
|
|
func (cfg *MeterConfig) SchemaURL() string {
|
2021-09-27 17:51:47 +02:00
|
|
|
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.
|
2022-02-01 23:51:23 +02:00
|
|
|
applyMeter(MeterConfig) 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 {
|
2022-02-01 23:51:23 +02:00
|
|
|
config = o.applyMeter(config)
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2022-02-01 23:51:23 +02:00
|
|
|
type meterOptionFunc func(MeterConfig) MeterConfig
|
2020-10-17 18:48:21 +02:00
|
|
|
|
2022-02-01 23:51:23 +02:00
|
|
|
func (fn meterOptionFunc) applyMeter(cfg MeterConfig) MeterConfig {
|
|
|
|
return 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 {
|
2022-02-01 23:51:23 +02:00
|
|
|
return meterOptionFunc(func(config MeterConfig) MeterConfig {
|
2021-09-27 17:51:47 +02:00
|
|
|
config.instrumentationVersion = version
|
2022-02-01 23:51:23 +02:00
|
|
|
return config
|
2021-09-27 17:51:47 +02:00
|
|
|
})
|
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 {
|
2022-02-01 23:51:23 +02:00
|
|
|
return meterOptionFunc(func(config MeterConfig) MeterConfig {
|
2021-09-27 17:51:47 +02:00
|
|
|
config.schemaURL = schemaURL
|
2022-02-01 23:51:23 +02:00
|
|
|
return config
|
2021-09-27 17:51:47 +02:00
|
|
|
})
|
2020-10-17 18:48:21 +02:00
|
|
|
}
|