123456 |
- {
- "html": "<div class=\"section\" id=\"installation\">\n<h2>Installation</h2>\n<p>Edit your mix.exs file to add it as a dependency and add the <code class=\"docutils literal\"><span class=\"pre\">:sentry</span></code> package to your applications:</p>\n<div class=\"highlight-elixir\"><div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\"><span></span><span style=\"color: #2eb0f7\">defp</span> <span style=\"color: #111111\">application</span> <span style=\"color: #2eb0f7\">do</span>\n <span style=\"color: #111111\">[</span><span style=\"color: #e8535a\">applications</span><span style=\"color: #111111\">:</span> <span style=\"color: #111111\">[</span><span style=\"color: #e8535a\">:sentry</span><span style=\"color: #111111\">,</span> <span style=\"color: #e8535a\">:logger</span><span style=\"color: #111111\">]]</span>\n<span style=\"color: #2eb0f7\">end</span>\n\n<span style=\"color: #2eb0f7\">defp</span> <span style=\"color: #111111\">deps</span> <span style=\"color: #2eb0f7\">do</span>\n <span style=\"color: #111111\">[{</span><span style=\"color: #e8535a\">:sentry</span><span style=\"color: #111111\">,</span> <span style=\"color: #e8535a\">"~> 6.0.0"</span><span style=\"color: #111111\">}]</span>\n<span style=\"color: #2eb0f7\">end</span>\n</pre></div>\n</div>\n</div>\n\n\n<div class=\"section\" id=\"configuration\">\n<h2>Configuration</h2>\n<p>Setup the application production environment in your <code class=\"docutils literal\"><span class=\"pre\">config/prod.exs</span></code></p>\n<div class=\"highlight-elixir\"><div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\"><span></span><span style=\"color: #111111\">config</span> <span style=\"color: #e8535a\">:sentry</span><span style=\"color: #111111\">,</span>\n <span style=\"color: #e8535a\">dsn</span><span style=\"color: #111111\">:</span> <span style=\"color: #e8535a\">"https://public:secret@app.getsentry.com/1"</span><span style=\"color: #111111\">,</span>\n <span style=\"color: #e8535a\">environment_name</span><span style=\"color: #111111\">:</span> <span style=\"color: #e8535a\">:prod</span><span style=\"color: #111111\">,</span>\n <span style=\"color: #e8535a\">enable_source_code_context</span><span style=\"color: #111111\">:</span> <span style=\"color: #111111\">true,</span>\n <span style=\"color: #e8535a\">root_source_code_path</span><span style=\"color: #111111\">:</span> <span style=\"color: #111111\">File</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">cwd!,</span>\n <span style=\"color: #e8535a\">tags</span><span style=\"color: #111111\">:</span> <span style=\"color: #111111\">%{</span>\n <span style=\"color: #e8535a\">env</span><span style=\"color: #111111\">:</span> <span style=\"color: #e8535a\">"production"</span>\n <span style=\"color: #111111\">},</span>\n <span style=\"color: #e8535a\">included_environments</span><span style=\"color: #111111\">:</span> <span style=\"color: #111111\">[</span><span style=\"color: #e8535a\">:prod</span><span style=\"color: #111111\">]</span>\n</pre></div>\n</div>\n<p>The <code class=\"docutils literal\"><span class=\"pre\">environment_name</span></code> and <code class=\"docutils literal\"><span class=\"pre\">included_environments</span></code> work together to determine\nif and when Sentry should record exceptions. The <code class=\"docutils literal\"><span class=\"pre\">environment_name</span></code> is the\nname of the current environment. In the example above, we have explicitly set\nthe environment to <code class=\"docutils literal\"><span class=\"pre\">:prod</span></code> which works well if you are inside an environment\nspecific configuration like <code class=\"docutils literal\"><span class=\"pre\">config/prod.exs</span></code>.</p>\n<p>An alternative is to use <code class=\"docutils literal\"><span class=\"pre\">Mix.env</span></code> in your general configuration file:</p>\n<div class=\"highlight-elixir\"><div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\"><span></span><span style=\"color: #111111\">config</span> <span style=\"color: #e8535a\">:sentry</span><span style=\"color: #111111\">,</span> <span style=\"color: #e8535a\">dsn</span><span style=\"color: #111111\">:</span> <span style=\"color: #e8535a\">"https://public:secret@app.getsentry.com/1"</span>\n <span style=\"color: #e8535a\">included_environments</span><span style=\"color: #111111\">:</span> <span style=\"color: #111111\">[</span><span style=\"color: #e8535a\">:prod</span><span style=\"color: #111111\">],</span>\n <span style=\"color: #e8535a\">environment_name</span><span style=\"color: #111111\">:</span> <span style=\"color: #111111\">Mix</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">env</span>\n</pre></div>\n</div>\n<p>This will set the environment name to whatever the current Mix environment\natom is, but it will only send events if the current environment is <code class=\"docutils literal\"><span class=\"pre\">:prod</span></code>,\nsince that is the only entry in the <code class=\"docutils literal\"><span class=\"pre\">included_environments</span></code> key.</p>\n<p>You can even rely on more custom determinations of the environment name. It’s\nnot uncommmon for most applications to have a “staging” environment. In order\nto handle this without adding an additional Mix environment, you can set an\nenvironment variable that determines the release level.</p>\n<div class=\"highlight-elixir\"><div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\"><span></span><span style=\"color: #111111\">config</span> <span style=\"color: #e8535a\">:sentry</span><span style=\"color: #111111\">,</span> <span style=\"color: #e8535a\">dsn</span><span style=\"color: #111111\">:</span> <span style=\"color: #e8535a\">"https://public:secret@app.getsentry.com/1"</span>\n <span style=\"color: #e8535a\">included_environments</span><span style=\"color: #111111\">:</span> <span style=\"color: #e8535a\">~w(production staging)</span><span style=\"color: #111111\">,</span>\n <span style=\"color: #e8535a\">environment_name</span><span style=\"color: #111111\">:</span> <span style=\"color: #111111\">System</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">get_env(</span><span style=\"color: #e8535a\">"RELEASE_LEVEL"</span><span style=\"color: #111111\">)</span> <span style=\"color: #4b4f5c\">||</span> <span style=\"color: #e8535a\">"development"</span>\n</pre></div>\n</div>\n<p>In this example, we are getting the environment name from the <code class=\"docutils literal\"><span class=\"pre\">RELEASE_LEVEL</span></code>\nenvironment variable. If that variable does not exist, we default to <code class=\"docutils literal\"><span class=\"pre\">"development"</span></code>.\nNow, on our servers, we can set the environment variable appropriately. On\nour local development machines, exceptions will never be sent, because the\ndefault value is not in the list of <code class=\"docutils literal\"><span class=\"pre\">included_environments</span></code>.</p>\n<p>If using an environment with Plug or Phoenix add the following to your router:</p>\n<div class=\"highlight-elixir\"><div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\"><span></span><span style=\"color: #2eb0f7\">use</span> <span style=\"color: #111111\">Plug.ErrorHandler</span>\n<span style=\"color: #2eb0f7\">use</span> <span style=\"color: #111111\">Sentry.Plug</span>\n</pre></div>\n</div>\n</div>\n\n\n<div class=\"section\" id=\"capturing-errors\">\n<h2>Capturing Errors</h2>\n<p>If you use the error logger and setup Plug/Phoenix then you are already done, all errors will bubble up to\nsentry.</p>\n<p>Otherwise we provide a simple way to capture exceptions:</p>\n<div class=\"highlight-elixir\"><div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\"><span></span><span style=\"color: #2eb0f7\">do</span>\n <span style=\"color: #111111\">ThisWillError</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">reall()</span>\n<span style=\"color: #2eb0f7\">rescue</span>\n <span style=\"color: #111111\">my_exception</span> <span style=\"color: #4b4f5c\">-></span>\n <span style=\"color: #111111\">Sentry</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">capture_exception(my_exception,</span> <span style=\"color: #111111\">[</span><span style=\"color: #e8535a\">stacktrace</span><span style=\"color: #111111\">:</span> <span style=\"color: #111111\">System</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">stacktrace(),</span> <span style=\"color: #e8535a\">extra</span><span style=\"color: #111111\">:</span> <span style=\"color: #111111\">%{</span><span style=\"color: #e8535a\">extra</span><span style=\"color: #111111\">:</span> <span style=\"color: #111111\">information}])</span>\n<span style=\"color: #2eb0f7\">end</span>\n</pre></div>\n</div>\n</div>\n",
- "link": "https://docs.getsentry.com/clients/elixir/",
- "id": "elixir",
- "name": "Elixir"
- }
|