fix: completely silence logs for noisy health check endpoints

Created FilterNoisyLogs plug that disables Phoenix logging for both
request and response logs by setting conn.private.phoenix_log to false.

This prevents both:
- Initial request log (Plug.Telemetry)
- Response log (Phoenix.Logger "Sent 200 in Xms")

Filters:
- GET /health (Kubernetes probes)
- HEAD / (external uptime monitors)
This commit is contained in:
Graham McIntire 2026-03-05 08:22:48 -06:00
parent 61cad41f5c
commit ec03a03cf5
No known key found for this signature in database
2 changed files with 30 additions and 9 deletions

View file

@ -51,12 +51,11 @@ defmodule ToweropsWeb.Endpoint do
plug Plug.RequestId
plug ToweropsWeb.Plugs.RemoteIpLogger
plug ToweropsWeb.Plugs.FilterNoisyLogs
plug ToweropsWeb.Plugs.BruteForceProtection
plug ToweropsWeb.Plugs.SecurityHeaders
plug Plug.Telemetry,
event_prefix: [:phoenix, :endpoint],
log: {__MODULE__, :log_level, []}
plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
@ -91,10 +90,4 @@ defmodule ToweropsWeb.Endpoint do
plug Plug.Head
plug Plug.Session, @session_options
plug ToweropsWeb.Router
# Disable logging for health check endpoint to reduce log noise from K8s probes
def log_level(%{path_info: ["health"]}), do: false
# Disable logging for HEAD requests to root path (external uptime monitors)
def log_level(%{method: "HEAD", path_info: []}), do: false
def log_level(_), do: :info
end

View file

@ -0,0 +1,28 @@
defmodule ToweropsWeb.Plugs.FilterNoisyLogs do
@moduledoc """
Disables Phoenix logging for noisy endpoints that don't need to be logged.
This prevents both the initial request log (via Plug.Telemetry) and the
response log (via Phoenix.Logger) by setting conn.private.phoenix_log to false.
Currently filters:
- GET /health (Kubernetes health checks)
- HEAD / (external uptime monitors)
"""
import Plug.Conn
def init(opts), do: opts
def call(conn, _opts) do
if should_filter?(conn) do
# Setting phoenix_log to false prevents Phoenix.Logger from logging this request
put_private(conn, :phoenix_log, false)
else
conn
end
end
defp should_filter?(%{method: "GET", path_info: ["health"]}), do: true
defp should_filter?(%{method: "HEAD", path_info: []}), do: true
defp should_filter?(_), do: false
end