|
| 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