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

Console log measurement name with conversion fun name #109

Merged
merged 3 commits into from
Mar 24, 2024
Merged
Changes from 1 commit
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
Next Next commit
Log measurement name with conversion fun name
  • Loading branch information
03juan committed Mar 23, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 9571519e532bf38fb3168bd73f16b380e6dd0f0c
11 changes: 10 additions & 1 deletion lib/telemetry_metrics/console_reporter.ex
Original file line number Diff line number Diff line change
@@ -85,7 +85,7 @@ defmodule Telemetry.Metrics.ConsoleReporter do
for %struct{} = metric <- metrics do
header = """

Metric measurement: #{inspect(metric.measurement)} (#{metric(struct)})
Metric measurement: #{measurement_name(metric)} (#{metric(struct)})
"""

[
@@ -133,6 +133,15 @@ defmodule Telemetry.Metrics.ConsoleReporter do
IO.puts(device, [prelude | parts])
end

defp measurement_name(%{measurement: measurement}) when is_atom(measurement),
do: inspect(measurement)

defp measurement_name(%{measurement: fun, name: name}) when is_function(fun) do
measurement = List.last(name)

"#{inspect(measurement)} [via #{inspect(fun)}]"
end

defp keep?(%{keep: nil}, _metadata), do: true
defp keep?(metric, metadata), do: metric.keep.(metadata)

44 changes: 41 additions & 3 deletions test/console_reporter_test.exs
Original file line number Diff line number Diff line change
@@ -30,7 +30,8 @@ defmodule Telemetry.Metrics.ConsoleReporterTest do
),
distribution("phoenix.endpoint.stop.duration",
measurement: &__MODULE__.measurement/1
)
),
summary("my_app.repo.query.query_time", unit: {:native, :millisecond})
]

{:ok, device} = StringIO.open("")
@@ -158,7 +159,7 @@ defmodule Telemetry.Metrics.ConsoleReporterTest do
All measurements: %{}
All metadata: %{key: :value}

Metric measurement: &Telemetry.Metrics.ConsoleReporterTest.metadata_measurement/2 (sum)
Metric measurement: :metadata [via &Telemetry.Metrics.ConsoleReporterTest.metadata_measurement/2] (sum)
With value: 1
Tag values: %{}

@@ -176,10 +177,47 @@ defmodule Telemetry.Metrics.ConsoleReporterTest do
All measurements: %{duration: 100}
All metadata: %{}

Metric measurement: &Telemetry.Metrics.ConsoleReporterTest.measurement/1 (distribution)
Metric measurement: :duration [via &Telemetry.Metrics.ConsoleReporterTest.measurement/1] (distribution)
With value: 100
Tag values: %{}

"""
end

test "can convert native system time unit", %{device: device, formatter: formatter} do
event = [:my_app, :repo, :query]
native_time = :erlang.system_time()

expected_millisecond = native_time * (1 / System.convert_time_unit(1, :millisecond, :native))
expected_measurement_fun = measurement_fun(event, :query_time, formatter, device)

:telemetry.execute(event, %{query_time: native_time})

{_in, out} = StringIO.contents(device)

assert out == """
[Telemetry.Metrics.ConsoleReporter] Got new event!
Event name: my_app.repo.query
All measurements: %{query_time: #{native_time}}
All metadata: %{}

Metric measurement: :query_time [via #{inspect(expected_measurement_fun)}] (summary)
With value: #{expected_millisecond} millisecond
Tag values: %{}

"""
end

defp measurement_fun(event, measurement, formatter, device) do
name = event ++ [measurement]

event
|> :telemetry.list_handlers()
|> Enum.find_value(fn
%{id: {Telemetry.Metrics.ConsoleReporter, ^event, ^formatter}, config: {config, ^device}} ->
Enum.find_value(config, fn %{name: ^name, measurement: fun} ->
fun
end)
end)
end
end