github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/observability/tracing/client_test.go (about) 1 // Copyright 2022 Gravitational, Inc 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 tracing 16 17 import ( 18 "context" 19 "testing" 20 21 "github.com/gravitational/trace" 22 "github.com/stretchr/testify/require" 23 "go.opentelemetry.io/otel/exporters/otlp/otlptrace" 24 otlp "go.opentelemetry.io/proto/otlp/trace/v1" 25 ) 26 27 var _ otlptrace.Client = (*mockClient)(nil) 28 29 type mockClient struct { 30 uploadError error 31 spans []*otlp.ResourceSpans 32 } 33 34 func (m mockClient) Start(ctx context.Context) error { 35 return nil 36 } 37 38 func (m mockClient) Stop(ctx context.Context) error { 39 return nil 40 } 41 42 func (m *mockClient) UploadTraces(ctx context.Context, protoSpans []*otlp.ResourceSpans) error { 43 m.spans = append(m.spans, protoSpans...) 44 return m.uploadError 45 } 46 47 func TestUploadTraces(t *testing.T) { 48 const ( 49 spanCount = 10 50 uploadCount = 5 51 ) 52 53 cases := []struct { 54 name string 55 client mockClient 56 spans []*otlp.ResourceSpans 57 errorAssertion require.ErrorAssertionFunc 58 spanAssertion require.ValueAssertionFunc 59 }{ 60 { 61 name: "no spans to upload", 62 spans: make([]*otlp.ResourceSpans, 0, spanCount), 63 errorAssertion: require.NoError, 64 spanAssertion: require.Empty, 65 }, 66 { 67 name: "successfully uploads spans", 68 spans: make([]*otlp.ResourceSpans, spanCount), 69 errorAssertion: require.NoError, 70 spanAssertion: func(t require.TestingT, i interface{}, i2 ...interface{}) { 71 require.NotEmpty(t, i, i2...) 72 require.Len(t, i, spanCount*uploadCount, i2...) 73 }, 74 }, 75 { 76 name: "error uploading spans", 77 spans: make([]*otlp.ResourceSpans, spanCount), 78 client: mockClient{uploadError: trace.ConnectionProblem(nil, "test")}, 79 errorAssertion: require.Error, 80 spanAssertion: func(t require.TestingT, i interface{}, i2 ...interface{}) { 81 require.NotEmpty(t, i, i2...) 82 require.Len(t, i, spanCount*uploadCount, i2...) 83 }, 84 }, 85 { 86 name: "not implemented", 87 spans: make([]*otlp.ResourceSpans, spanCount), 88 client: mockClient{uploadError: trace.NotImplemented("test")}, 89 errorAssertion: require.NoError, 90 spanAssertion: func(t require.TestingT, i interface{}, i2 ...interface{}) { 91 require.NotEmpty(t, i, i2...) 92 require.Len(t, i, spanCount, i2...) 93 }, 94 }, 95 } 96 97 for _, tt := range cases { 98 t.Run(tt.name, func(t *testing.T) { 99 client := &Client{ 100 Client: &tt.client, 101 } 102 103 for i := 0; i < uploadCount; i++ { 104 tt.errorAssertion(t, client.UploadTraces(context.Background(), tt.spans)) 105 } 106 tt.spanAssertion(t, tt.client.spans) 107 }) 108 } 109 }