@@ -10,43 +10,69 @@ var (
10
10
// ctxMarkerKey is the Context value marker used by *all* logging middleware.
11
11
// The logging middleware object must interf
12
12
ctxMarkerKey = & ctxMarker {}
13
+ // NoopTags is a trivial, minimum overhead implementation of Tags for which all operations are no-ops.
14
+ NoopTags = & noopTags {}
13
15
)
14
16
15
- // Tags is the struct used for storing request tags between Context calls.
16
- // This object is *not* thread safe, and should be handled only in the context of the request.
17
- type Tags struct {
17
+ // Tags is the interface used for storing request tags between Context calls.
18
+ // The default implementation is *not* thread safe, and should be handled only in the context of the request.
19
+ type Tags interface {
20
+ // Set sets the given key in the metadata tags.
21
+ Set (key string , value interface {}) Tags
22
+ // Has checks if the given key exists.
23
+ Has (key string ) bool
24
+ // Values returns a map of key to values.
25
+ // Do not modify the underlying map, please use Set instead.
26
+ Values () map [string ]interface {}
27
+ }
28
+
29
+ type mapTags struct {
18
30
values map [string ]interface {}
19
31
}
20
32
21
- // Set sets the given key in the metadata tags.
22
- func (t * Tags ) Set (key string , value interface {}) * Tags {
33
+ func (t * mapTags ) Set (key string , value interface {}) Tags {
23
34
t .values [key ] = value
24
35
return t
25
36
}
26
37
27
- // Has checks if the given key exists.
28
- func (t * Tags ) Has (key string ) bool {
38
+ func (t * mapTags ) Has (key string ) bool {
29
39
_ , ok := t .values [key ]
30
40
return ok
31
41
}
32
42
33
- // Values returns a map of key to values.
34
- // Do not modify the underlying map, please use Set instead.
35
- func (t * Tags ) Values () map [string ]interface {} {
43
+ func (t * mapTags ) Values () map [string ]interface {} {
36
44
return t .values
37
45
}
38
46
47
+ type noopTags struct {}
48
+
49
+ func (t * noopTags ) Set (key string , value interface {}) Tags {
50
+ return t
51
+ }
52
+
53
+ func (t * noopTags ) Has (key string ) bool {
54
+ return false
55
+ }
56
+
57
+ func (t * noopTags ) Values () map [string ]interface {} {
58
+ return nil
59
+ }
60
+
39
61
// Extracts returns a pre-existing Tags object in the Context.
40
62
// If the context wasn't set in a tag interceptor, a no-op Tag storage is returned that will *not* be propagated in context.
41
- func Extract (ctx context.Context ) * Tags {
42
- t , ok := ctx .Value (ctxMarkerKey ).(* Tags )
63
+ func Extract (ctx context.Context ) Tags {
64
+ t , ok := ctx .Value (ctxMarkerKey ).(Tags )
43
65
if ! ok {
44
- return & Tags { values : make ( map [ string ] interface {})}
66
+ return NoopTags
45
67
}
46
68
47
69
return t
48
70
}
49
71
50
- func setInContext (ctx context.Context , tags * Tags ) context.Context {
72
+ func setInContext (ctx context.Context , tags Tags ) context.Context {
51
73
return context .WithValue (ctx , ctxMarkerKey , tags )
52
74
}
75
+
76
+ func newTags () Tags {
77
+ return & mapTags {values : make (map [string ]interface {})}
78
+ }
0 commit comments