Skip to content

Commit

Permalink
Add tests to cover new liveview integration (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
derekkraan authored Nov 10, 2023
1 parent 66326e7 commit 5caf10a
Show file tree
Hide file tree
Showing 2 changed files with 1,658 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule OpentelemetryPhoenixTest do
require Record

alias PhoenixMeta, as: Meta
alias PhoenixLiveViewMeta, as: LiveViewMeta

for {name, spec} <- Record.extract_all(from_lib: "opentelemetry/include/otel_span.hrl") do
Record.defrecord(name, spec)
Expand Down Expand Up @@ -266,6 +267,168 @@ defmodule OpentelemetryPhoenixTest do
Enum.sort(Map.keys(:otel_attributes.map(event_attributes)))
end

test "records spans for Phoenix LiveView mount" do
OpentelemetryPhoenix.setup()

:telemetry.execute(
[:phoenix, :live_view, :mount, :start],
%{system_time: System.system_time()},
LiveViewMeta.mount_start()
)

:telemetry.execute(
[:phoenix, :live_view, :mount, :stop],
%{system_time: System.system_time()},
LiveViewMeta.mount_stop()
)

assert_receive {:span,
span(
name: "NnnnnWeb.MyTestLive.mount",
attributes: attributes
)}

assert %{} == :otel_attributes.map(attributes)
end

test "records spans for Phoenix LiveView handle_params" do
OpentelemetryPhoenix.setup()

:telemetry.execute(
[:phoenix, :live_view, :handle_params, :start],
%{system_time: System.system_time()},
LiveViewMeta.handle_params_start()
)

:telemetry.execute(
[:phoenix, :live_view, :handle_params, :stop],
%{system_time: System.system_time()},
LiveViewMeta.handle_params_stop()
)

assert_receive {:span,
span(
name: "NnnnnWeb.MyTestLive.handle_params",
attributes: attributes
)}

assert %{} == :otel_attributes.map(attributes)
end

test "records spans for Phoenix LiveView handle_event" do
OpentelemetryPhoenix.setup()

:telemetry.execute(
[:phoenix, :live_view, :handle_event, :start],
%{system_time: System.system_time()},
LiveViewMeta.handle_event_start()
)

:telemetry.execute(
[:phoenix, :live_view, :handle_event, :stop],
%{system_time: System.system_time()},
LiveViewMeta.handle_event_stop()
)

assert_receive {:span,
span(
name: "NnnnnWeb.MyTestLive.handle_event#hello",
attributes: attributes
)}

assert %{} == :otel_attributes.map(attributes)
end

test "handles exception during Phoenix LiveView handle_params" do
OpentelemetryPhoenix.setup()

:telemetry.execute(
[:phoenix, :live_view, :mount, :start],
%{system_time: System.system_time()},
LiveViewMeta.mount_start(:exception)
)

:telemetry.execute(
[:phoenix, :live_view, :mount, :stop],
%{system_time: System.system_time()},
LiveViewMeta.mount_stop(:exception)
)

:telemetry.execute(
[:phoenix, :live_view, :handle_params, :start],
%{system_time: System.system_time()},
LiveViewMeta.handle_params_start(:exception)
)

:telemetry.execute(
[:phoenix, :live_view, :handle_params, :exception],
%{system_time: System.system_time()},
LiveViewMeta.handle_params_exception(:exception)
)

assert_receive {:span,
span(
name: "NnnnnWeb.MyTestLive.mount",
attributes: attributes
)}

assert %{} == :otel_attributes.map(attributes)

assert_receive {:span,
span(
name: "NnnnnWeb.MyTestLive.handle_params",
attributes: attributes,
events: events
)}

assert %{} == :otel_attributes.map(attributes)

[
event(
name: "exception",
attributes: event_attributes
)
] = :otel_events.list(events)

assert [:"exception.message", :"exception.stacktrace", :"exception.type"] ==
Enum.sort(Map.keys(:otel_attributes.map(event_attributes)))
end

test "handles exceptions during Phoenix LiveView handle_event" do
OpentelemetryPhoenix.setup()

:telemetry.execute(
[:phoenix, :live_view, :handle_event, :start],
%{system_time: System.system_time()},
LiveViewMeta.handle_event_start(:exception)
)

:telemetry.execute(
[:phoenix, :live_view, :handle_event, :exception],
%{system_time: System.system_time()},
LiveViewMeta.handle_event_exception(:exception)
)

assert_receive {:span,
span(
name: "NnnnnWeb.MyTestLive.handle_event#hello",
attributes: attributes,
events: events
)}

assert %{} == :otel_attributes.map(attributes)

[
event(
name: "exception",
attributes: event_attributes
)
] = :otel_events.list(events)

assert [:"exception.message", :"exception.stacktrace", :"exception.type"] ==
Enum.sort(Map.keys(:otel_attributes.map(event_attributes)))
end

defp x_forwarded_for_request(x_forwarded_for) do
meta = Meta.endpoint_start()

Expand Down
Loading

0 comments on commit 5caf10a

Please sign in to comment.