blob: 3cd8fe802f0bc47664d6ed531355b059149eecb3 [file] [log] [blame]
Joey Armstronge8c091f2023-01-17 16:56:26 -05001// Copyright The OpenTelemetry Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package metric
16
17import "go.opentelemetry.io/otel/unit"
18
19// InstrumentConfig contains options for instrument descriptors.
20type InstrumentConfig struct {
21 // Description describes the instrument in human-readable terms.
22 Description string
23 // Unit describes the measurement unit for a instrument.
24 Unit unit.Unit
25 // InstrumentationName is the name of the library providing
26 // instrumentation.
27 InstrumentationName string
28 // InstrumentationVersion is the version of the library providing
29 // instrumentation.
30 InstrumentationVersion string
31}
32
33// InstrumentOption is an interface for applying instrument options.
34type InstrumentOption interface {
35 // ApplyMeter is used to set a InstrumentOption value of a
36 // InstrumentConfig.
37 ApplyInstrument(*InstrumentConfig)
38}
39
40// NewInstrumentConfig creates a new InstrumentConfig
41// and applies all the given options.
42func NewInstrumentConfig(opts ...InstrumentOption) InstrumentConfig {
43 var config InstrumentConfig
44 for _, o := range opts {
45 o.ApplyInstrument(&config)
46 }
47 return config
48}
49
50// WithDescription applies provided description.
51func WithDescription(desc string) InstrumentOption {
52 return descriptionOption(desc)
53}
54
55type descriptionOption string
56
57func (d descriptionOption) ApplyInstrument(config *InstrumentConfig) {
58 config.Description = string(d)
59}
60
61// WithUnit applies provided unit.
62func WithUnit(unit unit.Unit) InstrumentOption {
63 return unitOption(unit)
64}
65
66type unitOption unit.Unit
67
68func (u unitOption) ApplyInstrument(config *InstrumentConfig) {
69 config.Unit = unit.Unit(u)
70}
71
72// WithInstrumentationName sets the instrumentation name.
73func WithInstrumentationName(name string) InstrumentOption {
74 return instrumentationNameOption(name)
75}
76
77type instrumentationNameOption string
78
79func (i instrumentationNameOption) ApplyInstrument(config *InstrumentConfig) {
80 config.InstrumentationName = string(i)
81}
82
83// MeterConfig contains options for Meters.
84type MeterConfig struct {
85 // InstrumentationVersion is the version of the library providing
86 // instrumentation.
87 InstrumentationVersion string
88}
89
90// MeterOption is an interface for applying Meter options.
91type MeterOption interface {
92 // ApplyMeter is used to set a MeterOption value of a MeterConfig.
93 ApplyMeter(*MeterConfig)
94}
95
96// NewMeterConfig creates a new MeterConfig and applies
97// all the given options.
98func NewMeterConfig(opts ...MeterOption) MeterConfig {
99 var config MeterConfig
100 for _, o := range opts {
101 o.ApplyMeter(&config)
102 }
103 return config
104}
105
106// Option is an interface for applying Instrument or Meter options.
107type Option interface {
108 InstrumentOption
109 MeterOption
110}
111
112// WithInstrumentationVersion sets the instrumentation version.
113func WithInstrumentationVersion(version string) Option {
114 return instrumentationVersionOption(version)
115}
116
117type instrumentationVersionOption string
118
119func (i instrumentationVersionOption) ApplyMeter(config *MeterConfig) {
120 config.InstrumentationVersion = string(i)
121}
122
123func (i instrumentationVersionOption) ApplyInstrument(config *InstrumentConfig) {
124 config.InstrumentationVersion = string(i)
125}