github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/experimental/tracer/examples/doc_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 tracer_test 14 15 import ( 16 "context" 17 18 "github.com/facebookincubator/go-belt" 19 "github.com/facebookincubator/go-belt/beltctx" 20 "github.com/facebookincubator/go-belt/tool/experimental/tracer" 21 "github.com/facebookincubator/go-belt/tool/experimental/tracer/implementation/logger" 22 "github.com/facebookincubator/go-belt/tool/experimental/tracer/implementation/zipkin" 23 ) 24 25 func Example() { 26 // easy to use: 27 t := logger.Default() 28 someFunction(1, t) 29 30 // implementation agnostic: 31 t = zipkin.Default() 32 someFunction(2, t) 33 34 // one may still reuse all the features of the backend Tracer: 35 t.(*zipkin.TracerImpl).ZipkinTracer.SetNoop(true) 36 37 // use go-belt to manage the Tracer 38 obs := belt.New() 39 obs = tracer.BeltWithTracer(obs, t) 40 someBeltyFunction(3, obs) 41 42 // use context to manage the Tracer 43 ctx := context.Background() 44 ctx = tracer.CtxWithTracer(ctx, t) 45 someContextyFunction(ctx, 4) 46 47 // use a singletony Tracer: 48 tracer.Default = func() tracer.Tracer { 49 return t 50 } 51 yetOneMoreFunction(5) 52 } 53 54 func someFunction(arg int, t tracer.Tracer) { 55 // experience close to logrus/zap: 56 t = tracer.WithField(t, "arg", arg) 57 anotherFunction(t) 58 } 59 60 func anotherFunction(t tracer.Tracer) { 61 span := t.Start("hello", nil) 62 defer span.Finish() 63 // ..do something long here.. 64 oneMoreFunction(t, span) 65 } 66 67 func oneMoreFunction(t tracer.Tracer, parentSpan tracer.Span) { 68 span := t.Start("child", parentSpan) 69 defer span.Finish() 70 // ..do something meaningful here.. 71 } 72 73 func someBeltyFunction(arg int, obs *belt.Belt) { 74 obs = obs.WithField("arg", arg) 75 anotherBeltyFunction(obs) 76 } 77 78 func anotherBeltyFunction(obs *belt.Belt) { 79 span, obs := tracer.StartChildSpanFromBelt(obs, "hello") 80 defer span.Finish() 81 // ..do something long here.. 82 oneMoreBeltyFunction(obs) 83 } 84 85 func oneMoreBeltyFunction(obs *belt.Belt) { 86 span, obs := tracer.StartChildSpanFromBelt(obs, "child") 87 defer span.Finish() 88 // ..do something meaningful here.. 89 _ = obs 90 } 91 92 func someContextyFunction(ctx context.Context, arg int) { 93 ctx = beltctx.WithField(ctx, "arg", arg) 94 anotherContextyFunction(ctx) 95 } 96 97 func anotherContextyFunction(ctx context.Context) { 98 span, ctx := tracer.StartChildSpanFromCtx(ctx, "hello") 99 defer span.Finish() 100 // ..do something long here.. 101 oneMoreContextyFunction(ctx) 102 } 103 104 func oneMoreContextyFunction(ctx context.Context) { 105 span, ctx := tracer.StartChildSpanFromCtx(ctx, "child") 106 defer span.Finish() 107 // ..do something meaningful here.. 108 _ = ctx 109 } 110 111 func yetOneMoreFunction(arg int) { 112 t := tracer.Default() 113 t = tracer.WithField(t, "arg", arg) 114 span := t.Start("hello", nil) 115 defer span.Finish() 116 }