Add tests to cover new liveview integration (#221)

This commit is contained in:
Derek Kraan 2023-11-10 02:20:07 +01:00 committed by GitHub
parent 66326e7f42
commit 5caf10ae82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 1658 additions and 0 deletions

View File

@ -7,6 +7,7 @@ defmodule OpentelemetryPhoenixTest do
require Record require Record
alias PhoenixMeta, as: Meta alias PhoenixMeta, as: Meta
alias PhoenixLiveViewMeta, as: LiveViewMeta
for {name, spec} <- Record.extract_all(from_lib: "opentelemetry/include/otel_span.hrl") do for {name, spec} <- Record.extract_all(from_lib: "opentelemetry/include/otel_span.hrl") do
Record.defrecord(name, spec) Record.defrecord(name, spec)
@ -266,6 +267,168 @@ defmodule OpentelemetryPhoenixTest do
Enum.sort(Map.keys(:otel_attributes.map(event_attributes))) Enum.sort(Map.keys(:otel_attributes.map(event_attributes)))
end 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 defp x_forwarded_for_request(x_forwarded_for) do
meta = Meta.endpoint_start() meta = Meta.endpoint_start()

File diff suppressed because it is too large Load Diff