Skip to content

Commit

Permalink
Add tests to cover new liveview integration
Browse files Browse the repository at this point in the history
  • Loading branch information
derekkraan committed Nov 1, 2023
1 parent c7edb8a commit ac3d4f6
Show file tree
Hide file tree
Showing 3 changed files with 1,697 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ defmodule OpentelemetryPhoenix do
SemanticConventions.Trace.http_target() => conn.request_path,
SemanticConventions.Trace.http_user_agent() => user_agent,
SemanticConventions.Trace.net_host_name() => conn.host,
SemanticConventions.Trace.net_sock_host_addr() => to_string(:inet_parse.ntoa(conn.remote_ip)),
SemanticConventions.Trace.net_sock_host_addr() =>
to_string(:inet_parse.ntoa(conn.remote_ip)),
SemanticConventions.Trace.net_host_port() => conn.port,
SemanticConventions.Trace.net_sock_peer_addr() => to_string(:inet_parse.ntoa(peer_ip)),
SemanticConventions.Trace.net_peer_port() => peer_data.port,
Expand Down
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 @@ -59,7 +60,8 @@ defmodule OpentelemetryPhoenixTest do
"http.scheme": "http",
"http.status_code": 200,
"http.target": "/users/123",
"http.user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:81.0) Gecko/20100101 Firefox/81.0",
"http.user_agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:81.0) Gecko/20100101 Firefox/81.0",
"net.sock.host.addr": "10.211.55.2",
"net.host.port": 4000,
"net.sock.peer.addr": "10.211.55.2",
Expand Down Expand Up @@ -138,7 +140,8 @@ defmodule OpentelemetryPhoenixTest do
"http.scheme": "http",
"http.status_code": 500,
"http.target": "/users/123/exception",
"http.user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:81.0) Gecko/20100101 Firefox/81.0",
"http.user_agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:81.0) Gecko/20100101 Firefox/81.0",
"net.sock.host.addr": "10.211.55.2",
"net.host.port": 4000,
"net.sock.peer.addr": "10.211.55.2",
Expand Down Expand Up @@ -245,7 +248,8 @@ defmodule OpentelemetryPhoenixTest do
"http.scheme": "http",
"http.status_code": 500,
"http.target": "/users/123/exception",
"http.user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:81.0) Gecko/20100101 Firefox/81.0",
"http.user_agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:81.0) Gecko/20100101 Firefox/81.0",
"net.sock.host.addr": "10.211.55.2",
"net.host.port": 4000,
"net.sock.peer.addr": "10.211.55.2",
Expand All @@ -266,6 +270,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 ac3d4f6

Please sign in to comment.