Skip to content

Commit

Permalink
Accept all environments by default (#604)
Browse files Browse the repository at this point in the history
  • Loading branch information
whatyouhide authored Sep 4, 2023
1 parent 38e80ed commit f1d4f70
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Removed `Sentry.Event.do_put_source_context/3`
- Removed the `:async` value for the `:result` option in `Sentry.send_event/2` (and friends)
- Removed the `Sentry.Event.sentry_exception/0` type
- Accept all environments by default by changing the default for the `:included_environments` configuration option from `[:prod]` to `:all`

### Improvements

Expand Down
8 changes: 4 additions & 4 deletions lib/mix/tasks/sentry.send_test_event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ defmodule Mix.Tasks.Sentry.SendTestEvent do
Mix.shell().info("public_key: #{public_key}")
Mix.shell().info("secret_key: #{secret_key}")
Mix.shell().info("included_environments: #{inspect(included_environments())}")
Mix.shell().info("current environment_name: #{inspect(Config.environment_name())}")
Mix.shell().info("current environment_name: #{inspect(to_string(Config.environment_name()))}")
Mix.shell().info("hackney_opts: #{inspect(Config.hackney_opts())}\n")
end

defp included_environments do
case Application.fetch_env(:sentry, :included_environments) do
{:ok, envs} when is_list(envs) ->
{:ok, envs} when is_list(envs) or envs == :all ->
envs

_ ->
Expand All @@ -43,10 +43,10 @@ defmodule Mix.Tasks.Sentry.SendTestEvent do
end

defp maybe_send_event do
env_name = Config.environment_name()
env_name = to_string(Config.environment_name())
included_envs = included_environments()

if env_name in included_envs do
if included_envs == :all or env_name in included_envs do
Mix.shell().info("Sending test event...")

{:current_stacktrace, stacktrace} = Process.info(self(), :current_stacktrace)
Expand Down
32 changes: 22 additions & 10 deletions lib/sentry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,24 @@ defmodule Sentry do
}
The `:environment_name` and `:included_environments` options work together to determine
if and when Sentry should record exceptions. The `en:vironment_name` is the
if and when Sentry should record exceptions. The `:environment_name` is the
name of the current environment. In the example above, we have explicitly set
the environment to `:prod` which works well if you are inside an environment
specific configuration `config/prod.exs`.
An alternative is to use `Config.config_env/0` in your general configuration file:
An alternative is to use `Config.config_env/0` in your general `config/config.exs`
configuration file:
config :sentry, dsn: "https://public:secret@app.getsentry.com/1",
included_environments: [:prod],
included_environments: ["prod"],
environment_name: config_env()
This will set the environment name to whatever the current environment
is, but it will only send events if the current environment is `:prod`,
since that is the only entry in the `:included_environments` key.
You can even rely on more specific logic to determine the environment name. It's
not uncommmon for most applications to have a "staging" environment. In order
not uncommon for most applications to have a "staging" environment. In order
to handle this without adding an additional Mix environment, you can set an
environment variable that determines the release level. By default, Sentry
picks up the `SENTRY_ENVIRONMENT` variable. Otherwise, you can read the
Expand All @@ -51,7 +52,7 @@ defmodule Sentry do
# In config/runtime.exs
config :sentry, dsn: "https://public:secret@app.getsentry.com/1",
included_environments: ~w(production staging),
included_environments: ["production", "staging"],
environment_name: System.get_env("RELEASE_LEVEL", "development")
In this example, we are getting the environment name from the `RELEASE_LEVEL`
Expand All @@ -60,6 +61,15 @@ defmodule Sentry do
our local development machines, exceptions will never be sent, because the
default value is not in the list of `:included_environments`.
> #### Using the DSN To Send Events {: .warning}
>
> We recommend to use the `:dsn` configuration to control whether to report
> events. If `:dsn` is not set (or set to `nil`), then we won't report
> events to Sentry. Thanks to this behavior, you can essentially
> only set `:dsn` in environments where you want to report events to Sentry.
> In the future, we might remove the `:included_environments` configuration
> altogether.
Sentry supports many configuration options. See the [*Configuration*
section](#module-configuration) for complete documentation.
Expand Down Expand Up @@ -90,9 +100,11 @@ defmodule Sentry do
If the `SENTRY_ENVIRONMENT` environment variable is set, it will
be used as the defaults value. Otherwise, defaults to `"dev"`.
* `:included_environments` (list of `t:atom/0` or `t:String.t/0`) -
the environments in which Sentry can report events. `:environment_name`
needs to be in this list for events to be reported. Defaults to `[:prod]`.
* `:included_environments` (list of `t:atom/0` or `t:String.t/0`, or the atom `:all`) -
the environments in which Sentry can report events. If this is a list,
then `:environment_name` needs to be in this list for events to be reported.
If this is `:all`, then Sentry will report events regardless of the value
of `:environment_name`. Defaults to `:all`.
* `:tags` (`t:map/0`) - a map of tags to be sent with every event.
Defaults to `%{}`.
Expand Down Expand Up @@ -463,9 +475,9 @@ defmodule Sentry do

def send_event(%Event{} = event, opts) do
included_environments = Config.included_environments()
environment_name = Config.environment_name()
environment_name = to_string(Config.environment_name())

if environment_name in included_environments do
if included_environments == :all or environment_name in included_environments do
Sentry.Client.send_event(event, opts)
else
:ignored
Expand Down
44 changes: 26 additions & 18 deletions lib/sentry/config.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule Sentry.Config do
@moduledoc false

@default_included_environments [:prod]
@default_max_hackney_connections 50
@default_hackney_timeout 5000
@default_exclude_patterns [~r"/_build/", ~r"/deps/", ~r"/priv/"]
Expand Down Expand Up @@ -32,25 +31,34 @@ defmodule Sentry.Config do

@spec validate_included_environments!() :: :ok
def validate_included_environments! do
case included_environments() do
comma_separated_envs when is_binary(comma_separated_envs) ->
IO.warn("""
setting :included_environments to a comma-separated string is deprecated and won't \
be supported in the next major version. Set :included_environments to a list of \
atoms instead.\
""")

Application.put_env(
:sentry,
:included_environments,
normalized_environments =
case included_environments() do
comma_separated_envs when is_binary(comma_separated_envs) ->
IO.warn("""
setting :included_environments to a comma-separated string is deprecated and won't \
be supported in the next major version. Set :included_environments to a list of \
atoms instead.\
""")

String.split(comma_separated_envs, ",")
)

list_of_atoms when is_list(list_of_atoms) ->
:ok
end
list when is_list(list) ->
Enum.map(list, fn
env when is_atom(env) or is_binary(env) ->
to_string(env)

:ok
other ->
raise ArgumentError, """
expected environments in :included_environments to be atoms or strings, \
got: #{inspect(other)}\
"""
end)

:all ->
:all
end

:ok = Application.put_env(:sentry, :included_environments, normalized_environments)
end

@spec assert_dsn_has_no_query_params!() :: :ok
Expand Down Expand Up @@ -87,7 +95,7 @@ defmodule Sentry.Config do
end

def included_environments do
Application.get_env(:sentry, :included_environments, @default_included_environments)
Application.get_env(:sentry, :included_environments, :all)
end

def environment_name do
Expand Down
10 changes: 10 additions & 0 deletions pages/upgrade-9.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ The settings that are now *compile-time settings* are:
* `:source_code_path_pattern`
* `:source_code_exclude_patterns`

## Check Your Use of `:included_environments`

If you were relying on the default value for the `:included_environments` configuration option (which was `[:prod]`), then you'll need to make an adjustment. In v9.0.0, the default value is now `:all` (see [this issue](https://github.com/getsentry/sentry-elixir/issues/483) for some context). If you want to keep the old behavior, explicitly set this in your configuration:

```elixir
# In config/config.exs
config :sentry,
included_environments: [:prod]
```

## Stop Using `Sentry.Sources`

`Sentry.Sources` was meant to be private API and has been removed. Its functionality is very specific to Sentry, and it's not a good general mechanism to retrieve source code. This way, we can also have the freedom to improve this functionality without making potential breaking changes to the API of this library.
Expand Down
15 changes: 8 additions & 7 deletions test/mix/sentry.send_test_event_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ defmodule Mix.Tasks.Sentry.SendTestEventTest do
public_key: public
secret_key: secret
included_environments: []
current environment_name: :test
current environment_name: "test"
hackney_opts: [recv_timeout: 50]
:test is not in [] so no test event will be sent
"test" is not in [] so no test event will be sent
"""
end

Expand All @@ -35,7 +35,8 @@ defmodule Mix.Tasks.Sentry.SendTestEventTest do

modify_env(
:sentry,
dsn: "http://public:secret@localhost:#{bypass.port}/1"
dsn: "http://public:secret@localhost:#{bypass.port}/1",
included_environments: :all
)

assert capture_io(fn ->
Expand All @@ -45,8 +46,8 @@ defmodule Mix.Tasks.Sentry.SendTestEventTest do
server: http://localhost:#{bypass.port}/api/1/envelope/
public_key: public
secret_key: secret
included_environments: [:test]
current environment_name: :test
included_environments: :all
current environment_name: "test"
hackney_opts: [recv_timeout: 50]
Sending test event...
Expand Down Expand Up @@ -75,8 +76,8 @@ defmodule Mix.Tasks.Sentry.SendTestEventTest do
server: http://localhost:#{bypass.port}/api/1/envelope/
public_key: public
secret_key: secret
included_environments: [:test]
current environment_name: :test
included_environments: ["test"]
current environment_name: "test"
hackney_opts: [recv_timeout: 50]
Sending test event...
Expand Down

0 comments on commit f1d4f70

Please sign in to comment.