go.uber.org/cadence@v1.2.9/internal/tracer_test.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package internal
    22  
    23  import (
    24  	"context"
    25  	"testing"
    26  
    27  	"github.com/opentracing/opentracing-go"
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  	jaeger_config "github.com/uber/jaeger-client-go/config"
    31  	"go.uber.org/zap"
    32  
    33  	"go.uber.org/cadence/.gen/go/shared"
    34  )
    35  
    36  func TestTracingContextPropagator(t *testing.T) {
    37  	t.Parallel()
    38  	tracer, closer, err := jaeger_config.Configuration{ServiceName: "test-service"}.NewTracer()
    39  	require.NoError(t, err)
    40  	defer closer.Close()
    41  	ctxProp := NewTracingContextPropagator(zap.NewNop(), tracer)
    42  
    43  	span := tracer.StartSpan("test-operation")
    44  	ctx := context.Background()
    45  	ctx = opentracing.ContextWithSpan(ctx, span)
    46  	header := &shared.Header{
    47  		Fields: map[string][]byte{},
    48  	}
    49  
    50  	err = ctxProp.Inject(ctx, NewHeaderWriter(header))
    51  	require.NoError(t, err)
    52  
    53  	returnCtx := context.Background()
    54  	returnCtx, err = ctxProp.Extract(returnCtx, NewHeaderReader(header))
    55  	require.NoError(t, err)
    56  
    57  	spanCtx := returnCtx.Value(activeSpanContextKey)
    58  	assert.NotNil(t, spanCtx)
    59  }
    60  
    61  func TestTracingContextPropagatorNoSpan(t *testing.T) {
    62  	t.Parallel()
    63  	ctxProp := NewTracingContextPropagator(zap.NewNop(), opentracing.NoopTracer{})
    64  
    65  	header := &shared.Header{
    66  		Fields: map[string][]byte{},
    67  	}
    68  	err := ctxProp.Inject(context.Background(), NewHeaderWriter(header))
    69  	require.NoError(t, err)
    70  
    71  	returnCtx := context.Background()
    72  	returnCtx, err = ctxProp.Extract(returnCtx, NewHeaderReader(header))
    73  	assert.NoError(t, err)
    74  }
    75  
    76  func TestTracingContextPropagatorWorkflowContext(t *testing.T) {
    77  	t.Parallel()
    78  	tracer, closer, err := jaeger_config.Configuration{ServiceName: "test-service"}.NewTracer()
    79  	require.NoError(t, err)
    80  	defer closer.Close()
    81  	ctxProp := NewTracingContextPropagator(zap.NewNop(), tracer)
    82  
    83  	span := tracer.StartSpan("test-operation")
    84  	assert.NotNil(t, span.Context())
    85  	ctx := contextWithSpan(Background(), span.Context())
    86  	header := &shared.Header{
    87  		Fields: map[string][]byte{},
    88  	}
    89  
    90  	err = ctxProp.InjectFromWorkflow(ctx, NewHeaderWriter(header))
    91  	require.NoError(t, err)
    92  
    93  	returnCtx, err := ctxProp.ExtractToWorkflow(Background(), NewHeaderReader(header))
    94  	require.NoError(t, err)
    95  
    96  	returnCtx2, err := ctxProp.ExtractToWorkflow(Background(), NewHeaderReader(header))
    97  	require.NoError(t, err)
    98  
    99  	newSpanContext := spanFromContext(returnCtx)
   100  	assert.NotNil(t, newSpanContext)
   101  	newSpanContext2 := spanFromContext(returnCtx2)
   102  	assert.NotNil(t, newSpanContext2)
   103  	assert.Equal(t, newSpanContext2, newSpanContext)
   104  }
   105  
   106  func TestTracingContextPropagatorWorkflowContextNoSpan(t *testing.T) {
   107  	t.Parallel()
   108  	ctxProp := NewTracingContextPropagator(zap.NewNop(), opentracing.NoopTracer{})
   109  
   110  	header := &shared.Header{
   111  		Fields: map[string][]byte{},
   112  	}
   113  	err := ctxProp.InjectFromWorkflow(Background(), NewHeaderWriter(header))
   114  	require.NoError(t, err)
   115  
   116  	returnCtx := Background()
   117  	returnCtx, err = ctxProp.ExtractToWorkflow(returnCtx, NewHeaderReader(header))
   118  	assert.NoError(t, err)
   119  }
   120  
   121  func TestConsistentInjectionExtraction(t *testing.T) {
   122  	t.Parallel()
   123  	tracer, closer, err := jaeger_config.Configuration{ServiceName: "test-service"}.NewTracer()
   124  	require.NoError(t, err)
   125  	defer closer.Close()
   126  	ctxProp := NewTracingContextPropagator(zap.NewNop(), tracer)
   127  
   128  	span := tracer.StartSpan("test-operation")
   129  	// base64 encoded string '{}'
   130  	var baggageVal = "e30="
   131  	span.SetBaggageItem("request-tenancy", baggageVal)
   132  	assert.NotNil(t, span.Context())
   133  	ctx := contextWithSpan(Background(), span.Context())
   134  	header := &shared.Header{
   135  		Fields: map[string][]byte{},
   136  	}
   137  	err = ctxProp.InjectFromWorkflow(ctx, NewHeaderWriter(header))
   138  	require.NoError(t, err)
   139  
   140  	extractedCtx, err := ctxProp.ExtractToWorkflow(Background(), NewHeaderReader(header))
   141  	require.NoError(t, err)
   142  
   143  	extractedSpanContext := spanFromContext(extractedCtx)
   144  	extractedSpanContext.ForeachBaggageItem(func(k, v string) bool {
   145  		if k == "request-tenancy" {
   146  			assert.Equal(t, v, baggageVal)
   147  		}
   148  		return false
   149  	})
   150  }