Elixir
The zharp Hex package traces every request your Phoenix or Plug app handles, its LiveViews, queries, HTTP calls and Oban jobs, and sends its errors — crashing GenServers and Tasks included — each once, with the code that ran.
This SDK is new, and its API may still change before 1.0. Requires Elixir 1.16 or later on OTP 26 or later and, for Phoenix, Phoenix 1.7 or later.
One package for Phoenix (controllers and LiveView), Plug on Bandit or Cowboy, Ecto with Postgres or MySQL, Oban, outgoing HTTP through Finch (Req, Tesla, Swoosh) and Absinthe, built on OpenTelemetry Erlang. A dependency starts with your app, before the endpoint, and each instrumentation switches on when its library is there. Spans you make with OpenTelemetry.Tracer are sent too.
Phoenix
mix igniter.install zharp
Igniter adds the dependency, plug Zharp.Plug to your endpoint, config :zharp to config/runtime.exs (and turns Zharp off in tests), and the release step. Then set your key:
ZHARP_KEY=zpk_… mix phx.server # Application settings in Zharp
npm init zharp@latest does the same from a terminal, running Igniter when your project has it. The app's setup page in Zharp shows these steps with the key, service and environment filled in.
Without Igniter
The dependency, the plug, and ZHARP_KEY in the environment:
# mix.exs
defp deps do
[
{:zharp, "~> 0.1"},
# …
]
end# lib/my_app_web/endpoint.ex, before plug Plug.RequestId plug Zharp.Plug
Or keep the key in the runtime config:
# config/runtime.exs (what the installer writes)
config :zharp,
key: System.get_env("ZHARP_KEY"),
environment: to_string(config_env())
# config/test.exs
config :zharp, enabled: falsePlug (no Phoenix)
The dependency, the key, and the plug at the top of your router:
defmodule MyApp.Router do use Plug.Router plug Zharp.Plug plug :match plug :dispatch end
Requests are named by their route (GET /orders/:id), on Bandit or Cowboy.
Elixir: workers and scripts
The dependency and ZHARP_KEY; nothing else. A process that crashes is reported by itself, and an error that ends a mix run script is sent before it exits. Report what you rescue with Zharp.capture_exception/2.
Releases: the code that ran
Elixir stacks always have the file and line (lib/shop/cart.ex:12), so they read as sent. To show the code around each line, Zharp reads it on your server: in development from the files on disk, in a mix release — which ships compiled .beam files, not your .ex sources — from a copy of your lib/ the release step packs into it. Nothing is uploaded. The installer adds the step; by hand:
# mix.exs
def project do
[
# …
releases: [
my_app: [steps: [:assemble, &Zharp.Release.package_source/1]]
]
]
endKeep it after :assemble and before :tar.
Your code and the libraries'
Phoenix's frames are lib/phoenix/… just like your lib/shop/…, so Zharp tells them apart by OTP application: your project's applications (an umbrella's included) go with each error, and only their frames are yours — they group the issue and name where it happened. To choose them yourself:
config :zharp, in_app: [:my_app, :my_app_web]
Errors
Each failure is reported once, with its type as Elixir writes it (Ecto.NoResultsError), message, stack and code:
- an exception that escapes a request, a LiveView callback or an Oban job — unhandled, on that span;
- a process that crashes outside a request — a GenServer, a Task, a supervised process — from its crash report, unhandled, in the trace of the request that started it. An exit is type
exitwith its reason ({:timeout, {GenServer, :call, …}}); a:normalor:shutdownexit isn't an error; - an error you report:
try do
charge(order)
rescue
error ->
Zharp.capture_exception(error, stacktrace: __STACKTRACE__, extra: %{order_id: order.id})
endThe same error recorded by the server's span, Phoenix's and the process's crash report is one issue, not three. Exceptions Plug answers with a status below 500 — Phoenix.Router.NoRouteError, Ecto.NoResultsError, Phoenix.ActionClauseError — stay on the request's span and aren't reported.
Failed queries
A failed Ecto query is reported once, with the database, the query (Ecto's are parameterised, so it has no values) and the Postgres or MySQL error code; values quoted in database messages are dropped. One that escapes is unhandled; one your code carried on from is reported as handled, where your code ran it. Constraint violations your changesets turn into form errors (unique_constraint…) aren't reported. ZHARP_CAPTURE_QUERY_ERRORS=false turns it off.
Tracing
Each request is named by its route and continues the caller's traceparent, with spans for LiveView mounts and events, Ecto queries, outgoing HTTP calls, Oban jobs and GraphQL operations under it. Add your own with OpenTelemetry's API:
require OpenTelemetry.Tracer, as: Tracer Tracer.with_span "render invoice" do render(invoice) end
Request and response headers
Zharp.Plug records every request and response header on the request's span. Secret ones — authorization, cookies, API keys, tokens, sessions — as [REDACTED]. Name more to hide, or turn it off:
config :zharp, capture_headers: [redact: ["x-tenant"]] # filter more names config :zharp, capture_headers: false # or ZHARP_CAPTURE_HEADERS=false
Oban jobs
Every job is a span, and a job that raises is an error. Oban has no hook for changing a job as it's inserted, so to run a job in the trace of the request that queued it, insert it through Zharp:
MyApp.Workers.Receipt.new(%{order_id: order.id}) |> Zharp.Oban.insert()
# or, before an insert of your own
changeset |> Zharp.Oban.with_trace() |> Oban.insert()Outgoing HTTP
Calls through Finch — Req, Tesla's Finch adapter, Swoosh — are spans with nothing to add. To join the service you call to your trace:
# Req: carry the trace to the service you call req = Req.new(base_url: "https://billing.internal") |> Zharp.Req.attach() # Tesla plug Zharp.Tesla
Logs
Off by default. Each console line written during a request ends with the request's trace_id=… span_id=… (two fields on a LoggerJSON line). Where the Zharp agent reads your app's output, it brings those lines in and links each one to its request. ZHARP_LOG_CORRELATION=false turns the stamp off.
Where there is no agent, ZHARP_LOGS=true sends every Logger line from the SDK, in its request's trace (ZHARP_LOG_LEVEL sets the level, info by default). Zharp.log is always sent.
Logs only
If you only want logs, turn tracing off. Logs, errors, and the user and session they belong to are still sent; no spans are recorded and no trace headers are added to outgoing requests. A request from a Zharp front end is still read, so its logs keep the caller's user, session and trace ID.
# config/runtime.exs
config :zharp, key: System.get_env("ZHARP_KEY"), tracing: false, logs: trueOr leave the code as it is and set ZHARP_TRACING=false ZHARP_LOGS=true in the environment.
The logs show on the Logs page under the service name, and on the app's Logs tab with the user and session of each line. The key comes from an app — see Get a key. To add tracing later, remove the option; nothing else changes.
User context
Requests from the Zharp browser and mobile SDKs carry the user and session in baggage, and Zharp.Plug puts them on the request. When the server knows the user better:
# a plug after authentication, or a LiveView on_mount hook def call(conn, _opts) do if user = conn.assigns[:current_user], do: Zharp.set_user(id: user.id, email: user.email) conn end
The user is kept in the process, and Tasks started from it report with it.
The API
Zharp.capture_exception(error, stacktrace: __STACKTRACE__) # rescued, as a handled error
Zharp.set_user(id: user.id, email: user.email) # on the request's span, logs and errors, and its Tasks
Zharp.set_tag("tenant", "acme") # on every log and error from now on
Zharp.log(:info, "order paid", order_id: 42) # a log line sent from the SDK, whatever ZHARP_LOGS says
Zharp.flush() # send what's queued now (end of a script)Options
Every option can be set in config/runtime.exs (config :zharp, …) or in the environment; config wins.
| config :zharp | Environment | Default |
|---|---|---|
key: | ZHARP_KEY | — nothing is sent without one |
service: | ZHARP_SERVICE, OTEL_SERVICE_NAME | your app's name |
environment: | ZHARP_ENVIRONMENT | Mix.env() when Mix is there |
release: | ZHARP_RELEASE | — |
endpoint: | ZHARP_ENDPOINT | https://api.zharp.io/api/v1/otlp |
sample_rate: | ZHARP_SAMPLE_RATE | 1.0 (traces with an error are always kept) |
logs: | ZHARP_LOGS | false. See Logs. |
log_level: | ZHARP_LOG_LEVEL | :info |
log_correlation: | ZHARP_LOG_CORRELATION | true: the trace_id=… span_id=… stamp |
capture_headers: | ZHARP_CAPTURE_HEADERS | on; [redact: ["x-tenant"]] filters more names |
capture_query_errors: | ZHARP_CAPTURE_QUERY_ERRORS | true |
capture_source_context: | ZHARP_CAPTURE_SOURCE_CONTEXT | true: the code that ran, with each error |
capture_crashes: | ZHARP_CAPTURE_CRASHES | true: crash reports as errors |
tracing: | ZHARP_TRACING | true; false sends errors and logs only |
in_app: | — | your project's applications. See above. |
enabled: false | ZHARP_DISABLED=true | turns everything off |
Traces go to <endpoint>/v1/traces as OTLP http/protobuf, gzipped, with the key as a Bearer token. Zharp sets the exporter of OpenTelemetry's default batch processor when it starts, so config :opentelemetry needs nothing.
Supported versions
- Elixir 1.16 to 1.20, on OTP 26 or later (every Elixir version still getting security fixes).
- Phoenix 1.7 and 1.8, LiveView; Plug on Bandit or Cowboy.
- Ecto with Postgres or MySQL; Oban 2; Finch, Req, Tesla; Absinthe.
Troubleshooting
Nothing arrives
On the app's setup page (Applications → the app → settings → Setup instructions), the verify step shows the first event as soon as it lands, and Send a test event checks the key on its own. If the test event arrives but your app's don't, the SDK isn't starting or isn't reaching Zharp:
- Is
initcalled, and before the code you expect to see? Without a key, the SDK starts nothing and says so once. - Does the service name match? With an app key everything goes to that app whatever it says; with a workspace key,
servicedecides the app — see Keys. - Data is sent in batches every few seconds. A process that exits straight away should call
flushfirst. - Logs and errors arrive but no traces? Check that tracing isn't turned off — see Logs only.
- Nothing traced, but errors arrive: does your config set
config :opentelemetry, processors: …? Zharp gives its exporter to OpenTelemetry's default batch processor; with processors of your own, add{:otel_batch_processor, %{exporter: {Zharp.Exporter, Zharp.options()}}}. - Errors show no code in a release: is
&Zharp.Release.package_source/1in the release's steps, after:assemble? - Frames of your own marked as a library's: list your applications in
config :zharp, in_app: […].
401 or 403
Ingest answers 401 when the key is missing, mistyped, revoked or rolled, and 403 when the request comes from an origin or IP address the key doesn't allow. Neither is retried: the SDK warns once and drops what was refused, and it never throws into your code. Copy the key again from the app's settings.