...

Source file src/go.opentelemetry.io/otel/internal/global/propagator_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  	"context"
    19  	"testing"
    20  
    21  	"go.opentelemetry.io/otel/internal/internaltest"
    22  )
    23  
    24  func TestTextMapPropagatorDelegation(t *testing.T) {
    25  	ResetForTest(t)
    26  	ctx := context.Background()
    27  	carrier := internaltest.NewTextMapCarrier(nil)
    28  
    29  	// The default should be a noop.
    30  	initial := TextMapPropagator()
    31  	initial.Inject(ctx, carrier)
    32  	ctx = initial.Extract(ctx, carrier)
    33  	if !carrier.GotN(t, 0) || !carrier.SetN(t, 0) {
    34  		return
    35  	}
    36  
    37  	// Make sure the delegate woks as expected.
    38  	delegate := internaltest.NewTextMapPropagator("test")
    39  	delegate.Inject(ctx, carrier)
    40  	ctx = delegate.Extract(ctx, carrier)
    41  	if !delegate.InjectedN(t, carrier, 1) || !delegate.ExtractedN(t, ctx, 1) {
    42  		return
    43  	}
    44  
    45  	// The initial propagator should use the delegate after it is set as the
    46  	// global.
    47  	SetTextMapPropagator(delegate)
    48  	initial.Inject(ctx, carrier)
    49  	ctx = initial.Extract(ctx, carrier)
    50  	delegate.InjectedN(t, carrier, 2)
    51  	delegate.ExtractedN(t, ctx, 2)
    52  }
    53  
    54  func TestTextMapPropagatorDelegationNil(t *testing.T) {
    55  	ResetForTest(t)
    56  	ctx := context.Background()
    57  	carrier := internaltest.NewTextMapCarrier(nil)
    58  
    59  	// The default should be a noop.
    60  	initial := TextMapPropagator()
    61  	initial.Inject(ctx, carrier)
    62  	ctx = initial.Extract(ctx, carrier)
    63  	if !carrier.GotN(t, 0) || !carrier.SetN(t, 0) {
    64  		return
    65  	}
    66  
    67  	// Delegation to nil should not make a change.
    68  	SetTextMapPropagator(nil)
    69  	initial.Inject(ctx, carrier)
    70  	initial.Extract(ctx, carrier)
    71  	if !carrier.GotN(t, 0) || !carrier.SetN(t, 0) {
    72  		return
    73  	}
    74  }
    75  
    76  func TestTextMapPropagatorFields(t *testing.T) {
    77  	ResetForTest(t)
    78  	initial := TextMapPropagator()
    79  	delegate := internaltest.NewTextMapPropagator("test")
    80  	delegateFields := delegate.Fields()
    81  
    82  	// Sanity check on the initial Fields.
    83  	if got := initial.Fields(); fieldsEqual(got, delegateFields) {
    84  		t.Fatalf("testing fields (%v) matched Noop fields (%v)", delegateFields, got)
    85  	}
    86  	SetTextMapPropagator(delegate)
    87  	// Check previous returns from global not correctly delegate.
    88  	if got := initial.Fields(); !fieldsEqual(got, delegateFields) {
    89  		t.Errorf("global TextMapPropagator.Fields returned %v instead of delegating, want (%v)", got, delegateFields)
    90  	}
    91  	// Check new calls to global.
    92  	if got := TextMapPropagator().Fields(); !fieldsEqual(got, delegateFields) {
    93  		t.Errorf("global TextMapPropagator.Fields returned %v, want (%v)", got, delegateFields)
    94  	}
    95  }
    96  
    97  func fieldsEqual(f1, f2 []string) bool {
    98  	if len(f1) != len(f2) {
    99  		return false
   100  	}
   101  	for i := range f1 {
   102  		if f1[i] != f2[i] {
   103  			return false
   104  		}
   105  	}
   106  	return true
   107  }
   108  

View as plain text