...

Source file src/go.opentelemetry.io/otel/metric/noop/noop_test.go

Documentation: go.opentelemetry.io/otel/metric/noop

     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 noop // import "go.opentelemetry.io/otel/metric/noop"
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  
    23  	"go.opentelemetry.io/otel/metric"
    24  )
    25  
    26  func TestImplementationNoPanics(t *testing.T) {
    27  	// Check that if type has an embedded interface and that interface has
    28  	// methods added to it than the No-Op implementation implements them.
    29  	t.Run("MeterProvider", assertAllExportedMethodNoPanic(
    30  		reflect.ValueOf(MeterProvider{}),
    31  		reflect.TypeOf((*metric.MeterProvider)(nil)).Elem(),
    32  	))
    33  	t.Run("Meter", assertAllExportedMethodNoPanic(
    34  		reflect.ValueOf(Meter{}),
    35  		reflect.TypeOf((*metric.Meter)(nil)).Elem(),
    36  	))
    37  	t.Run("Observer", assertAllExportedMethodNoPanic(
    38  		reflect.ValueOf(Observer{}),
    39  		reflect.TypeOf((*metric.Observer)(nil)).Elem(),
    40  	))
    41  	t.Run("Registration", assertAllExportedMethodNoPanic(
    42  		reflect.ValueOf(Registration{}),
    43  		reflect.TypeOf((*metric.Registration)(nil)).Elem(),
    44  	))
    45  	t.Run("Int64Counter", assertAllExportedMethodNoPanic(
    46  		reflect.ValueOf(Int64Counter{}),
    47  		reflect.TypeOf((*metric.Int64Counter)(nil)).Elem(),
    48  	))
    49  	t.Run("Float64Counter", assertAllExportedMethodNoPanic(
    50  		reflect.ValueOf(Float64Counter{}),
    51  		reflect.TypeOf((*metric.Float64Counter)(nil)).Elem(),
    52  	))
    53  	t.Run("Int64UpDownCounter", assertAllExportedMethodNoPanic(
    54  		reflect.ValueOf(Int64UpDownCounter{}),
    55  		reflect.TypeOf((*metric.Int64UpDownCounter)(nil)).Elem(),
    56  	))
    57  	t.Run("Float64UpDownCounter", assertAllExportedMethodNoPanic(
    58  		reflect.ValueOf(Float64UpDownCounter{}),
    59  		reflect.TypeOf((*metric.Float64UpDownCounter)(nil)).Elem(),
    60  	))
    61  	t.Run("Int64Histogram", assertAllExportedMethodNoPanic(
    62  		reflect.ValueOf(Int64Histogram{}),
    63  		reflect.TypeOf((*metric.Int64Histogram)(nil)).Elem(),
    64  	))
    65  	t.Run("Float64Histogram", assertAllExportedMethodNoPanic(
    66  		reflect.ValueOf(Float64Histogram{}),
    67  		reflect.TypeOf((*metric.Float64Histogram)(nil)).Elem(),
    68  	))
    69  	t.Run("Int64ObservableCounter", assertAllExportedMethodNoPanic(
    70  		reflect.ValueOf(Int64ObservableCounter{}),
    71  		reflect.TypeOf((*metric.Int64ObservableCounter)(nil)).Elem(),
    72  	))
    73  	t.Run("Float64ObservableCounter", assertAllExportedMethodNoPanic(
    74  		reflect.ValueOf(Float64ObservableCounter{}),
    75  		reflect.TypeOf((*metric.Float64ObservableCounter)(nil)).Elem(),
    76  	))
    77  	t.Run("Int64ObservableGauge", assertAllExportedMethodNoPanic(
    78  		reflect.ValueOf(Int64ObservableGauge{}),
    79  		reflect.TypeOf((*metric.Int64ObservableGauge)(nil)).Elem(),
    80  	))
    81  	t.Run("Float64ObservableGauge", assertAllExportedMethodNoPanic(
    82  		reflect.ValueOf(Float64ObservableGauge{}),
    83  		reflect.TypeOf((*metric.Float64ObservableGauge)(nil)).Elem(),
    84  	))
    85  	t.Run("Int64ObservableUpDownCounter", assertAllExportedMethodNoPanic(
    86  		reflect.ValueOf(Int64ObservableUpDownCounter{}),
    87  		reflect.TypeOf((*metric.Int64ObservableUpDownCounter)(nil)).Elem(),
    88  	))
    89  	t.Run("Float64ObservableUpDownCounter", assertAllExportedMethodNoPanic(
    90  		reflect.ValueOf(Float64ObservableUpDownCounter{}),
    91  		reflect.TypeOf((*metric.Float64ObservableUpDownCounter)(nil)).Elem(),
    92  	))
    93  	t.Run("Int64Observer", assertAllExportedMethodNoPanic(
    94  		reflect.ValueOf(Int64Observer{}),
    95  		reflect.TypeOf((*metric.Int64Observer)(nil)).Elem(),
    96  	))
    97  	t.Run("Float64Observer", assertAllExportedMethodNoPanic(
    98  		reflect.ValueOf(Float64Observer{}),
    99  		reflect.TypeOf((*metric.Float64Observer)(nil)).Elem(),
   100  	))
   101  }
   102  
   103  func assertAllExportedMethodNoPanic(rVal reflect.Value, rType reflect.Type) func(*testing.T) {
   104  	return func(t *testing.T) {
   105  		for n := 0; n < rType.NumMethod(); n++ {
   106  			mType := rType.Method(n)
   107  			if !mType.IsExported() {
   108  				t.Logf("ignoring unexported %s", mType.Name)
   109  				continue
   110  			}
   111  			m := rVal.MethodByName(mType.Name)
   112  			if !m.IsValid() {
   113  				t.Errorf("unknown method for %s: %s", rVal.Type().Name(), mType.Name)
   114  			}
   115  
   116  			numIn := mType.Type.NumIn()
   117  			if mType.Type.IsVariadic() {
   118  				numIn--
   119  			}
   120  			args := make([]reflect.Value, numIn)
   121  			for i := range args {
   122  				aType := mType.Type.In(i)
   123  				args[i] = reflect.New(aType).Elem()
   124  			}
   125  
   126  			assert.NotPanicsf(t, func() {
   127  				_ = m.Call(args)
   128  			}, "%s.%s", rVal.Type().Name(), mType.Name)
   129  		}
   130  	}
   131  }
   132  
   133  func TestNewMeterProvider(t *testing.T) {
   134  	mp := NewMeterProvider()
   135  	assert.Equal(t, mp, MeterProvider{})
   136  	meter := mp.Meter("")
   137  	assert.Equal(t, meter, Meter{})
   138  }
   139  

View as plain text