github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/experimental/tracer/tracer.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 14 15 import ( 16 "context" 17 18 "github.com/facebookincubator/go-belt" 19 "github.com/facebookincubator/go-belt/pkg/field" 20 ) 21 22 // Tracer is a generic abstract distributed tracing system client. 23 type Tracer interface { 24 belt.Tool 25 26 // Start creates a new Span, given its name, parent and options. 27 Start(name string, parent Span, options ...SpanOption) Span 28 29 // StartWithBelt creates a new root Span, given Belt, name and options. 30 // 31 // The returned Belt is a derivative of the provided one, with the Span added. 32 StartWithBelt(belt *belt.Belt, name string, options ...SpanOption) (Span, *belt.Belt) 33 34 // StartChildWithBelt creates a new child Span, given Belt, name and options. 35 // The parent is extracted from the Belt. If one is not set in there then it is 36 // an equivalent of StartWithBelt (a nil parent is used). 37 // 38 // The returned Belt is a derivative of the provided one, with the Span added. 39 StartChildWithBelt(belt *belt.Belt, name string, options ...SpanOption) (Span, *belt.Belt) 40 41 // StartWithCtx creates a new root Span, given Context, name and options. 42 // 43 // The returned Context is a derivative of the provided one, with the Span added. 44 // Some implementations also injects a span structure with a specific key to the context. 45 StartWithCtx(ctx context.Context, name string, options ...SpanOption) (Span, context.Context) 46 47 // StartChildWithCtx creates a new child Span, given Context, name and options. 48 // The parent is extracted from the Belt from the Context. 49 // If one is not set in there then it is an equivalent of StartWithCtx (a nil parent is used). 50 // 51 // The returned Context is a derivative of the provided one, with the Span added. 52 // Some implementations also injects a span structure with a specific key to the context. 53 StartChildWithCtx(ctx context.Context, name string, options ...SpanOption) (Span, context.Context) 54 55 // WithPreHooks returns a Tracer which includes/appends pre-hooks from the arguments. 56 // 57 // PreHook is the same as "Hook", but executed on early stages of building a Span 58 // (before heavy computations). 59 // 60 // Special case: to reset hooks use `WithPreHooks()` (without any arguments). 61 WithPreHooks(...Hook) Tracer 62 63 // WithHooks returns a Tracer which includes/appends hooks from the arguments. 64 // 65 // See also description of "Hook". 66 // 67 // Special case: to reset hooks use `WithHooks()` (without any arguments). 68 WithHooks(...Hook) Tracer 69 } 70 71 // WithField adds a Tracer derivative with the field added. 72 func WithField(t Tracer, key field.Key, value field.Value) Tracer { 73 return t.WithContextFields(field.NewChainFromOne(key, value), 1).(Tracer) 74 }