config.go 2.3 KB

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