getsentry/sentry-elixir

No Events Show Up When Running Server

xanderdunn opened this issue ยท 12 comments

Environment

  • Elixir version (elixir -v): 1.5.1
  • Erlang/OTP version (erl): Erlang/OTP 20 [erts-9.0.5] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
  • Sentry version (mix deps): 6.0.3
  • Operating system: macOS 10.13.1

Description

Full diff of changes to add sentry to my project:

diff --git a/config/config.exs b/config/config.exs
index ac0d6bf..3250b5c 100644
--- a/config/config.exs
+++ b/config/config.exs
@@ -32,6 +32,13 @@ config :api_tournament, :sendgrid,
   signup_template: "2b592f2e-7928-41eb-b0c3-a7ca76bcddee",
   changed_template: "e683d87d-0e7e-437b-bad5-ab9913c9676d"
 
+config :sentry, dsn: "https://PUBLIC:SECRET@sentry.io/PORT",
+   included_environments: [:prod, :dev],
+   enable_source_code_context: true,
+   root_source_code_path: File.cwd!,
+   in_app_module_whitelist: [APITournament],
+   environment_name: Mix.env
+
 # Import environment specific config. This must remain at the bottom
 # of this file so it overrides the configuration defined above.
 import_config "#{Mix.env}.exs"
diff --git a/lib/application.ex b/lib/application.ex
index 8076c3d..7117978 100644
--- a/lib/application.ex
+++ b/lib/application.ex
@@ -37,6 +37,7 @@ defmodule APITournament.Application do
     # See https://hexdocs.pm/elixir/Supervisor.html
     # for other strategies and supported options
     opts = [strategy: :one_for_one, name: APITournament.Supervisor]
+    :ok = :error_logger.add_report_handler(Sentry.Logger)
     Supervisor.start_link(children, opts)
   end
 
diff --git a/lib/endpoint.ex b/lib/endpoint.ex
index 765d93d..4db57e4 100644
--- a/lib/endpoint.ex
+++ b/lib/endpoint.ex
@@ -8,6 +8,8 @@ defmodule APITournament.Endpoint do
   """
   use Phoenix.Endpoint, otp_app: :api_tournament
   use Absinthe.Phoenix.Endpoint
+  use Plug.ErrorHandler
+  use Sentry.Plug
 
   socket "/sock", APITournament.MainSocket
 
diff --git a/mix.exs b/mix.exs
index 8c2e04c..05fca95 100644
--- a/mix.exs
+++ b/mix.exs
@@ -28,7 +28,11 @@ defmodule APITournament.Mixfile do
   def application do
     [
       mod: {APITournament.Application, []},
-      extra_applications: [:logger, :runtime_tools, :cloud_watch, :ex_aws],
+      extra_applications: [:sentry,
+                           :logger,
+                           :runtime_tools,
+                           :cloud_watch,
+                           :ex_aws],
       included_applications: [:qrcode],
     ]
   end
@@ -80,6 +84,7 @@ defmodule APITournament.Mixfile do
       {:nimble_csv,       "~> 0.3"},
       {:math,             "~> 0.3"},
       {:cors_plug,        "~> 1.2"},
+      {:sentry,           "~> 6.0.0"},
     ]
   end
 
diff --git a/mix.lock b/mix.lock
index 23d0bbb..24f2d2c 100644
--- a/mix.lock
+++ b/mix.lock
@@ -54,6 +54,7 @@
   "qrcode": {:git, "https://github.com/numerai/qrcode", "55192958b08692fdb9444c47e66a6985f3217fb1", [ref: "5519295"]},
   "ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], [], "hexpm"},
   "sendgrid": {:hex, :sendgrid, "1.7.0", "a1c31fed719483b202d91ec7c8a5db79e1104dae04cb40e0c5dfe0bc53a0abeb", [], [{:httpoison, ">= 0.11.0", [hex: :httpoison, repo: "hexpm", optional: false]}, {:poison, ">= 2.0.0 or >= 3.0.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"},
+  "sentry": {:hex, :sentry, "6.0.3", "e83f21901008a6c76ff81cc696a053fbd1d6a12600050d3e61928c7945b3621e", [], [{:hackney, "~> 1.8 or 1.6.5", [hex: :hackney, repo: "hexpm", optional: false]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}, {:poison, "~> 1.5 or ~> 2.0 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}, {:uuid, "~> 1.0", [hex: :uuid, repo: "hexpm", optional: false]}], "hexpm"},
   "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], [], "hexpm"},
   "sweet_xml": {:hex, :sweet_xml, "0.6.5", "dd9cde443212b505d1b5f9758feb2000e66a14d3c449f04c572f3048c66e6697", [], [], "hexpm"},
   "timex": {:hex, :timex, "3.1.24", "d198ae9783ac807721cca0c5535384ebdf99da4976be8cefb9665a9262a1e9e3", [:mix], [{:combine, "~> 0.7", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 0.1.8 or ~> 0.5", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm"},

PUBLIC, SECRET, and PORT are filled in with the correct values on my end and running MIX_ENV=dev mix sentry.send_test_event successfully causes an issue to show up in sentry.io. However, when I run my server with mix phx.server and cause an error with raise "Sentry Test Error 12345" in one of my endpoints, nothing appears on sentry.io. I'm doing all of this testing locally on my Mac in the dev environment.

Thanks for opening this!

Everything does look correct. Can you gist the endpoint or route?

Thanks for your help. We don't have a router. We have a single / GraphQL endpoint that receives all requests. I've created a secret gist with our full endpoint.ex and sent the link to Kelly. Let me know where to contact you directly if that will be easier.

@xanderdunn I'm not able to reproduce it locally from the gist. The config and code where you cause the error could help narrow it down as well. A full repo where I can test myself would be ideal, but I understand if that is difficult.

I can be contacted directly in the Elixir Slack (@mitchellhenke) if that is easier ๐Ÿ™‚

Hi @mitchellhenke, I'm having issues getting the Sentry error logger to send too.

I've put together a repo reproducing our scenario: https://github.com/amencarini/sentry-test

You can reproduce by doing:

curl -X "OPTIONS" "http://localhost:4000/" \
     -H 'Access-Control-Request-Method: POST' \
     -H 'Origin: any'

Happens on elixir 1.5.1 and 1.6.0-dev. The mix sentry.send_test_event reports correctly.
I'm also on the Elixir Slack under @amencarini if you quickly need to get in touch ๐Ÿ™‚

If that helps, I tried to debug the situation a bit and it seems that this function head (lib/sentry/logger.ex:49) is never hit

def handle_event({:error_report, _gl, {_pid, _type, [message | _]}}, state) when is_list(message) do

No :error_report event seems to be generated despite Sentry.Logger being added to the report handlers.

@amencarini thank you for the repo! I will take a look.

Got a bit delayed, but planning on looking into this tomorrow.

Thanks for the update ๐Ÿ˜ƒ

@amencarini apologies ahead of time as this is a part of Phoenix I'm not super knowledgable on.

The Corsica plug being in the Endpoint instead of the Router means the error gets caught here: https://github.com/phoenixframework/phoenix/blob/8b8e11305079413fa8b3ace36d9871c0e4e63f7c/lib/phoenix/endpoint.ex#L691-L702

      def call(conn, opts) do
        conn = put_in conn.secret_key_base, config(:secret_key_base)
        conn = put_in conn.script_name, script_name()
        conn = Plug.Conn.put_private(conn, :phoenix_endpoint, __MODULE__)

        try do
          super(conn, opts)
        catch
          kind, reason ->
            Phoenix.Endpoint.RenderErrors.__catch__(conn, kind, reason, @phoenix_render_errors)
        end
      end

I will have to do some research on how we can handle this.

I see. Thanks for trying that out! We'll see if we can handle the situation differently.

It's definitely solvable, but may require some heavier configuration when adding Sentry.

Sentry.Plug relies on Plug.ErrorHandler, but Phoenix.Endpoint doesn't work directly with that. The call/2 in the Endpoint is overridable, so you could add a similar Sentry.capture_exception in the catch, which may be what we have to do in Sentry.

I'll be exploring other options too, but hopefully your use case is solvable with that information for now. If you run into anything that could be helpful, I'd be happy to hear it ๐Ÿ™‚

To follow up on this, I've opened phoenixframework/phoenix#2791 with things I've tried and a suggested solution if none exists currently.