Skip to content

Commit 7b41bad

Browse files
committed
module for any and all configuration
1 parent 6647fa2 commit 7b41bad

File tree

6 files changed

+116
-37
lines changed

6 files changed

+116
-37
lines changed

lib/mix/tasks/sentry.send_test_event.ex

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule Mix.Tasks.Sentry.SendTestEvent do
22
use Mix.Task
3+
alias Sentry.Config
34

45
@shortdoc "Attempts to send a test event to check Sentry configuration"
56
@moduledoc """
@@ -21,8 +22,8 @@ defmodule Mix.Tasks.Sentry.SendTestEvent do
2122
Mix.shell.info "public_key: #{public_key}"
2223
Mix.shell.info "secret_key: #{secret_key}"
2324
Mix.shell.info "included_environments: #{inspect included_environments()}"
24-
Mix.shell.info "current environment_name: #{inspect environment_name()}"
25-
Mix.shell.info "hackney_opts: #{inspect hackney_opts()}\n"
25+
Mix.shell.info "current environment_name: #{inspect Config.environment_name()}"
26+
Mix.shell.info "hackney_opts: #{inspect Config.hackney_opts()}\n"
2627
end
2728

2829
defp included_environments do
@@ -33,12 +34,8 @@ defmodule Mix.Tasks.Sentry.SendTestEvent do
3334
end
3435
end
3536

36-
defp environment_name, do: Application.get_env(:sentry, :environment_name)
37-
38-
defp hackney_opts, do: Application.get_env(:sentry, :hackney_opts, [])
39-
4037
defp maybe_send_event do
41-
env_name = environment_name()
38+
env_name = Config.environment_name()
4239
included_envs = included_environments()
4340

4441
if env_name in included_envs do

lib/sentry.ex

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Sentry do
22
use Application
33
import Supervisor.Spec
4-
alias Sentry.Event
4+
alias Sentry.{Event, Config}
55
require Logger
66

77
@moduledoc """
@@ -91,17 +91,14 @@ defmodule Sentry do
9191
See `Sentry.Logger`
9292
"""
9393

94-
@use_error_logger Application.get_env(:sentry, :use_error_logger, false)
95-
@default_environment_name Mix.env
96-
@max_hackney_connections Application.get_env(:sentry, :hackney_pool_max_connections, 50)
97-
@hackney_timeout Application.get_env(:sentry, :hackney_pool_timeout, 5000)
94+
@use_error_logger Config.use_error_logger()
9895

9996
@type task :: {:ok, Task.t} | :error | :excluded | :ignored
10097

10198
def start(_type, _opts) do
10299
children = [
103100
supervisor(Task.Supervisor, [[name: Sentry.TaskSupervisor]]),
104-
:hackney_pool.child_spec(Sentry.Client.hackney_pool_name(), [timeout: @hackney_timeout, max_connections: @max_hackney_connections])
101+
:hackney_pool.child_spec(Sentry.Client.hackney_pool_name(), [timeout: Config.hackney_timeout(), max_connections: Config.max_hackney_connections()])
105102
]
106103
opts = [strategy: :one_for_one, name: Sentry.Supervisor]
107104

@@ -118,7 +115,7 @@ defmodule Sentry do
118115
"""
119116
@spec capture_exception(Exception.t, Keyword.t) :: task
120117
def capture_exception(exception, opts \\ []) do
121-
filter_module = Application.get_env(:sentry, :filter, Sentry.DefaultEventFilter)
118+
filter_module = Config.filter()
122119
{source, opts} = Keyword.pop(opts, :event_source)
123120

124121
if filter_module.exclude_exception?(exception, source) do
@@ -156,9 +153,9 @@ defmodule Sentry do
156153
:ignored
157154
end
158155
def send_event(%Event{} = event, opts) do
159-
included_environments = Application.get_env(:sentry, :included_environments, [:dev, :test, :prod])
160-
environment_name = Application.get_env(:sentry, :environment_name, @default_environment_name)
161-
client = Application.get_env(:sentry, :client, Sentry.Client)
156+
included_environments = Config.included_environments()
157+
environment_name = Config.environment_name()
158+
client = Config.client()
162159

163160
if environment_name in included_environments do
164161
client.send_event(event, opts)

lib/sentry/client.ex

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@ defmodule Sentry.Client do
3434
end
3535
"""
3636

37-
alias Sentry.{Event, Util}
37+
alias Sentry.{Event, Util, Config}
3838

3939
require Logger
4040

4141
@type get_dsn :: {String.t, String.t, Integer.t}
4242
@sentry_version 5
4343
@max_attempts 4
44-
@default_sample_rate 1.0
4544
@hackney_pool_name :sentry_pool
4645

4746
quote do
@@ -60,7 +59,7 @@ defmodule Sentry.Client do
6059
@spec send_event(Event.t) :: {:ok, Task.t | String.t} | :error | :unsampled
6160
def send_event(%Event{} = event, opts \\ []) do
6261
result = Keyword.get(opts, :result, :async)
63-
sample_rate = Keyword.get(opts, :sample_rate) || Application.get_env(:sentry, :sample_rate, @default_sample_rate)
62+
sample_rate = Keyword.get(opts, :sample_rate) || Config.sample_rate()
6463

6564
event = maybe_call_before_send_event(event)
6665

@@ -126,7 +125,7 @@ defmodule Sentry.Client do
126125
Hackney options can be set via the `hackney_opts` configuration option.
127126
"""
128127
def request(method, url, headers, body) do
129-
hackney_opts = Application.get_env(:sentry, :hackney_opts, [])
128+
hackney_opts = Config.hackney_opts()
130129
|> Keyword.put_new(:pool, @hackney_pool_name)
131130
with {:ok, 200, _, client} <- :hackney.request(method, url, headers, body, hackney_opts),
132131
{:ok, body} <- :hackney.body(client),
@@ -186,7 +185,7 @@ defmodule Sentry.Client do
186185
end
187186

188187
def maybe_call_after_send_event(result, event) do
189-
case Application.get_env(:sentry, :after_send_event) do
188+
case Config.after_send_event() do
190189
function when is_function(function, 2) ->
191190
function.(event, result)
192191
{module, function} ->
@@ -201,7 +200,7 @@ defmodule Sentry.Client do
201200
end
202201

203202
def maybe_call_before_send_event(event) do
204-
case Application.get_env(:sentry, :before_send_event) do
203+
case Config.before_send_event do
205204
function when is_function(function, 1) ->
206205
function.(event)
207206
{module, function} ->

lib/sentry/config.ex

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
defmodule Sentry.Config do
2+
@default_included_environments [:dev, :test, :prod]
3+
@default_environment_name Mix.env
4+
@default_max_hackney_connections 50
5+
@default_hackney_timeout 5000
6+
@default_exclude_patterns [~r"/_build/", ~r"/deps/", ~r"/priv/"]
7+
@default_path_pattern "**/*.ex"
8+
@default_context_lines 3
9+
@default_sample_rate 1.0
10+
11+
def validate_config! do
12+
end
13+
14+
def included_environments do
15+
Application.get_env(:sentry, :included_environments, @default_included_environments)
16+
end
17+
18+
def environment_name do
19+
Application.get_env(:sentry, :environment_name, @default_environment_name)
20+
end
21+
22+
def max_hackney_connections do
23+
Application.get_env(:sentry, :hackney_pool_max_connections, @default_max_hackney_connections)
24+
end
25+
26+
def hackney_timeout do
27+
Application.get_env(:sentry, :hackney_pool_timeout, @default_hackney_timeout)
28+
end
29+
30+
def tags do
31+
Application.get_env(:sentry, :tags, %{})
32+
end
33+
34+
def release do
35+
Application.get_env(:sentry, :release)
36+
end
37+
38+
def server_name do
39+
Application.get_env(:sentry, :server_name)
40+
end
41+
42+
def filter do
43+
Application.get_env(:sentry, :filter, Sentry.DefaultEventFilter)
44+
end
45+
46+
def client do
47+
Application.get_env(:sentry, :client, Sentry.Client)
48+
end
49+
50+
def use_error_logger do
51+
Application.get_env(:sentry, :use_error_logger, false)
52+
end
53+
54+
def root_path do
55+
Application.fetch_env!(:sentry, :root_source_code_path)
56+
end
57+
58+
def path_pattern do
59+
Application.get_env(:sentry, :source_code_path_pattern, @default_path_pattern)
60+
end
61+
62+
def exclude_patterns do
63+
Application.get_env(:sentry, :source_code_exclude_patterns, @default_exclude_patterns)
64+
end
65+
66+
def context_lines do
67+
Application.get_env(:sentry, :context_lines, @default_context_lines)
68+
end
69+
70+
def in_app_module_whitelist do
71+
Application.get_env(:sentry, :in_app_module_whitelist, [])
72+
end
73+
74+
def sample_rate do
75+
Application.get_env(:sentry, :sample_rate, @default_sample_rate)
76+
end
77+
78+
def hackney_opts do
79+
Application.get_env(:sentry, :hackney_opts, [])
80+
end
81+
82+
def before_send_event do
83+
Application.get_env(:sentry, :before_send_event)
84+
end
85+
86+
def after_send_event do
87+
Application.get_env(:sentry, :after_send_event)
88+
end
89+
end

lib/sentry/event.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ defmodule Sentry.Event do
3232

3333
@type t :: %__MODULE__{}
3434

35-
alias Sentry.{Event, Util}
35+
alias Sentry.{Event, Util, Config}
3636
@source_code_context_enabled Application.fetch_env!(:sentry, :enable_source_code_context)
3737
@source_files if(@source_code_context_enabled, do: Sentry.Sources.load_files(), else: nil)
3838

@@ -70,7 +70,7 @@ defmodule Sentry.Event do
7070
|> Map.merge(Keyword.get(opts, :extra, %{}))
7171
user = user_context
7272
|> Map.merge(Keyword.get(opts, :user, %{}))
73-
tags = Application.get_env(:sentry, :tags, %{})
73+
tags = Config.tags()
7474
|> Map.merge(tags_context)
7575
|> Map.merge(Keyword.get(opts, :tags, %{}))
7676
request = request_context
@@ -79,11 +79,11 @@ defmodule Sentry.Event do
7979

8080
level = Keyword.get(opts, :level, "error")
8181

82-
release = Application.get_env(:sentry, :release)
82+
release = Config.release()
8383

84-
server_name = Application.get_env(:sentry, :server_name)
84+
server_name = Config.server_name()
8585

86-
env = Application.get_env(:sentry, :environment_name)
86+
env = Config.environment_name()
8787

8888
%Event{
8989
culprit: culprit_from_stacktrace(stacktrace),
@@ -160,7 +160,7 @@ defmodule Sentry.Event do
160160

161161
@spec stacktrace_to_frames(Exception.stacktrace) :: [map]
162162
def stacktrace_to_frames(stacktrace) do
163-
in_app_module_whitelist = Application.get_env(:sentry, :in_app_module_whitelist, [])
163+
in_app_module_whitelist = Config.in_app_module_whitelist()
164164
stacktrace
165165
|> Enum.map(fn(line) ->
166166
{mod, function, arity, location} = line

lib/sentry/sources.ex

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule Sentry.Sources do
2+
alias Sentry.Config
23
@moduledoc """
34
This module is responsible for providing functionality that stores
45
the text of source files during compilation for displaying the
@@ -49,14 +50,10 @@ defmodule Sentry.Sources do
4950
@type file_map :: %{pos_integer() => String.t}
5051
@type source_map :: %{String.t => file_map}
5152

52-
@default_exclude_patterns [~r"/_build/", ~r"/deps/", ~r"/priv/"]
53-
@default_path_pattern "**/*.ex"
54-
@default_context_lines 3
55-
5653
def load_files do
57-
root_path = Application.fetch_env!(:sentry, :root_source_code_path)
58-
path_pattern = Application.get_env(:sentry, :source_code_path_pattern, @default_path_pattern)
59-
exclude_patterns = Application.get_env(:sentry, :source_code_exclude_patterns, @default_exclude_patterns)
54+
root_path = Config.root_path()
55+
path_pattern = Config.path_pattern()
56+
exclude_patterns = Config.exclude_patterns()
6057

6158
Path.join(root_path, path_pattern)
6259
|> Path.wildcard()
@@ -83,7 +80,7 @@ defmodule Sentry.Sources do
8380
"""
8481
@spec get_source_context(source_map, String.t, pos_integer()) :: {[String.t], String.t | nil, [String.t]}
8582
def get_source_context(files, file_name, line_number) do
86-
context_lines = Application.get_env(:sentry, :context_lines, @default_context_lines)
83+
context_lines = Config.context_lines()
8784
file = Map.get(files, file_name)
8885

8986
do_get_source_context(file, line_number, context_lines)

0 commit comments

Comments
 (0)