Skip to content

Commit

Permalink
Merge pull request #9 from podium/epinault/fix_client_ip
Browse files Browse the repository at this point in the history
fix: client ip is now serialize as a string
  • Loading branch information
Manu authored Jan 21, 2022
2 parents 89e5783 + d2a11c2 commit b8843a8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG


## v0.9.1 - 2022-01-21

### Changed
* Client ip is now properly serialize as a string

## v0.9 - 2022-01-05

### Added
Expand Down
12 changes: 11 additions & 1 deletion lib/uinta/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ if Code.ensure_loaded?(Plug) do
status: Integer.to_string(conn.status),
timing: formatted_diff(diff),
duration_ms: diff / 1000,
client_ip: conn.remote_ip,
client_ip: client_ip(conn),
user_agent: get_first_value_for_header(conn, "user-agent"),
referer: get_first_value_for_header(conn, "referer"),
x_forwarded_for: get_first_value_for_header(conn, "x-forwarded-for"),
Expand Down Expand Up @@ -196,6 +196,16 @@ if Code.ensure_loaded?(Plug) do
|> List.first()
end

def client_ip(conn) do
case :inet.ntoa(conn.remote_ip) do
{:error, _} ->
""

ip ->
List.to_string(ip)
end
end

@spec method(Plug.Conn.t(), graphql_info()) :: String.t()
defp method(_, %{type: type}), do: type
defp method(conn, _), do: conn.method
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Uinta.MixProject do
app: :uinta,
name: "Uinta",
description: "Simpler structured logs and lower log volume for Elixir apps",
version: "0.9.0",
version: "0.9.1",
elixir: "~> 1.8",
source_url: @project_url,
homepage_url: @project_url,
Expand Down
38 changes: 19 additions & 19 deletions test/uinta/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ defmodule Uinta.PlugTest do
JsonPlug.call(conn(:get, "/"), [])
end)

assert message =~ ~r/client_ip:.+/u
assert message =~ ~r/duration_ms: [0-9]+\.?[0-9]+/u
assert message =~ ~r/method: \"GET\"/u
assert message =~ ~r"path: \"/\""u
assert message =~ ~r/status: \"200\"/u
assert message =~ ~r/timing: \"[0-9]+[µm]s\"/u
assert message =~ ~r/client_ip\":\"127.0.0.1\"/u
assert message =~ ~r/duration_ms\":[0-9]+\.?[0-9]+/u
assert message =~ ~r/method\":\"GET\"/u
assert message =~ ~r"path\":\"/\""u
assert message =~ ~r/status\":\"200\"/u
assert message =~ ~r/timing\":\"[0-9]+[µm]s\"/u
end

test "logs graphql json to console" do
Expand All @@ -163,13 +163,13 @@ defmodule Uinta.PlugTest do
JsonPlug.call(conn(:post, "/graphql", params), [])
end)

assert message =~ ~r/client_ip:.+/u
assert message =~ ~r/duration_ms: [0-9]+\.?[0-9]+/u
assert message =~ ~r/method: \"QUERY\"/u
assert message =~ ~r/operation_name: \"getUser\"/u
assert message =~ ~r"path: \"/graphql\""u
assert message =~ ~r/status: \"200\"/u
assert message =~ ~r/timing: \"[0-9]+[µm]s\"/u
assert message =~ ~r/client_ip\":\"127.0.0.1\"/u
assert message =~ ~r/duration_ms\":[0-9]+\.?[0-9]+/u
assert message =~ ~r"path\":\"/graphql\""u
assert message =~ ~r/status\":\"200\"/u
assert message =~ ~r/timing\":\"[0-9]+[µm]s\"/u
assert message =~ ~r/method\":\"QUERY\"/u
assert message =~ ~r/operation_name\":\"getUser\"/u
end

test "logs graphql json to console with extra headers" do
Expand All @@ -190,12 +190,12 @@ defmodule Uinta.PlugTest do
JsonPlug.call(conn, [])
end)

assert message =~ ~r/client_ip:.+/u
assert message =~ ~r/x_forwarded_for: \"someip\"/u
assert message =~ ~r"referer: \"http://I.am.referer\""u
assert message =~ ~r/user_agent: \"Mozilla\"/u
assert message =~ ~r/x_forwarded_proto: \"http\"/u
assert message =~ ~r/x_forwarded_port: \"4000\"/u
assert message =~ ~r/client_ip\":\"127.0.0.1\"/u
assert message =~ ~r/x_forwarded_for\":\"someip\"/u
assert message =~ ~r"referer\":\"http://I.am.referer\""u
assert message =~ ~r/user_agent\":\"Mozilla\"/u
assert message =~ ~r/x_forwarded_proto\":\"http\"/u
assert message =~ ~r/x_forwarded_port\":\"4000\"/u
end

test "logs paths with double slashes and trailing slash" do
Expand Down

0 comments on commit b8843a8

Please sign in to comment.