1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
package tracing
import (
"context"
"fmt"
"io"
"net"
"testing"
"github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/require"
"github.com/uber/jaeger-client-go"
"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper"
grpctracing "gitlab.com/gitlab-org/labkit/tracing/grpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/interop/grpc_testing"
)
func TestExtractSpanContextFromEnv(t *testing.T) {
_, cleanup := testhelper.StubTracingReporter(t)
defer cleanup()
injectedSpan := opentracing.StartSpan("test", opentracing.Tag{Key: "do-not-carry", Value: "value"})
injectedSpan.SetBaggageItem("hi", "hello")
jaegerInjectedSpan := injectedSpan.(*jaeger.Span)
jaegerInjectedSpanContext := jaegerInjectedSpan.SpanContext()
createSpanContext := func() []string {
env := envMap{}
err := opentracing.GlobalTracer().Inject(injectedSpan.Context(), opentracing.TextMap, env)
require.NoError(t, err)
return env.toSlice()
}
tests := []struct {
desc string
envs []string
expectedContext opentracing.SpanContext
expectedError string
}{
{
desc: "empty environment map",
envs: []string{},
expectedError: "opentracing: SpanContext not found in Extract carrier",
},
{
desc: "irrelevant environment map",
envs: []string{"SOME_THING=A", "SOMETHING_ELSE=B"},
expectedError: "opentracing: SpanContext not found in Extract carrier",
},
{
desc: "environment variable includes span context",
envs: createSpanContext(),
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
spanContext, err := ExtractSpanContextFromEnv(tc.envs)
if tc.expectedError != "" {
require.Equal(t, tc.expectedError, err.Error())
} else {
require.NoError(t, err)
require.NotNil(t, spanContext)
span := opentracing.StartSpan("test", opentracing.ChildOf(spanContext))
jaegerSpan := span.(*jaeger.Span)
jaegerSpanContext := jaegerSpan.SpanContext()
require.Equal(t, jaegerInjectedSpanContext.TraceID(), jaegerSpanContext.TraceID())
require.Equal(t, jaegerInjectedSpanContext.SpanID(), jaegerSpanContext.ParentID())
require.Equal(t, opentracing.Tags{}, jaegerSpan.Tags())
require.Equal(t, "hello", jaegerSpan.BaggageItem("hi"))
}
})
}
}
func TestUnaryPassthroughInterceptor(t *testing.T) {
reporter, cleanup := testhelper.StubTracingReporter(t)
defer cleanup()
tests := []struct {
desc string
setup func(*testing.T) (jaeger.SpanID, opentracing.SpanContext, func())
expectedSpans []string
}{
{
desc: "empty span context",
setup: func(t *testing.T) (jaeger.SpanID, opentracing.SpanContext, func()) {
return 0, nil, func() {}
},
expectedSpans: []string{
"/grpc.testing.TestService/UnaryCall",
},
},
{
desc: "span context with a simple span",
setup: func(t *testing.T) (jaeger.SpanID, opentracing.SpanContext, func()) {
span := opentracing.GlobalTracer().StartSpan("root")
return span.(*jaeger.Span).SpanContext().SpanID(), span.Context(), span.Finish
},
expectedSpans: []string{
"/grpc.testing.TestService/UnaryCall",
"root",
},
},
{
desc: "span context with a trace chain",
setup: func(t *testing.T) (jaeger.SpanID, opentracing.SpanContext, func()) {
root := opentracing.GlobalTracer().StartSpan("root")
child := opentracing.GlobalTracer().StartSpan("child", opentracing.ChildOf(root.Context()))
grandChild := opentracing.GlobalTracer().StartSpan("grandChild", opentracing.ChildOf(child.Context()))
return grandChild.(*jaeger.Span).SpanContext().SpanID(), grandChild.Context(), func() {
grandChild.Finish()
child.Finish()
root.Finish()
}
},
expectedSpans: []string{
"/grpc.testing.TestService/UnaryCall",
"grandChild",
"child",
"root",
},
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
reporter.Reset()
var parentID jaeger.SpanID
service := &testSvc{
unaryCall: func(ctx context.Context, request *grpc_testing.SimpleRequest) (*grpc_testing.SimpleResponse, error) {
if span := opentracing.SpanFromContext(ctx); span != nil {
parentID = span.(*jaeger.Span).SpanContext().ParentID()
}
return &grpc_testing.SimpleResponse{}, nil
},
}
expectedParentID, spanContext, finishFunc := tc.setup(t)
client := startFakeGitalyServer(t, service, spanContext)
_, err := client.UnaryCall(testhelper.Context(t), &grpc_testing.SimpleRequest{})
require.NoError(t, err)
finishFunc()
require.Equal(t, expectedParentID, parentID)
require.Equal(t, tc.expectedSpans, reportedSpans(t, reporter))
})
}
}
func TestStreamPassthroughInterceptor(t *testing.T) {
reporter, cleanup := testhelper.StubTracingReporter(t)
defer cleanup()
tests := []struct {
desc string
setup func(*testing.T) (jaeger.SpanID, opentracing.SpanContext, func())
expectedSpans []string
}{
{
desc: "empty span context",
setup: func(t *testing.T) (jaeger.SpanID, opentracing.SpanContext, func()) {
return 0, nil, func() {}
},
expectedSpans: []string{
"/grpc.testing.TestService/FullDuplexCall",
},
},
{
desc: "span context with a simple span",
setup: func(t *testing.T) (jaeger.SpanID, opentracing.SpanContext, func()) {
span := opentracing.GlobalTracer().StartSpan("root")
return span.(*jaeger.Span).SpanContext().SpanID(), span.Context(), span.Finish
},
expectedSpans: []string{
"/grpc.testing.TestService/FullDuplexCall",
"root",
},
},
{
desc: "span context with a trace chain",
setup: func(t *testing.T) (jaeger.SpanID, opentracing.SpanContext, func()) {
root := opentracing.GlobalTracer().StartSpan("root")
child := opentracing.GlobalTracer().StartSpan("child", opentracing.ChildOf(root.Context()))
grandChild := opentracing.GlobalTracer().StartSpan("grandChild", opentracing.ChildOf(child.Context()))
return grandChild.(*jaeger.Span).SpanContext().SpanID(), grandChild.Context(), func() {
grandChild.Finish()
child.Finish()
root.Finish()
}
},
expectedSpans: []string{
"/grpc.testing.TestService/FullDuplexCall",
"grandChild",
"child",
"root",
},
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
reporter.Reset()
var parentID jaeger.SpanID
service := &testSvc{
fullDuplexCall: func(stream grpc_testing.TestService_FullDuplexCallServer) error {
_, err := stream.Recv()
require.NoError(t, err)
if span := opentracing.SpanFromContext(stream.Context()); span != nil {
parentID = span.(*jaeger.Span).SpanContext().ParentID()
}
require.NoError(t, stream.Send(&grpc_testing.StreamingOutputCallResponse{}))
return nil
},
}
expectedParentID, spanContext, finishFunc := tc.setup(t)
client := startFakeGitalyServer(t, service, spanContext)
stream, err := client.FullDuplexCall(testhelper.Context(t))
require.NoError(t, err)
require.NoError(t, stream.Send(&grpc_testing.StreamingOutputCallRequest{}))
resp, err := stream.Recv()
require.NoError(t, err)
testhelper.ProtoEqual(t, &grpc_testing.StreamingOutputCallResponse{}, resp)
resp, err = stream.Recv()
require.Equal(t, io.EOF, err)
require.Nil(t, resp)
finishFunc()
require.Equal(t, expectedParentID, parentID)
require.Equal(t, tc.expectedSpans, reportedSpans(t, reporter))
})
}
}
type testSvc struct {
grpc_testing.UnimplementedTestServiceServer
unaryCall func(context.Context, *grpc_testing.SimpleRequest) (*grpc_testing.SimpleResponse, error)
fullDuplexCall func(stream grpc_testing.TestService_FullDuplexCallServer) error
}
func (ts *testSvc) UnaryCall(ctx context.Context, r *grpc_testing.SimpleRequest) (*grpc_testing.SimpleResponse, error) {
return ts.unaryCall(ctx, r)
}
func (ts *testSvc) FullDuplexCall(stream grpc_testing.TestService_FullDuplexCallServer) error {
return ts.fullDuplexCall(stream)
}
func startFakeGitalyServer(t *testing.T, svc *testSvc, spanContext opentracing.SpanContext) grpc_testing.TestServiceClient {
t.Helper()
listener, err := net.Listen("tcp", "localhost:0")
require.NoError(t, err)
srv := grpc.NewServer(
grpc.StreamInterceptor(grpctracing.StreamServerTracingInterceptor()),
grpc.UnaryInterceptor(grpctracing.UnaryServerTracingInterceptor()),
)
grpc_testing.RegisterTestServiceServer(srv, svc)
go testhelper.MustServe(t, srv, listener)
t.Cleanup(srv.Stop)
conn, err := grpc.Dial(
listener.Addr().String(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithUnaryInterceptor(UnaryPassthroughInterceptor(spanContext)),
grpc.WithStreamInterceptor(StreamPassthroughInterceptor(spanContext)),
)
require.NoError(t, err)
return grpc_testing.NewTestServiceClient(conn)
}
// envMap implements opentracing.TextMapReader and opentracing.TextMapWriter. It is used to create
// testing environment maps used in below tests
type envMap map[string]string
func (e envMap) Set(key, val string) {
e[key] = val
}
func (e envMap) ForeachKey(handler func(key string, val string) error) error {
for key, val := range e {
if err := handler(key, val); err != nil {
return err
}
}
return nil
}
func (e envMap) toSlice() []string {
var envSlice []string
for key, value := range e {
envSlice = append(envSlice, fmt.Sprintf("%s=%s", key, value))
}
return envSlice
}
|