...

Source file src/go.opentelemetry.io/otel/internal/global/state_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
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  
    22  	"go.opentelemetry.io/otel/metric"
    23  	metricnoop "go.opentelemetry.io/otel/metric/noop"
    24  	"go.opentelemetry.io/otel/propagation"
    25  	"go.opentelemetry.io/otel/trace"
    26  	tracenoop "go.opentelemetry.io/otel/trace/noop"
    27  )
    28  
    29  type nonComparableTracerProvider struct {
    30  	trace.TracerProvider
    31  
    32  	nonComparable func() //nolint:structcheck,unused  // This is not called.
    33  }
    34  
    35  type nonComparableMeterProvider struct {
    36  	metric.MeterProvider
    37  
    38  	nonComparable func() //nolint:structcheck,unused  // This is not called.
    39  }
    40  
    41  func TestSetTracerProvider(t *testing.T) {
    42  	t.Run("Set With default is a noop", func(t *testing.T) {
    43  		ResetForTest(t)
    44  		SetTracerProvider(TracerProvider())
    45  
    46  		tp, ok := TracerProvider().(*tracerProvider)
    47  		if !ok {
    48  			t.Fatal("Global TracerProvider should be the default tracer provider")
    49  		}
    50  
    51  		if tp.delegate != nil {
    52  			t.Fatal("tracer provider should not delegate when setting itself")
    53  		}
    54  	})
    55  
    56  	t.Run("First Set() should replace the delegate", func(t *testing.T) {
    57  		ResetForTest(t)
    58  
    59  		SetTracerProvider(tracenoop.NewTracerProvider())
    60  
    61  		_, ok := TracerProvider().(*tracerProvider)
    62  		if ok {
    63  			t.Fatal("Global TracerProvider was not changed")
    64  		}
    65  	})
    66  
    67  	t.Run("Set() should delegate existing TracerProviders", func(t *testing.T) {
    68  		ResetForTest(t)
    69  
    70  		tp := TracerProvider()
    71  		SetTracerProvider(tracenoop.NewTracerProvider())
    72  
    73  		ntp := tp.(*tracerProvider)
    74  
    75  		if ntp.delegate == nil {
    76  			t.Fatal("The delegated tracer providers should have a delegate")
    77  		}
    78  	})
    79  
    80  	t.Run("non-comparable types should not panic", func(t *testing.T) {
    81  		ResetForTest(t)
    82  
    83  		tp := nonComparableTracerProvider{}
    84  		SetTracerProvider(tp)
    85  		assert.NotPanics(t, func() { SetTracerProvider(tp) })
    86  	})
    87  }
    88  
    89  func TestSetTextMapPropagator(t *testing.T) {
    90  	t.Run("Set With default is a noop", func(t *testing.T) {
    91  		ResetForTest(t)
    92  		SetTextMapPropagator(TextMapPropagator())
    93  
    94  		tmp, ok := TextMapPropagator().(*textMapPropagator)
    95  		if !ok {
    96  			t.Fatal("Global TextMapPropagator should be the default propagator")
    97  		}
    98  
    99  		if tmp.delegate != nil {
   100  			t.Fatal("TextMapPropagator should not delegate when setting itself")
   101  		}
   102  	})
   103  
   104  	t.Run("First Set() should replace the delegate", func(t *testing.T) {
   105  		ResetForTest(t)
   106  
   107  		SetTextMapPropagator(propagation.TraceContext{})
   108  
   109  		_, ok := TextMapPropagator().(*textMapPropagator)
   110  		if ok {
   111  			t.Fatal("Global TextMapPropagator was not changed")
   112  		}
   113  	})
   114  
   115  	t.Run("Set() should delegate existing propagators", func(t *testing.T) {
   116  		ResetForTest(t)
   117  
   118  		p := TextMapPropagator()
   119  		SetTextMapPropagator(propagation.TraceContext{})
   120  
   121  		np := p.(*textMapPropagator)
   122  
   123  		if np.delegate == nil {
   124  			t.Fatal("The delegated TextMapPropagators should have a delegate")
   125  		}
   126  	})
   127  
   128  	t.Run("non-comparable types should not panic", func(t *testing.T) {
   129  		ResetForTest(t)
   130  
   131  		// A composite TextMapPropagator is not comparable.
   132  		prop := propagation.NewCompositeTextMapPropagator(propagation.TraceContext{})
   133  		SetTextMapPropagator(prop)
   134  		assert.NotPanics(t, func() { SetTextMapPropagator(prop) })
   135  	})
   136  }
   137  
   138  func TestSetMeterProvider(t *testing.T) {
   139  	t.Run("Set With default is a noop", func(t *testing.T) {
   140  		ResetForTest(t)
   141  
   142  		SetMeterProvider(MeterProvider())
   143  
   144  		mp, ok := MeterProvider().(*meterProvider)
   145  		if !ok {
   146  			t.Fatal("Global MeterProvider should be the default meter provider")
   147  		}
   148  
   149  		if mp.delegate != nil {
   150  			t.Fatal("meter provider should not delegate when setting itself")
   151  		}
   152  	})
   153  
   154  	t.Run("First Set() should replace the delegate", func(t *testing.T) {
   155  		ResetForTest(t)
   156  
   157  		SetMeterProvider(metricnoop.NewMeterProvider())
   158  
   159  		_, ok := MeterProvider().(*meterProvider)
   160  		if ok {
   161  			t.Fatal("Global MeterProvider was not changed")
   162  		}
   163  	})
   164  
   165  	t.Run("Set() should delegate existing Meter Providers", func(t *testing.T) {
   166  		ResetForTest(t)
   167  
   168  		mp := MeterProvider()
   169  
   170  		SetMeterProvider(metricnoop.NewMeterProvider())
   171  
   172  		dmp := mp.(*meterProvider)
   173  
   174  		if dmp.delegate == nil {
   175  			t.Fatal("The delegated meter providers should have a delegate")
   176  		}
   177  	})
   178  
   179  	t.Run("non-comparable types should not panic", func(t *testing.T) {
   180  		ResetForTest(t)
   181  
   182  		mp := nonComparableMeterProvider{}
   183  		SetMeterProvider(mp)
   184  		assert.NotPanics(t, func() { SetMeterProvider(mp) })
   185  	})
   186  }
   187  

View as plain text