Skip to content

Commit

Permalink
Merge pull request #3 from nrfta/feat/context-degrade-gracefully
Browse files Browse the repository at this point in the history
degrade gracefully when context not properly initialized
  • Loading branch information
strobus authored Nov 11, 2020
2 parents 8a4da74 + b45f6f7 commit 680fa56
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
24 changes: 16 additions & 8 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,31 @@ func WithContext(parent context.Context, fields ...Field) context.Context {
return context.WithValue(parent, ContextKeyLogFields, makeFieldStack().push(fields))
}

// PushContextFields pushes the given fields onto the logging fields stack. PushContextFields panics if the
// context has not been initialized via WithContext.
// PushContextFields pushes the given fields onto the logging fields stack.
func PushContextFields(ctx context.Context, fields ...Field) {
stack := getStack(ctx)
if stack == nil {
return
}
stack.push(fields)
}

// PopContextFields pops the last entry off of the logging fields stack. PopContextFields panics if the
// context has not been initialized via WithContext.
// PopContextFields pops the last entry off of the logging fields stack.
func PopContextFields(ctx context.Context) {
stack := getStack(ctx)
if stack == nil {
return
}
stack.pop()
}

// GetContextFields retrieves the logging `Fields` from context. GetContextFields panics if the
// context has not been initialized via WithContext.
// GetContextFields retrieves the logging `Fields` from context. GetContextFields returns an empty Fields map
// if the context has not been initialized by calling WithContext.
func GetContextFields(ctx context.Context, additionalFields ...Field) Fields {
stack := getStack(ctx)
if stack == nil {
return make(Fields)
}
fields := stack.allFields()
for _, f := range additionalFields {
fields[f.Name] = f.Value
Expand All @@ -43,11 +50,12 @@ func GetContextFields(ctx context.Context, additionalFields ...Field) Fields {
func getStack(ctx context.Context) *fieldStack {
stackObj := ctx.Value(ContextKeyLogFields)
if stackObj == nil {
panic("logging fields have not been added to context yet; call WithContext")
Warn("context logging fields not initialized; call log.WithContext")
return nil
}
stack, ok := stackObj.(*fieldStack)
if !ok {
panic("logging fields are not of the correct type")
Warn("context logging fields has incorrect type")
}
return stack
}
19 changes: 19 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ var _ = Describe("Context", func() {
fields = GetContextFields(ctx)
g.Expect(len(fields)).To(g.Equal(1))
g.Expect(fields["foo"]).To(g.Equal(5))

// pop should be safe to call even beyond actual stack height
PopContextFields(ctx)
PopContextFields(ctx)
PopContextFields(ctx)
})

It("should safely ignore when context is not initialized", func() {
ctx := context.Background()

fields := GetContextFields(ctx)
g.Expect(len(fields)).To(g.Equal(0))

PushContextFields(ctx, MakeField("foo", 0))
PopContextFields(ctx)

ctx = context.WithValue(ctx, ContextKeyLogFields, "wrong")
PopContextFields(ctx)
PopContextFields(ctx)
})
})
})
Expand Down

0 comments on commit 680fa56

Please sign in to comment.