Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Grafana via prom_ex #1518

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
Fetch the correct request ip (Logger plug)
joshk committed Sep 13, 2024
commit dc240bde01841cd24365709644cef49495a471d0
16 changes: 13 additions & 3 deletions lib/nerves_hub_web/plugs/api/logger.ex
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ defmodule NervesHubWeb.API.Plugs.Logger do
method: conn.method,
path: request_path(conn),
status: conn.status,
remote_ip: formatted_ip(conn.remote_ip)
remote_ip: formatted_ip(conn)
)
end)

@@ -33,7 +33,17 @@ defmodule NervesHubWeb.API.Plugs.Logger do
def request_path(%{request_path: request_path}), do: request_path
def request_path(_), do: nil

defp formatted_ip(ip) do
to_string(:inet_parse.ntoa(ip))
defp formatted_ip(conn) do
case Plug.Conn.get_req_header(conn, "x-forwarded-for") do
[ips] ->
ips
|> String.split(",")
|> List.first()
|> String.trim()
_ ->
conn.remote_ip
|> :inet_parse.ntoa
|> to_string()
end
end
end
16 changes: 13 additions & 3 deletions lib/nerves_hub_web/plugs/logger.ex
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ defmodule NervesHubWeb.Plugs.Logger do
method: conn.method,
path: request_path(conn),
status: conn.status,
remote_ip: formatted_ip(conn.remote_ip)
remote_ip: formatted_ip(conn)
)
end)

@@ -33,7 +33,17 @@ defmodule NervesHubWeb.Plugs.Logger do
def request_path(%{request_path: request_path}), do: request_path
def request_path(_), do: nil

defp formatted_ip(ip) do
to_string(:inet_parse.ntoa(ip))
defp formatted_ip(conn) do
case Plug.Conn.get_req_header(conn, "x-forwarded-for") do
[ips] ->
ips
|> String.split(",")
|> List.first()
|> String.trim()
_ ->
conn.remote_ip
|> :inet_parse.ntoa
|> to_string()
end
end
end