Skip to content

Commit

Permalink
feat: enable support for HTTP request info from Plug.Conn in ECS logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart van Oort committed May 17, 2024
1 parent bee0aec commit 4e7b7b2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
21 changes: 8 additions & 13 deletions lib/logger_json/formatters/ecs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ defmodule LoggerJSON.Formatters.ECS do
|> maybe_merge(encode(message))
|> maybe_merge(encode(take_metadata(meta, metadata_selector)))
|> maybe_merge(format_logger_fields(meta))
# |> maybe_put(:request, format_http_request(meta))
|> maybe_merge(format_http_request(meta))
|> maybe_put(:"span.id", format_span_id(meta))
|> maybe_put(:"trace.id", format_trace_id(meta))
|> Jason.encode_to_iodata!()
Expand Down Expand Up @@ -213,18 +213,13 @@ defmodule LoggerJSON.Formatters.ECS do
if Code.ensure_loaded?(Plug.Conn) do
defp format_http_request(%{conn: %Plug.Conn{} = conn}) do
json_map(
connection:
json_map(
protocol: Plug.Conn.get_http_protocol(conn),
method: conn.method,
path: conn.request_path,
status: conn.status
),
client:
json_map(
user_agent: get_header(conn, "user-agent"),
ip: remote_ip(conn)
)
"client.ip": remote_ip(conn),
"http.version": Plug.Conn.get_http_protocol(conn),
"http.request.method": conn.method,
"http.request.referrer": get_header(conn, "referer"),
"http.response.status_code": conn.status,
"url.path": conn.request_path,
"user_agent.original": get_header(conn, "user-agent")
)
end
end
Expand Down
54 changes: 54 additions & 0 deletions test/formatters/ecs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,58 @@ defmodule LoggerJSON.Formatters.ECSTest do
}
} = log_entry
end

test "logs http context" do
conn =
Plug.Test.conn("GET", "/", "")
|> Plug.Conn.put_req_header("user-agent", "Mozilla/5.0")
|> Plug.Conn.put_req_header("referer", "http://www.example2.com/")
|> Plug.Conn.put_req_header("x-forwarded-for", "")
|> Plug.Conn.send_resp(200, "Hi!")

Logger.metadata(conn: conn)

log_entry =
capture_log(fn ->
Logger.debug("Hello")
end)
|> decode_or_print_error()

assert %{
"client.ip" => "",
"http.version" => "HTTP/1.1",
"http.request.method" => "GET",
"http.request.referrer" => "http://www.example2.com/",
"http.response.status_code" => 200,
"url.path" => "/",
"user_agent.original" => "Mozilla/5.0"
} = log_entry
end

test "logs exception http context" do
conn =
Plug.Test.conn("patch", "/", "")
|> Plug.Conn.put_req_header("user-agent", "Mozilla/5.0")
|> Plug.Conn.put_req_header("referer", "http://www.example.com/")
|> Plug.Conn.put_req_header("x-forwarded-for", "")
|> Plug.Conn.send_resp(503, "oops")

Logger.metadata(crash_reason: {{:EXIT, self()}, :foo}, conn: conn)

log_entry =
capture_log(fn ->
Logger.debug("Hello")
end)
|> decode_or_print_error()

assert %{
"client.ip" => "",
"http.version" => "HTTP/1.1",
"http.request.method" => "PATCH",
"http.request.referrer" => "http://www.example.com/",
"http.response.status_code" => 503,
"url.path" => "/",
"user_agent.original" => "Mozilla/5.0"
} = log_entry
end
end

0 comments on commit 4e7b7b2

Please sign in to comment.