gf/net/gtrace/gtrace.go

157 lines
5.5 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"
"github.com/gogf/gf/v2/internal/intlog"
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.7.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/internal/command"
2021-11-16 19:29:02 +08:00
"github.com/gogf/gf/v2/net/gipv4"
"github.com/gogf/gf/v2/util/gconv"
2021-02-01 17:10:50 +08:00
)
const (
tracingCommonKeyIpIntranet = `ip.intranet`
tracingCommonKeyIpHostname = `hostname`
commandEnvKeyForTraceEnabled = "gf.trace.enabled" // Main switch for tracing feature.
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()
traceEnabled = false // traceEnabled enables tracing feature for all.
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() {
traceEnabled = gconv.Bool(command.GetOptWithEnv(commandEnvKeyForTraceEnabled, "false"))
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
}
2021-02-03 15:27:41 +08:00
CheckSetDefaultTextMapPropagator()
intlog.Printf(context.TODO(), `traceEnabled initialized as: %v`, traceEnabled)
2021-02-03 15:27:41 +08:00
}
// SetEnabled enables or disables the tracing feature.
func SetEnabled(enabled bool) {
traceEnabled = enabled
intlog.Printf(context.TODO(), `traceEnabled SetEnabled: %v`, enabled)
}
// IsEnabled checks and returns if tracing feature is configured enabled.
func IsEnabled() bool {
return traceEnabled
}
// IsActivated checks given context and returns if tracing feature is actually activated in this context.
func IsActivated(ctx context.Context) bool {
return GetTraceID(ctx) != ""
}
// 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.HostNameKey.String(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)
}