Skip to content

Commit

Permalink
Record request information as metadata (#100)
Browse files Browse the repository at this point in the history
* Record request information as metadata

Aside from just adding request information to the environment field,
add it to the metadata as well to allow for use in filtering.

Fixes appsignal/support#333.

* Update lib/appsignal_phoenix/event_handler.ex

Co-authored-by: Tom de Bruijn <[email protected]>

* Update .changesets/record-request-information-as-metadata.md

Co-authored-by: Tom de Bruijn <[email protected]>

* Update lib/appsignal_phoenix/event_handler.ex

Co-authored-by: Tom de Bruijn <[email protected]>

* Update lib/appsignal_phoenix/event_handler.ex

Co-authored-by: Tom de Bruijn <[email protected]>

* Update metadata tests to match recent changes

Recent changes prefixed the metadata names with "request" or
"response", and removed the hostname field. This patch updates the
test to reflect those changes.

---------

Co-authored-by: Tom de Bruijn <[email protected]>
  • Loading branch information
jeffkreeftmeijer and tombruijn authored Sep 2, 2024
1 parent 0d89f7b commit 6e7444a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changesets/record-request-information-as-metadata.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: minor
type: add
---

Record request information as metadata like the request path, request method and response status.
10 changes: 9 additions & 1 deletion lib/appsignal_phoenix/event_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,19 @@ defmodule Appsignal.Phoenix.EventHandler do
end

defp set_span_data(span, %{conn: conn} = metadata) do
appsignal_metadata = Appsignal.Metadata.metadata(conn)

span
|> @span.set_name_if_nil(name(metadata))
|> @span.set_sample_data_if_nil("params", Appsignal.Metadata.params(conn))
|> @span.set_sample_data_if_nil("environment", Appsignal.Metadata.metadata(conn))
|> @span.set_sample_data_if_nil("environment", appsignal_metadata)
|> @span.set_sample_data_if_nil("session_data", Appsignal.Metadata.session(conn))
|> @span.set_sample_data("metadata", %{
"request_method" => appsignal_metadata["method"],
"request_path" => appsignal_metadata["request_path"],
"request_id" => appsignal_metadata["request_id"],
"response_status" => appsignal_metadata["status"]
})
end

defp name(%{conn: conn} = metadata) do
Expand Down
14 changes: 14 additions & 0 deletions test/appsignal_phoenix/event_handler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ defmodule Appsignal.Phoenix.EventHandlerTest do
"status" => 200
} == environment
end

test "sets the root span's metadata" do
{:ok, calls} = Test.Span.get(:set_sample_data)

[{%Span{}, "metadata", metadata}] =
Enum.filter(calls, fn {_span, key, _value} -> key == "metadata" end)

assert %{
"request_method" => "GET",
"request_id" => nil,
"request_path" => "/",
"response_status" => 200
} == metadata
end
end

describe "after receiving an router_dispatch-start and an router_dispatch-stop event without an event name in the conn" do
Expand Down

0 comments on commit 6e7444a

Please sign in to comment.