Debian Codegraph @

metric/config_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package metric_test
5
6 import (
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10
11 "go.opentelemetry.io/otel/attribute"
12 "go.opentelemetry.io/otel/metric"
13 )
14
15 func TestConfig(t *testing.T) {
16 version := "v1.1.1"
17 schemaURL := "https://opentelemetry.io/schemas/1.37.0"
18 attr := attribute.NewSet(
19 attribute.String("user", "alice"),
20 attribute.Bool("admin", true),
21 )
22
23 c := metric.NewMeterConfig(
24 metric.WithInstrumentationVersion(version),
25 metric.WithSchemaURL(schemaURL),
26 metric.WithInstrumentationAttributes(attr.ToSlice()...),
27 )
28
29 assert.Equal(t, version, c.InstrumentationVersion(), "instrumentation version")
30 assert.Equal(t, schemaURL, c.SchemaURL(), "schema URL")
31 assert.Equal(t, attr, c.InstrumentationAttributes(), "instrumentation attributes")
32 }
33
34 func TestWithInstrumentationAttributesNotLazy(t *testing.T) {
35 attrs := []attribute.KeyValue{
36 attribute.String("service", "test"),
37 attribute.Int("three", 3),
38 }
39 want := attribute.NewSet(attrs...)
40
41 // WithInstrumentationAttributes is expected to immediately
42 // create an immutable set from the attributes, so later changes
43 // to attrs should not affect the config.
44 opt := metric.WithInstrumentationAttributes(attrs...)
45 attrs[0] = attribute.String("service", "changed")
46
47 c := metric.NewMeterConfig(opt)
48 assert.Equal(t, want, c.InstrumentationAttributes(), "instrumentation attributes")
49 }
50
51 func TestWithInstrumentationAttributeSet(t *testing.T) {
52 attrs := attribute.NewSet(
53 attribute.String("service", "test"),
54 attribute.Int("three", 3),
55 )
56
57 c := metric.NewMeterConfig(
58 metric.WithInstrumentationAttributeSet(attrs),
59 )
60
61 assert.Equal(t, attrs, c.InstrumentationAttributes(), "instrumentation attributes")
62 }
63
64 func TestWithInstrumentationAttributesMerge(t *testing.T) {
65 aliceAttr := attribute.String("user", "Alice")
66 bobAttr := attribute.String("user", "Bob")
67 adminAttr := attribute.Bool("admin", true)
68
69 alice := attribute.NewSet(aliceAttr)
70 bob := attribute.NewSet(bobAttr)
71 aliceAdmin := attribute.NewSet(aliceAttr, adminAttr)
72 bobAdmin := attribute.NewSet(bobAttr, adminAttr)
73
74 t.Run("SameKey", func(t *testing.T) {
75 c := metric.NewMeterConfig(
76 metric.WithInstrumentationAttributes(aliceAttr),
77 metric.WithInstrumentationAttributes(bobAttr),
78 )
79 assert.Equal(t, bob, c.InstrumentationAttributes(),
80 "Later values for the same key should overwrite earlier ones.")
81 })
82
83 t.Run("DifferentKeys", func(t *testing.T) {
84 // Different keys should be merged
85 c := metric.NewMeterConfig(
86 metric.WithInstrumentationAttributes(aliceAttr),
87 metric.WithInstrumentationAttributes(adminAttr),
88 )
89 assert.Equal(t, aliceAdmin, c.InstrumentationAttributes(),
90 "Different keys should be merged.")
91 })
92
93 t.Run("Mixed", func(t *testing.T) {
94 c := metric.NewMeterConfig(
95 metric.WithInstrumentationAttributes(aliceAttr, adminAttr),
96 metric.WithInstrumentationAttributes(bobAttr),
97 )
98 assert.Equal(t, bobAdmin, c.InstrumentationAttributes(),
99 "Combination of same and different keys should be merged.")
100 })
101
102 t.Run("MergedEmpty", func(t *testing.T) {
103 c := metric.NewMeterConfig(
104 metric.WithInstrumentationAttributes(aliceAttr),
105 metric.WithInstrumentationAttributes(),
106 )
107 assert.Equal(t, alice, c.InstrumentationAttributes(),
108 "Empty attributes should not affect existing ones.")
109 })
110
111 t.Run("SameKeyWithSet", func(t *testing.T) {
112 c := metric.NewMeterConfig(
113 metric.WithInstrumentationAttributeSet(alice),
114 metric.WithInstrumentationAttributeSet(bob),
115 )
116 assert.Equal(t, bob, c.InstrumentationAttributes(),
117 "Later values for the same key should overwrite earlier ones.")
118 })
119
120 t.Run("DifferentKeysWithSet", func(t *testing.T) {
121 c := metric.NewMeterConfig(
122 metric.WithInstrumentationAttributeSet(alice),
123 metric.WithInstrumentationAttributeSet(attribute.NewSet(adminAttr)),
124 )
125 assert.Equal(t, aliceAdmin, c.InstrumentationAttributes(),
126 "Different keys should be merged.")
127 })
128
129 t.Run("MixedWithSet", func(t *testing.T) {
130 c := metric.NewMeterConfig(
131 metric.WithInstrumentationAttributeSet(aliceAdmin),
132 metric.WithInstrumentationAttributeSet(bob),
133 )
134 assert.Equal(t, bobAdmin, c.InstrumentationAttributes(),
135 "Combination of same and different keys should be merged.")
136 })
137
138 t.Run("MergedEmptyWithSet", func(t *testing.T) {
139 c := metric.NewMeterConfig(
140 metric.WithInstrumentationAttributeSet(alice),
141 metric.WithInstrumentationAttributeSet(attribute.NewSet()),
142 )
143 assert.Equal(t, alice, c.InstrumentationAttributes(),
144 "Empty attribute set should not affect existing ones.")
145 })
146
147 t.Run("MixedAttributesAndSet", func(t *testing.T) {
148 c := metric.NewMeterConfig(
149 metric.WithInstrumentationAttributes(aliceAttr),
150 metric.WithInstrumentationAttributeSet(attribute.NewSet(bobAttr, adminAttr)),
151 )
152 assert.Equal(t, bobAdmin, c.InstrumentationAttributes(),
153 "Attributes and attribute sets should be merged together.")
154 })
155 }
156
157 func BenchmarkNewMeterConfig(b *testing.B) {
158 for _, bb := range []struct {
159 name string
160 options []metric.MeterOption
161 }{
162 {
163 name: "with no options",
164 },
165 {
166 name: "with an instrumentation version",
167 options: []metric.MeterOption{
168 metric.WithInstrumentationVersion("testing version"),
169 },
170 },
171 {
172 name: "with a schema url",
173 options: []metric.MeterOption{
174 metric.WithSchemaURL("testing URL"),
175 },
176 },
177 {
178 name: "with instrumentation attribute",
179 options: []metric.MeterOption{
180 metric.WithInstrumentationAttributes(attribute.String("key", "value")),
181 },
182 },
183 {
184 name: "with instrumentation attribute set",
185 options: []metric.MeterOption{
186 metric.WithInstrumentationAttributeSet(attribute.NewSet(attribute.String("key", "value"))),
187 },
188 },
189 } {
190 b.Run(bb.name, func(b *testing.B) {
191 b.ReportAllocs()
192 b.ResetTimer()
193
194 for b.Loop() {
195 metric.NewMeterConfig(bb.options...)
196 }
197 })
198 }
199 }