From 92d948fa1f8db9546a9074789d7e1ff7264aa720 Mon Sep 17 00:00:00 2001 From: Tom de Bruijn Date: Fri, 12 Jan 2024 16:26:09 +0100 Subject: [PATCH] Support lists as sample data root arguments We documented that we support lists as sample data root values, but in practice we did not. This fix allows for setting lists as custom sample data (and other types of sample data) as shown in the changeset. --- .../support-lists-as-root-sample-data.md | 17 ++++++ lib/appsignal/span.ex | 2 +- test/appsignal/span_test.exs | 60 +++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 .changesets/support-lists-as-root-sample-data.md diff --git a/.changesets/support-lists-as-root-sample-data.md b/.changesets/support-lists-as-root-sample-data.md new file mode 100644 index 000000000..765c4f5f1 --- /dev/null +++ b/.changesets/support-lists-as-root-sample-data.md @@ -0,0 +1,17 @@ +--- +bump: "patch" +type: "change" +--- + +Add support for lists in the sample data as root values on spans, as shown below. Previously we only supported lists as nested objects in maps. + +```elixir +Appsignal.Span.set_sample_data( + Appsignal.Tracer.root_span, + "custom_data", + [ + "value 1", + "value 2" + ] +) +``` diff --git a/lib/appsignal/span.ex b/lib/appsignal/span.ex index 6058c857b..cc2ac4f66 100644 --- a/lib/appsignal/span.ex +++ b/lib/appsignal/span.ex @@ -242,7 +242,7 @@ defmodule Appsignal.Span do end defp do_set_sample_data(%Span{reference: reference} = span, key, value, setter) - when is_binary(key) and is_map(value) do + when is_binary(key) and (is_map(value) or is_list(value)) do data = Appsignal.Utils.DataEncoder.encode(value) :ok = setter.(reference, key, data) diff --git a/test/appsignal/span_test.exs b/test/appsignal/span_test.exs index 21ecc1ba5..67eb20b82 100644 --- a/test/appsignal/span_test.exs +++ b/test/appsignal/span_test.exs @@ -504,6 +504,36 @@ defmodule AppsignalSpanTest do end end + describe ".set_sample_data/3, with a list" do + setup :create_root_span + + setup %{span: span} do + Span.set_sample_data(span, "custom_data", ["abc", "def"]) + + :ok + end + + @tag :skip_env_test_no_nif + test "sets the list as sample data", %{span: span} do + assert %{"sample_data" => %{"custom_data" => "[\"abc\",\"def\"]"}} = Span.to_map(span) + end + end + + describe ".set_sample_data/3, with a keyword list" do + setup :create_root_span + + setup %{span: span} do + Span.set_sample_data(span, "custom_data", abc: "def") + + :ok + end + + @tag :skip_env_test_no_nif + test "sets the keyword list as sample data", %{span: span} do + assert %{"sample_data" => %{"custom_data" => "[[\"abc\",\"def\"]]"}} = Span.to_map(span) + end + end + describe ".set_sample_data/3, when passing invalid data" do setup :create_root_span @@ -655,6 +685,36 @@ defmodule AppsignalSpanTest do end end + describe ".set_sample_data_if_nil/3, with a list" do + setup :create_root_span + + setup %{span: span} do + Span.set_sample_data_if_nil(span, "custom_data", ["abc", "def"]) + + :ok + end + + @tag :skip_env_test_no_nif + test "sets the list as sample data", %{span: span} do + assert %{"sample_data" => %{"custom_data" => "[\"abc\",\"def\"]"}} = Span.to_map(span) + end + end + + describe ".set_sample_data_if_nil/3, with a keyword list" do + setup :create_root_span + + setup %{span: span} do + Span.set_sample_data_if_nil(span, "custom_data", abc: "def") + + :ok + end + + @tag :skip_env_test_no_nif + test "sets the keyword list as sample data", %{span: span} do + assert %{"sample_data" => %{"custom_data" => "[[\"abc\",\"def\"]]"}} = Span.to_map(span) + end + end + describe ".set_sample_data_if_nil/3, when passing invalid data" do setup :create_root_span