Skip to content

Commit

Permalink
fix(datadog): encode LoggerJSON.Plug's duration_us field as (in nanos…
Browse files Browse the repository at this point in the history
…econds)
  • Loading branch information
Bart van Oort committed Aug 27, 2024
1 parent 3712765 commit a3fcfd8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/logger_json/formatters/datadog.ex
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ defmodule LoggerJSON.Formatters.Datadog do
defp safe_chardata_to_string(other), do: other

if Code.ensure_loaded?(Plug.Conn) do
defp format_http_request(%{conn: %Plug.Conn{} = conn} = meta) do
defp format_http_request(%{conn: %Plug.Conn{} = conn, duration_us: duration_us} = meta) do
request_url = Plug.Conn.request_url(conn)
user_agent = LoggerJSON.Formatter.Plug.get_header(conn, "user-agent")
remote_ip = LoggerJSON.Formatter.Plug.remote_ip(conn)
Expand All @@ -223,8 +223,14 @@ defmodule LoggerJSON.Formatters.Datadog do
),
network: Jason.Helpers.json_map(client: Jason.Helpers.json_map(ip: remote_ip))
}
|> maybe_put(:duration, to_nanosecs(duration_us))
end

defp format_http_request(%{conn: %Plug.Conn{} = conn}), do: format_http_request(%{conn: conn, duration_us: nil})
end

defp format_http_request(_meta), do: nil

defp to_nanosecs(duration_us) when is_number(duration_us), do: duration_us * 1000
defp to_nanosecs(_), do: nil
end
43 changes: 42 additions & 1 deletion test/logger_json/formatters/datadog_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ defmodule LoggerJSON.Formatters.DatadogTest do
|> Plug.Conn.put_req_header("x-forwarded-for", "127.0.0.1")
|> Plug.Conn.send_resp(200, "Hi!")

Logger.metadata(conn: conn)
Logger.metadata(conn: conn, duration_us: 1337)

log =
capture_log(fn ->
Expand All @@ -332,6 +332,47 @@ defmodule LoggerJSON.Formatters.DatadogTest do
},
"useragent" => "Mozilla/5.0"
}

assert log["duration"] == 1_337_000
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", "127.0.0.1")
|> Plug.Conn.send_resp(503, "oops")

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

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

assert log["error"] == %{"message" => "Hello"}

assert log["network"] == %{"client" => %{"ip" => "127.0.0.1"}}

assert log["http"] == %{
"referer" => "http://www.example.com/",
"method" => "PATCH",
"request_id" => nil,
"status_code" => 503,
"url" => "http://www.example.com/",
"url_details" => %{
"host" => "www.example.com",
"path" => "/",
"port" => 80,
"queryString" => "",
"scheme" => "http"
},
"useragent" => "Mozilla/5.0"
}

assert log["duration"] == nil
end

test "logs throws" do
Expand Down

0 comments on commit a3fcfd8

Please sign in to comment.