Skip to content

Commit ebf5dfc

Browse files
committed
context docs
1 parent bef73be commit ebf5dfc

File tree

3 files changed

+71
-28
lines changed

3 files changed

+71
-28
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,30 @@ default value is not in the list of `included_environments`.
158158

159159
Sentry uses the [hackney HTTP client](https://github.com/benoitc/hackney) for HTTP requests. Sentry starts its own hackney pool named `:sentry_pool` with a default connection pool of 50, and a connection timeout of 5000 milliseconds. The pool can be configured with the `hackney_pool_max_connections` and `hackney_pool_timeout` configuration keys. If you need to set other [hackney configurations](https://github.com/benoitc/hackney/blob/master/doc/hackney.md#request5) for things like a proxy, using your own pool or response timeouts, the `hackney_opts` configuration is passed directly to hackney for each request.
160160

161+
### Context and Breadcrumbs
162+
163+
Sentry has multiple options for including contextual information. They are organized into "Tags", "User", and "Extra", and Sentry's documentation on them is [here](https://docs.sentry.io/learn/context/). Breadcrumbs are a similar concept and Sentry's documentation covers them [here](https://docs.sentry.io/learn/breadcrumbs/).
164+
165+
In Elixir this can be complicated due to processes being isolated from one another. Tags context can be set globally through configuration, and all contexts can be set within a process, and on individual events. If an event is sent within a process that has some context configured it will include that context in the event. Examples of each are below, and for more information see the documentation of [Sentry.Context](https://hexdocs.pm/sentry/Sentry.html#module-filtering-exceptions).
166+
167+
```elixir
168+
# Global Tags context via configuration:
169+
170+
config :sentry,
171+
tags: %{my_app_version: "14.30.10"}
172+
# ...
173+
174+
# Process-based Context
175+
Sentry.Context.set_extra_context(%{day_of_week: "Friday"})
176+
Sentry.Context.set_user_context(%{id: 24, username: "user_username", has_subscription: true})
177+
Sentry.Context.set_tags_context(%{locale: "en-us"})
178+
Sentry.Context.add_breadcrumb(%{category: "web.request"})
179+
180+
# Event-based Context
181+
Sentry.capture_exception(exception, [tags: %{locale: "en-us", }, user: %{id: 34},
182+
extra: %{day_of_week: "Friday"}, breadcrumbs: [%{timestamp: 1461185753845, category: "web.request"}]]
183+
```
184+
161185
### Fingerprinting
162186

163187
By default, Sentry aggregates reported events according to the attributes of the event, but users may need to override this functionality via [fingerprinting](https://docs.sentry.io/learn/rollups/#customize-grouping-with-fingerprints).

docs/index.rst

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -88,27 +88,6 @@ If using an environment with Plug or Phoenix add the following to your router:
8888
use Sentry.Plug
8989
9090
91-
Adding Context
92-
--------------
93-
94-
Sentry allows a user to provide context to all error reports, Elixir being multi-process makes this a special
95-
case. When setting a context we store that context in the process dictionary, which means if you spin up a
96-
new process and it fails you might lose your context. That said using the context is simple:
97-
98-
.. code-block:: elixir
99-
100-
# sets the logged in user
101-
Sentry.Context.set_user_context(%{email: "[email protected]"})
102-
103-
# sets the tag of interesting
104-
Sentry.Context.set_tags_context(%{interesting: "yes"})
105-
106-
# sends any additional context
107-
Sentry.Context.set_extra_context(%{my: "context"})
108-
109-
# adds an breadcrumb to the request to help debug
110-
Sentry.Context.add_breadcrumb(%{my: "crumb"})
111-
11291
Filtering Events
11392
----------------
11493

@@ -137,6 +116,36 @@ allows other exceptions to be sent.
137116
included_environments: ~w(production staging),
138117
environment_name: System.get_env("RELEASE_LEVEL") || "development"
139118
119+
Context and Breadcrumbs
120+
----------------
121+
122+
Sentry has multiple options for including contextual information. They are organized into
123+
"Tags", "User", and "Extra", and Sentry's documentation on them is `here <https://docs.sentry.io/learn/context/>`_.
124+
Breadcrumbs are a similar concept and Sentry's documentation covers them `here <https://docs.sentry.io/learn/breadcrumbs/>`_.
125+
126+
In Elixir this can be complicated due to processes being isolated from one another. Tags
127+
context can be set globally through configuration, and all contexts can be set within a
128+
process, and on individual events. If an event is sent within a process that has some context
129+
configured it will include that context in the event. Examples of each are below,
130+
and for more information see the documentation of `Sentry.Context <https://hexdocs.pm/sentry/Sentry.html#module-filtering-exceptions>`_.
131+
132+
.. code-block:: elixir
133+
# Global Tags context via configuration:
134+
135+
config :sentry,
136+
tags: %{my_app_version: "14.30.10"}
137+
# ...
138+
139+
# Process-based Context
140+
Sentry.Context.set_extra_context(%{day_of_week: "Friday"})
141+
Sentry.Context.set_user_context(%{id: 24, username: "user_username", has_subscription: true})
142+
Sentry.Context.set_tags_context(%{locale: "en-us"})
143+
Sentry.Context.add_breadcrumb(%{category: "web.request"})
144+
145+
# Event-based Context
146+
Sentry.capture_exception(exception, [tags: %{locale: "en-us", }, user: %{id: 34},
147+
extra: %{day_of_week: "Friday"}, breadcrumbs: [%{timestamp: 1461185753845, category: "web.request"}]]
148+
140149
Fingerprinting
141150
----------------
142151
By default, Sentry aggregates reported events according to the attributes of the event,

lib/sentry/context.ex

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
defmodule Sentry.Context do
22
@moduledoc """
3-
Provides a method to store user, tags, and extra context when an
4-
event is reported. The contexts will be fetched and merged into
5-
the event.
3+
Provides functionality to store user, tags, extra, and breadcrumbs context when an
4+
event is reported. The contexts will be fetched and merged into the event when it is sent.
65
7-
Sentry.Context uses the Process Dictionary to store this state.
8-
This imposes some limitations. The state will only exist within
6+
When calling Sentry.Context, the Process Dictionary is used to store this state.
7+
This imposes some limitations. The state will only exist within
98
the current process, and the context will die with the process.
109
11-
For example, if you add context inside your controller and an
12-
error happens in a Task the context won't send.
10+
For example, if you add context inside your controller and an
11+
error happens in a Task, that context will not be included.
12+
13+
A common use-case is to set context within Plug or Phoenix applications, as each
14+
request is its own process, and so any stored context will be included should an
15+
error be reported within that request process. Example:
16+
17+
# post_controller.ex
18+
def index(conn, _params) do
19+
Sentry.Context.set_user_context(%{id: conn.assigns.user_id})
20+
posts = Blog.list_posts()
21+
render(conn, "index.html", posts: posts)
22+
end
1323
"""
1424
@process_dictionary_key :sentry_context
1525
@user_key :user

0 commit comments

Comments
 (0)