Skip to main content

Track errors in Elixir and Phoenix

Start PostDeploy in your supervision tree and add one plug. Process crashes reach Observe through the logger, with the request and user you attach.

Samples run on , facts checked on

Before you begin

  • An Elixir project with a supervision tree.
  • An Observe key that starts with obs_. Read Get an ingest key.
  • Version 0.2.0 of the package. It depends on req, jason and telemetry.

Install

mix.exs elixir
# mix.exs
{:postdeploy_sdk, "~> 0.2"}

Run mix deps.get, then set POSTDEPLOY_API_KEY. POSTDEPLOY_ENVIRONMENT and POSTDEPLOY_RELEASE are optional.

Phoenix and Plug

Start PostDeploy before the endpoint. It attaches a logger handler that captures crashes, and it listens for Phoenix error renders. PostDeploy.Plug resets the scope for each request and adds the method, host, path and allow-listed headers.

application.ex elixir
# lib/my_app/application.ex
children = [
  {PostDeploy, []},
  MyAppWeb.Endpoint
]

# lib/my_app_web/endpoint.ex, first plug
plug PostDeploy.Plug

Capture an error yourself

Pass the stacktrace with the exception. Options given to {PostDeploy, opts} win over config :postdeploy_sdk, config: [...], which wins over environment variables.

lib/my_app/mailer.ex elixir
PostDeploy.set_user(%{id: "user_789", email: "[email protected]"})
PostDeploy.set_tag("plan", "scale")
PostDeploy.set_context("job", %{"queue" => "mailers"})

try do
  raise ArgumentError, "Mailer template is missing"
rescue
  exception ->
    PostDeploy.capture_exception(exception, stacktrace: __STACKTRACE__)
end

PostDeploy.capture_message("Low disk space", "warning")

Verify

  1. Step 1. Run a one-file check with the same key.

    check.exs elixir
    Mix.install([{:postdeploy_sdk, "0.2.0"}])
    
    {:ok, _pid} = PostDeploy.start_link(environment: "production", release: "1.0.0")
    
    try do
      raise ArgumentError, "Mailer template is missing"
    rescue
      exception -> PostDeploy.capture_exception(exception, stacktrace: __STACKTRACE__)
    end
    
    PostDeploy.flush()
    
  2. Step 2. Run it with POSTDEPLOY_API_KEY=obs_your_key elixir check.exs.

  3. Step 3. Open Observe. The table shows "ArgumentError: Mailer template is missing" with its source and environment.

Troubleshoot

A crash in a plug is not captured
The logger handler captures crashes, not the plug. Start {PostDeploy, []} in the supervision tree before the endpoint.
Request parameters are missing
The plug does not attach parameters. Add filtered values with PostDeploy.set_context("params", ...).
Local tests post to production
Set base_url: or POSTDEPLOY_INGEST_URL only in development and test.

Limits

  • PostDeploy.Plug does not attach request parameters or bodies.
  • The package README in 0.2.0 describes an older key format. Keys start with obs_.

Send your first signal today.

14 days free, no card required. Then $29 a month.

Start your 14-day free trial