From 2451b40d3ebaad3dd028d4ab7fdc4dfa81448313 Mon Sep 17 00:00:00 2001 From: jianchenma Date: Thu, 28 Jan 2021 13:11:09 +0800 Subject: [PATCH] improve package gtrace --- net/gtrace/gtrace.go | 46 ++++++++-------------- net/gtrace/gtrace_baggage.go | 75 ++++++++++++++++++++++++++++++++++++ net/gtrace/gtrace_span.go | 24 ++++++++++++ net/gtrace/gtrace_tracer.go | 27 +++++++++++++ 4 files changed, 142 insertions(+), 30 deletions(-) create mode 100644 net/gtrace/gtrace_baggage.go create mode 100644 net/gtrace/gtrace_span.go create mode 100644 net/gtrace/gtrace_tracer.go diff --git a/net/gtrace/gtrace.go b/net/gtrace/gtrace.go index c7964afdf..460ce4df0 100644 --- a/net/gtrace/gtrace.go +++ b/net/gtrace/gtrace.go @@ -9,16 +9,20 @@ package gtrace import ( "context" + "github.com/gogf/gf/container/gmap" "github.com/gogf/gf/container/gvar" "github.com/gogf/gf/net/gipv4" "github.com/gogf/gf/text/gstr" - "go.opentelemetry.io/otel" - "go.opentelemetry.io/otel/baggage" "go.opentelemetry.io/otel/label" "go.opentelemetry.io/otel/trace" "os" ) +const ( + tracingCommonKeyIpIntranet = `ip.intranet` + tracingCommonKeyIpHostname = `hostname` +) + var ( intranetIps, _ = gipv4.GetIntranetIpArray() hostname, _ = os.Hostname() @@ -33,20 +37,11 @@ func IsActivated(ctx context.Context) bool { // ip.intranet, hostname. func CommonLabels() []label.KeyValue { return []label.KeyValue{ - label.String(`ip.intranet`, gstr.Join(intranetIps, ",")), - label.String(`hostname`, hostname), + label.String(tracingCommonKeyIpIntranet, gstr.Join(intranetIps, ",")), + label.String(tracingCommonKeyIpHostname, hostname), } } -// Tracer is a short function for retrieve Tracer. -func Tracer(name ...string) trace.Tracer { - tracerName := "" - if len(name) > 0 { - tracerName = name[0] - } - return otel.Tracer(tracerName) -} - // GetTraceId retrieves and returns TraceId from context. // It returns an empty string is tracing feature is not activated. func GetTraceId(ctx context.Context) string { @@ -76,30 +71,21 @@ func GetSpanId(ctx context.Context) string { // 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)) + return NewBaggage(ctx).SetValue(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)) - } - return baggage.ContextWithValues(ctx, pairs...) + 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 { - if ctx == nil { - return gvar.New(nil) - } - value := baggage.Value(ctx, label.Key(key)) - return gvar.New(value.AsInterface()) + return NewBaggage(ctx).GetVar(key) } diff --git a/net/gtrace/gtrace_baggage.go b/net/gtrace/gtrace_baggage.go new file mode 100644 index 000000000..f06aaa9c7 --- /dev/null +++ b/net/gtrace/gtrace_baggage.go @@ -0,0 +1,75 @@ +// 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 + +import ( + "context" + "github.com/gogf/gf/container/gmap" + "github.com/gogf/gf/container/gvar" + "go.opentelemetry.io/otel/baggage" + "go.opentelemetry.io/otel/label" +) + +// Baggage holds the data through all tracing spans. +type Baggage struct { + ctx context.Context +} + +// NewBaggage creates and returns a new Baggage object from given tracing context. +func NewBaggage(ctx context.Context) *Baggage { + if ctx == nil { + ctx = context.Background() + } + return &Baggage{ + ctx: ctx, + } +} + +// Ctx returns the context that Baggage holds. +func (b *Baggage) Ctx() context.Context { + return b.ctx +} + +// SetValue 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 (b *Baggage) SetValue(key string, value interface{}) context.Context { + b.ctx = baggage.ContextWithValues(b.ctx, label.Any(key, value)) + return b.ctx +} + +// SetMap 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 (b *Baggage) SetMap(data map[string]interface{}) context.Context { + pairs := make([]label.KeyValue, 0) + for k, v := range data { + pairs = append(pairs, label.Any(k, v)) + } + b.ctx = baggage.ContextWithValues(b.ctx, pairs...) + return b.ctx +} + +// GetMap retrieves and returns the baggage values as map. +func (b *Baggage) GetMap() *gmap.StrAnyMap { + m := gmap.NewStrAnyMap() + set := baggage.Set(b.ctx) + if length := set.Len(); length > 0 { + if length == 0 { + return m + } + inter := set.Iter() + for inter.Next() { + m.Set(string(inter.Label().Key), inter.Label().Value.AsInterface()) + } + } + return m +} + +// GetVar retrieves value and returns a *gvar.Var for specified key from baggage. +func (b *Baggage) GetVar(key string) *gvar.Var { + value := baggage.Value(b.ctx, label.Key(key)) + return gvar.New(value.AsInterface()) +} diff --git a/net/gtrace/gtrace_span.go b/net/gtrace/gtrace_span.go new file mode 100644 index 000000000..f9163a60f --- /dev/null +++ b/net/gtrace/gtrace_span.go @@ -0,0 +1,24 @@ +// 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 + +import ( + "context" + "go.opentelemetry.io/otel/trace" +) + +type Span struct { + trace.Span +} + +// NewSpan creates a span using default tracer. +func NewSpan(ctx context.Context, spanName string, opts ...trace.SpanOption) (context.Context, *Span) { + ctx, span := NewTracer().Start(ctx, spanName, opts...) + return ctx, &Span{ + Span: span, + } +} diff --git a/net/gtrace/gtrace_tracer.go b/net/gtrace/gtrace_tracer.go new file mode 100644 index 000000000..8394fcebf --- /dev/null +++ b/net/gtrace/gtrace_tracer.go @@ -0,0 +1,27 @@ +// 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 + +import ( + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/trace" +) + +type Tracer struct { + trace.Tracer +} + +// Tracer is a short function for retrieving Tracer. +func NewTracer(name ...string) *Tracer { + tracerName := "" + if len(name) > 0 { + tracerName = name[0] + } + return &Tracer{ + Tracer: otel.Tracer(tracerName), + } +}