mirror of
https://gitee.com/johng/gf
synced 2026-07-03 03:39:35 +08:00
improve package gtrace
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
||||
75
net/gtrace/gtrace_baggage.go
Normal file
75
net/gtrace/gtrace_baggage.go
Normal file
@ -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())
|
||||
}
|
||||
24
net/gtrace/gtrace_span.go
Normal file
24
net/gtrace/gtrace_span.go
Normal file
@ -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,
|
||||
}
|
||||
}
|
||||
27
net/gtrace/gtrace_tracer.go
Normal file
27
net/gtrace/gtrace_tracer.go
Normal file
@ -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),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user