-
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| defmodule Zexbox.Logging.JsonFormatter do | ||
| @moduledoc """ | ||
| Returns a `:logger` formatter tuple suitable for Logstash / Elasticsearch | ||
| ingestion, wrapping `LoggerJSON.Formatters.Basic` with Zappi-standard | ||
| defaults. | ||
|
|
||
| Splat into `config/runtime.exs`: | ||
|
|
||
| config :logger, :default_handler, | ||
| formatter: Zexbox.Logging.JsonFormatter.config() | ||
|
|
||
| Mirrors opsbox's Ruby-side `JsonFormatter`: every log event becomes one | ||
| JSON object on one line, so multi-line content (struct inspections, | ||
| multi-line SQL, stack traces) collapses into a single Elasticsearch | ||
| document at the ingest layer. | ||
|
|
||
| ## Why declarative | ||
|
|
||
| `:logger` is configured before `Application.start/2` runs. Logs emitted | ||
| during application boot use the formatter that has been *configured* — | ||
| not one installed imperatively at runtime. Pure config makes JSON | ||
| active from the first log line emitted by the BEAM. | ||
|
|
||
| ## Encoder | ||
|
|
||
| Consumers choose the JSON encoder. The default for `LoggerJSON` is | ||
| Jason; on Elixir 1.18+ you can opt into the stdlib `JSON` module with: | ||
|
|
||
| config :logger_json, encoder: JSON | ||
|
|
||
| This is a compile-time setting in `config/config.exs`. | ||
| """ | ||
|
|
||
| alias LoggerJSON.{Formatters.Basic, Redactors.RedactKeys} | ||
|
|
||
| @default_metadata [ | ||
| :request_id, | ||
| :trace_id, | ||
| :span_id, | ||
| :user_id, | ||
| :pid, | ||
| :module, | ||
| :function, | ||
| :line, | ||
| :application | ||
| ] | ||
|
|
||
| @default_redactors [ | ||
| {RedactKeys, ["password", "secret", "token", "authorization", "api_key", "session"]} | ||
| ] | ||
|
|
||
| @doc """ | ||
| Returns the formatter tuple for `config :logger, :default_handler, formatter: ...`. | ||
|
|
||
| ## Options | ||
|
|
||
| * `:metadata` — list of `Logger` metadata keys to include in the JSON | ||
| output, or `:all` to include every key set via `Logger.metadata/1`. | ||
| Defaults to a curated Zappi list (see `default_metadata/0`). Pass | ||
| an explicit list to override; metadata is a security-adjacent | ||
| surface and `:all` is **not** recommended for production. | ||
|
|
||
| * `:redactors` — list of `LoggerJSON.Redactor` tuples. Defaults to a | ||
| single `RedactKeys` redactor stripping common credential keys | ||
| (see `default_redactors/0`). Pass `[]` to disable redaction; pass | ||
| a list to extend or override. | ||
|
|
||
| ## Examples | ||
|
|
||
| iex> {mod, _config} = Zexbox.Logging.JsonFormatter.config() | ||
| iex> mod | ||
| LoggerJSON.Formatters.Basic | ||
| """ | ||
| @spec config(keyword()) :: {module(), map()} | ||
| def config(opts \\ []) do | ||
| Basic.new( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm okay with being optinionated on this, but the json_logger spec is new(opts)with opts defined as type @type opts :: [
{:encoder_opts, encoder_opts()}
| {:metadata, :all | {:all_except, [atom()]} | [atom()]}
| {:redactors, [{module(), term()}]}
| {atom(), term()}
]But we are explicitly passing in only metadata and redactors, meaning you can't use encoder_opts or their other atom opts possibilities. A cheeky way to do this with minimal change would be to make use of Keyword.merge/2 Keyword.merge([a: 1, b: 2], [a: 3, d: 4])
[b: 2, a: 3, d: 4]
Keyword.merge([a: 1, b: 2], [a: 3, d: 4, a: 5])
[b: 2, a: 3, d: 4, a: 5]Which for us we can do [metadata: @default_metadata, redactors: @default_redactors]
|> Keyword.merge(opts)
|> Basic.new()To test I did this in my console metadata = [
:request_id,
:trace_id,
:span_id,
:user_id,
:pid,
:module,
:function,
:line,
:application
]
[:request_id, :trace_id, :span_id, :user_id, :pid, :module, :function, :line,
:application]
iex(5)> [m: metadata, r: 1234] |> Keyword.merge(r: 9999, o: "A random key")
[
m: [:request_id, :trace_id, :span_id, :user_id, :pid, :module, :function,
:line, :application],
r: 9999,
o: "A random key"
] |
||
| metadata: Keyword.get(opts, :metadata, @default_metadata), | ||
| redactors: Keyword.get(opts, :redactors, @default_redactors) | ||
| ) | ||
| end | ||
|
|
||
| @doc "The default metadata allow-list, exposed for inspection or extension." | ||
| @spec default_metadata() :: list(atom()) | ||
| def default_metadata, do: @default_metadata | ||
|
|
||
| @doc "The default redactor list, exposed for inspection or extension." | ||
| @spec default_redactors() :: list({module(), term()}) | ||
| def default_redactors, do: @default_redactors | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| defmodule Zexbox.Logging.JsonFormatterTest do | ||
| use ExUnit.Case, async: true | ||
|
|
||
| alias Zexbox.Logging.JsonFormatter | ||
|
|
||
| describe "config/1" do | ||
| test "returns LoggerJSON.Formatters.Basic with sensible defaults" do | ||
| assert {LoggerJSON.Formatters.Basic, opts} = JsonFormatter.config() | ||
| assert :request_id in opts.metadata | ||
| assert :user_id in opts.metadata | ||
| refute opts.redactors == [] | ||
| end | ||
|
|
||
| test "metadata override is honoured" do | ||
| assert {_module, %{metadata: [:foo, :bar]}} = | ||
| JsonFormatter.config(metadata: [:foo, :bar]) | ||
| end | ||
|
|
||
| test "redactors override is honoured" do | ||
| assert {_module, %{redactors: []}} = JsonFormatter.config(redactors: []) | ||
| end | ||
|
|
||
| test "default lists are exposed for inspection" do | ||
| assert :request_id in JsonFormatter.default_metadata() | ||
| assert [{LoggerJSON.Redactors.RedactKeys, _keys}] = JsonFormatter.default_redactors() | ||
| end | ||
| end | ||
|
|
||
| describe "Zexbox.Logging.json_formatter_config/1 delegate" do | ||
| test "returns the same config" do | ||
| assert Zexbox.Logging.json_formatter_config() == JsonFormatter.config() | ||
| end | ||
| end | ||
| end |
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