mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
improve tracing feature
This commit is contained in:
@ -11,6 +11,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gogf/gf"
|
||||
"github.com/gogf/gf/net/gtrace"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/label"
|
||||
@ -19,11 +20,7 @@ import (
|
||||
|
||||
// addSqlToTracing adds sql information to tracer if it's enabled.
|
||||
func (c *Core) addSqlToTracing(ctx context.Context, sql *Sql) {
|
||||
if ctx == nil {
|
||||
return
|
||||
}
|
||||
spanCtx := trace.SpanContextFromContext(ctx)
|
||||
if traceId := spanCtx.TraceID; !traceId.IsValid() {
|
||||
if gtrace.IsActivated(ctx) {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@ -13,6 +13,7 @@ import (
|
||||
"github.com/gogf/gf"
|
||||
"github.com/gogf/gf/container/gvar"
|
||||
"github.com/gogf/gf/internal/json"
|
||||
"github.com/gogf/gf/net/gtrace"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"github.com/gomodule/redigo/redis"
|
||||
@ -65,11 +66,7 @@ func (c *Conn) do(timeout time.Duration, commandName string, args ...interface{}
|
||||
timestampMilli2 := gtime.TimestampMilli()
|
||||
|
||||
// Tracing.
|
||||
if c.ctx == nil {
|
||||
return
|
||||
}
|
||||
spanCtx := trace.SpanContextFromContext(c.ctx)
|
||||
if traceId := spanCtx.TraceID; !traceId.IsValid() {
|
||||
if gtrace.IsActivated(c.ctx) {
|
||||
return
|
||||
}
|
||||
tr := otel.GetTracerProvider().Tracer(
|
||||
|
||||
@ -16,6 +16,11 @@ import (
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
// IsActivated checks and returns if tracing feature is activated.
|
||||
func IsActivated(ctx context.Context) bool {
|
||||
return GetTraceId(ctx) != ""
|
||||
}
|
||||
|
||||
// Tracer is a short function for retrieve Tracer.
|
||||
func Tracer(name ...string) trace.Tracer {
|
||||
tracerName := ""
|
||||
@ -26,24 +31,46 @@ func Tracer(name ...string) trace.Tracer {
|
||||
}
|
||||
|
||||
// GetTraceId retrieves and returns TraceId from context.
|
||||
// It returns an empty string is tracing feature is not activated.
|
||||
func GetTraceId(ctx context.Context) string {
|
||||
return trace.SpanContextFromContext(ctx).TraceID.String()
|
||||
if ctx == nil {
|
||||
return ""
|
||||
}
|
||||
traceId := trace.SpanContextFromContext(ctx).TraceID
|
||||
if traceId.IsValid() {
|
||||
return traceId.String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetSpanId retrieves and returns SpanId from context.
|
||||
// It returns an empty string is tracing feature is not activated.
|
||||
func GetSpanId(ctx context.Context) string {
|
||||
return trace.SpanContextFromContext(ctx).SpanID.String()
|
||||
if ctx == nil {
|
||||
return ""
|
||||
}
|
||||
spanId := trace.SpanContextFromContext(ctx).SpanID
|
||||
if spanId.IsValid() {
|
||||
return spanId.String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// SetBaggageValue is a convenient function for adding one key-value pair to baggage.
|
||||
// Note that it uses label.Any to set the key-value pair.
|
||||
func SetBaggageValue(ctx context.Context, key string, value interface{}) context.Context {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
return baggage.ContextWithValues(ctx, label.Any(key, value))
|
||||
}
|
||||
|
||||
// SetBaggageMap is a convenient function for adding map key-value pairs to baggage.
|
||||
// Note that it uses label.Any to set the key-value pair.
|
||||
func SetBaggageMap(ctx context.Context, data map[string]interface{}) context.Context {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
pairs := make([]label.KeyValue, 0)
|
||||
for k, v := range data {
|
||||
pairs = append(pairs, label.Any(k, v))
|
||||
@ -53,6 +80,9 @@ func SetBaggageMap(ctx context.Context, data map[string]interface{}) context.Con
|
||||
|
||||
// GetBaggageVar retrieves value and returns a *gvar.Var for specified key from baggage.
|
||||
func GetBaggageVar(ctx context.Context, key string) *gvar.Var {
|
||||
if ctx == nil {
|
||||
return gvar.New(nil)
|
||||
}
|
||||
value := baggage.Value(ctx, label.Key(key))
|
||||
return gvar.New(value.AsInterface())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user