Skip to content

Commit

Permalink
Allow to configure EctoLogger through :telemetry
Browse files Browse the repository at this point in the history
For complex umbrella apps, having a global config for the :spandex_ecto
library can be limiting - particularly when a different tracer is to be
used depending on the repo.

This change allows to pass configuration to the underlying EctoLogger
module, like the `:tracer` option, through the `config` argument in
`:telemetry.attach/4`.
  • Loading branch information
kamilkowalski committed Apr 3, 2020
1 parent dad60bd commit 1cc732b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 6 additions & 2 deletions lib/spandex_ecto/ecto_logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ defmodule SpandexEcto.EctoLogger do

@empty_query_placeholder "unknown (unsupported ecto adapter?)"

def trace(log_entry, database) do
def trace(log_entry, database, config \\ []) do
# Put in your own configuration here
config = Application.get_env(:spandex_ecto, __MODULE__)
config =
:spandex_ecto
|> Application.get_env(__MODULE__)
|> Keyword.merge(config || [])

tracer = config[:tracer] || raise "tracer is a required option for #{inspect(__MODULE__)}"
service = config[:service] || :ecto
truncate = config[:truncate] || 5000
Expand Down
8 changes: 4 additions & 4 deletions lib/spandex_ecto/telemetry_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ defmodule SpandexEcto.TelemetryAdapter do
alias SpandexEcto.EctoLogger

# this is for ecto_sql 3.0.x
def handle_event([_app_name, repo_name, :query], total_time, log_entry, _config) when is_integer(total_time) do
EctoLogger.trace(log_entry, "#{repo_name}_database")
def handle_event([_app_name, repo_name, :query], total_time, log_entry, config) when is_integer(total_time) do
EctoLogger.trace(log_entry, "#{repo_name}_database", config)
end

# This is for ecto_sql >= 3.1
def handle_event([_app_name, repo_name, :query], measurements, metadata, _config) when is_map(measurements) do
def handle_event([_app_name, repo_name, :query], measurements, metadata, config) when is_map(measurements) do
log_entry = %{
query: metadata.query,
source: metadata.source,
Expand All @@ -22,7 +22,7 @@ defmodule SpandexEcto.TelemetryAdapter do
result: wrap_result(metadata.result)
}

EctoLogger.trace(log_entry, "#{repo_name}_database")
EctoLogger.trace(log_entry, "#{repo_name}_database", config)
end

def handle_event(event_name, measurements, log_entry, config) when is_list(event_name) do
Expand Down

0 comments on commit 1cc732b

Please sign in to comment.