vitess.io/vitess@v0.16.2/go/trace/trace_test.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package trace 18 19 import ( 20 "context" 21 "fmt" 22 "io" 23 "strings" 24 "testing" 25 26 "google.golang.org/grpc" 27 ) 28 29 func TestFakeSpan(t *testing.T) { 30 ctx := context.Background() 31 32 // It should be safe to call all the usual methods as if a plugin were installed. 33 span1, ctx := NewSpan(ctx, "label") 34 span1.Finish() 35 36 span2, ctx := NewSpan(ctx, "label") 37 span2.Annotate("key", 42) 38 span2.Finish() 39 40 span3, _ := NewSpan(ctx, "label") 41 span3.Annotate("key", 42) 42 span3.Finish() 43 } 44 45 func TestRegisterService(t *testing.T) { 46 fakeName := "test" 47 tracingBackendFactories[fakeName] = func(serviceName string) (tracingService, io.Closer, error) { 48 tracer := &fakeTracer{name: serviceName} 49 return tracer, tracer, nil 50 } 51 52 tracingServer = fakeName 53 54 serviceName := "vtservice" 55 closer := StartTracing(serviceName) 56 tracer, ok := closer.(*fakeTracer) 57 if !ok { 58 t.Fatalf("did not get the expected tracer") 59 } 60 61 if tracer.name != serviceName { 62 t.Fatalf("expected the name to be `%v` but it was `%v`", serviceName, tracer.name) 63 } 64 } 65 66 type fakeTracer struct { 67 name string 68 log []string 69 } 70 71 func (f *fakeTracer) NewFromString(parent, label string) (Span, error) { 72 panic("implement me") 73 } 74 75 func (f *fakeTracer) New(parent Span, label string) Span { 76 f.log = append(f.log, "span started") 77 78 return &mockSpan{tracer: f} 79 } 80 81 func (f *fakeTracer) FromContext(ctx context.Context) (Span, bool) { 82 return nil, false 83 } 84 85 func (f *fakeTracer) NewContext(parent context.Context, span Span) context.Context { 86 return parent 87 } 88 89 func (f *fakeTracer) AddGrpcServerOptions(addInterceptors func(s grpc.StreamServerInterceptor, u grpc.UnaryServerInterceptor)) { 90 panic("implement me") 91 } 92 93 func (f *fakeTracer) AddGrpcClientOptions(addInterceptors func(s grpc.StreamClientInterceptor, u grpc.UnaryClientInterceptor)) { 94 panic("implement me") 95 } 96 97 func (f *fakeTracer) Close() error { 98 panic("implement me") 99 } 100 101 func (f *fakeTracer) assertNoSpanWith(t *testing.T, substr string) { 102 t.Helper() 103 for _, logLine := range f.log { 104 if strings.Contains(logLine, substr) { 105 t.Fatalf("expected to not find [%v] but found it in [%v]", substr, logLine) 106 } 107 } 108 } 109 110 type mockSpan struct { 111 tracer *fakeTracer 112 } 113 114 func (m *mockSpan) Finish() { 115 m.tracer.log = append(m.tracer.log, "span finished") 116 } 117 118 func (m *mockSpan) Annotate(key string, value any) { 119 m.tracer.log = append(m.tracer.log, fmt.Sprintf("key: %v values:%v", key, value)) 120 }