...

Source file src/go.opentelemetry.io/otel/internal/global/meter_types_test.go

Documentation: go.opentelemetry.io/otel/internal/global

     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 global // import "go.opentelemetry.io/otel/internal/global"
    16  
    17  import (
    18  	"context"
    19  
    20  	"go.opentelemetry.io/otel/metric"
    21  	"go.opentelemetry.io/otel/metric/embedded"
    22  )
    23  
    24  type testMeterProvider struct {
    25  	embedded.MeterProvider
    26  
    27  	count int
    28  }
    29  
    30  func (p *testMeterProvider) Meter(name string, opts ...metric.MeterOption) metric.Meter {
    31  	p.count++
    32  
    33  	return &testMeter{}
    34  }
    35  
    36  type testMeter struct {
    37  	embedded.Meter
    38  
    39  	afCount   int
    40  	afUDCount int
    41  	afGauge   int
    42  
    43  	aiCount   int
    44  	aiUDCount int
    45  	aiGauge   int
    46  
    47  	sfCount   int
    48  	sfUDCount int
    49  	sfHist    int
    50  
    51  	siCount   int
    52  	siUDCount int
    53  	siHist    int
    54  
    55  	callbacks []metric.Callback
    56  }
    57  
    58  func (m *testMeter) Int64Counter(name string, options ...metric.Int64CounterOption) (metric.Int64Counter, error) {
    59  	m.siCount++
    60  	return &testCountingIntInstrument{}, nil
    61  }
    62  
    63  func (m *testMeter) Int64UpDownCounter(name string, options ...metric.Int64UpDownCounterOption) (metric.Int64UpDownCounter, error) {
    64  	m.siUDCount++
    65  	return &testCountingIntInstrument{}, nil
    66  }
    67  
    68  func (m *testMeter) Int64Histogram(name string, options ...metric.Int64HistogramOption) (metric.Int64Histogram, error) {
    69  	m.siHist++
    70  	return &testCountingIntInstrument{}, nil
    71  }
    72  
    73  func (m *testMeter) Int64ObservableCounter(name string, options ...metric.Int64ObservableCounterOption) (metric.Int64ObservableCounter, error) {
    74  	m.aiCount++
    75  	return &testCountingIntInstrument{}, nil
    76  }
    77  
    78  func (m *testMeter) Int64ObservableUpDownCounter(name string, options ...metric.Int64ObservableUpDownCounterOption) (metric.Int64ObservableUpDownCounter, error) {
    79  	m.aiUDCount++
    80  	return &testCountingIntInstrument{}, nil
    81  }
    82  
    83  func (m *testMeter) Int64ObservableGauge(name string, options ...metric.Int64ObservableGaugeOption) (metric.Int64ObservableGauge, error) {
    84  	m.aiGauge++
    85  	return &testCountingIntInstrument{}, nil
    86  }
    87  
    88  func (m *testMeter) Float64Counter(name string, options ...metric.Float64CounterOption) (metric.Float64Counter, error) {
    89  	m.sfCount++
    90  	return &testCountingFloatInstrument{}, nil
    91  }
    92  
    93  func (m *testMeter) Float64UpDownCounter(name string, options ...metric.Float64UpDownCounterOption) (metric.Float64UpDownCounter, error) {
    94  	m.sfUDCount++
    95  	return &testCountingFloatInstrument{}, nil
    96  }
    97  
    98  func (m *testMeter) Float64Histogram(name string, options ...metric.Float64HistogramOption) (metric.Float64Histogram, error) {
    99  	m.sfHist++
   100  	return &testCountingFloatInstrument{}, nil
   101  }
   102  
   103  func (m *testMeter) Float64ObservableCounter(name string, options ...metric.Float64ObservableCounterOption) (metric.Float64ObservableCounter, error) {
   104  	m.afCount++
   105  	return &testCountingFloatInstrument{}, nil
   106  }
   107  
   108  func (m *testMeter) Float64ObservableUpDownCounter(name string, options ...metric.Float64ObservableUpDownCounterOption) (metric.Float64ObservableUpDownCounter, error) {
   109  	m.afUDCount++
   110  	return &testCountingFloatInstrument{}, nil
   111  }
   112  
   113  func (m *testMeter) Float64ObservableGauge(name string, options ...metric.Float64ObservableGaugeOption) (metric.Float64ObservableGauge, error) {
   114  	m.afGauge++
   115  	return &testCountingFloatInstrument{}, nil
   116  }
   117  
   118  // RegisterCallback captures the function that will be called during Collect.
   119  func (m *testMeter) RegisterCallback(f metric.Callback, i ...metric.Observable) (metric.Registration, error) {
   120  	m.callbacks = append(m.callbacks, f)
   121  	return testReg{
   122  		f: func(idx int) func() {
   123  			return func() { m.callbacks[idx] = nil }
   124  		}(len(m.callbacks) - 1),
   125  	}, nil
   126  }
   127  
   128  type testReg struct {
   129  	embedded.Registration
   130  
   131  	f func()
   132  }
   133  
   134  func (r testReg) Unregister() error {
   135  	r.f()
   136  	return nil
   137  }
   138  
   139  // This enables async collection.
   140  func (m *testMeter) collect() {
   141  	ctx := context.Background()
   142  	o := observationRecorder{ctx: ctx}
   143  	for _, f := range m.callbacks {
   144  		if f == nil {
   145  			// Unregister.
   146  			continue
   147  		}
   148  		_ = f(ctx, o)
   149  	}
   150  }
   151  
   152  type observationRecorder struct {
   153  	embedded.Observer
   154  
   155  	ctx context.Context
   156  }
   157  
   158  func (o observationRecorder) ObserveFloat64(i metric.Float64Observable, value float64, _ ...metric.ObserveOption) {
   159  	iImpl, ok := i.(*testCountingFloatInstrument)
   160  	if ok {
   161  		iImpl.observe()
   162  	}
   163  }
   164  
   165  func (o observationRecorder) ObserveInt64(i metric.Int64Observable, value int64, _ ...metric.ObserveOption) {
   166  	iImpl, ok := i.(*testCountingIntInstrument)
   167  	if ok {
   168  		iImpl.observe()
   169  	}
   170  }
   171  

View as plain text