Skip to content

Commit

Permalink
Record request information as metadata
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jeffkreeftmeijer committed Aug 30, 2024
1 parent 0d89f7b commit 49a2f25
Show file tree
Hide file tree
Showing 3 changed files with 31 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
11 changes: 10 additions & 1 deletion lib/appsignal_phoenix/event_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,20 @@ 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", %{
"hostname" => appsignal_metadata["host"],
"method" => appsignal_metadata["method"],
"path" => appsignal_metadata["request_path"],
"request_id" => appsignal_metadata["request_id"],
"status" => appsignal_metadata["status"]
})
end

defp name(%{conn: conn} = metadata) do
Expand Down
15 changes: 15 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,21 @@ 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 %{
"hostname" => "www.example.com",
"method" => "GET",
"request_id" => nil,
"path" => "/",
"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 49a2f25

Please sign in to comment.