...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package metric
16
17 import (
18 "context"
19 "testing"
20
21 "github.com/stretchr/testify/assert"
22 "github.com/stretchr/testify/require"
23
24 "go.opentelemetry.io/otel/metric/embedded"
25 )
26
27 func TestFloat64ObservableConfiguration(t *testing.T) {
28 const (
29 token float64 = 43
30 desc = "Instrument description."
31 uBytes = "By"
32 )
33
34 run := func(got float64ObservableConfig) func(*testing.T) {
35 return func(t *testing.T) {
36 assert.Equal(t, desc, got.Description(), "description")
37 assert.Equal(t, uBytes, got.Unit(), "unit")
38
39
40 cBacks := got.Callbacks()
41 require.Len(t, cBacks, 1, "callbacks")
42 o := &float64Observer{}
43 err := cBacks[0](context.Background(), o)
44 require.NoError(t, err)
45 assert.Equal(t, token, o.got, "callback not set")
46 }
47 }
48
49 cback := func(ctx context.Context, obsrv Float64Observer) error {
50 obsrv.Observe(token)
51 return nil
52 }
53
54 t.Run("Float64ObservableCounter", run(
55 NewFloat64ObservableCounterConfig(
56 WithDescription(desc),
57 WithUnit(uBytes),
58 WithFloat64Callback(cback),
59 ),
60 ))
61
62 t.Run("Float64ObservableUpDownCounter", run(
63 NewFloat64ObservableUpDownCounterConfig(
64 WithDescription(desc),
65 WithUnit(uBytes),
66 WithFloat64Callback(cback),
67 ),
68 ))
69
70 t.Run("Float64ObservableGauge", run(
71 NewFloat64ObservableGaugeConfig(
72 WithDescription(desc),
73 WithUnit(uBytes),
74 WithFloat64Callback(cback),
75 ),
76 ))
77 }
78
79 type float64ObservableConfig interface {
80 Description() string
81 Unit() string
82 Callbacks() []Float64Callback
83 }
84
85 type float64Observer struct {
86 embedded.Float64Observer
87 Observable
88 got float64
89 }
90
91 func (o *float64Observer) Observe(v float64, _ ...ObserveOption) {
92 o.got = v
93 }
94
View as plain text