config.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package metric // import "go.opentelemetry.io/otel/metric"
  4. import (
  5. "slices"
  6. "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. // Ensure forward compatibility by explicitly making this not comparable.
  14. noCmp [0]func() //nolint: unused // This is indeed used.
  15. }
  16. // InstrumentationVersion returns the version of the library providing
  17. // instrumentation.
  18. func (cfg MeterConfig) InstrumentationVersion() string {
  19. return cfg.instrumentationVersion
  20. }
  21. // InstrumentationAttributes returns the attributes associated with the library
  22. // providing instrumentation.
  23. func (cfg MeterConfig) InstrumentationAttributes() attribute.Set {
  24. return cfg.attrs
  25. }
  26. // SchemaURL is the schema_url of the library providing instrumentation.
  27. func (cfg MeterConfig) SchemaURL() string {
  28. return cfg.schemaURL
  29. }
  30. // MeterOption is an interface for applying Meter options.
  31. type MeterOption interface {
  32. // applyMeter is used to set a MeterOption value of a MeterConfig.
  33. applyMeter(MeterConfig) MeterConfig
  34. }
  35. // NewMeterConfig creates a new MeterConfig and applies
  36. // all the given options.
  37. func NewMeterConfig(opts ...MeterOption) MeterConfig {
  38. var config MeterConfig
  39. for _, o := range opts {
  40. config = o.applyMeter(config)
  41. }
  42. return config
  43. }
  44. type meterOptionFunc func(MeterConfig) MeterConfig
  45. func (fn meterOptionFunc) applyMeter(cfg MeterConfig) MeterConfig {
  46. return fn(cfg)
  47. }
  48. // WithInstrumentationVersion sets the instrumentation version.
  49. func WithInstrumentationVersion(version string) MeterOption {
  50. return meterOptionFunc(func(config MeterConfig) MeterConfig {
  51. config.instrumentationVersion = version
  52. return config
  53. })
  54. }
  55. // WithInstrumentationAttributes adds the instrumentation attributes.
  56. //
  57. // This is equivalent to calling [WithInstrumentationAttributeSet] with an
  58. // [attribute.Set] created from a clone of the passed attributes.
  59. // [WithInstrumentationAttributeSet] is recommended for more control.
  60. //
  61. // If multiple [WithInstrumentationAttributes] or [WithInstrumentationAttributeSet]
  62. // options are passed, the attributes will be merged together in the order
  63. // they are passed. Attributes with duplicate keys will use the last value passed.
  64. func WithInstrumentationAttributes(attr ...attribute.KeyValue) MeterOption {
  65. set := attribute.NewSet(slices.Clone(attr)...)
  66. return WithInstrumentationAttributeSet(set)
  67. }
  68. // WithInstrumentationAttributeSet adds the instrumentation attributes.
  69. //
  70. // If multiple [WithInstrumentationAttributes] or [WithInstrumentationAttributeSet]
  71. // options are passed, the attributes will be merged together in the order
  72. // they are passed. Attributes with duplicate keys will use the last value passed.
  73. func WithInstrumentationAttributeSet(set attribute.Set) MeterOption {
  74. if set.Len() == 0 {
  75. return meterOptionFunc(func(config MeterConfig) MeterConfig {
  76. return config
  77. })
  78. }
  79. return meterOptionFunc(func(config MeterConfig) MeterConfig {
  80. if config.attrs.Len() == 0 {
  81. config.attrs = set
  82. } else {
  83. config.attrs = mergeSets(config.attrs, set)
  84. }
  85. return config
  86. })
  87. }
  88. // WithSchemaURL sets the schema URL.
  89. func WithSchemaURL(schemaURL string) MeterOption {
  90. return meterOptionFunc(func(config MeterConfig) MeterConfig {
  91. config.schemaURL = schemaURL
  92. return config
  93. })
  94. }