forked from honeybadger-io/honeybadger-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FromContext and Context.WithContext for lots of context
This allows building up a honeybadger.Context riding along with a context.Context so it can be sent with errors. The goal is to make it so that the context sent to Honeybadger is tied to the request (or stack) rather than multiple requests clobbering the global state (see honeybadger-io#35). Fixes honeybadger-io#35 Closes honeybadger-io#37
- Loading branch information
Showing
9 changed files
with
185 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,33 @@ | ||
package honeybadger | ||
|
||
import "context" | ||
|
||
// Context is used to send extra data to Honeybadger. | ||
type Context hash | ||
|
||
// ctxKey is use in WithContext and FromContext to store and load the | ||
// honeybadger.Context into a context.Context. | ||
type ctxKey struct{} | ||
|
||
// Update applies the values in other Context to context. | ||
func (context Context) Update(other Context) { | ||
func (c Context) Update(other Context) { | ||
for k, v := range other { | ||
context[k] = v | ||
c[k] = v | ||
} | ||
} | ||
|
||
// WithContext adds the honeybadger.Context to the given context.Context and | ||
// returns the new context.Context. | ||
func (c Context) WithContext(ctx context.Context) context.Context { | ||
return context.WithValue(ctx, ctxKey{}, c) | ||
} | ||
|
||
// FromContext retrieves a honeybadger.Context from the context.Context. | ||
// FromContext will return nil if no Honeybadger context exists in ctx. | ||
func FromContext(ctx context.Context) Context { | ||
if c, ok := ctx.Value(ctxKey{}).(Context); ok { | ||
return c | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package honeybadger | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"sync" | ||
) | ||
|
||
// MemoryBackend is a Backend that writes error notices to a slice. The | ||
// MemoryBackend is mainly useful for testing and will cause issues if used in | ||
// production. MemoryBackend is thread safe but order can't be guaranteed. | ||
type MemoryBackend struct { | ||
Notices []*Notice | ||
mu sync.Mutex | ||
} | ||
|
||
// NewMemoryBackend creates a new MemoryBackend. | ||
func NewMemoryBackend() *MemoryBackend { | ||
return &MemoryBackend{ | ||
Notices: make([]*Notice, 0), | ||
} | ||
} | ||
|
||
// Notify adds the given payload (if it is a Notice) to Notices. | ||
func (b *MemoryBackend) Notify(_ Feature, payload Payload) error { | ||
notice, ok := payload.(*Notice) | ||
if !ok { | ||
return fmt.Errorf("memory backend does not support payload of type %q", reflect.TypeOf(payload)) | ||
} | ||
|
||
b.mu.Lock() | ||
defer b.mu.Unlock() | ||
|
||
b.Notices = append(b.Notices, notice) | ||
|
||
return nil | ||
} | ||
|
||
// Reset clears the set of Notices | ||
func (b *MemoryBackend) Reset() { | ||
b.mu.Lock() | ||
defer b.mu.Unlock() | ||
|
||
b.Notices = b.Notices[:0] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters