From f254e28bb57736d917d444e23d172a019e8b9302 Mon Sep 17 00:00:00 2001 From: abc3 Date: Fri, 28 Jul 2023 17:52:37 +0200 Subject: [PATCH 1/7] feat: add region and instance id to logger --- config/config.exs | 2 +- lib/supavisor/client_handler.ex | 8 +++++++- lib/supavisor/db_handler.ex | 8 +++++++- lib/supavisor/manager.ex | 8 +++++++- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/config/config.exs b/config/config.exs index d89917c6..6f9a2d6c 100644 --- a/config/config.exs +++ b/config/config.exs @@ -22,7 +22,7 @@ config :supavisor, SupavisorWeb.Endpoint, # Configures Elixir's Logger config :logger, :console, format: "$time $metadata[$level] $message\n", - metadata: [:request_id, :project, :user] + metadata: [:request_id, :project, :user, :region, :instance_id] # Use Jason for JSON parsing in Phoenix config :phoenix, :json_library, Jason diff --git a/lib/supavisor/client_handler.ex b/lib/supavisor/client_handler.ex index a880dd95..55f7da9a 100644 --- a/lib/supavisor/client_handler.ex +++ b/lib/supavisor/client_handler.ex @@ -107,7 +107,13 @@ defmodule Supavisor.ClientHandler do hello = decode_startup_packet(bin) Logger.debug("Client startup message: #{inspect(hello)}") {user, external_id} = parse_user_info(hello.payload["user"]) - Logger.metadata(project: external_id, user: user) + + Logger.metadata( + project: external_id, + user: user, + region: System.get_env("REGION"), + instance_id: System.get_env("INSTANCE_ID") + ) sni_hostname = try_get_sni(sock) diff --git a/lib/supavisor/db_handler.ex b/lib/supavisor/db_handler.ex index c7d7ad8c..2d8895f0 100644 --- a/lib/supavisor/db_handler.ex +++ b/lib/supavisor/db_handler.ex @@ -30,7 +30,13 @@ defmodule Supavisor.DbHandler do @impl true def init(args) do Process.flag(:trap_exit, true) - Logger.metadata(project: args.tenant, user: args.user_alias) + + Logger.metadata( + project: args.tenant, + user: args.user_alias, + region: System.get_env("REGION"), + instance_id: System.get_env("INSTANCE_ID") + ) data = %{ id: args.id, diff --git a/lib/supavisor/manager.ex b/lib/supavisor/manager.ex index 3d8452d6..bb10bf85 100644 --- a/lib/supavisor/manager.ex +++ b/lib/supavisor/manager.ex @@ -45,7 +45,13 @@ defmodule Supavisor.Manager do default_parameter_status: args.default_parameter_status } - Logger.metadata(project: args.tenant, user: args.user_alias) + Logger.metadata( + project: args.tenant, + user: args.user_alias, + region: System.get_env("REGION"), + instance_id: System.get_env("INSTANCE_ID") + ) + Registry.register(Supavisor.Registry.ManagerTables, {args.tenant, args.user_alias}, tid) {:ok, state} From 741a7f96c2b1917b82c7e82ff093d49acf3a2f55 Mon Sep 17 00:00:00 2001 From: abc3 Date: Fri, 28 Jul 2023 17:53:36 +0200 Subject: [PATCH 2/7] bump version --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 599028f5..109a20f1 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.21 +0.2.22 From 9868ae0973570a5db82fffc2e90faf47ca73213b Mon Sep 17 00:00:00 2001 From: abc3 Date: Mon, 31 Jul 2023 15:36:49 +0200 Subject: [PATCH 3/7] add globally region and instance id to logger --- lib/supavisor/application.ex | 11 +++++++++++ lib/supavisor/client_handler.ex | 7 +------ lib/supavisor/db_handler.ex | 7 +------ lib/supavisor/manager.ex | 7 +------ 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/lib/supavisor/application.ex b/lib/supavisor/application.ex index c9fe66f4..c8783110 100644 --- a/lib/supavisor/application.ex +++ b/lib/supavisor/application.ex @@ -9,6 +9,17 @@ defmodule Supavisor.Application do @impl true def start(_type, _args) do + primary_config = :logger.get_primary_config() + + :ok = + :logger.set_primary_config( + :metadata, + Enum.into( + [region: System.get_env("REGION"), instance_id: System.get_env("INSTANCE_ID")], + primary_config.metadata + ) + ) + :ok = :gen_event.swap_sup_handler( :erl_signal_server, diff --git a/lib/supavisor/client_handler.ex b/lib/supavisor/client_handler.ex index 55f7da9a..f978428a 100644 --- a/lib/supavisor/client_handler.ex +++ b/lib/supavisor/client_handler.ex @@ -108,12 +108,7 @@ defmodule Supavisor.ClientHandler do Logger.debug("Client startup message: #{inspect(hello)}") {user, external_id} = parse_user_info(hello.payload["user"]) - Logger.metadata( - project: external_id, - user: user, - region: System.get_env("REGION"), - instance_id: System.get_env("INSTANCE_ID") - ) + Logger.metadata(project: external_id, user: user) sni_hostname = try_get_sni(sock) diff --git a/lib/supavisor/db_handler.ex b/lib/supavisor/db_handler.ex index 2d8895f0..2bb98ce7 100644 --- a/lib/supavisor/db_handler.ex +++ b/lib/supavisor/db_handler.ex @@ -31,12 +31,7 @@ defmodule Supavisor.DbHandler do def init(args) do Process.flag(:trap_exit, true) - Logger.metadata( - project: args.tenant, - user: args.user_alias, - region: System.get_env("REGION"), - instance_id: System.get_env("INSTANCE_ID") - ) + Logger.metadata(project: args.tenant, user: args.user_alias) data = %{ id: args.id, diff --git a/lib/supavisor/manager.ex b/lib/supavisor/manager.ex index bb10bf85..c0a13529 100644 --- a/lib/supavisor/manager.ex +++ b/lib/supavisor/manager.ex @@ -45,12 +45,7 @@ defmodule Supavisor.Manager do default_parameter_status: args.default_parameter_status } - Logger.metadata( - project: args.tenant, - user: args.user_alias, - region: System.get_env("REGION"), - instance_id: System.get_env("INSTANCE_ID") - ) + Logger.metadata(project: args.tenant, user: args.user_alias) Registry.register(Supavisor.Registry.ManagerTables, {args.tenant, args.user_alias}, tid) From 00e8d98bad51bf6dc326f0834beffcd6fa8efc67 Mon Sep 17 00:00:00 2001 From: Stas Date: Mon, 31 Jul 2023 19:34:57 +0300 Subject: [PATCH 4/7] Update lib/supavisor/client_handler.ex --- lib/supavisor/client_handler.ex | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/supavisor/client_handler.ex b/lib/supavisor/client_handler.ex index f978428a..a880dd95 100644 --- a/lib/supavisor/client_handler.ex +++ b/lib/supavisor/client_handler.ex @@ -107,7 +107,6 @@ defmodule Supavisor.ClientHandler do hello = decode_startup_packet(bin) Logger.debug("Client startup message: #{inspect(hello)}") {user, external_id} = parse_user_info(hello.payload["user"]) - Logger.metadata(project: external_id, user: user) sni_hostname = try_get_sni(sock) From f464d4c7f9e8460bc2ed1503f329aba90c641cf3 Mon Sep 17 00:00:00 2001 From: Stas Date: Mon, 31 Jul 2023 19:35:11 +0300 Subject: [PATCH 5/7] Update lib/supavisor/db_handler.ex --- lib/supavisor/db_handler.ex | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/supavisor/db_handler.ex b/lib/supavisor/db_handler.ex index 2bb98ce7..c7d7ad8c 100644 --- a/lib/supavisor/db_handler.ex +++ b/lib/supavisor/db_handler.ex @@ -30,7 +30,6 @@ defmodule Supavisor.DbHandler do @impl true def init(args) do Process.flag(:trap_exit, true) - Logger.metadata(project: args.tenant, user: args.user_alias) data = %{ From 1f83e12ebca03b9a98ee48b2f596022ff5d30368 Mon Sep 17 00:00:00 2001 From: Stas Date: Mon, 31 Jul 2023 19:35:24 +0300 Subject: [PATCH 6/7] Update lib/supavisor/manager.ex --- lib/supavisor/manager.ex | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/supavisor/manager.ex b/lib/supavisor/manager.ex index c0a13529..3d8452d6 100644 --- a/lib/supavisor/manager.ex +++ b/lib/supavisor/manager.ex @@ -46,7 +46,6 @@ defmodule Supavisor.Manager do } Logger.metadata(project: args.tenant, user: args.user_alias) - Registry.register(Supavisor.Registry.ManagerTables, {args.tenant, args.user_alias}, tid) {:ok, state} From ae14b8373ca3d7e92c2c219f2a47c4e625373402 Mon Sep 17 00:00:00 2001 From: abc3 Date: Mon, 31 Jul 2023 18:49:11 +0200 Subject: [PATCH 7/7] update test --- test/supavisor/client_handler_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/supavisor/client_handler_test.exs b/test/supavisor/client_handler_test.exs index e50fae78..44795f12 100644 --- a/test/supavisor/client_handler_test.exs +++ b/test/supavisor/client_handler_test.exs @@ -14,7 +14,7 @@ defmodule Supavisor.ClientHandlerTest do test "username consists only of username" do username = "username" {user, nil} = ClientHandler.parse_user_info(username) - assert username == "username" + assert username == user end end end