github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/experimental/tracer/span.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 "github.com/facebookincubator/go-belt/pkg/field" 20 ) 21 22 // Span is a single time interval. 23 type Span interface { 24 // ID is the ID for the Span. It may have different types, depending 25 // on specific implementation of the Tracer. 26 // 27 // Not all tracing systems may support that. 28 ID() any 29 30 // TraceIDs are the set if unique IDs associated with the current context. 31 // 32 // See the description of belt.TraceIDs. 33 // 34 // Not all tracing systems may support that. 35 TraceIDs() belt.TraceIDs 36 37 // Name is the name of the Span. Usually it explains what happens within 38 // this span. 39 Name() string 40 41 // StartTS returns the timestamp of the beginning of the Span. 42 // 43 // Can be zero value if unknown. For example it could 44 // happen due sampling, or it could just be not supported 45 // byt a specific implementation. 46 StartTS() time.Time 47 48 // Fields is the set of structured data attached to the Span. 49 Fields() field.AbstractFields 50 51 // Parent is the parent Span. If there is no parent then an untyped nil is returned. 52 Parent() Span 53 54 // SetName sets the Name (see the description of method Name). 55 SetName(string) 56 57 // Annotate adds an event with the specified timestamp and description to the Span. 58 // 59 // Not all tracing systems may support that. 60 Annotate(ts time.Time, description string) 61 62 // SetField sets a structured field in the Span. 63 // 64 // Not all tracing systems may support that. 65 SetField(field.Key, field.Value) 66 67 // SetField set multiple structured fields at once in the Span. 68 // 69 // Not all tracing systems may support that. 70 SetFields(field.AbstractFields) 71 72 // Finish closes the Span using time.Now() as the ending timestamp and sends it. 73 Finish() 74 75 // FinishWithDuration closes the Span using startTS+duration as the ending timestamp and sends it. 76 FinishWithDuration(duration time.Duration) 77 78 // Flush forces to immediatelly empty all the buffers (send if something was delayed and so on). 79 Flush() 80 } 81 82 var _ belt.Artifact = (Span)(nil) 83 84 // Spans is a collection of Span-s. 85 type Spans []Span 86 87 // Earliest returns the Span with the lowest start timestamp. 88 func (s Spans) Earliest() Span { 89 if len(s) == 0 { 90 return nil 91 } 92 earliest := s[0] 93 for _, span := range s { 94 if span.StartTS().Before(earliest.StartTS()) { 95 earliest = span 96 } 97 } 98 return earliest 99 }