Skip to content

Commit

Permalink
chore: adds user agent to logs
Browse files Browse the repository at this point in the history
  • Loading branch information
papey committed Jan 13, 2025
1 parent a10742b commit 5f2bb24
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions neurow/lib/neurow/ecs_log_formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ defmodule Neurow.EcsLogFormatter do
|> with_optional_attribute(metadata[:error_code], "error.code")
|> with_optional_attribute(metadata[:client_ip], "client.ip")
|> with_optional_attribute(metadata[:authorization_header], "http.request.authorization")
|> with_optional_attribute(metadata[:user_agent_header], "user_agent.original")
|> :jiffy.encode()
|> newline()
end
Expand Down
1 change: 1 addition & 0 deletions neurow/lib/neurow/jwt_auth_plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ defmodule Neurow.JwtAuthPlug do
category: "security",
error_code: "jwt_authentication.#{error_code}",
authorization_header: conn |> get_req_header("authorization") |> List.first(),
user_agent_header: conn |> get_req_header("user-agent") |> List.first(),
trace_id: conn |> get_req_header("x-request-id") |> List.first(),
client_ip: conn |> get_req_header("x-forwarded-for") |> List.first()
)
Expand Down
34 changes: 34 additions & 0 deletions neurow/test/neurow/ecs_log_formatter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,40 @@ defmodule Neurow.EcsLogFormatterTest do
}
end

test "supports optional user_agent_header metadata" do
metadata = %{
time: 1_728_556_213_722_376,
mfa: {Neurow.EcsLogFormatterTest, :fake_function, 4},
file: "test/neurow/ecs_log_formatter_test.exs",
line: 10,
user_agent_header: "Mozilla/5.0"
}

json_log =
Neurow.EcsLogFormatter.format(:info, "Hello, world!", nil, metadata)
|> :jiffy.decode([:return_maps])

assert json_log == %{
"@timestamp" => "2024-10-10T10:30:13.722376Z",
"log.level" => "info",
"log.name" => "Elixir.Neurow.EcsLogFormatterTest.fake_function/4",
"log.source" => %{
"file" => %{
"name" => "test/neurow/ecs_log_formatter_test.exs",
"line" => 10
}
},
"ecs.version" => "8.11.0",
"message" => "Hello, world!",
"category" => "app",
"service" => %{
"name" => "neurow",
"version" => "unknown"
},
"user_agent.original" => "Mozilla/5.0"
}
end

test "supports optional client_ip metadata" do
metadata = %{
time: 1_728_556_213_722_376,
Expand Down

0 comments on commit 5f2bb24

Please sign in to comment.