config.go (2898B)
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 // import "go.opentelemetry.io/otel/metric" 16 17 import "go.opentelemetry.io/otel/attribute" 18 19 // MeterConfig contains options for Meters. 20 type MeterConfig struct { 21 instrumentationVersion string 22 schemaURL string 23 attrs attribute.Set 24 25 // Ensure forward compatibility by explicitly making this not comparable. 26 noCmp [0]func() //nolint: unused // This is indeed used. 27 } 28 29 // InstrumentationVersion returns the version of the library providing 30 // instrumentation. 31 func (cfg MeterConfig) InstrumentationVersion() string { 32 return cfg.instrumentationVersion 33 } 34 35 // InstrumentationAttributes returns the attributes associated with the library 36 // providing instrumentation. 37 func (cfg MeterConfig) InstrumentationAttributes() attribute.Set { 38 return cfg.attrs 39 } 40 41 // SchemaURL is the schema_url of the library providing instrumentation. 42 func (cfg MeterConfig) SchemaURL() string { 43 return cfg.schemaURL 44 } 45 46 // MeterOption is an interface for applying Meter options. 47 type MeterOption interface { 48 // applyMeter is used to set a MeterOption value of a MeterConfig. 49 applyMeter(MeterConfig) MeterConfig 50 } 51 52 // NewMeterConfig creates a new MeterConfig and applies 53 // all the given options. 54 func NewMeterConfig(opts ...MeterOption) MeterConfig { 55 var config MeterConfig 56 for _, o := range opts { 57 config = o.applyMeter(config) 58 } 59 return config 60 } 61 62 type meterOptionFunc func(MeterConfig) MeterConfig 63 64 func (fn meterOptionFunc) applyMeter(cfg MeterConfig) MeterConfig { 65 return fn(cfg) 66 } 67 68 // WithInstrumentationVersion sets the instrumentation version. 69 func WithInstrumentationVersion(version string) MeterOption { 70 return meterOptionFunc(func(config MeterConfig) MeterConfig { 71 config.instrumentationVersion = version 72 return config 73 }) 74 } 75 76 // WithInstrumentationAttributes sets the instrumentation attributes. 77 // 78 // The passed attributes will be de-duplicated. 79 func WithInstrumentationAttributes(attr ...attribute.KeyValue) MeterOption { 80 return meterOptionFunc(func(config MeterConfig) MeterConfig { 81 config.attrs = attribute.NewSet(attr...) 82 return config 83 }) 84 } 85 86 // WithSchemaURL sets the schema URL. 87 func WithSchemaURL(schemaURL string) MeterOption { 88 return meterOptionFunc(func(config MeterConfig) MeterConfig { 89 config.schemaURL = schemaURL 90 return config 91 }) 92 }