Skip to content

Commit

Permalink
Fix PlugRouter to handle request not resolved by Plug.Router (#96)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Eduardo Moraga authored Sep 5, 2021
1 parent e74e6e5 commit e4343d0
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions lib/prom_ex/plugins/plug_router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e4343d0

Please sign in to comment.