Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate logging from metrics #1571

Merged
merged 5 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Config

# Configures Elixir's Logger
config :logger, :default_formatter,
format: {NervesHub.LoggerFormatter, :format},
format: {NervesHub.Logger, :format},
metadata: :all

config :phoenix,
Expand Down
8 changes: 5 additions & 3 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,11 @@ if System.get_env("SENTRY_DSN_URL") do
]
end

config :nerves_hub, :statsd,
host: System.get_env("STATSD_HOST", "localhost"),
port: String.to_integer(System.get_env("STATSD_PORT", "8125"))
if host = System.get_env("STATSD_HOST") do
config :nerves_hub, :statsd,
host: System.get_env("STATSD_HOST"),
port: String.to_integer(System.get_env("STATSD_PORT", "8125"))
end

config :nerves_hub, :audit_logs,
enabled: System.get_env("TRUNATE_AUDIT_LOGS_ENABLED", "false") == "true",
Expand Down
11 changes: 3 additions & 8 deletions lib/nerves_hub/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule NervesHub.Application do
config: %{metadata: [:file, :line]}
})

NervesHub.Logger.install()
NervesHub.Logger.attach()

topologies = Application.get_env(:libcluster, :topologies, [])

Expand All @@ -29,8 +29,9 @@ defmodule NervesHub.Application do
{Registry, keys: :unique, name: NervesHub.Devices.Registry},
{Finch, name: Swoosh.Finch}
] ++
metrics(deploy_env()) ++
NervesHub.StatsdMetricsReporter.config() ++
[
NervesHub.MetricsPoller.child_spec(),
NervesHub.RateLimit,
NervesHub.Repo,
NervesHub.ObanRepo,
Expand All @@ -51,12 +52,6 @@ defmodule NervesHub.Application do
:ok
end

defp metrics("test"), do: []

defp metrics(_env) do
[NervesHub.Metrics]
end

defp deployments_supervisor("test"), do: []

defp deployments_supervisor(_) do
Expand Down
104 changes: 76 additions & 28 deletions lib/nerves_hub/logger.ex
Original file line number Diff line number Diff line change
@@ -1,42 +1,93 @@
defmodule NervesHub.Logger do
require Logger

@doc false
def install() do
handlers = %{
[:phoenix, :endpoint, :stop] => &__MODULE__.phoenix_endpoint_stop/4
}
@metadata_ignore_list [:line, :file, :domain, :application, :pid, :mfa]
@pattern Logger.Formatter.compile("$time [$level] msg=\"$message\" $metadata\n")

for {key, fun} <- handlers do
:ok = :telemetry.attach({__MODULE__, key}, key, fun, :ok)
end
def format(level, message, timestamp, metadata) do
metadata = Keyword.drop(metadata, ignore_list())
Logger.Formatter.format(@pattern, level, message, timestamp, metadata)
end

:ok
@doc false
def attach() do
events = [
[:phoenix, :endpoint, :stop],
[:nerves_hub, :devices, :connect],
[:nerves_hub, :devices, :disconnect],
[:nerves_hub, :devices, :duplicate_connection],
[:nerves_hub, :devices, :update, :automatic]
]

Enum.each(events, fn event ->
:ok = :telemetry.attach({__MODULE__, event}, event, &__MODULE__.log_event/4, :ok)
end)
end

# Phoenix request logging

@doc false
def phoenix_endpoint_stop(_, %{duration: duration}, %{conn: conn} = metadata, _) do
case log_level(metadata[:options][:log], conn) do
false ->
:ok

level ->
Logger.log(level, fn ->
Logfmt.encode(
duration: duration(duration),
method: conn.method,
path: request_path(conn),
status: conn.status,
remote_ip: formatted_ip(conn)
)
end)
end
def log_event([:phoenix, :endpoint, :stop], %{duration: duration}, %{conn: conn}, _) do
Logger.info("Request completed", %{
duration: duration(duration),
method: conn.method,
path: request_path(conn),
status: conn.status,
remote_ip: formatted_ip(conn)
})
end

def log_event([:nerves_hub, :devices, :connect], _, metadata, _) do
Logger.info("Device connected",
event: "nerves_hub.devices.connect",
identifier: metadata[:identifier],
firmware_uuid: metadata[:firmware_uuid]
)
end

def log_event([:nerves_hub, :devices, :duplicate_connection], _, metadata, _) do
Logger.info("Device duplicate connection detected",
event: "nerves_hub.devices.duplicate_connection",
ref_id: metadata[:ref_id],
identifier: metadata[:device].identifier
)
end

def log_event([:nerves_hub, :devices, :disconnect], _, metadata, _) do
Logger.info("Device disconnected",
event: "nerves_hub.devices.disconnect",
ref_id: metadata[:ref_id],
identifier: metadata[:identifier]
)
end

def log_event([:nerves_hub, :devices, :update, :automatic], _, metadata, _) do
Logger.info("Device received update",
event: "nerves_hub.devices.update.automatic",
ref_id: metadata[:ref_id],
identifier: metadata[:identifier],
firmware_uuid: metadata[:firmware_uuid]
)
end

def log_event([:nerves_hub, :devices, :update, :successful], _, metadata, _) do
Logger.info("Device updated firmware",
event: "nerves_hub.devices.update.successful",
identifier: metadata[:identifier],
firmware_uuid: metadata[:firmware_uuid]
)
end

# Helper functions

defp ignore_list() do
if Application.get_env(:nerves_hub, :log_include_mfa) do
@metadata_ignore_list -- [:mfa]
else
@metadata_ignore_list
end
end

defp duration(duration) do
duration = System.convert_time_unit(duration, :native, :microsecond)

Expand Down Expand Up @@ -69,7 +120,4 @@ defmodule NervesHub.Logger do
|> to_string()
end
end

defp log_level(nil, _conn), do: :info
defp log_level(level, _conn) when is_atom(level), do: level
end
17 changes: 0 additions & 17 deletions lib/nerves_hub/logger_formatter.ex

This file was deleted.

185 changes: 0 additions & 185 deletions lib/nerves_hub/metrics.ex

This file was deleted.

Loading
Loading