Use a plug for Prometheus metrics

The plug is injected as early as possible in order to respond quickly,
without polluting logs.
This commit is contained in:
Claudio Ortolina
2025-03-04 20:21:15 +00:00
parent d73f2dca3e
commit b111558f07
4 changed files with 24 additions and 11 deletions
+22
View File
@@ -0,0 +1,22 @@
defmodule MusicLibraryWeb.Telemetry.Plug do
@behaviour Plug
import Plug.Conn
def init(opts) do
Keyword.validate!(opts, [:at])
end
def call(conn, opts) do
at = Keyword.fetch!(opts, :at)
if conn.request_path == at do
conn
|> put_resp_content_type("text/plain")
|> send_resp(200, TelemetryMetricsPrometheus.Core.scrape())
|> halt()
else
conn
end
end
end