Skip to content

Commit 337bf8d

Browse files
committed
use Logger.metadata for Sentry.Context
1 parent 89ba002 commit 337bf8d

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

lib/sentry/context.ex

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ defmodule Sentry.Context do
33
Provides functionality to store user, tags, extra, and breadcrumbs context when an
44
event is reported. The contexts will be fetched and merged into the event when it is sent.
55
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
6+
When calling Sentry.Context, Logger metadata is used to store this state.
7+
This imposes some limitations. The metadata will only exist within
88
the current process, and the context will die with the process.
99
1010
For example, if you add context inside your controller and an
@@ -24,7 +24,7 @@ defmodule Sentry.Context do
2424
It should be noted that the `set_*_context/1` functions merge with the
2525
existing context rather than entirely overwriting it.
2626
"""
27-
@process_dictionary_key :sentry_context
27+
@logger_metadata_key :sentry
2828
@user_key :user
2929
@tags_key :tags
3030
@extra_key :extra
@@ -55,7 +55,7 @@ defmodule Sentry.Context do
5555
breadcrumbs: list()
5656
}
5757
def get_all do
58-
context = get_context()
58+
context = get_sentry_context()
5959

6060
%{
6161
user: Map.get(context, @user_key, %{}),
@@ -75,11 +75,11 @@ defmodule Sentry.Context do
7575
## Example
7676
7777
iex> Sentry.Context.set_extra_context(%{id: 123})
78-
nil
78+
:ok
7979
iex> Sentry.Context.set_extra_context(%{detail: "bad_error"})
80-
%{extra: %{id: 123}}
80+
:ok
8181
iex> Sentry.Context.set_extra_context(%{message: "Oh no"})
82-
%{extra: %{detail: "bad_error", id: 123}}
82+
:ok
8383
iex> Sentry.Context.get_all()
8484
%{
8585
user: %{},
@@ -89,10 +89,9 @@ defmodule Sentry.Context do
8989
breadcrumbs: []
9090
}
9191
"""
92-
@spec set_extra_context(map()) :: nil | map()
92+
@spec set_extra_context(map()) :: :ok
9393
def set_extra_context(map) when is_map(map) do
94-
get_context()
95-
|> set_context(@extra_key, map)
94+
set_context(@extra_key, map)
9695
end
9796

9897
@doc """
@@ -104,9 +103,9 @@ defmodule Sentry.Context do
104103
## Example
105104
106105
iex> Sentry.Context.set_user_context(%{id: 123})
107-
nil
106+
:ok
108107
iex> Sentry.Context.set_user_context(%{username: "george"})
109-
%{user: %{id: 123}}
108+
:ok
110109
iex> Sentry.Context.get_all()
111110
%{
112111
user: %{id: 123, username: "george"},
@@ -116,10 +115,9 @@ defmodule Sentry.Context do
116115
breadcrumbs: []
117116
}
118117
"""
119-
@spec set_user_context(map()) :: nil | map()
118+
@spec set_user_context(map()) :: :ok
120119
def set_user_context(map) when is_map(map) do
121-
get_context()
122-
|> set_context(@user_key, map)
120+
set_context(@user_key, map)
123121
end
124122

125123
@doc """
@@ -131,9 +129,9 @@ defmodule Sentry.Context do
131129
## Example
132130
133131
iex> Sentry.Context.set_tags_context(%{id: 123})
134-
nil
132+
:ok
135133
iex> Sentry.Context.set_tags_context(%{other_id: 456})
136-
%{tags: %{id: 123}}
134+
:ok
137135
iex> Sentry.Context.get_all()
138136
%{
139137
breadcrumbs: [],
@@ -143,10 +141,9 @@ defmodule Sentry.Context do
143141
user: %{}
144142
}
145143
"""
146-
@spec set_tags_context(map()) :: nil | map()
144+
@spec set_tags_context(map()) :: :ok
147145
def set_tags_context(map) when is_map(map) do
148-
get_context()
149-
|> set_context(@tags_key, map)
146+
set_context(@tags_key, map)
150147
end
151148

152149
@doc """
@@ -159,9 +156,9 @@ defmodule Sentry.Context do
159156
## Example
160157
161158
iex(1)> Sentry.Context.set_http_context(%{id: 123})
162-
nil
159+
:ok
163160
iex(2)> Sentry.Context.set_http_context(%{url: "www.example.com"})
164-
%{request: %{id: 123}}
161+
:ok
165162
iex(3)> Sentry.Context.get_all()
166163
%{
167164
breadcrumbs: [],
@@ -171,10 +168,9 @@ defmodule Sentry.Context do
171168
user: %{}
172169
}
173170
"""
174-
@spec set_http_context(map()) :: nil | map()
171+
@spec set_http_context(map()) :: :ok
175172
def set_http_context(map) when is_map(map) do
176-
get_context()
177-
|> set_context(@request_key, map)
173+
set_context(@request_key, map)
178174
end
179175

180176
@doc """
@@ -183,18 +179,22 @@ defmodule Sentry.Context do
183179
## Example
184180
185181
iex> Sentry.Context.set_tags_context(%{id: 123})
186-
nil
182+
:ok
187183
iex> Sentry.Context.clear_all()
188-
%{tags: %{id: 123}}
184+
:ok
189185
iex> Sentry.Context.get_all()
190186
%{breadcrumbs: [], extra: %{}, request: %{}, tags: %{}, user: %{}}
191187
"""
192188
def clear_all do
193-
Process.delete(@process_dictionary_key)
189+
:logger.update_process_metadata(%{sentry: %{}})
194190
end
195191

196-
defp get_context do
197-
Process.get(@process_dictionary_key) || %{}
192+
defp get_sentry_context do
193+
case :logger.get_process_metadata() do
194+
%{@logger_metadata_key => sentry} -> sentry
195+
%{} -> %{}
196+
:undefined -> %{}
197+
end
198198
end
199199

200200
@doc """
@@ -208,7 +208,7 @@ defmodule Sentry.Context do
208208
## Example
209209
210210
iex> Sentry.Context.add_breadcrumb(message: "first_event")
211-
nil
211+
:ok
212212
iex> Sentry.Context.add_breadcrumb(%{message: "second_event", type: "auth"})
213213
%{breadcrumbs: [%{:message => "first_event", "timestamp" => 1562007480}]}
214214
iex> Sentry.Context.add_breadcrumb(%{message: "response"})
@@ -231,7 +231,7 @@ defmodule Sentry.Context do
231231
user: %{}
232232
}
233233
"""
234-
@spec add_breadcrumb(keyword() | map()) :: nil | map()
234+
@spec add_breadcrumb(keyword() | map()) :: :ok
235235
def add_breadcrumb(list) when is_list(list) do
236236
if Keyword.keyword?(list) do
237237
list
@@ -248,21 +248,21 @@ defmodule Sentry.Context do
248248
def add_breadcrumb(map) when is_map(map) do
249249
map = Map.put_new(map, "timestamp", Sentry.Util.unix_timestamp())
250250

251-
context =
252-
get_context()
251+
sentry_metadata =
252+
get_sentry_context()
253253
|> Map.update(@breadcrumbs_key, [map], &[map | &1])
254254

255-
Process.put(@process_dictionary_key, context)
255+
:logger.update_process_metadata(%{sentry: sentry_metadata})
256256
end
257257

258-
defp set_context(current, key, new) when is_map(current) and is_map(new) do
259-
merged_context =
260-
current
261-
|> Map.get(key, %{})
262-
|> Map.merge(new)
258+
defp set_context(key, new) when is_map(new) do
259+
sentry_metadata =
260+
case :logger.get_process_metadata() do
261+
%{sentry: sentry} -> Map.update(sentry, key, new, &Map.merge(&1, new))
262+
_ -> %{key => new}
263+
end
263264

264-
new_context = Map.put(current, key, merged_context)
265-
Process.put(@process_dictionary_key, new_context)
265+
:logger.update_process_metadata(%{sentry: sentry_metadata})
266266
end
267267

268268
@doc """

0 commit comments

Comments
 (0)