Debian Codegraph @

metric/asyncint64_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package metric // import "go.opentelemetry.io/otel/metric"
5
6 import (
7 "context"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12
13 "go.opentelemetry.io/otel/metric/embedded"
14 )
15
16 func TestInt64ObservableConfiguration(t *testing.T) {
17 const (
18 token int64 = 43
19 desc = "Instrument description."
20 uBytes = "By"
21 )
22
23 run := func(got int64ObservableConfig) func(*testing.T) {
24 return func(t *testing.T) {
25 assert.Equal(t, desc, got.Description(), "description")
26 assert.Equal(t, uBytes, got.Unit(), "unit")
27
28 // Functions are not comparable.
29 cBacks := got.Callbacks()
30 require.Len(t, cBacks, 1, "callbacks")
31 o := &int64Observer{}
32 err := cBacks[0](t.Context(), o)
33 require.NoError(t, err)
34 assert.Equal(t, token, o.got, "callback not set")
35 }
36 }
37
38 cback := func(_ context.Context, obsrv Int64Observer) error {
39 obsrv.Observe(token)
40 return nil
41 }
42
43 t.Run("Int64ObservableCounter", run(
44 NewInt64ObservableCounterConfig(
45 WithDescription(desc),
46 WithUnit(uBytes),
47 WithInt64Callback(cback),
48 ),
49 ))
50
51 t.Run("Int64ObservableUpDownCounter", run(
52 NewInt64ObservableUpDownCounterConfig(
53 WithDescription(desc),
54 WithUnit(uBytes),
55 WithInt64Callback(cback),
56 ),
57 ))
58
59 t.Run("Int64ObservableGauge", run(
60 NewInt64ObservableGaugeConfig(
61 WithDescription(desc),
62 WithUnit(uBytes),
63 WithInt64Callback(cback),
64 ),
65 ))
66 }
67
68 type int64ObservableConfig interface {
69 Description() string
70 Unit() string
71 Callbacks() []Int64Callback
72 }
73
74 type int64Observer struct {
75 embedded.Int64Observer
76 Observable
77 got int64
78 }
79
80 func (o *int64Observer) Observe(v int64, _ ...ObserveOption) {
81 o.got = v
82 }