Skip to content

Commit

Permalink
improve package gtrace
Browse files Browse the repository at this point in the history
  • Loading branch information
jianchenma committed Jan 28, 2021
1 parent a2b322a commit 2451b40
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 30 deletions.
46 changes: 16 additions & 30 deletions net/gtrace/gtrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 {
Expand Down Expand Up @@ -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 changes: 75 additions & 0 deletions net/gtrace/gtrace_baggage.go
Original file line number Diff line number Diff line change
@@ -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 changes: 24 additions & 0 deletions net/gtrace/gtrace_span.go
Original file line number Diff line number Diff line change
@@ -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 changes: 27 additions & 0 deletions net/gtrace/gtrace_tracer.go
Original file line number Diff line number Diff line change
@@ -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),
}
}

0 comments on commit 2451b40

Please sign in to comment.