gitlab.com/gitlab-org/labkit@v1.21.0/tracing/impl/stackdriver_tracer_test.go (about) 1 // +build tracer_static,tracer_static_stackdriver 2 3 package impl 4 5 import ( 6 "fmt" 7 "github.com/opentracing/opentracing-go" 8 "github.com/stretchr/testify/require" 9 "reflect" 10 "strings" 11 "testing" 12 13 "go.opencensus.io/trace" 14 ) 15 16 type unsupportedType struct{} 17 18 func TestOcSpanAdapterCastToAttribute(t *testing.T) { 19 tests := []struct { 20 name string 21 key string 22 value interface{} 23 want []trace.Attribute 24 }{ 25 { 26 name: "true", 27 key: "foo", 28 value: true, 29 want: []trace.Attribute{trace.BoolAttribute("foo", true)}, 30 }, 31 { 32 name: "false", 33 key: "foo", 34 value: false, 35 want: []trace.Attribute{trace.BoolAttribute("foo", false)}, 36 }, 37 { 38 name: "0", 39 key: "foo", 40 value: 0, 41 want: []trace.Attribute{trace.Int64Attribute("foo", 0)}, 42 }, 43 { 44 name: "42", 45 key: "foo", 46 value: 42, 47 want: []trace.Attribute{trace.Int64Attribute("foo", 42)}, 48 }, 49 { 50 name: "42.1", 51 key: "foo", 52 value: 42.1, 53 want: []trace.Attribute{trace.Float64Attribute("foo", 42.1)}, 54 }, 55 { 56 name: "short string", 57 key: "foo", 58 value: "bar", 59 want: []trace.Attribute{trace.StringAttribute("foo", "bar")}, 60 }, 61 { 62 name: "empty string", 63 key: "foo", 64 value: "", 65 want: []trace.Attribute{trace.StringAttribute("foo", "")}, 66 }, 67 { 68 name: "string length 255", 69 key: "foo", 70 value: strings.Repeat("a", 255), 71 want: []trace.Attribute{trace.StringAttribute("foo", strings.Repeat("a", 255))}, 72 }, 73 { 74 name: "string length 256", 75 key: "foo", 76 value: strings.Repeat("a", 256), 77 want: []trace.Attribute{trace.StringAttribute("foo", strings.Repeat("a", 256))}, 78 }, 79 { 80 name: "string length 257", 81 key: "foo", 82 value: strings.Repeat("a", 257), 83 want: []trace.Attribute{ 84 trace.StringAttribute("foo", strings.Repeat("a", 256)), 85 trace.StringAttribute("foo.1", strings.Repeat("a", 1)), 86 }, 87 }, 88 { 89 name: "string length 511", 90 key: "foo", 91 value: strings.Repeat("a", 511), 92 want: []trace.Attribute{ 93 trace.StringAttribute("foo", strings.Repeat("a", 256)), 94 trace.StringAttribute("foo.1", strings.Repeat("a", 255)), 95 }, 96 }, 97 { 98 name: "string length 512", 99 key: "foo", 100 value: strings.Repeat("a", 512), 101 want: []trace.Attribute{ 102 trace.StringAttribute("foo", strings.Repeat("a", 256)), 103 trace.StringAttribute("foo.1", strings.Repeat("a", 256)), 104 }, 105 }, 106 { 107 name: "string length 513", 108 key: "foo", 109 value: strings.Repeat("a", 513), 110 want: []trace.Attribute{ 111 trace.StringAttribute("foo", strings.Repeat("a", 256)), 112 trace.StringAttribute("foo.1", strings.Repeat("a", 256)), 113 trace.StringAttribute("foo.2", strings.Repeat("a", 1)), 114 }, 115 }, 116 { 117 name: "unsupported type", 118 key: "foo", 119 value: unsupportedType{}, 120 want: []trace.Attribute{ 121 trace.StringAttribute("foo", "castToAttribute not implemented for type struct"), 122 }, 123 }, 124 } 125 for _, tt := range tests { 126 t.Run(tt.name, func(t *testing.T) { 127 got := castToAttribute(tt.key, tt.value) //nolint 128 129 if !reflect.DeepEqual(got, tt.want) { 130 t.Errorf("castToAttribute() got = %v, want %v", got, tt.want) 131 } 132 }) 133 } 134 } 135 func TestIsSampled_stackdriver(t *testing.T) { 136 t.Parallel() 137 138 for _, tc := range []struct { 139 desc string 140 sampled bool 141 }{ 142 { 143 desc: "stackdriver sampled", 144 sampled: true, 145 }, 146 { 147 desc: "stackdriver not sampled", 148 sampled: false, 149 }, 150 } { 151 t.Run(tc.desc, func(t *testing.T) { 152 stackdriverTracer := &adapterTracer{nil} 153 if tc.sampled { 154 trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()}) 155 } else { 156 trace.ApplyConfig(trace.Config{DefaultSampler: trace.NeverSample()}) 157 } 158 159 span := stackdriverTracer.StartSpan("rootSpan") 160 for i := 0; i < 10; i++ { 161 require.Equal(t, tc.sampled, IsSampled(span)) 162 span = stackdriverTracer.StartSpan(fmt.Sprintf("span%d", i), opentracing.ChildOf(span.Context())) 163 } 164 }) 165 } 166 }