...

Source file src/go.opentelemetry.io/otel/trace/context_test.go

Documentation: go.opentelemetry.io/otel/trace

     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 trace // import "go.opentelemetry.io/otel/trace"
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  )
    23  
    24  type testSpan struct {
    25  	noopSpan
    26  
    27  	ID     byte
    28  	Remote bool
    29  }
    30  
    31  func (s testSpan) SpanContext() SpanContext {
    32  	return SpanContext{
    33  		traceID: [16]byte{1},
    34  		spanID:  [8]byte{s.ID},
    35  		remote:  s.Remote,
    36  	}
    37  }
    38  
    39  var (
    40  	emptySpan   = noopSpan{}
    41  	localSpan   = testSpan{ID: 1, Remote: false}
    42  	remoteSpan  = testSpan{ID: 1, Remote: true}
    43  	wrappedSpan = nonRecordingSpan{sc: remoteSpan.SpanContext()}
    44  )
    45  
    46  func TestSpanFromContext(t *testing.T) {
    47  	testCases := []struct {
    48  		name         string
    49  		context      context.Context
    50  		expectedSpan Span
    51  	}{
    52  		{
    53  			name:         "empty context",
    54  			context:      nil,
    55  			expectedSpan: emptySpan,
    56  		},
    57  		{
    58  			name:         "background context",
    59  			context:      context.Background(),
    60  			expectedSpan: emptySpan,
    61  		},
    62  		{
    63  			name:         "local span",
    64  			context:      ContextWithSpan(context.Background(), localSpan),
    65  			expectedSpan: localSpan,
    66  		},
    67  		{
    68  			name:         "remote span",
    69  			context:      ContextWithSpan(context.Background(), remoteSpan),
    70  			expectedSpan: remoteSpan,
    71  		},
    72  		{
    73  			name:         "wrapped remote span",
    74  			context:      ContextWithRemoteSpanContext(context.Background(), remoteSpan.SpanContext()),
    75  			expectedSpan: wrappedSpan,
    76  		},
    77  		{
    78  			name:         "wrapped local span becomes remote",
    79  			context:      ContextWithRemoteSpanContext(context.Background(), localSpan.SpanContext()),
    80  			expectedSpan: wrappedSpan,
    81  		},
    82  	}
    83  
    84  	for _, tc := range testCases {
    85  		t.Run(tc.name, func(t *testing.T) {
    86  			assert.Equal(t, tc.expectedSpan, SpanFromContext(tc.context))
    87  
    88  			// Ensure SpanContextFromContext is just
    89  			// SpanFromContext(…).SpanContext().
    90  			assert.Equal(t, tc.expectedSpan.SpanContext(), SpanContextFromContext(tc.context))
    91  		})
    92  	}
    93  }
    94  

View as plain text