Plug Web Metrics

Basic telemetry metrics for a Plug-based, Elixir backend

Why?

I was building a Plug-powered backend service in Elixir and wanted some standard service metrics to aggregate like the number of incoming requests, number of successful/failed requests, etc. I couldn't find a Plug like that anywhere.

Plug's default telemetry events (an event on plug entry and exit) forces you to work around metadata and naming to get similar metrics.

Feel free to add additional metrics that you find helpful.

What doesn't this do?

This module generates telemetry measurements during the lifecycle of a request. It does not aggregate those measurements over time.

To aggregrate measurements over time, you'd typically use TelemetryMetrics and a reporter to send these measurements to a metrics aggregator/store (like Prometheus) to do the actual aggregations over time.

Installation

def deps do
  [
    {:plugwebmetrics, "~> 0.1.0"}
  ]
end

Usage

Use this similar to how you would use Plug.Telemetry (no event_prefix needed).

defmodule MyRouter do
  use Plug.Router
  require Plugwebmetrics

  plug(:match)
  plug Plugwebmetrics
  plug(:dispatch)

  get("/hello", do: send_resp(conn, 200, "world"))
end

Measurements tracked

plug.incoming_requests.count

plug.successful_requests.count

plug.failed_requests.count

plug.response_time.duration

Using this with Telemetry Metrics

To get these measurements aggregated over time, you would wrap these measurements using TelemetryMetrics like:

counter("plug.incoming_requests.count", description: "The total number of incoming requests", unit: :request),
counter("plug.successful_requests.count", description: "The total number of successful (2xx, 4xx) requests", unit: :request),
counter("plug.failed_requests.count", description: "The total number of failed (5xx) requests", unit: :request),
last_value("plug.response_time.duration", description: "The total response time for a request", unit: :microsecond)

You would then use any telemetry reporter to forward these measurements to a metrics aggregator.

License

MIT License