-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add JSON formatter config for Logstash / Elasticsearch ingestion #57
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
Open
tsu-shiuan
wants to merge
3
commits into
master
Choose a base branch
from
feat/json-log-handler
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| defmodule Zexbox.Logging.JsonHandler do | ||
| @moduledoc """ | ||
| Replaces the default `:logger` handler's formatter with a JSON formatter | ||
| suitable for Logstash / Elasticsearch ingestion. | ||
|
|
||
| Emits one JSON object per line, so multi-line content (Elixir struct | ||
| inspections, multi-line SQL, stack traces) collapses into a single log | ||
| event at the ingest layer instead of fanning out into N separate | ||
| Elasticsearch documents. | ||
|
|
||
| This is the Phoenix / Elixir equivalent of opsbox's `JsonFormatter` for Ruby. | ||
| Wraps `LoggerJSON.Formatters.Basic` with sensible defaults for the | ||
| Zappi log-ingest pipeline. | ||
|
|
||
| ## Setup | ||
|
|
||
| In your application's `config/runtime.exs`: | ||
|
|
||
| if config_env() == :prod do | ||
| Zexbox.Logging.JsonHandler.install!() | ||
| end | ||
|
|
||
| After install, every log line is a single JSON object: | ||
|
|
||
| {"time":"...","severity":"info","message":"...","metadata":{...}} | ||
|
|
||
| Logstash's existing `kubernetes.container.name` rules can then JSON-parse | ||
| these into structured fields the same way they do for opsbox-formatted | ||
| Ruby logs. | ||
|
|
||
| ## Options | ||
|
|
||
| * `:metadata` - which `Logger` metadata keys to include. Defaults to | ||
| `:all` (every key set via `Logger.metadata/1` or | ||
| `Logger.put_application_level/2`). Pass a list (e.g. | ||
| `[:request_id, :trace_id]`) to filter, or `[]` to omit metadata. | ||
|
|
||
| * `:redactors` - a list of `LoggerJSON.Redactor` modules applied to | ||
| metadata values before serialisation, e.g. for stripping sensitive | ||
| keys. Defaults to `[]`. | ||
|
|
||
| ## Idempotency | ||
|
|
||
| Safe to call multiple times — `install!/1` simply swaps the formatter | ||
| on the existing default handler. Subsequent calls overwrite the previous | ||
| formatter config. | ||
| """ | ||
|
|
||
| @doc """ | ||
| Install the JSON formatter on the `:default` `:logger` handler. | ||
|
|
||
| Returns `:ok` on success or `{:error, reason}` if the default handler | ||
| hasn't been configured (typically only in unusual test setups). | ||
|
|
||
| ## Examples | ||
|
|
||
| iex> Zexbox.Logging.JsonHandler.install!() | ||
| :ok | ||
|
|
||
| iex> Zexbox.Logging.JsonHandler.install!(metadata: [:request_id, :trace_id]) | ||
| :ok | ||
| """ | ||
| @spec install!(keyword()) :: :ok | {:error, term()} | ||
| def install!(opts \\ []) do | ||
| formatter_config = %{ | ||
| metadata: Keyword.get(opts, :metadata, :all), | ||
| redactors: Keyword.get(opts, :redactors, []) | ||
| } | ||
|
|
||
| :logger.update_handler_config( | ||
| :default, | ||
| :formatter, | ||
| {LoggerJSON.Formatters.Basic, formatter_config} | ||
| ) | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| defmodule Zexbox.Logging.JsonHandlerTest do | ||
| use ExUnit.Case, async: false | ||
|
|
||
| alias Zexbox.Logging.JsonHandler | ||
|
|
||
| setup do | ||
| {:ok, original_config} = :logger.get_handler_config(:default) | ||
| on_exit(fn -> :logger.update_handler_config(:default, original_config) end) | ||
| :ok | ||
| end | ||
|
|
||
| describe "install!/1" do | ||
| test "swaps the default handler's formatter to LoggerJSON.Formatters.Basic" do | ||
| assert :ok = JsonHandler.install!() | ||
|
|
||
| {:ok, %{formatter: {formatter_module, _config}}} = | ||
| :logger.get_handler_config(:default) | ||
|
|
||
| assert formatter_module == LoggerJSON.Formatters.Basic | ||
| end | ||
|
|
||
| test "passes metadata: :all by default" do | ||
| assert :ok = JsonHandler.install!() | ||
|
|
||
| {:ok, %{formatter: {_module, config}}} = | ||
| :logger.get_handler_config(:default) | ||
|
|
||
| assert config.metadata == :all | ||
| end | ||
|
|
||
| test "passes through a metadata allow-list" do | ||
| assert :ok = JsonHandler.install!(metadata: [:request_id, :trace_id]) | ||
|
|
||
| {:ok, %{formatter: {_module, config}}} = | ||
| :logger.get_handler_config(:default) | ||
|
|
||
| assert config.metadata == [:request_id, :trace_id] | ||
| end | ||
|
|
||
| test "passes through redactors" do | ||
| redactors = [{LoggerJSON.Redactors.RedactKeys, ["password"]}] | ||
| assert :ok = JsonHandler.install!(redactors: redactors) | ||
|
|
||
| {:ok, %{formatter: {_module, config}}} = | ||
| :logger.get_handler_config(:default) | ||
|
|
||
| assert config.redactors == redactors | ||
| end | ||
|
|
||
| test "is idempotent across repeated calls" do | ||
| assert :ok = JsonHandler.install!() | ||
| assert :ok = JsonHandler.install!() | ||
| assert :ok = JsonHandler.install!(metadata: [:request_id]) | ||
|
|
||
| {:ok, %{formatter: {formatter_module, config}}} = | ||
| :logger.get_handler_config(:default) | ||
|
|
||
| assert formatter_module == LoggerJSON.Formatters.Basic | ||
| assert config.metadata == [:request_id] | ||
| end | ||
| end | ||
|
|
||
| describe "Zexbox.Logging.install_json_handler!/1 delegate" do | ||
| test "the parent module exposes the same function" do | ||
| assert :ok = Zexbox.Logging.install_json_handler!() | ||
|
|
||
| {:ok, %{formatter: {formatter_module, _config}}} = | ||
| :logger.get_handler_config(:default) | ||
|
|
||
| assert formatter_module == LoggerJSON.Formatters.Basic | ||
| end | ||
| end | ||
| end |
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.
You only want to put configurations affected by environment variables in
config/runtime.exs, for something like logger you want to put it insideconfig/dev.exsorconfig/prod.exs.The reason for this is that
config/{dev|test|prod}.exsis set at compile time so for deploys it can't inject env variables, where in this case the formatter is staticThere 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.
Reread brendon's review to double check. They do mention you can configure it at runtime, but in our cases that's not necessary unless we want to feature flag it (which we dont), unless you want to get fancy for opening the console.... which almost makes me backtrack the thought.
I'd suggest taking a similar approach in the readme to logger_json specifying both options
https://hexdocs.pm/logger_json/readme.html