github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/experimental/tracer/belt.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 "time" 17 18 "github.com/facebookincubator/go-belt" 19 ) 20 21 // Default is the (overridable) function which returns returns the default tracer. 22 // 23 // It is used by FromBelt and FromCtx functions if a Tracer is not set in the Belt. 24 var Default = func() Tracer { 25 return noopTracer{} 26 } 27 28 // StartChildSpanFromBelt returns a child span given an Belt. The parent Span is extracted 29 // from the Belt. If one is not set, then a `nil` parent is set in the new Span. 30 // 31 // The Span and an Belt which includes this Span are returned. 32 func StartChildSpanFromBelt(belt *belt.Belt, name string, options ...SpanOption) (Span, *belt.Belt) { 33 return FromBelt(belt).StartChildWithBelt(belt, name, options...) 34 } 35 36 // StartSpanFromBelt returns a span (without a parent) given an Belt. 37 // 38 // The Span and an Belt which includes this Span are returned. 39 func StartSpanFromBelt(belt *belt.Belt, name string, options ...SpanOption) (Span, *belt.Belt) { 40 return FromBelt(belt).StartWithBelt(belt, name, options...) 41 } 42 43 // SpanFromBelt extracts a Span from a Belt (for example, previously derived using StartChildSpanFromBelt, 44 // StartChildSpanFromCtx or BeltWithSpan). 45 func SpanFromBelt(belt *belt.Belt) Span { 46 loggerIface := belt.Artifacts().GetByID(ArtifactIDSpan) 47 if loggerIface == nil { 48 return NewNoopSpan("", nil, time.Time{}) 49 } 50 return loggerIface.(Span) 51 } 52 53 // BeltWithSpan returns a Belt derivative with the specified Span. 54 func BeltWithSpan(belt *belt.Belt, span Span) *belt.Belt { 55 return belt.WithArtifact(ArtifactIDSpan, span) 56 } 57 58 // FromBelt returns a Tracer given a Belt. 59 func FromBelt(belt *belt.Belt) Tracer { 60 loggerIface := belt.Tools().GetByID(ToolID) 61 if loggerIface == nil { 62 return Default() 63 } 64 return loggerIface.(Tracer) 65 } 66 67 // BeltWithTracer returns a Belt derivative with the specified Tracer. 68 func BeltWithTracer(belt *belt.Belt, tracer Tracer) *belt.Belt { 69 return belt.WithTool(ToolID, tracer) 70 }