Skip to content

Commit cdfc826

Browse files
committed
Removes honeybadger.SetContext and Client.SetContext
Calling honeybadger.SetContext applies to all Go routines that use honeybadger.Notify or DefaultClient which means that setting values from one request could end up being reported in the error from a request on a different Go routine / thread. This API works fine in languages like Ruby which have thread local storage but I think it is too easily misunderstood and will likely cause problems so I've decided to remove it.
1 parent d238514 commit cdfc826

File tree

7 files changed

+7
-133
lines changed

7 files changed

+7
-133
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ adheres to [Semantic Versioning](http://semver.org/).
55

66
## [Unreleased][unreleased]
77

8+
### Changed
9+
- Removed honeybadger.SetContext and client.SetContext (#35) -@gaffneyc
10+
811
## [0.5.0] - 2019-10-17
912
### Added
1013
- Added Sync mode

README.md

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -156,25 +156,6 @@ honeybadger.Notify(err, honeybadger.Tags{"timeout", "http"})
156156

157157
---
158158

159-
160-
### `honeybadger.SetContext()`: Set metadata to be sent if an error occurs
161-
162-
This method lets you set context data that will be sent if an error should occur.
163-
164-
For example, it's often useful to record the current user's ID when an error occurs in a web app. To do that, just use `SetContext` to set the user id on each request. If an error occurs, the id will be reported with it.
165-
166-
**Note**: This method is currently shared across goroutines, and therefore may not be optimal for use in highly concurrent use cases, such as HTTP requests. See [issue #35](https://github.com/honeybadger-io/honeybadger-go/issues/35).
167-
168-
#### Examples:
169-
170-
```go
171-
honeybadger.SetContext(honeybadger.Context{
172-
"user_id": 1,
173-
})
174-
```
175-
176-
---
177-
178159
### ``defer honeybadger.Monitor()``: Automatically report panics from your functions
179160

180161
To automatically report panics in your functions or methods, add

client.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ type noticeHandler func(*Notice) error
2323
// the configuration and implements the public API.
2424
type Client struct {
2525
Config *Configuration
26-
context *contextSync
2726
worker worker
2827
beforeNotifyHandlers []noticeHandler
2928
}
@@ -33,11 +32,6 @@ func (client *Client) Configure(config Configuration) {
3332
client.Config.update(&config)
3433
}
3534

36-
// SetContext updates the client context with supplied context.
37-
func (client *Client) SetContext(context Context) {
38-
client.context.Update(context)
39-
}
40-
4135
// Flush blocks until the worker has processed its queue.
4236
func (client *Client) Flush() {
4337
client.worker.Flush()
@@ -52,7 +46,6 @@ func (client *Client) BeforeNotify(handler func(notice *Notice) error) {
5246

5347
// Notify reports the error err to the Honeybadger service.
5448
func (client *Client) Notify(err interface{}, extra ...interface{}) (string, error) {
55-
extra = append([]interface{}{client.context.internal}, extra...)
5649
notice := newNotice(client.Config, newError(err, 2), extra...)
5750
for _, handler := range client.beforeNotifyHandlers {
5851
if err := handler(notice); err != nil {
@@ -113,9 +106,8 @@ func New(c Configuration) *Client {
113106
worker := newBufferedWorker(config)
114107

115108
client := Client{
116-
Config: config,
117-
worker: worker,
118-
context: newContextSync(),
109+
Config: config,
110+
worker: worker,
119111
}
120112

121113
return &client

client_test.go

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package honeybadger
22

33
import (
4-
"sync"
54
"testing"
65
)
76

@@ -29,48 +28,6 @@ func TestConfigureClientEndpoint(t *testing.T) {
2928
}
3029
}
3130

32-
func TestClientContext(t *testing.T) {
33-
client := New(Configuration{})
34-
35-
client.SetContext(Context{"foo": "bar"})
36-
client.SetContext(Context{"bar": "baz"})
37-
38-
context := client.context.internal
39-
40-
if context["foo"] != "bar" {
41-
t.Errorf("Expected client to merge global context. expected=%#v actual=%#v", "bar", context["foo"])
42-
}
43-
44-
if context["bar"] != "baz" {
45-
t.Errorf("Expected client to merge global context. expected=%#v actual=%#v", "baz", context["bar"])
46-
}
47-
}
48-
49-
func TestClientConcurrentContext(t *testing.T) {
50-
var wg sync.WaitGroup
51-
52-
client := New(Configuration{})
53-
newContext := Context{"foo": "bar"}
54-
55-
wg.Add(2)
56-
57-
go updateContext(&wg, client, newContext)
58-
go updateContext(&wg, client, newContext)
59-
60-
wg.Wait()
61-
62-
context := client.context.internal
63-
64-
if context["foo"] != "bar" {
65-
t.Errorf("Expected context value. expected=%#v result=%#v", "bar", context["foo"])
66-
}
67-
}
68-
69-
func updateContext(wg *sync.WaitGroup, client *Client, context Context) {
70-
client.SetContext(context)
71-
wg.Done()
72-
}
73-
7431
func TestNotifyPushesTheEnvelope(t *testing.T) {
7532
client, worker, _ := mockClient(Configuration{})
7633

@@ -121,9 +78,8 @@ func mockClient(c Configuration) (Client, *mockWorker, *mockBackend) {
12178
backendConfig.update(&c)
12279

12380
client := Client{
124-
Config: newConfig(*backendConfig),
125-
worker: worker,
126-
context: newContextSync(),
81+
Config: newConfig(*backendConfig),
82+
worker: worker,
12783
}
12884

12985
return client, worker, backend

context_sync.go

Lines changed: 0 additions & 22 deletions
This file was deleted.

context_sync_test.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

honeybadger.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ func Configure(c Configuration) {
5454
DefaultClient.Configure(c)
5555
}
5656

57-
// SetContext merges c Context into the Context of the global client.
58-
func SetContext(c Context) {
59-
DefaultClient.SetContext(c)
60-
}
61-
6257
// Notify reports the error err to the Honeybadger service.
6358
//
6459
// The first argument err may be an error, a string, or any other type in which

0 commit comments

Comments
 (0)