-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2b322a
commit 2451b40
Showing
4 changed files
with
142 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |