github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/experimental/tracer/tests/zipkin_test.go (about) 1 // Copyright 2022 Meta Platforms, Inc. and affiliates. 2 // 3 // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 4 // 5 // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 6 // 7 // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 8 // 9 // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 10 // 11 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 12 13 package tests 14 15 import ( 16 "fmt" 17 "time" 18 19 "github.com/facebookincubator/go-belt/tool/experimental/tracer" 20 zipkinimpl "github.com/facebookincubator/go-belt/tool/experimental/tracer/implementation/zipkin" 21 "github.com/openzipkin/zipkin-go" 22 zipkinmodel "github.com/openzipkin/zipkin-go/model" 23 zipkinreporter "github.com/openzipkin/zipkin-go/reporter" 24 "github.com/xaionaro-go/unsafetools" 25 ) 26 27 type dummyZipkinReporter struct { 28 onSend func(tracer.Span) 29 tracer *zipkinimpl.TracerImpl 30 } 31 32 type dummyZipkinSpan struct { 33 data zipkinmodel.SpanModel 34 reporter *dummyZipkinReporter 35 } 36 37 func (span *dummyZipkinSpan) Context() zipkinmodel.SpanContext { 38 return span.data.SpanContext 39 } 40 func (span *dummyZipkinSpan) SetName(name string) { 41 span.data.Name = name 42 } 43 func (span *dummyZipkinSpan) SetRemoteEndpoint(*zipkinmodel.Endpoint) { 44 panic("not implemented") 45 } 46 func (span *dummyZipkinSpan) Annotate(time.Time, string) { 47 panic("not implemented") 48 } 49 func (span *dummyZipkinSpan) Tag(key, value string) { 50 span.data.Tags[key] = value 51 } 52 func (span *dummyZipkinSpan) Finish() { 53 panic("not implemented") 54 } 55 func (span *dummyZipkinSpan) FinishedWithDuration(duration time.Duration) { 56 panic("not implemented") 57 } 58 func (span *dummyZipkinSpan) Flush() { 59 panic("not implemented") 60 } 61 62 func (r *dummyZipkinReporter) Send(span zipkinmodel.SpanModel) { 63 spanImpl := &zipkinimpl.SpanImpl{} 64 *unsafetools.FieldByName(spanImpl, "name").(*string) = span.Name 65 *unsafetools.FieldByName(spanImpl, "zipkinSpan").(*zipkin.Span) = &dummyZipkinSpan{data: span, reporter: r} 66 *unsafetools.FieldByName(spanImpl, "tracer").(**zipkinimpl.TracerImpl) = r.tracer 67 r.onSend(spanImpl) 68 } 69 70 func (r *dummyZipkinReporter) OnSend(onSend func(tracer.Span)) { 71 r.onSend = onSend 72 } 73 74 func (r *dummyZipkinReporter) Close() error { 75 return nil 76 } 77 78 var _ zipkinreporter.Reporter = (*dummyZipkinReporter)(nil) 79 80 func init() { 81 implementations = append(implementations, implementationCase{ 82 Name: "zipkin", 83 Factory: func() (tracer.Tracer, DummyReporter) { 84 var reporter dummyZipkinReporter 85 86 zipkinTracer, err := zipkin.NewTracer(&reporter) 87 if err != nil { 88 panic(fmt.Errorf("unable to create a zipkin tracer: %v", err)) 89 } 90 91 tracer := zipkinimpl.New(zipkinTracer) 92 return tracer, &reporter 93 }, 94 }) 95 }