From e4343d0ed468fb7e853259c6706997f566369d72 Mon Sep 17 00:00:00 2001 From: Eduardo Moraga Date: Sat, 4 Sep 2021 23:38:59 -0400 Subject: [PATCH] Fix PlugRouter to handle request not resolved by Plug.Router (#96) * handles request without conn.private.plug_route info, sets path tag as Unknown, also allows ignore the request using conn.request_path * dialyzer fix, switch when for case --- lib/prom_ex/plugins/plug_router.ex | 32 +++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/prom_ex/plugins/plug_router.ex b/lib/prom_ex/plugins/plug_router.ex index f6999ed8..5635b758 100644 --- a/lib/prom_ex/plugins/plug_router.ex +++ b/lib/prom_ex/plugins/plug_router.ex @@ -208,29 +208,47 @@ if Code.ensure_loaded?(Plug.Router) do end end - defp path(conn) do - {path, _} = conn.private.plug_route - path + defp route(%Plug.Conn{private: %{plug_route: {route, _}}}) do + route + end + + defp route(_conn) do + "Unknown" + end + + defp route_or_path(conn) do + case Map.get(conn.private, :plug_route) do + {route, _} -> + route + + nil -> + conn.request_path + end end defp get_tags(%{conn: conn = %Conn{}}) do %{ status: conn.status || 500, method: conn.method, - path: path(conn) + path: route(conn) } end defp drop_ignored(ignored_routes, routers) do fn %{conn: conn = %Conn{}, router: router} -> - path = path(conn) + value = route_or_path(conn) disallowed_router? = !Enum.member?(routers, router) - ignored_route? = MapSet.member?(ignored_routes, path) - + ignored_route? = MapSet.member?(ignored_routes, value) disallowed_router? || ignored_route? + %{conn: conn = %Conn{}} -> + value = route_or_path(conn) + + ignored_route? = MapSet.member?(ignored_routes, value) + ignored_route? + _meta -> false end