@@ -38,7 +38,7 @@ defmodule Sentry.Context do
3838
3939 """
4040
41- alias Sentry.Interfaces
41+ alias Sentry . { Attachment , Interfaces }
4242
4343 @ typedoc """
4444 User context.
@@ -149,6 +149,7 @@ defmodule Sentry.Context do
149149 @ extra_key :extra
150150 @ request_key :request
151151 @ breadcrumbs_key :breadcrumbs
152+ @ attachments_key :attachments
152153
153154 @ doc """
154155 Retrieves all currently-set context on the current process.
@@ -163,7 +164,8 @@ defmodule Sentry.Context do
163164 tags: %{message_id: 456},
164165 extra: %{},
165166 request: %{},
166- breadcrumbs: []
167+ breadcrumbs: [],
168+ attachments: []
167169 }
168170
169171 """
@@ -172,7 +174,8 @@ defmodule Sentry.Context do
172174 request: request_context ( ) ,
173175 tags: tags ( ) ,
174176 extra: extra ( ) ,
175- breadcrumbs: list ( )
177+ breadcrumbs: list ( ) ,
178+ attachments: list ( Attachment . t ( ) )
176179 }
177180 def get_all do
178181 context = get_sentry_context ( )
@@ -182,7 +185,8 @@ defmodule Sentry.Context do
182185 tags: Map . get ( context , @ tags_key , % { } ) ,
183186 extra: Map . get ( context , @ extra_key , % { } ) ,
184187 request: Map . get ( context , @ request_key , % { } ) ,
185- breadcrumbs: Map . get ( context , @ breadcrumbs_key , [ ] ) |> Enum . reverse ( ) |> Enum . to_list ( )
188+ breadcrumbs: Map . get ( context , @ breadcrumbs_key , [ ] ) |> Enum . reverse ( ) |> Enum . to_list ( ) ,
189+ attachments: Map . get ( context , @ attachments_key , [ ] ) |> Enum . reverse ( ) |> Enum . to_list ( )
186190 }
187191 end
188192
@@ -206,7 +210,8 @@ defmodule Sentry.Context do
206210 tags: %{},
207211 extra: %{detail: "bad_error", id: 123, message: "Oh no"},
208212 request: %{},
209- breadcrumbs: []
213+ breadcrumbs: [],
214+ attachments: []
210215 }
211216
212217 """
@@ -241,7 +246,8 @@ defmodule Sentry.Context do
241246 tags: %{},
242247 extra: %{},
243248 request: %{},
244- breadcrumbs: []
249+ breadcrumbs: [],
250+ attachments: []
245251 }
246252
247253 """
@@ -264,6 +270,7 @@ defmodule Sentry.Context do
264270 :ok
265271 iex> Sentry.Context.get_all()
266272 %{
273+ attachments: [],
267274 breadcrumbs: [],
268275 extra: %{},
269276 request: %{},
@@ -303,6 +310,7 @@ defmodule Sentry.Context do
303310 :ok
304311 iex> Sentry.Context.get_all()
305312 %{
313+ attachments: [],
306314 breadcrumbs: [],
307315 extra: %{},
308316 request: %{method: "GET", headers: %{"accept" => "application/json"}, url: "example.com"},
@@ -326,7 +334,7 @@ defmodule Sentry.Context do
326334 iex> Sentry.Context.clear_all()
327335 :ok
328336 iex> Sentry.Context.get_all()
329- %{breadcrumbs: [], extra: %{}, request: %{}, tags: %{}, user: %{}}
337+ %{breadcrumbs: [], extra: %{}, request: %{}, tags: %{}, user: %{}, attachments: [] }
330338
331339 """
332340 @ spec clear_all ( ) :: :ok
@@ -374,6 +382,7 @@ defmodule Sentry.Context do
374382 }
375383 iex> Sentry.Context.get_all()
376384 %{
385+ attachments: [],
377386 breadcrumbs: [
378387 %{:message => "first_event", "timestamp" => 1562007480},
379388 %{:message => "second_event", :type => "auth", "timestamp" => 1562007505},
@@ -425,17 +434,79 @@ defmodule Sentry.Context do
425434 :logger . update_process_metadata ( % { @ logger_metadata_key => sentry_metadata } )
426435 end
427436
437+ @ doc """
438+ Adds an **attachment** to the current context.
439+
440+ Attachments stored in the context will be sent alongside each event that is
441+ reported *within that context* (that is, within the process that the context
442+ was set in).
443+
444+ Currently, there is no limit to how many attachments you can add to the context
445+ through this function, even though there might be limits on the Sentry server side.
446+ To clear attachments, use `clear_attachments/0`.
447+
448+ ## Examples
449+
450+ iex> Sentry.Context.add_attachment(%Sentry.Attachment{filename: "foo.txt", data: "foo"})
451+ :ok
452+ iex> Sentry.Context.add_attachment(%Sentry.Attachment{filename: "bar.txt", data: "bar"})
453+ :ok
454+ iex> Sentry.Context.get_all()
455+ %{
456+ attachments: [
457+ %Sentry.Attachment{filename: "bar.txt", data: "bar"},
458+ %Sentry.Attachment{filename: "foo.txt", data: "foo"}
459+ ],
460+ breadcrumbs: [],
461+ extra: %{},
462+ request: %{},
463+ tags: %{},
464+ user: %{}
465+ }
466+
467+ """
468+ @ doc since: "10.1.0"
469+ @ spec add_attachment ( Attachment . t ( ) ) :: :ok
470+ def add_attachment ( % Attachment { } = attachment ) do
471+ new_context =
472+ Map . update ( get_sentry_context ( ) , @ attachments_key , [ attachment ] , & ( & 1 ++ [ attachment ] ) )
473+
474+ :logger . update_process_metadata ( % { @ logger_metadata_key => new_context } )
475+ end
476+
477+ @ doc """
478+ Clears all attachments from the current context.
479+
480+ See `add_attachment/1`.
481+
482+ ## Examples
483+
484+ iex> Sentry.Context.add_attachment(%Sentry.Attachment{filename: "foo.txt", data: "foo"})
485+ :ok
486+ iex> Sentry.Context.clear_attachments()
487+ :ok
488+ iex> Sentry.Context.get_all().attachments
489+ []
490+
491+ """
492+ @ doc since: "10.1.0"
493+ @ spec clear_attachments ( ) :: :ok
494+ def clear_attachments do
495+ new_context = Map . delete ( get_sentry_context ( ) , @ attachments_key )
496+ :logger . update_process_metadata ( % { @ logger_metadata_key => new_context } )
497+ end
498+
428499 @ doc """
429500 Returns the keys used to store context in the current process' logger metadata.
430501
431502 ## Example
432503
433504 iex> Sentry.Context.context_keys()
434- [:breadcrumbs, :tags, :user, :extra, :request]
505+ [:breadcrumbs, :tags, :user, :extra, :request, :attachments ]
435506
436507 """
437508 @ spec context_keys ( ) :: [ atom ( ) , ... ]
438509 def context_keys do
439- [ @ breadcrumbs_key , @ tags_key , @ user_key , @ extra_key , @ request_key ]
510+ [ @ breadcrumbs_key , @ tags_key , @ user_key , @ extra_key , @ request_key , @ attachments_key ]
440511 end
441512end
0 commit comments