Skip to content

Commit

Permalink
cover some edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
marcdel committed May 18, 2023
1 parent 6015272 commit ba00af2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/open_telemetry_decorator/attributes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ defmodule OpenTelemetryDecorator.Attributes do
defp recursive_get_in(obj, []), do: obj

defp recursive_get_in(obj, [key | nested_keys]) do
value =
nested_obj =
case get_in(obj, [key]) do
value when is_struct(value) -> Map.from_struct(value)
value -> value
nested_obj when is_struct(nested_obj) -> Map.from_struct(nested_obj)
nested_obj -> nested_obj
end

recursive_get_in(value, nested_keys)
recursive_get_in(nested_obj, nested_keys)
end

defp derived_name([attribute_name | nested_keys]) do
Expand Down
9 changes: 9 additions & 0 deletions test/open_telemetry_decorator/attributes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ defmodule OpenTelemetryDecorator.AttributesTest do
assert Attributes.get([obj: two_levels], [[:obj, :failed, :count]]) == [obj_failed_count: 3]
end

test "handles invalid requests for nested structs" do
one_level = %SomeStruct{beep: "boop"}
assert Attributes.get([obj: one_level], [[:obj, :wrong]]) == []

two_levels = %SomeStruct{failed: %SomeStruct{count: 3}}
assert Attributes.get([obj: two_levels], [[:obj, :wrong, :count]]) == []
assert Attributes.get([obj: two_levels], [[:obj, :failed, :wrong]]) == []
end

test "handles flat and nested attributes" do
attrs = Attributes.get([error: "whoops", obj: %{id: 1}], [:error, [:obj, :id]])
assert attrs == [{:obj_id, 1}, {:error, "whoops"}]
Expand Down

0 comments on commit ba00af2

Please sign in to comment.