Skip to content

Commit a2bc02e

Browse files
author
Jeff Dupont
committed
added a find metric helper, useful for testing
1 parent a42777c commit a2bc02e

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

otmetric/find.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package otmetric
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"go.opentelemetry.io/otel/attribute"
8+
otsdkmetric "go.opentelemetry.io/otel/sdk/metric"
9+
"go.opentelemetry.io/otel/sdk/metric/metricdata"
10+
)
11+
12+
// FindMetricVal returns the current value of a [metricName] from the [ManualReader].
13+
func FindMetricVal[V int64 | float64](ctx context.Context, r *otsdkmetric.ManualReader, metricName string, kvs attribute.Set) (V, error) {
14+
var zv V
15+
res := metricdata.ResourceMetrics{}
16+
if err := r.Collect(ctx, &res); err != nil {
17+
return zv, err
18+
}
19+
20+
for _, sm := range res.ScopeMetrics {
21+
for _, m := range sm.Metrics {
22+
if m.Name != metricName {
23+
continue
24+
}
25+
switch vs := m.Data.(type) {
26+
case metricdata.Gauge[V]:
27+
for _, d := range vs.DataPoints {
28+
if !kvs.Equals(&d.Attributes) {
29+
continue
30+
}
31+
return d.Value, nil
32+
}
33+
case metricdata.Sum[V]:
34+
for _, d := range vs.DataPoints {
35+
if !kvs.Equals(&d.Attributes) {
36+
continue
37+
}
38+
return d.Value, nil
39+
}
40+
default:
41+
// wrong metric-type (or histogram, which we'll have to implement later)
42+
continue
43+
}
44+
}
45+
}
46+
return zv, fmt.Errorf("metric %s not found", metricName)
47+
}

otmetric/find_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package otmetric
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"go.opentelemetry.io/otel/attribute"
8+
"go.opentelemetry.io/otel/metric"
9+
otsdkmetric "go.opentelemetry.io/otel/sdk/metric"
10+
)
11+
12+
func TestFindMetricVal(t *testing.T) {
13+
ctx := context.Background()
14+
15+
r := otsdkmetric.NewManualReader()
16+
mp := otsdkmetric.NewMeterProvider(otsdkmetric.WithReader(r))
17+
18+
otM := mp.Meter("foobar")
19+
attrSet := attribute.NewSet(attribute.String("fizzlebit", "foobar"), attribute.String("ouch", "icky"))
20+
21+
// read an int64 counter
22+
counter, countErr := otM.Int64Counter("a_counter_int")
23+
if countErr != nil {
24+
t.Fatalf("failed to register counter metric: %s", countErr)
25+
}
26+
counter.Add(ctx, 2, metric.WithAttributeSet(attrSet))
27+
28+
counterVal, counterValErr := FindMetricVal[int64](ctx, r, "a_counter_int", attrSet)
29+
if counterValErr != nil {
30+
t.Errorf("failed to fetch counter metric value: %s", counterValErr)
31+
}
32+
if counterVal != 2 {
33+
t.Errorf("unexpected counter metric value: want %d; got %d", 2, counterVal)
34+
}
35+
36+
// read an int64 gauge
37+
gauge, gaugeErr := otM.Int64Gauge("a_gauge_int")
38+
if gaugeErr != nil {
39+
t.Fatalf("failed to register gauge metric: %s", gaugeErr)
40+
}
41+
gauge.Record(ctx, 3, metric.WithAttributeSet(attrSet))
42+
43+
gaugeVal, gaugeValErr := FindMetricVal[int64](ctx, r, "a_gauge_int", attrSet)
44+
if gaugeValErr != nil {
45+
t.Errorf("failed to fetch gauge metric value: %s", gaugeValErr)
46+
}
47+
if gaugeVal != 3 {
48+
t.Errorf("unexpected gauge metric value: want %d; got %d", 3, gaugeVal)
49+
}
50+
}

0 commit comments

Comments
 (0)