From 6702fa53e6ffb7d56ea0c7cfc9f0c7caa1cf0d9a Mon Sep 17 00:00:00 2001 From: Bart van Oort Date: Wed, 14 Aug 2024 12:09:31 +0200 Subject: [PATCH 1/2] =?UTF-8?q?fix(elastic):=20encode=20duration=5F=CE=BCs?= =?UTF-8?q?=20inserted=20by=20LoggerJSON.Plug=20as=20event.duration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/logger_json/formatters/elastic.ex | 6 ++++-- test/logger_json/formatters/elastic_test.exs | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/logger_json/formatters/elastic.ex b/lib/logger_json/formatters/elastic.ex index 24ec3d4..a61e9b7 100644 --- a/lib/logger_json/formatters/elastic.ex +++ b/lib/logger_json/formatters/elastic.ex @@ -260,7 +260,8 @@ defmodule LoggerJSON.Formatters.Elastic do # - http.*: https://www.elastic.co/guide/en/ecs/8.11/ecs-http.html # - url.path: https://www.elastic.co/guide/en/ecs/8.11/ecs-url.html # - user_agent.original: https://www.elastic.co/guide/en/ecs/8.11/ecs-user_agent.html - defp format_http_request(%{conn: %Plug.Conn{} = conn}) do + # - event.duration (note: ns, not μs): https://www.elastic.co/guide/en/ecs/current/ecs-event.html#field-event-duration + defp format_http_request(%{conn: %Plug.Conn{} = conn, duration_μs: duration_μs}) do %{ "client.ip": LoggerJSON.Formatter.Plug.remote_ip(conn), "http.version": Plug.Conn.get_http_protocol(conn), @@ -268,7 +269,8 @@ defmodule LoggerJSON.Formatters.Elastic do "http.request.referrer": LoggerJSON.Formatter.Plug.get_header(conn, "referer"), "http.response.status_code": conn.status, "url.path": conn.request_path, - "user_agent.original": LoggerJSON.Formatter.Plug.get_header(conn, "user-agent") + "user_agent.original": LoggerJSON.Formatter.Plug.get_header(conn, "user-agent"), + "event.duration": duration_μs * 1000 } end end diff --git a/test/logger_json/formatters/elastic_test.exs b/test/logger_json/formatters/elastic_test.exs index 9d14720..a1334a7 100644 --- a/test/logger_json/formatters/elastic_test.exs +++ b/test/logger_json/formatters/elastic_test.exs @@ -368,7 +368,7 @@ defmodule LoggerJSON.Formatters.ElasticTest do |> Plug.Conn.put_req_header("x-forwarded-for", "") |> Plug.Conn.send_resp(200, "Hi!") - Logger.metadata(conn: conn) + Logger.metadata(conn: conn, duration_μs: 1337) log_entry = capture_log(fn -> @@ -378,6 +378,7 @@ defmodule LoggerJSON.Formatters.ElasticTest do assert %{ "client.ip" => "", + "event.duration" => 1_337_000, "http.version" => "HTTP/1.1", "http.request.method" => "GET", "http.request.referrer" => "http://www.example2.com/", From c9d41dbf33fa84478b8daefc82e6af768eb84dd3 Mon Sep 17 00:00:00 2001 From: Bart van Oort Date: Wed, 14 Aug 2024 12:31:27 +0200 Subject: [PATCH 2/2] =?UTF-8?q?fix(elastic):=20ensure=20Plug.Conn=20detail?= =?UTF-8?q?s=20are=20still=20logged=20if=20duration=5F=CE=BCs=20is=20not?= =?UTF-8?q?=20defined?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/logger_json/formatters/elastic.ex | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/logger_json/formatters/elastic.ex b/lib/logger_json/formatters/elastic.ex index a61e9b7..a456839 100644 --- a/lib/logger_json/formatters/elastic.ex +++ b/lib/logger_json/formatters/elastic.ex @@ -269,10 +269,12 @@ defmodule LoggerJSON.Formatters.Elastic do "http.request.referrer": LoggerJSON.Formatter.Plug.get_header(conn, "referer"), "http.response.status_code": conn.status, "url.path": conn.request_path, - "user_agent.original": LoggerJSON.Formatter.Plug.get_header(conn, "user-agent"), - "event.duration": duration_μs * 1000 + "user_agent.original": LoggerJSON.Formatter.Plug.get_header(conn, "user-agent") } + |> maybe_put(:"event.duration", to_nanosecs(duration_μs)) end + + defp format_http_request(%{conn: %Plug.Conn{} = conn}), do: format_http_request(%{conn: conn, duration_μs: nil}) end defp format_http_request(_meta), do: nil @@ -290,4 +292,7 @@ defmodule LoggerJSON.Formatters.Elastic do end defp safe_chardata_to_string(other), do: other + + defp to_nanosecs(duration) when is_number(duration), do: duration * 1000 + defp to_nanosecs(_), do: nil end