OeditusCredo.Check.Warning.TelemetryInRecursiveFunction (OeditusCredo v0.6.3)

View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

This check has a base priority of high and works with any version of Elixir.

Explanation

Telemetry events should not be emitted inside recursive functions.

Emitting telemetry in recursive functions causes metric spam and performance degradation. Instead, wrap the entire recursive operation with telemetry.

Bad:

defp process_list([head | tail]) do
  :telemetry.execute([:app, :process_item], %{})  # Called N times!
  do_work(head)
  process_list(tail)
end
defp process_list([]), do: :ok

Good:

def process_list(items) do
  :telemetry.span([:app, :process_list], %{count: length(items)}, fn ->
    {do_process_list(items), %{}}
  end)
end

defp do_process_list([]), do: :ok
defp do_process_list([head | tail]) do
  do_work(head)
  do_process_list(tail)
end

Check-Specific Parameters

Use the following parameters to configure this check:

:exclude_test_files

Set to true to skip test files (default: false)

This parameter defaults to nil.

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.