config.go 2.8 KB

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