Skip to content

Commit d974153

Browse files
authored
fix: remove healthcheck request log (#1677)
DISABLE_HEALTHCHECK_LOGGING ENV to decide logging or not. Defauls to false
1 parent eb34634 commit d974153

File tree

6 files changed

+113
-4
lines changed

6 files changed

+113
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ If you're using the default tenant, the URL is `ws://realtime-dev.localhost:4000
160160
| SEED_SELF_HOST | boolean | Seeds the system with default tenant |
161161
| SELF_HOST_TENANT_NAME | string | Tenant reference to be used for self host. Do keep in mind to use a URL compatible name |
162162
| LOG_LEVEL | string | Sets log level for Realtime logs. Defaults to info, supported levels are: info, emergency, alert, critical, error, warning, notice, debug |
163+
| DISABLE_HEALTHCHECK_LOGGING | boolean | Disables request logging for healthcheck endpoints (/healthcheck and /api/tenants/:tenant_id/health). Defaults to false. |
163164
| RUN_JANITOR | boolean | Do you want to janitor tasks to run |
164165
| JANITOR_SCHEDULE_TIMER_IN_MS | number | Time in ms to run the janitor task |
165166
| JANITOR_SCHEDULE_RANDOMIZE | boolean | Adds a randomized value of minutes to the timer |

config/runtime.exs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ tenant_max_joins_per_second = Env.get_integer("TENANT_MAX_JOINS_PER_SECOND", 100
6262
rpc_timeout = Env.get_integer("RPC_TIMEOUT", :timer.seconds(30))
6363
max_gen_rpc_clients = Env.get_integer("MAX_GEN_RPC_CLIENTS", 5)
6464
run_janitor? = Env.get_boolean("RUN_JANITOR", false)
65+
disable_healthcheck_logging = Env.get_boolean("DISABLE_HEALTHCHECK_LOGGING", false)
6566
janitor_schedule_randomize = Env.get_boolean("JANITOR_SCHEDULE_RANDOMIZE", true)
6667
janitor_max_children = Env.get_integer("JANITOR_MAX_CHILDREN", 5)
6768
janitor_chunk_size = Env.get_integer("JANITOR_CHUNK_SIZE", 10)
@@ -142,7 +143,8 @@ config :realtime,
142143
regional_broadcasting: regional_broadcasting,
143144
master_region: master_region,
144145
metrics_tags: metrics_tags,
145-
measure_traffic_interval_in_ms: measure_traffic_interval_in_ms
146+
measure_traffic_interval_in_ms: measure_traffic_interval_in_ms,
147+
disable_healthcheck_logging: disable_healthcheck_logging
146148

147149
if config_env() != :test && run_janitor? do
148150
config :realtime,

lib/realtime_web/endpoint.ex

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,21 @@ defmodule RealtimeWeb.Endpoint do
6767
cookie_key: "request_logger"
6868

6969
plug BaggageRequestId, baggage_key: BaggageRequestId.baggage_key()
70-
plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
70+
71+
plug Plug.Telemetry,
72+
event_prefix: [:phoenix, :endpoint],
73+
log: {__MODULE__, :log_level, []}
74+
75+
# Disables logging for routes /healthcheck and /api/tenants/:tenant_id/health when DISABLE_HEALTHCHECK_LOGGING=true
76+
def log_level(%{path_info: ["healthcheck"]}) do
77+
if Application.get_env(:realtime, :disable_healthcheck_logging, false), do: false, else: :info
78+
end
79+
80+
def log_level(%{path_info: ["api", "tenants", _, "health"]}) do
81+
if Application.get_env(:realtime, :disable_healthcheck_logging, false), do: false, else: :info
82+
end
83+
84+
def log_level(_), do: :info
7185

7286
plug Plug.Parsers,
7387
parsers: [:urlencoded, :multipart, :json],

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule Realtime.MixProject do
44
def project do
55
[
66
app: :realtime,
7-
version: "2.71.4",
7+
version: "2.71.5",
88
elixir: "~> 1.18",
99
elixirc_paths: elixirc_paths(Mix.env()),
1010
start_permanent: Mix.env() == :prod,

test/realtime_web/controllers/page_controller_test.exs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
defmodule RealtimeWeb.PageControllerTest do
2-
use RealtimeWeb.ConnCase
2+
use RealtimeWeb.ConnCase, async: false
3+
4+
import ExUnit.CaptureLog
35

46
test "GET / renders index page", %{conn: conn} do
57
conn = get(conn, "/")
@@ -10,4 +12,48 @@ defmodule RealtimeWeb.PageControllerTest do
1012
conn = get(conn, "/healthcheck")
1113
assert text_response(conn, 200) == "ok"
1214
end
15+
16+
describe "GET /healthcheck logging behavior" do
17+
setup do
18+
original_value = Application.get_env(:realtime, :disable_healthcheck_logging, false)
19+
on_exit(fn -> Application.put_env(:realtime, :disable_healthcheck_logging, original_value) end)
20+
:ok
21+
end
22+
23+
test "logs request when DISABLE_HEALTHCHECK_LOGGING is false", %{conn: conn} do
24+
Application.put_env(:realtime, :disable_healthcheck_logging, false)
25+
26+
log =
27+
capture_log(fn ->
28+
conn = get(conn, "/healthcheck")
29+
assert text_response(conn, 200) == "ok"
30+
end)
31+
32+
assert log =~ "GET /healthcheck"
33+
end
34+
35+
test "does not log request when DISABLE_HEALTHCHECK_LOGGING is true", %{conn: conn} do
36+
Application.put_env(:realtime, :disable_healthcheck_logging, true)
37+
38+
log =
39+
capture_log(fn ->
40+
conn = get(conn, "/healthcheck")
41+
assert text_response(conn, 200) == "ok"
42+
end)
43+
44+
refute log =~ "GET /healthcheck"
45+
end
46+
47+
test "logs request when DISABLE_HEALTHCHECK_LOGGING is not set (default)", %{conn: conn} do
48+
Application.delete_env(:realtime, :disable_healthcheck_logging)
49+
50+
log =
51+
capture_log(fn ->
52+
conn = get(conn, "/healthcheck")
53+
assert text_response(conn, 200) == "ok"
54+
end)
55+
56+
assert log =~ "GET /healthcheck"
57+
end
58+
end
1359
end

test/realtime_web/controllers/tenant_controller_test.exs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule RealtimeWeb.TenantControllerTest do
33
# Also using global otel_simple_processor
44
use RealtimeWeb.ConnCase, async: false
55

6+
import ExUnit.CaptureLog
67
require OpenTelemetry.Tracer, as: Tracer
78

89
alias Realtime.Api.Tenant
@@ -443,6 +444,51 @@ defmodule RealtimeWeb.TenantControllerTest do
443444

444445
assert attributes(map: %{external_id: ^external_id}) = attributes
445446
end
447+
448+
test "logs request when DISABLE_HEALTHCHECK_LOGGING is false", %{conn: conn, tenant: tenant} do
449+
original_value = Application.get_env(:realtime, :disable_healthcheck_logging, false)
450+
Application.put_env(:realtime, :disable_healthcheck_logging, false)
451+
on_exit(fn -> Application.put_env(:realtime, :disable_healthcheck_logging, original_value) end)
452+
453+
log =
454+
capture_log(fn ->
455+
conn = get(conn, ~p"/api/tenants/#{tenant.external_id}/health")
456+
assert json_response(conn, 200)
457+
end)
458+
459+
assert log =~ "GET /api/tenants"
460+
assert log =~ "/health"
461+
end
462+
463+
test "does not log request when DISABLE_HEALTHCHECK_LOGGING is true", %{conn: conn, tenant: tenant} do
464+
original_value = Application.get_env(:realtime, :disable_healthcheck_logging, false)
465+
Application.put_env(:realtime, :disable_healthcheck_logging, true)
466+
on_exit(fn -> Application.put_env(:realtime, :disable_healthcheck_logging, original_value) end)
467+
468+
log =
469+
capture_log(fn ->
470+
conn = get(conn, ~p"/api/tenants/#{tenant.external_id}/health")
471+
assert json_response(conn, 200)
472+
end)
473+
474+
refute log =~ "GET /api/tenants"
475+
refute log =~ "/health"
476+
end
477+
478+
test "logs request when DISABLE_HEALTHCHECK_LOGGING is not set (default)", %{conn: conn, tenant: tenant} do
479+
original_value = Application.get_env(:realtime, :disable_healthcheck_logging, false)
480+
Application.delete_env(:realtime, :disable_healthcheck_logging)
481+
on_exit(fn -> Application.put_env(:realtime, :disable_healthcheck_logging, original_value) end)
482+
483+
log =
484+
capture_log(fn ->
485+
conn = get(conn, ~p"/api/tenants/#{tenant.external_id}/health")
486+
assert json_response(conn, 200)
487+
end)
488+
489+
assert log =~ "GET /api/tenants"
490+
assert log =~ "/health"
491+
end
446492
end
447493

448494
defp default_tenant_attrs(port) do

0 commit comments

Comments
 (0)