github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/debug/trace/trace.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); 2 // you may not use this file except in compliance with the License. 3 // You may obtain a copy of the License at 4 // 5 // https://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, 9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 // See the License for the specific language governing permissions and 11 // limitations under the License. 12 // 13 // Original source: github.com/micro/go-micro/v3/debug/trace/trace.go 14 15 // Package trace provides an interface for distributed tracing 16 package trace 17 18 import ( 19 "context" 20 "time" 21 22 "github.com/tickoalcantara12/micro/v3/service/context/metadata" 23 ) 24 25 // Tracer is an interface for distributed tracing 26 type Tracer interface { 27 // Start a trace 28 Start(ctx context.Context, name string) (context.Context, *Span) 29 // Finish the trace 30 Finish(*Span) error 31 // Read the traces 32 Read(...ReadOption) ([]*Span, error) 33 } 34 35 // SpanType describe the nature of the trace span 36 type SpanType int 37 38 const ( 39 // SpanTypeRequestInbound is a span created when serving a request 40 SpanTypeRequestInbound SpanType = iota 41 // SpanTypeRequestOutbound is a span created when making a service call 42 SpanTypeRequestOutbound 43 ) 44 45 // Span is used to record an entry 46 type Span struct { 47 // Id of the trace 48 Trace string 49 // name of the span 50 Name string 51 // id of the span 52 Id string 53 // parent span id 54 Parent string 55 // Start time 56 Started time.Time 57 // Duration in nano seconds 58 Duration time.Duration 59 // associated data 60 Metadata map[string]string 61 // Type 62 Type SpanType 63 } 64 65 const ( 66 traceIDKey = "Micro-Trace-Id" 67 spanIDKey = "Micro-Span-Id" 68 ) 69 70 // FromContext returns a span from context 71 func FromContext(ctx context.Context) (traceID string, parentSpanID string, isFound bool) { 72 traceID, traceOk := metadata.Get(ctx, traceIDKey) 73 microID, microOk := metadata.Get(ctx, "Micro-Id") 74 if !traceOk && !microOk { 75 isFound = false 76 return 77 } 78 if !traceOk { 79 traceID = microID 80 } 81 parentSpanID, ok := metadata.Get(ctx, spanIDKey) 82 return traceID, parentSpanID, ok 83 } 84 85 // ToContext saves the trace and span ids in the context 86 func ToContext(ctx context.Context, traceID, parentSpanID string) context.Context { 87 return metadata.MergeContext(ctx, map[string]string{ 88 traceIDKey: traceID, 89 spanIDKey: parentSpanID, 90 }, true) 91 } 92 93 var ( 94 DefaultTracer Tracer = new(noop) 95 ) 96 97 type noop struct{} 98 99 func (n *noop) Init(...Option) error { 100 return nil 101 } 102 103 func (n *noop) Start(ctx context.Context, name string) (context.Context, *Span) { 104 return nil, nil 105 } 106 107 func (n *noop) Finish(*Span) error { 108 return nil 109 } 110 111 func (n *noop) Read(...ReadOption) ([]*Span, error) { 112 return nil, nil 113 }