-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
gtrace_baggage.go
75 lines (66 loc) · 2.1 KB
/
gtrace_baggage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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/attribute"
"go.opentelemetry.io/otel/baggage"
)
// 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 attribute.Any to set the key-value pair.
func (b *Baggage) SetValue(key string, value interface{}) context.Context {
b.ctx = baggage.ContextWithValues(b.ctx, attribute.Any(key, value))
return b.ctx
}
// SetMap is a convenient function for adding map key-value pairs to baggage.
// Note that it uses attribute.Any to set the key-value pair.
func (b *Baggage) SetMap(data map[string]interface{}) context.Context {
pairs := make([]attribute.KeyValue, 0)
for k, v := range data {
pairs = append(pairs, attribute.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, attribute.Key(key))
return gvar.New(value.AsInterface())
}