Skip to content

Commit 505971d

Browse files
authored
fix(logging): nil pointer (#205)
* fix(logging): nil pointer * fix(logging): nil pointer
1 parent a4ee6b7 commit 505971d

3 files changed

Lines changed: 11 additions & 0 deletions

File tree

ctxmeta/ctxmeta.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ func (b *Baggage) Delete(key interface{}) (err error) {
159159

160160
// Slice returns a slice of key/value pairs in the order in which they were set.
161161
func (b *Baggage) Slice() []KeyVal {
162+
if b == nil {
163+
return nil
164+
}
162165
s := <-b.c
163166
defer func() { b.c <- s }()
164167

@@ -169,6 +172,10 @@ func (b *Baggage) Slice() []KeyVal {
169172

170173
// Map returns a map of key to value.
171174
func (b *Baggage) Map() map[string]interface{} {
175+
if b == nil {
176+
return nil
177+
}
178+
172179
s := <-b.c
173180
defer func() { b.c <- s }()
174181

ctxmeta/ctxmeta_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ func TestContextMeta_ErrNoBaggae(t *testing.T) {
6262

6363
err = baggage.Delete("foo")
6464
assert.ErrorIs(t, err, ErrNoBaggage)
65+
66+
assert.Nil(t, baggage.Slice())
67+
assert.Nil(t, baggage.Map())
6568
}
6669

6770
func TestContextMeta_ErrNotFound(t *testing.T) {

logging/log.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ func WithContext(logger log.Logger, ctx context.Context) log.Logger {
113113
var args []interface{}
114114

115115
bag := ctxmeta.GetBaggage(ctx)
116+
116117
for _, kv := range bag.Slice() {
117118
args = append(args, kv.Key, kv.Val)
118119
}

0 commit comments

Comments
 (0)