Skip to content

Commit

Permalink
Fix 'table identifier does not refer to an existing ETS table' error …
Browse files Browse the repository at this point in the history
…when inserting metrics into the observability ETS (#835)

* stalker: fix 'non existing ETS' error, wrap ets.insert with try-rescue

* improve logging errors from components
  • Loading branch information
mat-hek committed Jun 28, 2024
1 parent 010f29e commit 2a545e3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 32 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 1.1.1
* Fix 'table identifier does not refer to an existing ETS table' error when inserting metrics into the observability ETS [#835](https://github.com/membraneframework/membrane_core/pull/835)

## 1.1.0
* Add new callbacks `handle_child_setup_completed/3` and `handle_child_playing/3` in Bins and Pipelines. [#801](https://github.com/membraneframework/membrane_core/pull/801)
* Deprecate `handle_spec_started/3` callback in Bins and Pipelines. [#708](https://github.com/membraneframework/membrane_core/pull/708)
Expand Down
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
7 changes: 3 additions & 4 deletions lib/membrane/core/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ defmodule Membrane.Core.Utils do
try do
unquote(code)
rescue
e ->
error ->
require Membrane.Logger

Membrane.Logger.error("""
Error occured in #{unquote(error_source)}:
#{inspect(e, pretty: true, limit: :infinity)}
#{Exception.format_stacktrace(__STACKTRACE__)}
#{Exception.format(:error, error, __STACKTRACE__)}
""")

reraise e, __STACKTRACE__
reraise error, __STACKTRACE__
end
end
end
Expand Down

0 comments on commit 2a545e3

Please sign in to comment.