Skip to content

Event context

You can add custom metadata to the events sent to Honeybadger Insights by using the SetEventContext function. This metadata will be merged into all events sent via honeybadger.Event().

Use honeybadger.SetEventContext() to set context data that will be included with all events:

honeybadger.SetEventContext(honeybadger.Context{
"user_id": 123,
"account": "acme",
})

Event data passed directly to Event() takes precedence over event context if there are conflicting keys.

To clear all event context data that was previously set:

honeybadger.ClearEventContext()

A common pattern is to set event context early in a request lifecycle:

func handleRequest(w http.ResponseWriter, r *http.Request) {
user := getCurrentUser(r)
honeybadger.SetEventContext(honeybadger.Context{
"user_id": user.ID,
"account_id": user.AccountID,
})
// All events sent during this request will include user context
honeybadger.Event("page_view", map[string]any{
"path": r.URL.Path,
})
}

Note: Event context is stored globally and shared across goroutines. For highly concurrent applications, consider passing context data directly to Event() instead.