@@ -21,7 +21,6 @@ import (
2121 "testing"
2222
2323 "go.opentelemetry.io/otel/attribute"
24- "go.opentelemetry.io/otel/metric"
2524 sdkmetric "go.opentelemetry.io/otel/sdk/metric"
2625 "go.opentelemetry.io/otel/sdk/metric/metricdata"
2726 "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
@@ -30,126 +29,202 @@ import (
3029)
3130
3231func TestFloat64GaugeSetValueSemantics (t * testing.T ) {
33- // Set up SDK with ManualReader for testing
32+ // Reset global state for isolated testing
33+ otelutil .ResetForTesting ()
34+
35+ // Set up SDK with ManualReader for testing our metrics
3436 reader := sdkmetric .NewManualReader ()
35- provider := sdkmetric .NewMeterProvider (
36- sdkmetric .WithResource (otelutil .GetResource ()),
37- sdkmetric .WithReader (reader ),
38- )
39- meter := provider .Meter ("test" )
4037
41- // Create a gauge metric
42- gauge , err := meter .Float64Gauge ("test_float_gauge" ,
43- metric .WithDescription ("Test float64 gauge metric" ),
44- metric .WithUnit ("percent" ),
38+ // Register reader with our global meter provider for our metrics to use
39+ otelutil .AddMetricReader (reader )
40+ otelutil .InitializeMeterProvider ()
41+
42+ // Create a gauge using our actual NewFloat64Metric function
43+ gauge , err := NewFloat64Metric (
44+ "test_float_gauge" ,
45+ "test_float_gauge" ,
46+ "Test float64 gauge metric" ,
47+ "percent" ,
48+ LastValue ,
49+ []string {"component" , "state" },
4550 )
4651 if err != nil {
4752 t .Fatalf ("Failed to create gauge metric: %v" , err )
4853 }
4954
5055 ctx := context .Background ()
51- attrs := []attribute. KeyValue {
52- attribute . String ( "component" , "cpu" ) ,
53- attribute . String ( "state" , "usage" ) ,
56+ labels := map [ string ] string {
57+ "component" : "cpu" ,
58+ "state" : "usage" ,
5459 }
5560
5661 // Set initial value to 0.0 (initialization)
57- gauge .Record (ctx , 0.0 , metric .WithAttributes (attrs ... ))
62+ if err := gauge .Record (labels , 0.0 ); err != nil {
63+ t .Fatalf ("Failed to record initial value: %v" , err )
64+ }
5865
5966 // Set value to 42.5 (current reading)
60- gauge .Record (ctx , 42.5 , metric .WithAttributes (attrs ... ))
67+ if err := gauge .Record (labels , 42.5 ); err != nil {
68+ t .Fatalf ("Failed to record updated value: %v" , err )
69+ }
6170
6271 // Collect metrics and verify gauge shows the last value (42.5)
6372 var rm metricdata.ResourceMetrics
6473 if err := reader .Collect (ctx , & rm ); err != nil {
6574 t .Fatalf ("Failed to collect metrics: %v" , err )
6675 }
6776
68- expected := metricdata.Metrics {
69- Name : "test_float_gauge" ,
70- Description : "Test float64 gauge metric" ,
71- Unit : "percent" ,
72- Data : metricdata.Gauge [float64 ]{
73- DataPoints : []metricdata.DataPoint [float64 ]{
74- {
75- Attributes : attribute .NewSet (attrs ... ),
76- Value : 42.5 ,
77- },
78- },
79- },
77+ // Find our metric in the collected data
78+ foundMetric := false
79+ for _ , sm := range rm .ScopeMetrics {
80+ for _ , m := range sm .Metrics {
81+ if m .Name == "test_float_gauge" {
82+ foundMetric = true
83+
84+ expected := metricdata.Metrics {
85+ Name : "test_float_gauge" ,
86+ Description : "Test float64 gauge metric" ,
87+ Unit : "percent" ,
88+ Data : metricdata.Gauge [float64 ]{
89+ DataPoints : []metricdata.DataPoint [float64 ]{
90+ {
91+ Attributes : attribute .NewSet (
92+ attribute .String ("component" , "cpu" ),
93+ attribute .String ("state" , "usage" ),
94+ ),
95+ Value : 42.5 ,
96+ },
97+ },
98+ },
99+ }
100+
101+ metricdatatest .AssertEqual (t , expected , m , metricdatatest .IgnoreTimestamp ())
102+ }
103+ }
80104 }
81105
82- metricdatatest .AssertEqual (t , expected , rm .ScopeMetrics [0 ].Metrics [0 ], metricdatatest .IgnoreTimestamp ())
106+ if ! foundMetric {
107+ t .Fatal ("test_float_gauge metric not found in collected metrics" )
108+ }
83109
84110 // Set value to 15.3 (new reading)
85- gauge .Record (ctx , 15.3 , metric .WithAttributes (attrs ... ))
111+ if err := gauge .Record (labels , 15.3 ); err != nil {
112+ t .Fatalf ("Failed to record new value: %v" , err )
113+ }
86114
87115 // Collect again and verify gauge now shows 15.3
88116 rm = metricdata.ResourceMetrics {}
89117 if err := reader .Collect (ctx , & rm ); err != nil {
90118 t .Fatalf ("Failed to collect metrics: %v" , err )
91119 }
92120
93- expected .Data = metricdata.Gauge [float64 ]{
94- DataPoints : []metricdata.DataPoint [float64 ]{
95- {
96- Attributes : attribute .NewSet (attrs ... ),
97- Value : 15.3 ,
98- },
99- },
121+ foundMetric = false
122+ for _ , sm := range rm .ScopeMetrics {
123+ for _ , m := range sm .Metrics {
124+ if m .Name == "test_float_gauge" {
125+ foundMetric = true
126+
127+ expected := metricdata.Metrics {
128+ Name : "test_float_gauge" ,
129+ Description : "Test float64 gauge metric" ,
130+ Unit : "percent" ,
131+ Data : metricdata.Gauge [float64 ]{
132+ DataPoints : []metricdata.DataPoint [float64 ]{
133+ {
134+ Attributes : attribute .NewSet (
135+ attribute .String ("component" , "cpu" ),
136+ attribute .String ("state" , "usage" ),
137+ ),
138+ Value : 15.3 ,
139+ },
140+ },
141+ },
142+ }
143+
144+ metricdatatest .AssertEqual (t , expected , m , metricdatatest .IgnoreTimestamp ())
145+ }
146+ }
100147 }
101148
102- metricdatatest .AssertEqual (t , expected , rm .ScopeMetrics [0 ].Metrics [0 ], metricdatatest .IgnoreTimestamp ())
149+ if ! foundMetric {
150+ t .Fatal ("test_float_gauge metric not found in second collection" )
151+ }
103152}
104153
105154func TestFloat64CounterAddSemantics (t * testing.T ) {
106- // Set up SDK with ManualReader for testing
155+ // Reset global state for isolated testing
156+ otelutil .ResetForTesting ()
157+
158+ // Set up SDK with ManualReader for testing our metrics
107159 reader := sdkmetric .NewManualReader ()
108- provider := sdkmetric .NewMeterProvider (
109- sdkmetric .WithResource (otelutil .GetResource ()),
110- sdkmetric .WithReader (reader ),
111- )
112- meter := provider .Meter ("test" )
113160
114- // Create a counter metric
115- counter , err := meter .Float64Counter ("test_float_counter" ,
116- metric .WithDescription ("Test float64 counter metric" ),
117- metric .WithUnit ("bytes" ),
161+ // Register reader with our global meter provider for our metrics to use
162+ otelutil .AddMetricReader (reader )
163+ otelutil .InitializeMeterProvider ()
164+
165+ // Create a counter using our actual NewFloat64Metric function
166+ counter , err := NewFloat64Metric (
167+ "test_float_counter" ,
168+ "test_float_counter" ,
169+ "Test float64 counter metric" ,
170+ "bytes" ,
171+ Sum ,
172+ []string {"operation" },
118173 )
119174 if err != nil {
120175 t .Fatalf ("Failed to create counter metric: %v" , err )
121176 }
122177
123178 ctx := context .Background ()
124- attrs := []attribute. KeyValue {
125- attribute . String ( "operation" , "read" ) ,
179+ labels := map [ string ] string {
180+ "operation" : "read" ,
126181 }
127182
128- // Add to counter multiple times with fractional values
129- counter .Add (ctx , 1024.5 , metric .WithAttributes (attrs ... ))
130- counter .Add (ctx , 2048.75 , metric .WithAttributes (attrs ... ))
183+ // Add to counter multiple times with fractional values using our Record method
184+ if err := counter .Record (labels , 1024.5 ); err != nil {
185+ t .Fatalf ("Failed to record first value: %v" , err )
186+ }
187+ if err := counter .Record (labels , 2048.75 ); err != nil {
188+ t .Fatalf ("Failed to record second value: %v" , err )
189+ }
131190
132191 // Collect metrics and verify counter accumulated the sum
133192 var rm metricdata.ResourceMetrics
134193 if err := reader .Collect (ctx , & rm ); err != nil {
135194 t .Fatalf ("Failed to collect metrics: %v" , err )
136195 }
137196
138- expected := metricdata.Metrics {
139- Name : "test_float_counter" ,
140- Description : "Test float64 counter metric" ,
141- Unit : "bytes" ,
142- Data : metricdata.Sum [float64 ]{
143- Temporality : metricdata .CumulativeTemporality ,
144- IsMonotonic : true ,
145- DataPoints : []metricdata.DataPoint [float64 ]{
146- {
147- Attributes : attribute .NewSet (attrs ... ),
148- Value : 3073.25 , // 1024.5 + 2048.75
149- },
150- },
151- },
197+ // Find our metric in the collected data
198+ foundMetric := false
199+ for _ , sm := range rm .ScopeMetrics {
200+ for _ , m := range sm .Metrics {
201+ if m .Name == "test_float_counter" {
202+ foundMetric = true
203+
204+ expected := metricdata.Metrics {
205+ Name : "test_float_counter" ,
206+ Description : "Test float64 counter metric" ,
207+ Unit : "bytes" ,
208+ Data : metricdata.Sum [float64 ]{
209+ Temporality : metricdata .CumulativeTemporality ,
210+ IsMonotonic : true ,
211+ DataPoints : []metricdata.DataPoint [float64 ]{
212+ {
213+ Attributes : attribute .NewSet (
214+ attribute .String ("operation" , "read" ),
215+ ),
216+ Value : 3073.25 , // 1024.5 + 2048.75
217+ },
218+ },
219+ },
220+ }
221+
222+ metricdatatest .AssertEqual (t , expected , m , metricdatatest .IgnoreTimestamp ())
223+ }
224+ }
152225 }
153226
154- metricdatatest .AssertEqual (t , expected , rm .ScopeMetrics [0 ].Metrics [0 ], metricdatatest .IgnoreTimestamp ())
227+ if ! foundMetric {
228+ t .Fatal ("test_float_counter metric not found in collected metrics" )
229+ }
155230}
0 commit comments