github.com/gogf/gf/v2@v2.7.4/net/gtrace/gtrace.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 // Package gtrace provides convenience wrapping functionality for tracing feature using OpenTelemetry. 8 package gtrace 9 10 import ( 11 "context" 12 "os" 13 "strings" 14 15 "go.opentelemetry.io/otel" 16 "go.opentelemetry.io/otel/attribute" 17 "go.opentelemetry.io/otel/propagation" 18 semconv "go.opentelemetry.io/otel/semconv/v1.18.0" 19 "go.opentelemetry.io/otel/trace" 20 21 "github.com/gogf/gf/v2/container/gmap" 22 "github.com/gogf/gf/v2/container/gvar" 23 "github.com/gogf/gf/v2/errors/gcode" 24 "github.com/gogf/gf/v2/errors/gerror" 25 "github.com/gogf/gf/v2/internal/command" 26 "github.com/gogf/gf/v2/net/gipv4" 27 "github.com/gogf/gf/v2/net/gtrace/internal/provider" 28 "github.com/gogf/gf/v2/text/gstr" 29 "github.com/gogf/gf/v2/util/gconv" 30 ) 31 32 const ( 33 tracingCommonKeyIpIntranet = `ip.intranet` 34 tracingCommonKeyIpHostname = `hostname` 35 commandEnvKeyForMaxContentLogSize = "gf.gtrace.max.content.log.size" // To avoid too big tracing content. 36 commandEnvKeyForTracingInternal = "gf.gtrace.tracing.internal" // For detailed controlling for tracing content. 37 ) 38 39 var ( 40 intranetIps, _ = gipv4.GetIntranetIpArray() 41 intranetIpStr = strings.Join(intranetIps, ",") 42 hostname, _ = os.Hostname() 43 tracingInternal = true // tracingInternal enables tracing for internal type spans. 44 tracingMaxContentLogSize = 512 * 1024 // Max log size for request and response body, especially for HTTP/RPC request. 45 // defaultTextMapPropagator is the default propagator for context propagation between peers. 46 defaultTextMapPropagator = propagation.NewCompositeTextMapPropagator( 47 propagation.TraceContext{}, 48 propagation.Baggage{}, 49 ) 50 ) 51 52 func init() { 53 tracingInternal = gconv.Bool(command.GetOptWithEnv(commandEnvKeyForTracingInternal, "true")) 54 if maxContentLogSize := gconv.Int(command.GetOptWithEnv(commandEnvKeyForMaxContentLogSize)); maxContentLogSize > 0 { 55 tracingMaxContentLogSize = maxContentLogSize 56 } 57 // Default trace provider. 58 otel.SetTracerProvider(provider.New()) 59 CheckSetDefaultTextMapPropagator() 60 } 61 62 // IsUsingDefaultProvider checks and return if currently using default trace provider. 63 func IsUsingDefaultProvider() bool { 64 _, ok := otel.GetTracerProvider().(*provider.TracerProvider) 65 return ok 66 } 67 68 // IsTracingInternal returns whether tracing spans of internal components. 69 func IsTracingInternal() bool { 70 return tracingInternal 71 } 72 73 // MaxContentLogSize returns the max log size for request and response body, especially for HTTP/RPC request. 74 func MaxContentLogSize() int { 75 return tracingMaxContentLogSize 76 } 77 78 // CommonLabels returns common used attribute labels: 79 // ip.intranet, hostname. 80 func CommonLabels() []attribute.KeyValue { 81 return []attribute.KeyValue{ 82 attribute.String(tracingCommonKeyIpHostname, hostname), 83 attribute.String(tracingCommonKeyIpIntranet, intranetIpStr), 84 semconv.HostName(hostname), 85 } 86 } 87 88 // CheckSetDefaultTextMapPropagator sets the default TextMapPropagator if it is not set previously. 89 func CheckSetDefaultTextMapPropagator() { 90 p := otel.GetTextMapPropagator() 91 if len(p.Fields()) == 0 { 92 otel.SetTextMapPropagator(GetDefaultTextMapPropagator()) 93 } 94 } 95 96 // GetDefaultTextMapPropagator returns the default propagator for context propagation between peers. 97 func GetDefaultTextMapPropagator() propagation.TextMapPropagator { 98 return defaultTextMapPropagator 99 } 100 101 // GetTraceID retrieves and returns TraceId from context. 102 // It returns an empty string is tracing feature is not activated. 103 func GetTraceID(ctx context.Context) string { 104 if ctx == nil { 105 return "" 106 } 107 traceID := trace.SpanContextFromContext(ctx).TraceID() 108 if traceID.IsValid() { 109 return traceID.String() 110 } 111 return "" 112 } 113 114 // GetSpanID retrieves and returns SpanId from context. 115 // It returns an empty string is tracing feature is not activated. 116 func GetSpanID(ctx context.Context) string { 117 if ctx == nil { 118 return "" 119 } 120 spanID := trace.SpanContextFromContext(ctx).SpanID() 121 if spanID.IsValid() { 122 return spanID.String() 123 } 124 return "" 125 } 126 127 // SetBaggageValue is a convenient function for adding one key-value pair to baggage. 128 // Note that it uses attribute.Any to set the key-value pair. 129 func SetBaggageValue(ctx context.Context, key string, value interface{}) context.Context { 130 return NewBaggage(ctx).SetValue(key, value) 131 } 132 133 // SetBaggageMap is a convenient function for adding map key-value pairs to baggage. 134 // Note that it uses attribute.Any to set the key-value pair. 135 func SetBaggageMap(ctx context.Context, data map[string]interface{}) context.Context { 136 return NewBaggage(ctx).SetMap(data) 137 } 138 139 // GetBaggageMap retrieves and returns the baggage values as map. 140 func GetBaggageMap(ctx context.Context) *gmap.StrAnyMap { 141 return NewBaggage(ctx).GetMap() 142 } 143 144 // GetBaggageVar retrieves value and returns a *gvar.Var for specified key from baggage. 145 func GetBaggageVar(ctx context.Context, key string) *gvar.Var { 146 return NewBaggage(ctx).GetVar(key) 147 } 148 149 // WithUUID injects custom trace id with UUID into context to propagate. 150 func WithUUID(ctx context.Context, uuid string) (context.Context, error) { 151 return WithTraceID(ctx, gstr.Replace(uuid, "-", "")) 152 } 153 154 // WithTraceID injects custom trace id into context to propagate. 155 func WithTraceID(ctx context.Context, traceID string) (context.Context, error) { 156 generatedTraceID, err := trace.TraceIDFromHex(traceID) 157 if err != nil { 158 return ctx, gerror.WrapCodef( 159 gcode.CodeInvalidParameter, 160 err, 161 `invalid custom traceID "%s", a traceID string should be composed with [0-f] and fixed length 32`, 162 traceID, 163 ) 164 } 165 sc := trace.SpanContextFromContext(ctx) 166 if !sc.HasTraceID() { 167 var span trace.Span 168 ctx, span = NewSpan(ctx, "gtrace.WithTraceID") 169 defer span.End() 170 sc = trace.SpanContextFromContext(ctx) 171 } 172 ctx = trace.ContextWithRemoteSpanContext(ctx, trace.NewSpanContext(trace.SpanContextConfig{ 173 TraceID: generatedTraceID, 174 SpanID: sc.SpanID(), 175 TraceFlags: sc.TraceFlags(), 176 TraceState: sc.TraceState(), 177 Remote: sc.IsRemote(), 178 })) 179 return ctx, nil 180 }