Skip to content

Commit

Permalink
Merge pull request #988 from eyra/sentry-hotfix
Browse files Browse the repository at this point in the history
Integrated Sentry Event Logging for better crash management
  • Loading branch information
mellelieuwes authored Oct 23, 2024
2 parents 22d431e + e65201d commit 0ba39c8
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

## \#3 unreleased

* Added: Sentry support for crash management
* Added: Snapchat support for assignment tasks
* Added: Confirmation dialog when skipping content pages or consent pages in assignment configuration.

Expand Down
5 changes: 0 additions & 5 deletions core/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,6 @@ config :core, BankingClient,
certfile: "../banking_proxy/certs/client_certificate.pem",
keyfile: "../banking_proxy/certs/client_key.pem"

config :sentry,
included_environments: ~w(prod test),
environment_name: Mix.env(),
tags: %{app_version: System.get_env("VERSION", "dev")}

module =
case Code.ensure_compiled(Bundle) do
{:module, module} ->
Expand Down
4 changes: 3 additions & 1 deletion core/config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ if config_env() == :prod do
if sentry_dsn = System.get_env("SENTRY_DSN") do
config :sentry,
dsn: sentry_dsn,
environment_name: System.get_env("RELEASE_ENV") || "prod"
environment_name: app_domain,
enable_source_code_context: true,
root_source_code_paths: [File.cwd!()]
end

config :core, :storage,
Expand Down
2 changes: 2 additions & 0 deletions core/lib/core/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ defmodule Core.Application do
use Application

def start(_type, _args) do
:logger.add_handler(:sentry_handler, Sentry.LoggerHandler, %{})

topologies = [
example: [
strategy: Cluster.Strategy.Epmd,
Expand Down
75 changes: 75 additions & 0 deletions core/lib/core/sentry/gen.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
defmodule Core.Sentry.Gen do
alias Sentry.Config
require Logger

defp print(message) do
Logger.notice(message)
end

@moduledoc """
Sends a test event to Sentry to check if your Sentry configuration is correct.
This task reports a `RuntimeError` exception like this one:
%RuntimeError{message: "Testing sending Sentry event"}
## Options
* `compile` - boolean, use to does not compile, even if files require compilation.
* `type` - `exception` or `message`. Defaults to `exception`. *Available since v10.1.0.*.
* `stacktrace` - does not include a stacktrace in the reported event. *Available since
v10.1.0.*.
"""

def test_event() do
{:ok, _} = Application.ensure_all_started(:sentry)

print_environment_info()

if Config.dsn() do
send_event()
else
print("Event not sent because the :dsn option is not set (or set to nil)")
end
end

defp print_environment_info do
print("Client configuration:")

if dsn = Config.dsn() do
print("server: #{dsn.endpoint_uri}")
print("public_key: #{dsn.public_key}")
print("secret_key: #{dsn.secret_key}")
end

print("current environment_name: #{inspect(to_string(Config.environment_name()))}")
print("hackney_opts: #{inspect(Config.hackney_opts())}\n")
end

defp send_event() do
{:current_stacktrace, stacktrace} = Process.info(self(), :current_stacktrace)

print("Sending test event...")

exception = %RuntimeError{message: "Testing sending Sentry event"}
result = Sentry.capture_exception(exception, result: :sync, stacktrace: stacktrace)

case result do
{:ok, id} ->
print("Test event sent, event ID: #{id}")

{:error, reason} ->
print("Error sending event:\n\n#{inspect(reason)}")

:excluded ->
print("No test event was sent because the event was excluded by a filter")

:unsampled ->
print("""
No test event was sent because the event was excluded according to the :sample_rate \
configuration option.
""")
end
end
end
16 changes: 16 additions & 0 deletions core/lib/core/sentry/mix.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Mix.Tasks.Core.Sentry.Gen.TestEvent do
@moduledoc """
Generates a Sentry Test Event.
Usage:
$ mix core.sentry.gen.testevent
"""

use Mix.Task

@shortdoc "Generate Sentry Test Event"
def run(_args) do
Mix.Task.run("app.start")
Core.Sentry.Gen.test_event()
end
end
3 changes: 2 additions & 1 deletion core/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ defmodule Core.MixProject do
{:logger_json, "~> 4.3"},
{:mime, "~> 2.0"},
{:nimble_parsec, "~> 1.2"},
{:nimble_options, "~> 1.0.0"},
{:oban, "~> 2.13.3"},
{:packmatic, "~> 1.2.0"},
{:phoenix_ecto, "~> 4.4"},
Expand All @@ -99,7 +100,7 @@ defmodule Core.MixProject do
{:plug_cowboy, "~> 2.5"},
{:postgrex, ">= 0.15.13"},
{:remote_ip, "~> 1.1"},
{:sentry, "~> 8.0"},
{:sentry, "~> 10.7"},
{:slugify, "~> 1.3"},
{:statistics, "~> 0.6.2"},
{:tailwind, "~> 0.2.0", runtime: Mix.env() == :dev},
Expand Down
19 changes: 10 additions & 9 deletions core/mix.lock

Large diffs are not rendered by default.

0 comments on commit 0ba39c8

Please sign in to comment.