From 653c395cf333f62949fd5dcb01d13c37dc83d55d Mon Sep 17 00:00:00 2001 From: Noemi <45180344+unflxw@users.noreply.github.com> Date: Fri, 11 Oct 2024 14:39:28 +0200 Subject: [PATCH] Set render metadata as tags When an error happens during rendering, the backtrace of it is limited to the Elixir process that is spawned for the rendering task, lacking any context from the customer's own code. To ameliorate this issue, add the rendering metadata as tags to the root span, providing some additional context in the resulting error sample. --- lib/appsignal_phoenix/event_handler.ex | 6 ++++++ test/appsignal_phoenix/event_handler_test.exs | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/appsignal_phoenix/event_handler.ex b/lib/appsignal_phoenix/event_handler.ex index e3b0653..0252cfc 100644 --- a/lib/appsignal_phoenix/event_handler.ex +++ b/lib/appsignal_phoenix/event_handler.ex @@ -106,6 +106,12 @@ defmodule Appsignal.Phoenix.EventHandler do def phoenix_template_render_start(_event, _measurements, metadata, _config) do parent = @tracer.current_span() + _ = @span.set_sample_data_if_nil(@tracer.root_span(), "tags", %{ + "phoenix_template" => metadata.template, + "phoenix_format" => metadata.format, + "phoenix_view" => module_name(metadata.view) + }) + "http_request" |> @tracer.create_span(parent) |> @span.set_name( diff --git a/test/appsignal_phoenix/event_handler_test.exs b/test/appsignal_phoenix/event_handler_test.exs index 13cc153..c452820 100644 --- a/test/appsignal_phoenix/event_handler_test.exs +++ b/test/appsignal_phoenix/event_handler_test.exs @@ -254,6 +254,18 @@ defmodule Appsignal.Phoenix.EventHandlerTest do assert {:ok, [{%Span{}, "appsignal:category", "render.phoenix_template"}]} = Test.Span.get(:set_attribute) end + + test "sets the root span's tags" do + assert {:ok, + [ + {%Span{}, "tags", + %{ + "phoenix_template" => "template", + "phoenix_format" => "html", + "phoenix_view" => "PhoenixWeb.View" + }} + ]} = Test.Span.get(:set_sample_data_if_nil) + end end describe "after receiving an render-start and an render-stop event" do