...

Source file src/go.opentelemetry.io/otel/propagation/propagators_test.go

Documentation: go.opentelemetry.io/otel/propagation

     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 propagation_test
    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/propagation"
    25  	"go.opentelemetry.io/otel/trace"
    26  )
    27  
    28  const (
    29  	traceIDStr = "4bf92f3577b34da6a3ce929d0e0e4736"
    30  	spanIDStr  = "00f067aa0ba902b7"
    31  )
    32  
    33  var (
    34  	traceID = mustTraceIDFromHex(traceIDStr)
    35  	spanID  = mustSpanIDFromHex(spanIDStr)
    36  )
    37  
    38  func mustTraceIDFromHex(s string) (t trace.TraceID) {
    39  	var err error
    40  	t, err = trace.TraceIDFromHex(s)
    41  	if err != nil {
    42  		panic(err)
    43  	}
    44  	return
    45  }
    46  
    47  func mustSpanIDFromHex(s string) (t trace.SpanID) {
    48  	var err error
    49  	t, err = trace.SpanIDFromHex(s)
    50  	if err != nil {
    51  		panic(err)
    52  	}
    53  	return
    54  }
    55  
    56  type outOfThinAirPropagator struct {
    57  	t *testing.T
    58  }
    59  
    60  var _ propagation.TextMapPropagator = outOfThinAirPropagator{}
    61  
    62  func (p outOfThinAirPropagator) Extract(ctx context.Context, carrier propagation.TextMapCarrier) context.Context {
    63  	sc := trace.NewSpanContext(trace.SpanContextConfig{
    64  		TraceID:    traceID,
    65  		SpanID:     spanID,
    66  		TraceFlags: 0,
    67  	})
    68  	require.True(p.t, sc.IsValid())
    69  	return trace.ContextWithRemoteSpanContext(ctx, sc)
    70  }
    71  
    72  func (outOfThinAirPropagator) Inject(context.Context, propagation.TextMapCarrier) {}
    73  
    74  func (outOfThinAirPropagator) Fields() []string {
    75  	return nil
    76  }
    77  
    78  type nilCarrier struct{}
    79  
    80  var _ propagation.TextMapCarrier = nilCarrier{}
    81  
    82  func (nilCarrier) Keys() []string {
    83  	return nil
    84  }
    85  
    86  func (nilCarrier) Get(key string) string {
    87  	return ""
    88  }
    89  
    90  func (nilCarrier) Set(key string, value string) {}
    91  
    92  func TestMultiplePropagators(t *testing.T) {
    93  	ootaProp := outOfThinAirPropagator{t: t}
    94  	ns := nilCarrier{}
    95  	testProps := []propagation.TextMapPropagator{
    96  		propagation.TraceContext{},
    97  	}
    98  	bg := context.Background()
    99  	// sanity check of oota propagator, ensuring that it really
   100  	// generates the valid span context out of thin air
   101  	{
   102  		ctx := ootaProp.Extract(bg, ns)
   103  		sc := trace.SpanContextFromContext(ctx)
   104  		require.True(t, sc.IsValid(), "oota prop failed sanity check")
   105  		require.True(t, sc.IsRemote(), "oota prop is remote")
   106  	}
   107  	// sanity check for real propagators, ensuring that they
   108  	// really are not putting any valid span context into an empty
   109  	// go context in absence of the HTTP headers.
   110  	for _, prop := range testProps {
   111  		ctx := prop.Extract(bg, ns)
   112  		sc := trace.SpanContextFromContext(ctx)
   113  		require.Falsef(t, sc.IsValid(), "%#v failed sanity check", prop)
   114  		require.Falsef(t, sc.IsRemote(), "%#v prop set a remote", prop)
   115  	}
   116  	for _, prop := range testProps {
   117  		props := propagation.NewCompositeTextMapPropagator(ootaProp, prop)
   118  		ctx := props.Extract(bg, ns)
   119  		sc := trace.SpanContextFromContext(ctx)
   120  		assert.Truef(t, sc.IsRemote(), "%#v prop is remote", prop)
   121  		assert.Truef(t, sc.IsValid(), "%#v clobbers span context", prop)
   122  	}
   123  }
   124  

View as plain text