@@ -6,37 +6,50 @@ import (
6
6
"fmt"
7
7
"math/rand"
8
8
"strconv"
9
-
10
- "github.com/cabify/go-logging/internal"
11
9
)
12
10
11
+ // BaggageContextKey is the key to be used for the baggage map[string]interface{} in
12
+ // context.*Value.
13
+ // It's intentionally a public string type, so the deprecation of this library is easier, since a public
14
+ // string type can be defined in another package
15
+ var BaggageContextKey interface {} = "logctx-data-map-string-interface"
16
+
13
17
const loggerRequestIDLength = 7
14
18
19
+ // Baggage is a handy helper to extract the baggage from the context, probably to be used with another logger
20
+ func Baggage (ctx context.Context ) map [string ]interface {} {
21
+ if baggage , ok := ctx .Value (BaggageContextKey ).(map [string ]interface {}); ok {
22
+ return baggage
23
+ }
24
+
25
+ return map [string ]interface {}{}
26
+ }
27
+
15
28
// WithBaggageValue returns a context with the added key value pair in the baggage store.
16
29
func WithBaggageValue (ctx context.Context , key , value string ) context.Context {
17
- oldBaggage , ok := ctx .Value (internal . BaggageContextKey ).(internal. Baggage )
30
+ oldBaggage , ok := ctx .Value (BaggageContextKey ).(map [ string ] interface {} )
18
31
19
32
if ! ok {
20
- return context .WithValue (ctx , internal . BaggageContextKey , internal. Baggage {key : value })
33
+ return context .WithValue (ctx , BaggageContextKey , map [ string ] interface {} {key : value })
21
34
}
22
35
23
- newBaggage := make (internal. Baggage , len (oldBaggage )+ 1 )
36
+ newBaggage := make (map [ string ] interface {} , len (oldBaggage )+ 1 )
24
37
for oldKey , oldValue := range oldBaggage {
25
38
newBaggage [oldKey ] = oldValue
26
39
}
27
40
newBaggage [key ] = value
28
41
29
- return context .WithValue (ctx , internal . BaggageContextKey , newBaggage )
42
+ return context .WithValue (ctx , BaggageContextKey , newBaggage )
30
43
}
31
44
32
45
// WithBaggageValues returns a context with all key value pairs added to the baggage store.
33
46
func WithBaggageValues (ctx context.Context , keyValue map [string ]string ) context.Context {
34
- oldBaggage , ok := ctx .Value (internal . BaggageContextKey ).(internal. Baggage )
47
+ oldBaggage , ok := ctx .Value (BaggageContextKey ).(map [ string ] interface {} )
35
48
if ! ok {
36
- return context .WithValue (ctx , internal . BaggageContextKey , internal . Baggage (keyValue ))
49
+ return context .WithValue (ctx , BaggageContextKey , toMapStringInterface (keyValue ))
37
50
}
38
51
39
- newBaggage := make (internal. Baggage , len (oldBaggage )+ len (keyValue ))
52
+ newBaggage := make (map [ string ] interface {} , len (oldBaggage )+ len (keyValue ))
40
53
for oldKey , oldValue := range oldBaggage {
41
54
newBaggage [oldKey ] = oldValue
42
55
}
@@ -45,16 +58,16 @@ func WithBaggageValues(ctx context.Context, keyValue map[string]string) context.
45
58
newBaggage [newKey ] = newValue
46
59
}
47
60
48
- return context .WithValue (ctx , internal . BaggageContextKey , newBaggage )
61
+ return context .WithValue (ctx , BaggageContextKey , newBaggage )
49
62
}
50
63
51
64
// NewContextWithBaggageFrom returns a new context with baggage values obtained from another context
52
65
func NewContextWithBaggageFrom (ctx context.Context ) context.Context {
53
- oldBaggage , ok := ctx .Value (internal . BaggageContextKey ).(internal. Baggage )
66
+ oldBaggage , ok := ctx .Value (BaggageContextKey ).(map [ string ] interface {} )
54
67
if ! ok {
55
68
return context .Background ()
56
69
}
57
- return context .WithValue (context .Background (), internal . BaggageContextKey , oldBaggage )
70
+ return context .WithValue (context .Background (), BaggageContextKey , oldBaggage )
58
71
}
59
72
60
73
// NewContextFromWithValue creates a new context with baggage values from ctx plus the provided one
@@ -71,3 +84,11 @@ func NewID() string {
71
84
encoded := md5 .Sum ([]byte (strconv .FormatInt (data , 16 )))
72
85
return fmt .Sprintf ("%x" , encoded )[:loggerRequestIDLength ]
73
86
}
87
+
88
+ func toMapStringInterface (original map [string ]string ) map [string ]interface {} {
89
+ m := make (map [string ]interface {}, len (original ))
90
+ for k , v := range original {
91
+ m [k ] = v
92
+ }
93
+ return m
94
+ }
0 commit comments