github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/experimental/tracer/README.md (about) 1 # Disclaimer 2 3 This API is experimental and has no stability guarantees. 4 5 # Example 6 ```go 7 package tracer_test 8 9 import ( 10 "context" 11 12 "github.com/facebookincubator/go-belt" 13 "github.com/facebookincubator/go-belt/beltctx" 14 "github.com/facebookincubator/go-belt/tool/experimental/tracer" 15 "github.com/facebookincubator/go-belt/tool/experimental/tracer/implementation/logger" 16 "github.com/facebookincubator/go-belt/tool/experimental/tracer/implementation/zipkin" 17 ) 18 19 func Example() { 20 // easy to use: 21 t := logger.Default() 22 someFunction(1, t) 23 24 // implementation agnostic: 25 t = zipkin.Default() 26 someFunction(2, t) 27 28 // one may still reuse all the features of the backend Tracer: 29 t.(*zipkin.TracerImpl).ZipkinTracer.SetNoop(true) 30 31 // use go-belt to manage the Tracer 32 obs := belt.New() 33 obs = tracer.BeltWithTracer(obs, t) 34 someBeltyFunction(3, obs) 35 36 // use context to manage the Tracer 37 ctx := context.Background() 38 ctx = tracer.CtxWithTracer(ctx, t) 39 someContextyFunction(ctx, 4) 40 41 // use a singletony Tracer: 42 tracer.Default = func() tracer.Tracer { 43 return t 44 } 45 yetOneMoreFunction(5) 46 } 47 48 func someFunction(arg int, t tracer.Tracer) { 49 // experience close to logrus/zap: 50 t = tracer.WithField(t, "arg", arg) 51 anotherFunction(t) 52 } 53 54 func anotherFunction(t tracer.Tracer) { 55 span := t.Start("hello", nil) 56 defer span.Finish() 57 // ..do something long here.. 58 oneMoreFunction(t, span) 59 } 60 61 func oneMoreFunction(t tracer.Tracer, parentSpan tracer.Span) { 62 span := t.Start("child", parentSpan) 63 defer span.Finish() 64 // ..do something meaningful here.. 65 } 66 67 func someBeltyFunction(arg int, obs *belt.Belt) { 68 obs = obs.WithField("arg", arg) 69 anotherBeltyFunction(obs) 70 } 71 72 func anotherBeltyFunction(obs *belt.Belt) { 73 span, obs := tracer.StartChildSpanFromBelt(obs, "hello") 74 defer span.Finish() 75 // ..do something long here.. 76 oneMoreBeltyFunction(obs) 77 } 78 79 func oneMoreBeltyFunction(obs *belt.Belt) { 80 span, obs := tracer.StartChildSpanFromBelt(obs, "child") 81 defer span.Finish() 82 // ..do something meaningful here.. 83 _ = obs 84 } 85 86 func someContextyFunction(ctx context.Context, arg int) { 87 ctx = beltctx.WithField(ctx, "arg", arg) 88 anotherContextyFunction(ctx) 89 } 90 91 func anotherContextyFunction(ctx context.Context) { 92 span, ctx := tracer.StartChildSpanFromCtx(ctx, "hello") 93 defer span.Finish() 94 // ..do something long here.. 95 oneMoreContextyFunction(ctx) 96 } 97 98 func oneMoreContextyFunction(ctx context.Context) { 99 span, ctx := tracer.StartChildSpanFromCtx(ctx, "child") 100 defer span.Finish() 101 // ..do something meaningful here.. 102 _ = ctx 103 } 104 105 func yetOneMoreFunction(arg int) { 106 t := tracer.Default() 107 t = tracer.WithField(t, "arg", arg) 108 span := t.Start("hello", nil) 109 defer span.Finish() 110 } 111 ``` 112 113 # Interface 114 115 ```go 116 type Tracer interface { 117 belt.Tool 118 119 // Start creates a new Span, given its name, parent and options. 120 Start(name string, parent Span, options ...SpanOption) Span 121 122 // StartWithBelt creates a new root Span, given Belt, name and options. 123 // 124 // The returned Belt is a derivative of the provided one, with the Span added. 125 StartWithBelt(belt *belt.Belt, name string, options ...SpanOption) (Span, *belt.Belt) 126 127 // StartChildWithBelt creates a new child Span, given Belt, name and options. 128 // The parent is extracted from the Belt. If one is not set in there then it is 129 // an equivalent of StartWithBelt (a nil parent is used). 130 // 131 // The returned Belt is a derivative of the provided one, with the Span added. 132 StartChildWithBelt(belt *belt.Belt, name string, options ...SpanOption) (Span, *belt.Belt) 133 134 // StartWithCtx creates a new root Span, given Context, name and options. 135 // 136 // The returned Context is a derivative of the provided one, with the Span added. 137 // Some implementations also injects a span structure with a specific key to the context. 138 StartWithCtx(ctx context.Context, name string, options ...SpanOption) (Span, context.Context) 139 140 // StartChildWithCtx creates a new child Span, given Context, name and options. 141 // The parent is extracted from the Belt from the Context. 142 // If one is not set in there then it is an equivalent of StartWithCtx (a nil parent is used). 143 // 144 // The returned Context is a derivative of the provided one, with the Span added. 145 // Some implementations also injects a span structure with a specific key to the context. 146 StartChildWithCtx(ctx context.Context, name string, options ...SpanOption) (Span, context.Context) 147 148 // WithPreHooks returns a Tracer which includes/appends pre-hooks from the arguments. 149 // 150 // PreHook is the same as "Hook", but executed on early stages of building a Span 151 // (before heavy computations). 152 // 153 // Special case: to reset hooks use `WithPreHooks()` (without any arguments). 154 WithPreHooks(...Hook) Tracer 155 156 // WithHooks returns a Tracer which includes/appends hooks from the arguments. 157 // 158 // See also description of "Hook". 159 // 160 // Special case: to reset hooks use `WithHooks()` (without any arguments). 161 WithHooks(...Hook) Tracer 162 163 // Flush forces to flush all buffers. 164 Flush() 165 } 166 ``` 167