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,jasonandtelemetry.
Install
# 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.
# 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.
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
-
Step 1. Run a one-file check with the same key.
check.exs elixirMix.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() -
Step 2. Run it with
POSTDEPLOY_API_KEY=obs_your_key elixir check.exs. -
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:orPOSTDEPLOY_INGEST_URLonly in development and test.
Limits
-
PostDeploy.Plugdoes not attach request parameters or bodies. -
The package README in 0.2.0 describes an older key format. Keys start with
obs_.