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

chore(add-user-agent-in-logs): Add User-Agent header to ECS logs #47

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading