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:
@@ -1,7 +0,0 @@
|
||||
defmodule MusicLibraryWeb.MetricsController do
|
||||
use MusicLibraryWeb, :controller
|
||||
|
||||
def index(conn, _params) do
|
||||
send_resp(conn, 200, TelemetryMetricsPrometheus.Core.scrape())
|
||||
end
|
||||
end
|
||||
@@ -15,6 +15,8 @@ defmodule MusicLibraryWeb.Endpoint do
|
||||
encrypt: true
|
||||
]
|
||||
|
||||
plug MusicLibraryWeb.Telemetry.Plug, at: "/metrics"
|
||||
|
||||
socket "/live", Phoenix.LiveView.Socket,
|
||||
websocket: [connect_info: [session: @session_options]],
|
||||
longpoll: [connect_info: [session: @session_options]]
|
||||
|
||||
@@ -69,10 +69,6 @@ defmodule MusicLibraryWeb.Router do
|
||||
use ErrorTracker.Web, :router
|
||||
import Phoenix.LiveDashboard.Router
|
||||
|
||||
scope "/metrics", MusicLibraryWeb do
|
||||
get "/", MetricsController, :index
|
||||
end
|
||||
|
||||
scope "/dev" do
|
||||
pipe_through [:browser, :logged_in]
|
||||
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user