-
Notifications
You must be signed in to change notification settings - Fork 0
Prometheus + JSON logging for grafana #82
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
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
eb04f33
Prometheus + JSON logging for grafana
naps62 3964670
wip
naps62 838781f
wip
naps62 3e28628
wip
naps62 8fecfb8
wip
naps62 7ad1a96
wip
naps62 3170e80
wip
naps62 b1a5026
wip
naps62 a8ceda9
wip
naps62 934c3a0
wip
naps62 a309f8f
wip
naps62 bc6d8ce
wip
naps62 c447783
wip
naps62 9acf22e
wip
naps62 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| defmodule Ethui.Telemetry do | ||
| @moduledoc """ | ||
| Helper module for executing telemetry events with the [:ethui] prefix. | ||
| """ | ||
|
|
||
| @spec exec(event :: [atom()], metadata :: map()) :: :ok | ||
| def exec(event, metadata \\ %{}) when is_list(event) do | ||
| :telemetry.execute([:ethui | event], %{count: 1}, metadata) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| defmodule EthuiWeb.MetricsController do | ||
| use EthuiWeb, :controller | ||
|
|
||
| @moduledoc """ | ||
| Exposes Prometheus metrics for scraping. | ||
| """ | ||
|
|
||
| def index(conn, _params) do | ||
| metrics = TelemetryMetricsPrometheus.Core.scrape(EthuiWeb.Telemetry.Prometheus) | ||
|
|
||
| conn | ||
| |> put_resp_content_type("text/plain") | ||
| |> send_resp(200, metrics) | ||
| end | ||
| end | ||
naps62 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| defmodule EthuiWeb.Plugs.LogMetadata do | ||
| @moduledoc """ | ||
| Adds request-specific metadata to logs for structured JSON logging. | ||
|
|
||
| This plug enriches logs with contextual information including: | ||
| - Request ID, remote IP, HTTP method, and path | ||
| - User ID (if authenticated) | ||
| - Stack slug (if available from subdomain routing) | ||
| - Response status and duration (added before sending response) | ||
| """ | ||
|
|
||
| import Plug.Conn | ||
| require Logger | ||
|
|
||
| def init(opts), do: opts | ||
|
|
||
| def call(conn, _opts) do | ||
| Logger.metadata( | ||
| request_id: get_request_id(conn), | ||
| remote_ip: format_ip(conn.remote_ip), | ||
| method: conn.method, | ||
| path: conn.request_path | ||
| ) | ||
|
|
||
| # Add user_id if authenticated | ||
| case conn.assigns[:current_user] do | ||
| %{id: user_id} -> Logger.metadata(user_id: user_id) | ||
| _ -> :ok | ||
| end | ||
|
|
||
| # Add stack_slug if available (from subdomain routing) | ||
| case conn.assigns[:stack] do | ||
| %{slug: slug} -> Logger.metadata(stack_slug: slug) | ||
| _ -> :ok | ||
| end | ||
|
|
||
| register_before_send(conn, fn conn -> | ||
| # Add response metadata before sending | ||
| Logger.metadata( | ||
| status: conn.status, | ||
| duration: calculate_duration(conn) | ||
| ) | ||
|
|
||
| # Emit API request telemetry event | ||
| Ethui.Telemetry.exec( | ||
| [:api, :requests], | ||
| %{method: conn.method, path: conn.request_path, status: conn.status} | ||
| ) | ||
|
|
||
| conn | ||
| end) | ||
naps62 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| defp get_request_id(conn) do | ||
| case get_resp_header(conn, "x-request-id") do | ||
| [request_id] -> request_id | ||
naps62 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| _ -> Logger.metadata()[:request_id] | ||
| end | ||
| end | ||
|
|
||
| defp format_ip({a, b, c, d}), do: "#{a}.#{b}.#{c}.#{d}" | ||
naps62 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| defp format_ip({a, b, c, d, e, f, g, h}) do | ||
| # IPv6 address - convert to standard colon-separated hex notation | ||
| :inet.ntoa({a, b, c, d, e, f, g, h}) |> to_string() | ||
| end | ||
|
|
||
| defp calculate_duration(conn) do | ||
| case conn.private[:phoenix_endpoint_start] do | ||
| %{system: start} -> | ||
| System.monotonic_time() | ||
| |> Kernel.-(start) | ||
| |> System.convert_time_unit(:native, :microsecond) | ||
|
|
||
| _ -> | ||
| nil | ||
| end | ||
| end | ||
| end | ||
naps62 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
metadata: :allin the logger configuration may capture excessive metadata and could lead to performance issues or large log sizes, especially in production. This setting captures all metadata fields from the Logger context, including internal Erlang/OTP fields and any custom metadata added throughout the application.Consider explicitly listing the metadata fields you want to capture instead of using
:all. The:consoleconfiguration already has an explicit list of metadata fields (lines 92-107), which is the recommended approach. Apply the same pattern to the:default_handlerconfiguration.