Skip to content

Commit

Permalink
stalker: fix 'non existing ETS' error, wrap ets.insert with try-rescue
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-hek committed Jun 25, 2024
1 parent 010f29e commit e47f9bc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule Membrane.Core.Parent.ChildLifeController.StartupUtils do
sinks =
children
|> Enum.filter(
&(Membrane.Element.element?(&1.module) and &1.module.membrane_element_type == :sink)
&(Membrane.Element.element?(&1.module) and &1.module.membrane_element_type.() == :sink)
)
|> Enum.map(& &1.name)

Expand Down
63 changes: 36 additions & 27 deletions lib/membrane/core/stalker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ defmodule Membrane.Core.Stalker do
@spec new(component_config(), pid()) :: t()
def new(config, supervisor) do
{:ok, pid} =
Membrane.Core.SubprocessSupervisor.start_link_utility(
Membrane.Core.SubprocessSupervisor.start_utility(
supervisor,
{__MODULE__, %{pipeline: self()}}
)
Expand Down Expand Up @@ -280,35 +280,23 @@ defmodule Membrane.Core.Stalker do

if @metrics_enabled do
defmacro report_metric(metric, value, opts \\ []) do
quote do
ets = Process.get(:__membrane_stalker_ets__)

if ets do
:ets.insert(
ets,
{{unquote(metric), unquote(opts)[:component_path] || ComponentPath.get(),
unquote(opts)[:pad]}, unquote(value)}
)
end

:ok
end
try_insert(
quote do
{unquote(metric), unquote(opts)[:component_path] || ComponentPath.get(),
unquote(opts)[:pad]}
end,
value
)
end

defmacro register_metric_function(metric, function, opts \\ []) do
quote do
ets = Process.get(:__membrane_stalker_ets__)

if ets do
:ets.insert(
ets,
{{unquote(metric), unquote(opts)[:component_path] || ComponentPath.get(),
unquote(opts)[:pad]}, unquote({@function_metric, function})}
)
end

:ok
end
try_insert(
quote do
{unquote(metric), unquote(opts)[:component_path] || ComponentPath.get(),
unquote(opts)[:pad]}
end,
{@function_metric, function}
)
end
else
defmacro report_metric(metric, value, opts \\ []) do
Expand Down Expand Up @@ -336,6 +324,27 @@ defmodule Membrane.Core.Stalker do
end
end

defp try_insert(key, value) do
quote do
ets = Process.get(:__membrane_stalker_ets__)

if ets do
try do
:ets.insert(ets, {unquote(key), unquote(value)})
rescue
error ->
require Logger
pretty_error = Exception.format(:error, error, __STACKTRACE__)

Logger.warning("""
Failed to insert a metric into the observability ETS.
Error: #{pretty_error}
""")
end
end
end
end

@impl true
def init(options) do
Utils.log_on_error do
Expand Down

0 comments on commit e47f9bc

Please sign in to comment.