Joey Armstrong | a6af152 | 2023-01-17 16:06:16 -0500 | [diff] [blame] | 1 | // 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 | |
| 15 | package metric |
| 16 | |
| 17 | import "go.opentelemetry.io/otel/unit" |
| 18 | |
| 19 | // InstrumentConfig contains options for instrument descriptors. |
| 20 | type 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. |
| 34 | type 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. |
| 42 | func 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. |
| 51 | func WithDescription(desc string) InstrumentOption { |
| 52 | return descriptionOption(desc) |
| 53 | } |
| 54 | |
| 55 | type descriptionOption string |
| 56 | |
| 57 | func (d descriptionOption) ApplyInstrument(config *InstrumentConfig) { |
| 58 | config.Description = string(d) |
| 59 | } |
| 60 | |
| 61 | // WithUnit applies provided unit. |
| 62 | func WithUnit(unit unit.Unit) InstrumentOption { |
| 63 | return unitOption(unit) |
| 64 | } |
| 65 | |
| 66 | type unitOption unit.Unit |
| 67 | |
| 68 | func (u unitOption) ApplyInstrument(config *InstrumentConfig) { |
| 69 | config.Unit = unit.Unit(u) |
| 70 | } |
| 71 | |
| 72 | // WithInstrumentationName sets the instrumentation name. |
| 73 | func WithInstrumentationName(name string) InstrumentOption { |
| 74 | return instrumentationNameOption(name) |
| 75 | } |
| 76 | |
| 77 | type instrumentationNameOption string |
| 78 | |
| 79 | func (i instrumentationNameOption) ApplyInstrument(config *InstrumentConfig) { |
| 80 | config.InstrumentationName = string(i) |
| 81 | } |
| 82 | |
| 83 | // MeterConfig contains options for Meters. |
| 84 | type 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. |
| 91 | type 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. |
| 98 | func 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. |
| 107 | type Option interface { |
| 108 | InstrumentOption |
| 109 | MeterOption |
| 110 | } |
| 111 | |
| 112 | // WithInstrumentationVersion sets the instrumentation version. |
| 113 | func WithInstrumentationVersion(version string) Option { |
| 114 | return instrumentationVersionOption(version) |
| 115 | } |
| 116 | |
| 117 | type instrumentationVersionOption string |
| 118 | |
| 119 | func (i instrumentationVersionOption) ApplyMeter(config *MeterConfig) { |
| 120 | config.InstrumentationVersion = string(i) |
| 121 | } |
| 122 | |
| 123 | func (i instrumentationVersionOption) ApplyInstrument(config *InstrumentConfig) { |
| 124 | config.InstrumentationVersion = string(i) |
| 125 | } |