Collector

Reports your Elixir app's errors to Collector, which groups them into issues, emails you when one is new or comes back, and shows you what broke and why.

Installation

Add the dependency to mix.exs:

def deps do
  [
    {:collector, ">= 0.6.0"}
  ]
end

Give it your project's DSN (from its setup page in Collector) in the COLLECTOR_DSN environment variable of the deployed app. Or, to take it from somewhere else, in config/runtime.exs:

config :collector, dsn: System.get_env("MY_COLLECTOR_DSN")

That's it. From now on, crashes are reported on their own: controllers, LiveViews, GenServers, Tasks, any process. Oban job failures too, when Oban is around (not the jobs that return {:cancel, _} or {:discard, _}). Exceptions that Plug turns into a 4xx, like Ecto.NoResultsError, aren't bugs and aren't reported.

It also sends a heartbeat when the app starts and every hour after, so Collector knows the app is up even when nothing breaks, and shows its release, environment and SDK version.

Without a DSN nothing is sent, so dev and test stay quiet: don't export COLLECTOR_DSN there.

Checking the setup

Send a test event and see Collector's answer:

MIX_ENV=prod mix collector.test

It prints the event's id when Collector accepted it, or what went wrong (no DSN, a refused key, Collector unreachable), and exits with 1 then. In a release, where mix tasks aren't available:

bin/my_app rpc 'IO.inspect(Collector.test_event())'

Request details and users

Add the plug to your endpoint to see the request each error happened in (credentials such as cookies are never sent):

# lib/my_app_web/endpoint.ex
plug Collector.Plug

And say who hit the error, by id:

Collector.set_user(current_user.id)

Source lines in releases

Collector shows the lines around each frame of your code. Releases don't ship source files, so package them right before building the release:

MIX_ENV=prod mix collector.package_source
MIX_ENV=prod mix release

In a Dockerfile, that's one line before mix release.

Logs, requests and sign-ins

To let Collector catch attacks (login abuse, scanning, unusual volume), the app can also send what it logs, every request it serves and its sign-ins. That needs a server key, a secret made in the project's settings in Collector (not the DSN's key, which is public). Set it next to the DSN:

COLLECTOR_SERVER_KEY=clts_…

With it, without any other change, Collector gets:

Collector.sign_in(:failed, email, conn: conn, method: :password)
Collector.sign_in(:succeeded, email, conn: conn, method: :password, user: user.id)

In an app made with mix phx.gen.auth, call Collector.PhxGenAuth.sign_in(conn, user_params, user_or_nil) in both branches of UserSessionController.create; its docs show where.

Behind a load balancer, list its networks so the client's address is read from X-Forwarded-For (which is believed only from them):

config :collector,
  logs: [
    trusted_proxies: ["10.0.0.0/8"],
    skip_paths: ["/health"],  # exact, or a prefix as "/up/*"
    level: :info,             # the lowest level sent
    lines: true,              # false: no log lines
    requests: true,           # false: no requests
    sign_ins: true            # false: no sign-ins
  ]

logs: false switches it all off. Path segments for route params named like token, password or secret are sent as their name, so a sign-in link isn't stored. Collector keeps what's sent for a year, as sent: what the app logs is its responsibility. Records go in batches every second, in the background; when a release stops, what's left is sent, waiting up to 5 seconds. See Collector.Logs for the details.

Oban job args

A failed Oban job's issue shows the job's args, id and attempt (as "3 of 3"), so the input that broke it is at hand. Values under keys that look secret (password, token, secret, api_key, key, auth, cookie, session, …, at any depth) are sent as "[FILTERED]", and args past 2 KB are cut. For apps whose job args are sensitive by nature:

config :collector, oban: [args: false]

Keeping secrets out

Collector stores what it's sent as sent. To change or drop anything before it leaves the app, give it hooks. Each gets the item as it would be sent, a map with string keys, and returns it, changed or not, or nil to drop it:

config :collector,
  before_send: {MyApp.CollectorFilter, :before_send},          # errors and messages
  before_send_log: {MyApp.CollectorFilter, :before_send_log},  # log records
  before_breadcrumb: {MyApp.CollectorFilter, :before_breadcrumb}
defmodule MyApp.CollectorFilter do
  # These exceptions quote the values they were given.
  @quoting ~w(MatchError KeyError CaseClauseError FunctionClauseError WithClauseError)
  @token ~r/\bmyapp_live_[A-Za-z0-9]+/

  def before_send(%{"exception" => %{"type" => type}} = event) when type in @quoting,
    do: event |> put_in(["exception", "message"], "[FILTERED]") |> scrub()

  def before_send(event), do: scrub(event)

  def before_send_log(%{"type" => "request", "path" => "/health"}), do: nil
  def before_send_log(record), do: scrub(record)

  def before_breadcrumb(crumb), do: scrub(crumb)

  # Tokens, wherever they are: messages, tags, breadcrumbs, metadata.
  defp scrub(value) when is_binary(value), do: Regex.replace(@token, value, "[FILTERED]")
  defp scrub(value) when is_map(value), do: Map.new(value, fn {k, v} -> {k, scrub(v)} end)
  defp scrub(value) when is_list(value), do: Enum.map(value, &scrub/1)
  defp scrub(value), do: value
end

before_send sees each error and message event, its breadcrumbs included; before_send_log each log record, whose "type" is "log", "request" or "sign_in"; before_breadcrumb each breadcrumb as it's recorded. A hook runs in the process that reports, so keep it quick. It fails closed: one that raises or returns anything but a map or nil drops the item, and the process logs one warning naming the hook, never the item. mix collector.test says so when the hook drops its event. See Collector.Hooks.

Reporting by hand

try do
  charge!(order)
rescue
  e ->
    Collector.capture_exception(e, stacktrace: __STACKTRACE__)
    {:error, :payment_failed}
end

Collector.capture_message("Payment gateway slow", level: :warning)

Options

Option Default
:dsn COLLECTOR_DSN https://clt_…@app.clt.chrom.ar: where Collector runs plus the project's key. Nothing is sent without it
:key, :endpoint COLLECTOR_KEY, https://app.clt.chrom.ar The DSN's two halves, set apart. A DSN wins over both
:environment COLLECTOR_ENVIRONMENT, else the Mix env the app runs in (prod in a release) prod, staging, …
:release COLLECTOR_RELEASE, else RELEASE_VSN Version or git sha
:in_app Your app's own applications Which applications count as "your code"
:server_key COLLECTOR_SERVER_KEY The secret key logs are sent with. Setting it turns logs on
:logs see above Tunes logs, or false to switch them off
:oban [args: true] [args: false] leaves job args out of Oban failures
:before_send, :before_send_log, :before_breadcrumb none Hooks that change or drop what's sent; see above
:repos none Ecto repo telemetry prefixes, e.g. [[:my_app, :repo]], to record queries as breadcrumbs

How it behaves

Events are sent in the background, one at a time, from a bounded queue (the oldest is dropped past 1000), so a slow or unreachable Collector never slows your app down. Failed sends are retried with the same id, so nothing is counted twice; when Collector asks to slow down, it does. Heartbeats share that queue, at most one waiting at a time. They have their own rate limit, so being slowed down on heartbeats never pauses errors, and they never log anything.

The protocol is documented in Collector's docs/protocol.md, if you'd like to report from another language.

License

MIT. See LICENSE.