telemetry_decorator icon indicating copy to clipboard operation
telemetry_decorator copied to clipboard

Include missing bindings from inside `case` statements

Open aej opened this issue 1 year ago • 0 comments

Hi

I've just been experimenting with the library and notice that the include option does not capture items from inside a case statement. For example the following test-case will fail:

  defmodule MyApp.MyModule do
    use TelemetryDecorator

    @decorate telemetry([:my_app, :succeed_with_case], include: [:type, :foo])
    def succeed_with_case(why, opts \\ []) do
      type = Keyword.get(opts, :type, :ok)

      case type do
        :ok ->
          foo = "foo"
          {:ok, why}
        _ ->
          {:ok, why}
      end
    end
  end

  describe "happy path" do
    test "got stop with case" do
      :telemetry.attach(self(), [:my_app, :succeed_with_case, :stop], &send_handler/4, self())
      MyApp.MyModule.succeed_with_case(:result)

      assert_received {
        :test_telemetry,
        [:my_app, :succeed_with_case, :stop],
        %{duration: _},
        %{opts: [], why: :result, type: :ok, foo: "foo", result: {:ok, :result}}
      }
    end
  end

In the "got stop with case" test the binding foo = "foo" will not exist in the end metadata.

I'm guessing that by the time Kernel.bindings() is called, foo is out of scope and therefore no longer considered bound. Are you aware of any potential workarounds? I sense this is a limitation at the language level though

aej avatar May 02 '24 19:05 aej