config.go raw

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