Skip to content
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
2 changes: 2 additions & 0 deletions neurow/lib/neurow/ecs_log_formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ 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[:http_method], "http.request.method")
|> with_optional_attribute(metadata[:http_path], "http.request.path")
|> with_optional_attribute(metadata[:user_agent_header], "user_agent.original")
|> :jiffy.encode()
|> newline()
Expand Down
9 changes: 7 additions & 2 deletions neurow/lib/neurow/jwt_auth_plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,13 @@ defmodule Neurow.JwtAuthPlug do

defp unauthorized(conn, error_code, error_message, options) do
Logger.error(
"JWT authentication error: #{error_code} - #{error_message}, path: '#{conn.request_path}'",
"JWT authentication error: #{error_code} - #{error_message}, method: '#{conn.method}', path: '#{conn.request_path}'",
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(),
http_path: conn.request_path,
http_method: conn.method,
trace_id: conn |> get_req_header("x-request-id") |> List.first(),
client_ip: conn |> get_req_header("x-forwarded-for") |> List.first()
)
Expand All @@ -241,10 +243,13 @@ defmodule Neurow.JwtAuthPlug do
end

Logger.error(
"JWT authentication error: #{error_code} - #{error_message}, path: '#{conn.request_path}', audience: '#{options |> Options.audience()}', token: '#{jwt_token}'",
"JWT authentication error: #{error_code} - #{error_message}, method: '#{conn.method}', path: '#{conn.request_path}', audience: '#{options |> Options.audience()}', token: '#{jwt_token}'",
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(),
http_method: conn.method,
http_path: conn.request_path,
trace_id: conn |> get_req_header("x-request-id") |> List.first(),
client_ip: conn |> get_req_header("x-forwarded-for") |> List.first()
)
Expand Down
68 changes: 68 additions & 0 deletions neurow/test/neurow/ecs_log_formatter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,74 @@ defmodule Neurow.EcsLogFormatterTest do
}
end

test "supports optional http_method 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,
http_method: "POST"
}

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"
},
"http.request.method" => "POST"
}
end

test "supports optional http_path 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,
http_path: "/foobar"
}

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"
},
"http.request.path" => "/foobar"
}
end

test "multiline messages are inlined" do
metadata = %{
time: 1_728_556_213_722_376,
Expand Down