gf/net/gtrace/gtrace.go

181 lines
6.1 KiB
Go
Raw Normal View History

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package gtrace provides convenience wrapping functionality for tracing feature using OpenTelemetry.
package gtrace
import (
"context"
"os"
"strings"
2021-02-03 15:27:41 +08:00
"go.opentelemetry.io/otel"
2021-03-18 10:39:23 +08:00
"go.opentelemetry.io/otel/attribute"
2021-02-03 15:14:07 +08:00
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.18.0"
"go.opentelemetry.io/otel/trace"
2021-11-16 19:29:02 +08:00
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/command"
2021-11-16 19:29:02 +08:00
"github.com/gogf/gf/v2/net/gipv4"
2021-12-19 00:44:31 +08:00
"github.com/gogf/gf/v2/net/gtrace/internal/provider"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
2021-02-01 17:10:50 +08:00
)
const (
tracingCommonKeyIpIntranet = `ip.intranet`
tracingCommonKeyIpHostname = `hostname`
commandEnvKeyForMaxContentLogSize = "gf.gtrace.max.content.log.size" // To avoid too big tracing content.
commandEnvKeyForTracingInternal = "gf.gtrace.tracing.internal" // For detailed controlling for tracing content.
2021-01-27 19:15:14 +08:00
)
var (
2021-03-18 10:39:23 +08:00
intranetIps, _ = gipv4.GetIntranetIpArray()
intranetIpStr = strings.Join(intranetIps, ",")
hostname, _ = os.Hostname()
tracingInternal = true // tracingInternal enables tracing for internal type spans.
2021-11-13 23:23:55 +08:00
tracingMaxContentLogSize = 512 * 1024 // Max log size for request and response body, especially for HTTP/RPC request.
2021-02-03 15:14:07 +08:00
// defaultTextMapPropagator is the default propagator for context propagation between peers.
defaultTextMapPropagator = propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{},
propagation.Baggage{},
)
)
2021-02-03 15:27:41 +08:00
func init() {
2021-11-27 01:10:00 +08:00
tracingInternal = gconv.Bool(command.GetOptWithEnv(commandEnvKeyForTracingInternal, "true"))
if maxContentLogSize := gconv.Int(command.GetOptWithEnv(commandEnvKeyForMaxContentLogSize)); maxContentLogSize > 0 {
2021-03-18 10:39:23 +08:00
tracingMaxContentLogSize = maxContentLogSize
}
// Default trace provider.
otel.SetTracerProvider(provider.New())
2021-02-03 15:27:41 +08:00
CheckSetDefaultTextMapPropagator()
}
// IsUsingDefaultProvider checks and return if currently using default trace provider.
func IsUsingDefaultProvider() bool {
_, ok := otel.GetTracerProvider().(*provider.TracerProvider)
return ok
}
// IsTracingInternal returns whether tracing spans of internal components.
func IsTracingInternal() bool {
return tracingInternal
}
2021-03-18 10:39:23 +08:00
// MaxContentLogSize returns the max log size for request and response body, especially for HTTP/RPC request.
func MaxContentLogSize() int {
return tracingMaxContentLogSize
}
2021-01-27 19:15:14 +08:00
// CommonLabels returns common used attribute labels:
// ip.intranet, hostname.
2021-03-18 10:39:23 +08:00
func CommonLabels() []attribute.KeyValue {
return []attribute.KeyValue{
attribute.String(tracingCommonKeyIpHostname, hostname),
attribute.String(tracingCommonKeyIpIntranet, intranetIpStr),
semconv.HostName(hostname),
2021-01-27 19:15:14 +08:00
}
}
2021-02-03 15:27:41 +08:00
// CheckSetDefaultTextMapPropagator sets the default TextMapPropagator if it is not set previously.
func CheckSetDefaultTextMapPropagator() {
p := otel.GetTextMapPropagator()
if len(p.Fields()) == 0 {
otel.SetTextMapPropagator(GetDefaultTextMapPropagator())
}
}
// GetDefaultTextMapPropagator returns the default propagator for context propagation between peers.
func GetDefaultTextMapPropagator() propagation.TextMapPropagator {
2021-02-03 15:14:07 +08:00
return defaultTextMapPropagator
}
2021-11-13 23:23:55 +08:00
// GetTraceID retrieves and returns TraceId from context.
2021-01-27 13:28:12 +08:00
// It returns an empty string is tracing feature is not activated.
2021-11-13 23:23:55 +08:00
func GetTraceID(ctx context.Context) string {
2021-01-27 13:28:12 +08:00
if ctx == nil {
return ""
}
2021-11-13 23:23:55 +08:00
traceID := trace.SpanContextFromContext(ctx).TraceID()
if traceID.IsValid() {
return traceID.String()
2021-01-27 13:28:12 +08:00
}
return ""
2021-01-26 16:06:20 +08:00
}
2021-11-13 23:23:55 +08:00
// GetSpanID retrieves and returns SpanId from context.
2021-01-27 13:28:12 +08:00
// It returns an empty string is tracing feature is not activated.
2021-11-13 23:23:55 +08:00
func GetSpanID(ctx context.Context) string {
2021-01-27 13:28:12 +08:00
if ctx == nil {
return ""
}
2021-11-13 23:23:55 +08:00
spanID := trace.SpanContextFromContext(ctx).SpanID()
if spanID.IsValid() {
return spanID.String()
2021-01-27 13:28:12 +08:00
}
return ""
2021-01-26 16:06:20 +08:00
}
// SetBaggageValue is a convenient function for adding one key-value pair to baggage.
2021-03-18 10:39:23 +08:00
// Note that it uses attribute.Any to set the key-value pair.
func SetBaggageValue(ctx context.Context, key string, value interface{}) context.Context {
2021-01-28 13:11:09 +08:00
return NewBaggage(ctx).SetValue(key, value)
}
// SetBaggageMap is a convenient function for adding map key-value pairs to baggage.
2021-03-18 10:39:23 +08:00
// Note that it uses attribute.Any to set the key-value pair.
func SetBaggageMap(ctx context.Context, data map[string]interface{}) context.Context {
2021-01-28 13:11:09 +08:00
return NewBaggage(ctx).SetMap(data)
}
// GetBaggageMap retrieves and returns the baggage values as map.
func GetBaggageMap(ctx context.Context) *gmap.StrAnyMap {
return NewBaggage(ctx).GetMap()
}
// GetBaggageVar retrieves value and returns a *gvar.Var for specified key from baggage.
func GetBaggageVar(ctx context.Context, key string) *gvar.Var {
2021-01-28 13:11:09 +08:00
return NewBaggage(ctx).GetVar(key)
}
2021-12-17 00:06:38 +08:00
// WithUUID injects custom trace id with UUID into context to propagate.
func WithUUID(ctx context.Context, uuid string) (context.Context, error) {
return WithTraceID(ctx, gstr.Replace(uuid, "-", ""))
}
// WithTraceID injects custom trace id into context to propagate.
func WithTraceID(ctx context.Context, traceID string) (context.Context, error) {
generatedTraceID, err := trace.TraceIDFromHex(traceID)
if err != nil {
return ctx, gerror.WrapCodef(
gcode.CodeInvalidParameter,
err,
`invalid custom traceID "%s", a traceID string should be composed with [0-f] and fixed length 32`,
traceID,
)
}
2021-12-17 00:06:38 +08:00
sc := trace.SpanContextFromContext(ctx)
if !sc.HasTraceID() {
var span trace.Span
ctx, span = NewSpan(ctx, "gtrace.WithTraceID")
2021-12-17 00:06:38 +08:00
defer span.End()
sc = trace.SpanContextFromContext(ctx)
}
ctx = trace.ContextWithRemoteSpanContext(ctx, trace.NewSpanContext(trace.SpanContextConfig{
TraceID: generatedTraceID,
2021-12-17 00:06:38 +08:00
SpanID: sc.SpanID(),
TraceFlags: sc.TraceFlags(),
TraceState: sc.TraceState(),
Remote: sc.IsRemote(),
}))
return ctx, nil
2021-12-17 00:06:38 +08:00
}