gitlab.com/gitlab-org/labkit@v1.21.0/tracing/impl/jaeger_tracer_test.go (about)

     1  // +build tracer_static,tracer_static_jaeger
     2  
     3  package impl
     4  
     5  import (
     6  	"fmt"
     7  	"github.com/opentracing/opentracing-go"
     8  	"github.com/stretchr/testify/require"
     9  	"testing"
    10  
    11  	"gitlab.com/gitlab-org/labkit/tracing/connstr"
    12  )
    13  
    14  func TestTracerFactory(t *testing.T) {
    15  	tests := []struct {
    16  		connectionString string
    17  		wantErr          bool
    18  		strict           bool
    19  	}{
    20  		{
    21  			connectionString: "opentracing://jaeger",
    22  			wantErr:          false,
    23  			strict:           true,
    24  		},
    25  		{
    26  			connectionString: "opentracing://jaeger?debug=true",
    27  			wantErr:          false,
    28  			strict:           true,
    29  		},
    30  		{
    31  			connectionString: "opentracing://jaeger?sampler=const&sampler_param=0",
    32  			wantErr:          false,
    33  			strict:           true,
    34  		},
    35  		{
    36  			connectionString: "opentracing://jaeger?sampler=probabilistic&sampler_param=0.1",
    37  			wantErr:          false,
    38  			strict:           true,
    39  		},
    40  		{
    41  			connectionString: "opentracing://jaeger?http_endpoint=http%3A%2F%2Flocalhost%3A14268%2Fapi%2Ftraces",
    42  			wantErr:          false,
    43  			strict:           true,
    44  		},
    45  		{
    46  			connectionString: "opentracing://jaeger?udp_endpoint=10.0.0.1:1234",
    47  			wantErr:          false,
    48  			strict:           true,
    49  		},
    50  		{
    51  			connectionString: "opentracing://jaeger?service_name=api",
    52  			wantErr:          false,
    53  			strict:           true,
    54  		},
    55  		{
    56  			connectionString: "opentracing://jaeger?invalid_option=blah&relaxed",
    57  			wantErr:          false,
    58  			strict:           false,
    59  		},
    60  		{
    61  			connectionString: "opentracing://jaeger?invalid_option=blah&strict",
    62  			wantErr:          true,
    63  			strict:           true,
    64  		},
    65  	}
    66  	for _, tt := range tests {
    67  		t.Run(tt.connectionString, func(t *testing.T) {
    68  			_, options, err := connstr.Parse(tt.connectionString)
    69  			if err != nil {
    70  				t.Errorf("TracerFactory() error = unable to parse connection string: %v", err)
    71  			}
    72  			if tt.strict {
    73  				options[keyStrictConnectionParsing] = "1"
    74  			}
    75  
    76  			options["service_name"] = "test"
    77  
    78  			gotTracer, gotCloser, err := jaegerTracerFactory(options)
    79  
    80  			if (err != nil) != tt.wantErr {
    81  				t.Errorf("TracerFactory() error = %v, wantErr %v", err, tt.wantErr)
    82  				return
    83  			}
    84  
    85  			if !tt.wantErr {
    86  				if gotTracer == nil {
    87  					t.Errorf("TracerFactory() expected a tracer, got nil")
    88  				}
    89  				if gotCloser == nil {
    90  					t.Errorf("TracerFactory() expected a closed, got nil")
    91  				}
    92  			}
    93  		})
    94  	}
    95  }
    96  
    97  func TestIsSampled_jaeger(t *testing.T) {
    98  	t.Parallel()
    99  
   100  	for _, tc := range []struct {
   101  		desc       string
   102  		connection string
   103  		sampled    bool
   104  	}{
   105  		{
   106  			desc:       "jaeger sampled",
   107  			connection: "opentracing://jaeger?sampler=probabilistic&sampler_param=1",
   108  			sampled:    true,
   109  		},
   110  		{
   111  			desc:       "jaeger not sampled",
   112  			connection: "opentracing://jaeger?sampler=probabilistic&sampler_param=0",
   113  			sampled:    false,
   114  		},
   115  	} {
   116  		t.Run(tc.desc, func(t *testing.T) {
   117  			_, options, err := connstr.Parse(tc.connection)
   118  			require.NoError(t, err)
   119  			options["service_name"] = "test"
   120  
   121  			jaegerTracer, closer, err := jaegerTracerFactory(options)
   122  			require.NoError(t, err)
   123  
   124  			span := jaegerTracer.StartSpan("rootSpan")
   125  			for i := 0; i < 10; i++ {
   126  				require.Equal(t, tc.sampled, IsSampled(span))
   127  				span = jaegerTracer.StartSpan(fmt.Sprintf("span%d", i), opentracing.ChildOf(span.Context()))
   128  			}
   129  			require.NoError(t, closer.Close())
   130  		})
   131  	}
   132  }