Zharp for Elixir

Application Monitoring for Elixir apps: traces, errors and logs, sent to Zharp. One package for Phoenix (controllers and LiveView), Plug on Bandit or Cowboy, Ecto, Oban, outgoing HTTP (Finch, Req, Tesla), Absinthe and plain Elixir, built on OpenTelemetry Erlang.

Elixir 1.16+ and OTP 26+. Phoenix 1.7+.

Phoenix

mix igniter.install zharp

That adds the dependency, plug Zharp.Plug to your endpoint, config :zharp to config/runtime.exs and the release step to mix.exs. Then set your key, from Settings → Applications in Zharp:

ZHARP_KEY=zpk_… mix phx.server

npm init zharp does the same from a terminal (it runs Igniter when your project has it).

Without Igniter

# mix.exs
{:zharp, "~> 0.1"}
# lib/my_app_web/endpoint.ex, before plug Plug.RequestId
plug Zharp.Plug

and ZHARP_KEY in the environment. That's all: Zharp starts with your app, before the endpoint.

Plug (no Phoenix)

The dependency, ZHARP_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

Spans are named by the route (GET /orders/:id), on Bandit or Cowboy.

Elixir: scripts, workers

The dependency and ZHARP_KEY. Crashing processes are reported by themselves; report what you rescue with Zharp.capture_exception/2. At the end of a script, Zharp.flush() sends what's queued.

Releases: the code that ran

A mix release ships compiled .beam files, not your .ex sources. Add the release step (the installer does) and the release carries your lib/, read on your server when an error happens. Nothing is uploaded.

# mix.exs
releases: [
  my_app: [steps: [:assemble, &Zharp.Release.package_source/1]]
]

Keep it after :assemble and before :tar. In development (mix phx.server) the files are read from disk and need nothing.

Which frames are your code is decided by OTP application, not by path (Phoenix's frames are lib/phoenix/… just like yours): your project's applications, an umbrella's included, are sent with each error. Set config :zharp, in_app: [:my_app, :my_app_web] to choose them yourself.

Settings

Every setting can be set in config/runtime.exs (config :zharp, key: …) or the environment. Config wins over the environment.

config :zharp Environment Default
key: ZHARP_KEY none: 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 none
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
log_level: ZHARP_LOG_LEVEL :info (with logs: true)
log_correlation: ZHARP_LOG_CORRELATION true
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
capture_crashes: ZHARP_CAPTURE_CRASHES true
tracing: ZHARP_TRACING true; false sends errors and logs only
in_app: none your project's applications
enabled: false ZHARP_DISABLED=true turns everything off (the installer sets it for tests)

Traces go to <endpoint>/v1/traces as OTLP http/protobuf, gzipped, with Authorization: Bearer <key>; logs and errors outside a span to <endpoint>/v1/logs. Zharp sets the exporter of OpenTelemetry's default batch processor at startup, so config :opentelemetry needs nothing. If you configure your own :processors, add {:otel_batch_processor, %{exporter: {Zharp.Exporter, Zharp.options()}}} yourself.

API

Zharp.capture_exception(error, stacktrace: __STACKTRACE__, extra: %{order_id: 42}) # rescued, as handled
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)

Spans of your own, with OpenTelemetry's API:

require OpenTelemetry.Tracer, as: Tracer
Tracer.with_span "render invoice" do
  render(invoice)
end

A browser or mobile app using a Zharp SDK sends its user and session with each request (W3C baggage); Zharp.Plug puts them on the request's span.

Oban

Oban has no hook for changing a job as it is inserted. 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 Zharp.Oban.with_trace(changeset) before your own insert). Every job is a span either way, and a job that raises is an error.

Outgoing HTTP

Calls through Finch (Req, Tesla with its Finch adapter, Swoosh) are spans with nothing to add. To carry the trace to the service you call, so its spans join yours:

req = Req.new(base_url: "https://billing.internal") |> Zharp.Req.attach()
# a Tesla client
plug Zharp.Tesla

What is reported

Development

Everything runs in Docker (official elixir images, Postgres, MySQL):

e2e/unit.sh                  # mix test, database tests included, on Elixir 1.16/OTP 26 and 1.20/OTP 29
scripts/mix.sh 1.20-otp-29 test test/servers_test.exs

e2e/ also has end-to-end apps (Phoenix 1.8 on Bandit as a release with Postgres, Phoenix 1.7 on Cowboy with MySQL, a Plug app, an umbrella, a script, ZHARP_LOGS) sending to a stand-in OTLP receiver: node e2e/run.mjs <case>.

License

MIT