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