...

Source file src/go.opentelemetry.io/otel/metric/asyncfloat64_test.go

Documentation: go.opentelemetry.io/otel/metric

     1  // Copyright The OpenTelemetry Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package metric // import "go.opentelemetry.io/otel/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  			// Functions are not comparable.
    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