mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
* Update README.md * Add structured JSON production logging (#105) * Log requests and background failures (#107) * Document production logging and the VPS runbook
This commit is contained in:
@@ -41,7 +41,10 @@ defmodule ChesstrainerWeb.Endpoint do
|
||||
cookie_key: "request_logger"
|
||||
|
||||
plug Plug.RequestId
|
||||
plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
|
||||
|
||||
# Access lines come from ChesstrainerWeb.RequestLogger (one structured line
|
||||
# per request); disable Phoenix.Logger's endpoint start/stop pair.
|
||||
plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint], log: false
|
||||
|
||||
plug Plug.Parsers,
|
||||
parsers: [:urlencoded, :multipart, :json],
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
defmodule ChesstrainerWeb.Plugs.PutRequestId do
|
||||
@moduledoc """
|
||||
Copies `Plug.RequestId`'s request id into the session.
|
||||
|
||||
LiveView processes handle later events after the original HTTP request has
|
||||
finished. Keeping the id in the session lets `UserAuth.mount_current_scope/2`
|
||||
put it into `Logger.metadata/1`, so LiveView logs remain correlatable with the
|
||||
request that mounted them.
|
||||
"""
|
||||
|
||||
@behaviour Plug
|
||||
|
||||
import Plug.Conn
|
||||
|
||||
@impl true
|
||||
def init(opts), do: opts
|
||||
|
||||
@impl true
|
||||
def call(conn, _opts) do
|
||||
case conn.assigns[:request_id] do
|
||||
nil -> conn
|
||||
request_id -> put_session(conn, :request_id, request_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,66 @@
|
||||
defmodule ChesstrainerWeb.RequestLogger do
|
||||
@moduledoc """
|
||||
Emits one structured access line per HTTP request.
|
||||
|
||||
The line carries the method, path, status, duration in milliseconds and the
|
||||
request id when one is available. Query parameters and bodies are never
|
||||
logged. Path segments that look like tokens or identifiers (20 or more
|
||||
URL-safe characters) are replaced with `[REDACTED]`.
|
||||
|
||||
`Phoenix.Logger`'s own endpoint start/stop lines are disabled in
|
||||
`ChesstrainerWeb.Endpoint` so each request produces exactly one line.
|
||||
"""
|
||||
|
||||
require Logger
|
||||
|
||||
@event [:phoenix, :endpoint, :stop]
|
||||
@handler_id {__MODULE__, :access_log}
|
||||
@token_segment ~r/\A[A-Za-z0-9_.-]{20,}\z/
|
||||
|
||||
@doc false
|
||||
def install do
|
||||
case :telemetry.attach(@handler_id, @event, &__MODULE__.handle_event/4, :ok) do
|
||||
:ok -> :ok
|
||||
{:error, :already_exists} -> :ok
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_event(_event, measurements, metadata, _config) do
|
||||
conn = metadata.conn
|
||||
duration_ms = duration_ms(measurements.duration)
|
||||
path = redact_path(conn.request_path)
|
||||
status = conn.status || 0
|
||||
|
||||
metadata =
|
||||
[
|
||||
method: conn.method,
|
||||
path: path,
|
||||
status: status,
|
||||
duration_ms: duration_ms
|
||||
]
|
||||
|> put_request_id(conn.assigns[:request_id])
|
||||
|
||||
Logger.info("#{conn.method} #{path} #{status} in #{duration_ms}ms", metadata)
|
||||
end
|
||||
|
||||
defp duration_ms(native) do
|
||||
native
|
||||
|> System.convert_time_unit(:native, :microsecond)
|
||||
|> Kernel./(1000)
|
||||
|> Float.round(1)
|
||||
end
|
||||
|
||||
defp put_request_id(metadata, nil), do: metadata
|
||||
defp put_request_id(metadata, request_id), do: Keyword.put(metadata, :request_id, request_id)
|
||||
|
||||
defp redact_path(path) do
|
||||
path
|
||||
|> String.split("/")
|
||||
|> Enum.map_join("/", &redact_segment/1)
|
||||
end
|
||||
|
||||
defp redact_segment(segment) do
|
||||
if Regex.match?(@token_segment, segment), do: "[REDACTED]", else: segment
|
||||
end
|
||||
end
|
||||
@@ -6,6 +6,7 @@ defmodule ChesstrainerWeb.Router do
|
||||
pipeline :browser do
|
||||
plug :accepts, ["html"]
|
||||
plug :fetch_session
|
||||
plug ChesstrainerWeb.Plugs.PutRequestId
|
||||
plug :fetch_live_flash
|
||||
plug :put_root_layout, html: {ChesstrainerWeb.Layouts, :root}
|
||||
plug :protect_from_forgery
|
||||
|
||||
@@ -8,6 +8,8 @@ defmodule ChesstrainerWeb.Telemetry do
|
||||
|
||||
@impl true
|
||||
def init(_arg) do
|
||||
ChesstrainerWeb.RequestLogger.install()
|
||||
|
||||
children = [
|
||||
# Telemetry poller will execute the given period measurements
|
||||
# every 10_000ms. Learn more here: https://telemetry-metrics.hexdocs.pm
|
||||
|
||||
@@ -290,6 +290,10 @@ defmodule ChesstrainerWeb.UserAuth do
|
||||
end
|
||||
|
||||
defp mount_current_scope(socket, session) do
|
||||
if request_id = session["request_id"] do
|
||||
Logger.metadata(request_id: request_id)
|
||||
end
|
||||
|
||||
Phoenix.Component.assign_new(socket, :current_scope, fn ->
|
||||
{user, _} =
|
||||
if user_token = session["user_token"] do
|
||||
|
||||
Reference in New Issue
Block a user