| 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 TestFloat64ObservableConfiguration(t *testing.T) { |
| 17 |
const ( |
| 18 |
token float64 = 43 |
| 19 |
desc = "Instrument description." |
| 20 |
uBytes = "By" |
| 21 |
) |
| 22 |
|
| 23 |
run := func(got float64ObservableConfig) 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 := &float64Observer{} |
| 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 Float64Observer) error { |
| 39 |
obsrv.Observe(token) |
| 40 |
return nil |
| 41 |
} |
| 42 |
|
| 43 |
t.Run("Float64ObservableCounter", run( |
| 44 |
NewFloat64ObservableCounterConfig( |
| 45 |
WithDescription(desc), |
| 46 |
WithUnit(uBytes), |
| 47 |
WithFloat64Callback(cback), |
| 48 |
), |
| 49 |
)) |
| 50 |
|
| 51 |
t.Run("Float64ObservableUpDownCounter", run( |
| 52 |
NewFloat64ObservableUpDownCounterConfig( |
| 53 |
WithDescription(desc), |
| 54 |
WithUnit(uBytes), |
| 55 |
WithFloat64Callback(cback), |
| 56 |
), |
| 57 |
)) |
| 58 |
|
| 59 |
t.Run("Float64ObservableGauge", run( |
| 60 |
NewFloat64ObservableGaugeConfig( |
| 61 |
WithDescription(desc), |
| 62 |
WithUnit(uBytes), |
| 63 |
WithFloat64Callback(cback), |
| 64 |
), |
| 65 |
)) |
| 66 |
} |
| 67 |
|
| 68 |
type float64ObservableConfig interface { |
| 69 |
Description() string |
| 70 |
Unit() string |
| 71 |
Callbacks() []Float64Callback |
| 72 |
} |
| 73 |
|
| 74 |
type float64Observer struct { |
| 75 |
embedded.Float64Observer |
| 76 |
Observable |
| 77 |
got float64 |
| 78 |
} |
| 79 |
|
| 80 |
func (o *float64Observer) Observe(v float64, _ ...ObserveOption) { |
| 81 |
o.got = v |
| 82 |
} |