{"id":16007544,"url":"https://github.com/mrjoelkemp/plug-web-metrics","last_synced_at":"2025-04-05T02:15:15.854Z","repository":{"id":57535261,"uuid":"251071623","full_name":"mrjoelkemp/plug-web-metrics","owner":"mrjoelkemp","description":"Telemetry metrics you'd want for a website powered by Plug","archived":false,"fork":false,"pushed_at":"2020-03-29T19:03:48.000Z","size":10,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-01T18:57:53.108Z","etag":null,"topics":["elixir","metrics","plug","telemetry"],"latest_commit_sha":null,"homepage":null,"language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mrjoelkemp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-29T15:56:32.000Z","updated_at":"2020-05-01T11:34:35.000Z","dependencies_parsed_at":"2022-09-26T18:30:24.529Z","dependency_job_id":null,"html_url":"https://github.com/mrjoelkemp/plug-web-metrics","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrjoelkemp%2Fplug-web-metrics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrjoelkemp%2Fplug-web-metrics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrjoelkemp%2Fplug-web-metrics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrjoelkemp%2Fplug-web-metrics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mrjoelkemp","download_url":"https://codeload.github.com/mrjoelkemp/plug-web-metrics/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247276188,"owners_count":20912288,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["elixir","metrics","plug","telemetry"],"created_at":"2024-10-08T12:03:55.333Z","updated_at":"2025-04-05T02:15:15.838Z","avatar_url":"https://github.com/mrjoelkemp.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Plug Web Metrics\n\n\u003e Basic telemetry metrics for a Plug-based, Elixir backend\n\n## Why?\nI 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.\n\nPlug's default telemetry events (an event on plug entry and exit) forces you to work around metadata and naming to get similar metrics.\n\nFeel free to add additional metrics that you find helpful.\n\n## What doesn't this do?\nThis module generates telemetry measurements during the lifecycle of a request. It **does not** aggregate those measurements over time.\n\nTo 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.\n\n## Installation\n\n```elixir\ndef deps do\n  [\n    {:plugwebmetrics, \"~\u003e 0.1.0\"}\n  ]\nend\n```\n\n## Usage\n\nUse this similar to how you would use [Plug.Telemetry](https://hexdocs.pm/plug/Plug.Telemetry.html) (no event_prefix needed).\n\n```elixir\ndefmodule MyRouter do\n  use Plug.Router\n  require Plugwebmetrics\n\n  plug(:match)\n  plug Plugwebmetrics\n  plug(:dispatch)\n\n  get(\"/hello\", do: send_resp(conn, 200, \"world\"))\nend\n```\n\n## Measurements tracked\n\n### `plug.incoming_requests.count`\n\n* Triggered on every request\n* The count will always be 1. Aggregations are done outside of this plug.\n\n### `plug.successful_requests.count`\n\n* Triggered on a successful request (a status code \u003c 500)\n* The count will always be 1. Aggregations are done outside of this plug.\n\n### `plug.failed_requests.count`\n\n* Triggered on a failed request (a status code \u003e= 500)\n* The count will always be 1. Aggregations are done outside of this plug.\n\n### `plug.response_time.duration`\n\n* Triggered on every request.\n* The time is took for you to respond in microseconds.\n  * This is in microseconds because fast endpoints could be sub-millisecond and show up as a 0 if we tracked this in millis.\n\n## Using this with Telemetry Metrics\n\nTo get these measurements aggregated over time, you would wrap these measurements using [TelemetryMetrics](https://hexdocs.pm/telemetry_metrics/Telemetry.Metrics.html) like:\n\n```elixir\ncounter(\"plug.incoming_requests.count\", description: \"The total number of incoming requests\", unit: :request),\ncounter(\"plug.successful_requests.count\", description: \"The total number of successful (2xx, 4xx) requests\", unit: :request),\ncounter(\"plug.failed_requests.count\", description: \"The total number of failed (5xx) requests\", unit: :request),\nlast_value(\"plug.response_time.duration\", description: \"The total response time for a request\", unit: :microsecond)\n```\n\nYou would then use any [telemetry reporter](https://hexdocs.pm/telemetry_metrics/Telemetry.Metrics.html#module-reporters) to forward these measurements to a metrics aggregator.\n\n## License\n\nMIT License\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrjoelkemp%2Fplug-web-metrics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmrjoelkemp%2Fplug-web-metrics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrjoelkemp%2Fplug-web-metrics/lists"}