Skip to content

Commit 3425b79

Browse files
jfmyers9mitchellhenke
authored andcommitted
allow users to configure maximum number of breadcrumbs
This configuration option allows a user to specify the maximum number of breadcrumbs when posting events to Sentry. The default value is 100. This is helpful in situations in which long running processes can add a large number of breadcrumbs resulting in failures to create Sentry events when the message becomes too large. Fixes #407
1 parent 8701e9d commit 3425b79

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ The full range of options is the following:
133133
| `filter` | False | | Module where the filter rules are defined (see [Filtering Exceptions](https://hexdocs.pm/sentry/Sentry.html#module-filtering-exceptions)) |
134134
| `json_library` | False | `Jason` | |
135135
| `log_level` | False | `:warn` | This sets the log level used when Sentry fails to send an event due to an invalid event or API error |
136+
| `max_breadcrumbs` | False | 100 | This sets the maximum number of breadcrumbs to send to Sentry when creating an event |
136137

137138
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.
138139

lib/sentry/config.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ defmodule Sentry.Config do
133133
get_config(:log_level, default: :warn, check_dsn: false)
134134
end
135135

136+
def max_breadcrumbs do
137+
get_config(:max_breadcrumbs, default: 100, check_dsn: false)
138+
end
139+
136140
def permitted_log_level_values, do: @permitted_log_level_values
137141

138142
defp get_config(key, opts \\ []) when is_atom(key) do

lib/sentry/event.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ defmodule Sentry.Event do
126126
request_context
127127
|> Map.merge(Keyword.get(opts, :request, %{}))
128128

129-
breadcrumbs = Keyword.get(opts, :breadcrumbs, []) ++ breadcrumbs_context
129+
breadcrumbs =
130+
Keyword.get(opts, :breadcrumbs, [])
131+
|> Kernel.++(breadcrumbs_context)
132+
|> Enum.take(-1 * Config.max_breadcrumbs())
130133

131134
level = Keyword.get(opts, :level, "error")
132135

test/event_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ defmodule Sentry.EventTest do
140140
} = Event.create_event(message: "Test message")
141141
end
142142

143+
test "respects the max_breadcrumbs configuration" do
144+
breadcrumbs =
145+
1..150
146+
|> Enum.map(fn x -> %{message: "breadcrumb-#{x}"} end)
147+
148+
event = Event.create_event(message: "Test message", breadcrumbs: breadcrumbs)
149+
assert length(event.breadcrumbs) == 100
150+
assert event.breadcrumbs == Enum.take(breadcrumbs, -100)
151+
end
152+
143153
test "only sending fingerprint when set" do
144154
exception = RuntimeError.exception("error")
145155
event = Sentry.Event.transform_exception(exception, fingerprint: ["hello", "world"])

0 commit comments

Comments
 (0)