Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changesets/allow-for-customization-of-the-namespaces.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
bump: patch
type: change
---

Allow for more customization of trace namespaces during the trace's lifetime.
Previously, it was not possible to customize the namespace of Absinthe traces before the Absinthe instrumentation had run.
This is now possible, as the Absinthe instrumentation will only set the namespace if it has not been set already.
8 changes: 6 additions & 2 deletions lib/appsignal/absinthe.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ defmodule Appsignal.Absinthe do
|> @span.set_name(operation_name || "graphql")
|> @span.set_attribute("appsignal:category", "call.graphql")

root_span = @tracer.root_span()

root_span
|> @span.set_namespace_if_nil("graphql")

if operation_name do
@tracer.root_span()
root_span
|> @span.set_name_if_nil(operation_name)
|> @span.set_namespace("graphql")
end
end

Expand Down
39 changes: 27 additions & 12 deletions lib/appsignal/span.ex
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,28 @@ defmodule Appsignal.Span do
|> Appsignal.Span.set_namespace("http_request")

"""
def set_namespace(%Span{reference: reference} = span, namespace) when is_binary(namespace) do
:ok = @nif.set_span_namespace(reference, namespace)
span
end
def set_namespace(span, namespace) when is_binary(namespace),
do: set_attribute(span, "appsignal.namespace", namespace)

def set_namespace(span, _name), do: span

@spec set_namespace_if_nil(t() | nil, String.t()) :: t() | nil
@doc """
Sets an `Appsignal.Span`'s namespace. The namespace is `"http_request"` or
`"background_job'` to add the span to the "web" and "background" namespaces
respectively. Passing another string creates a custom namespace to store the
`Appsignal.Span`'s samples in.

## Example
Appsignal.Tracer.root_span()
|> Appsignal.Span.set_namespace("http_request")

"""
def set_namespace_if_nil(span, namespace) when is_binary(namespace),
do: set_attribute(span, "appsignal.namespace_if_nil", namespace)

def set_namespace_if_nil(span, _name), do: span

@spec set_attribute(t() | nil, String.t(), String.t() | integer() | boolean() | float()) ::
t() | nil
@doc """
Expand All @@ -153,30 +168,30 @@ defmodule Appsignal.Span do

"""
def set_attribute(%Span{reference: reference} = span, key, true) when is_binary(key) do
:ok = Nif.set_span_attribute_bool(reference, key, 1)
:ok = @nif.set_span_attribute_bool(reference, key, 1)
span
end

def set_attribute(%Span{reference: reference} = span, key, false) when is_binary(key) do
:ok = Nif.set_span_attribute_bool(reference, key, 0)
:ok = @nif.set_span_attribute_bool(reference, key, 0)
span
end

def set_attribute(%Span{reference: reference} = span, key, value)
when is_binary(key) and is_binary(value) do
:ok = Nif.set_span_attribute_string(reference, key, value)
:ok = @nif.set_span_attribute_string(reference, key, value)
span
end

def set_attribute(%Span{reference: reference} = span, key, value)
when is_binary(key) and is_integer(value) do
:ok = Nif.set_span_attribute_int(reference, key, value)
:ok = @nif.set_span_attribute_int(reference, key, value)
span
end

def set_attribute(%Span{reference: reference} = span, key, value)
when is_binary(key) and is_float(value) do
:ok = Nif.set_span_attribute_double(reference, key, value)
:ok = @nif.set_span_attribute_double(reference, key, value)
span
end

Expand All @@ -192,7 +207,7 @@ defmodule Appsignal.Span do

"""
def set_sql(%Span{reference: reference} = span, body) when is_binary(body) do
:ok = Nif.set_span_attribute_sql_string(reference, "appsignal:body", body)
:ok = @nif.set_span_attribute_sql_string(reference, "appsignal:body", body)
span
end

Expand All @@ -213,7 +228,7 @@ defmodule Appsignal.Span do
Application.get_env(:appsignal, :config),
key,
value,
&Nif.set_span_sample_data/3
&@nif.set_span_sample_data/3
)
end

Expand All @@ -232,7 +247,7 @@ defmodule Appsignal.Span do
Application.get_env(:appsignal, :config),
key,
value,
&Nif.set_span_sample_data_if_nil/3
&@nif.set_span_sample_data_if_nil/3
)
end

Expand Down
5 changes: 5 additions & 0 deletions lib/appsignal/test/span.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ defmodule Appsignal.Test.Span do
Span.set_namespace(span, name)
end

def set_namespace_if_nil(span, name) do
add(:set_namespace_if_nil, {span, name})
Span.set_namespace_if_nil(span, name)
end

def set_sample_data(span, key, value) do
add(:set_sample_data, {span, key, value})
Span.set_sample_data(span, key, value)
Expand Down
11 changes: 9 additions & 2 deletions test/appsignal/absinthe_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ defmodule Appsignal.AbsintheTest do
assert {:ok, [{"graphql", nil}]} = Test.Tracer.get(:create_span)
end

test "it updates the root span's namespace" do
execute_operation_start()
root_span = Appsignal.Tracer.root_span()

assert {:ok, [{^root_span, "graphql"}]} =
Test.Span.get(:set_namespace_if_nil)
end

test "without operation name it sets the span's name to graphql" do
execute_operation_start(%{options: []})
assert {:ok, [{%Span{}, "graphql"}]} = Test.Span.get(:set_name)
Expand All @@ -57,11 +65,10 @@ defmodule Appsignal.AbsintheTest do
assert {:ok, [{%Span{}, "OperationName"}]} = Test.Span.get(:set_name)
end

test "with operation name it updates the root span's name and namespace" do
test "it updates the root span's name" do
execute_operation_start(%{options: [operation_name: "OperationName"]})
root_span = Appsignal.Tracer.root_span()
assert {:ok, [{^root_span, "OperationName"}]} = Test.Span.get(:set_name_if_nil)
assert {:ok, [{^root_span, "graphql"}]} = Test.Span.get(:set_namespace)
end

test "sets the span's category" do
Expand Down
48 changes: 45 additions & 3 deletions test/appsignal/span_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,11 @@ defmodule AppsignalSpanTest do
assert return == span
end

test "sets the namespace through the Nif", %{span: %Span{reference: reference}} do
assert [{^reference, "test"}] = Test.Nif.get!(:set_span_namespace)
test "sets the namespace as an attribute through the Nif", %{
span: %Span{reference: reference}
} do
assert [{^reference, "appsignal.namespace", "test"}] =
Test.Nif.get!(:set_span_attribute_string)
end

test "returns nil when passing a nil-span" do
Expand All @@ -278,7 +281,46 @@ defmodule AppsignalSpanTest do
end

test "does not set the namespace" do
assert :error = Test.Nif.get(:set_span_namespace)
assert :error = Test.Nif.get(:set_span_attribute_string)
end
end

describe ".set_namespace_if_nil/2" do
setup :create_root_span

setup %{span: span} do
%{return: Span.set_namespace_if_nil(span, "test")}
end

test "returns the span", %{return: return, span: span} do
assert return == span
end

test "sets the namespace as an attribute through the Nif", %{
span: %Span{reference: reference}
} do
assert [{^reference, "appsignal.namespace_if_nil", "test"}] =
Test.Nif.get!(:set_span_attribute_string)
end

test "returns nil when passing a nil-span" do
assert Span.set_namespace_if_nil(nil, "test") == nil
end
end

describe ".set_namespace_if_nil/2, when passing a non-string namespace" do
setup :create_root_span

setup %{span: span} do
%{return: Span.set_namespace_if_nil(span, :non_string)}
end

test "returns the span", %{return: return, span: span} do
assert return == span
end

test "does not set the namespace" do
assert :error = Test.Nif.get(:set_span_attribute_string)
end
end

Expand Down
35 changes: 35 additions & 0 deletions test/support/appsignal/test_nif.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,41 @@ defmodule Appsignal.Test.Nif do
Nif.add_span_error(reference, name, message, stacktrace)
end

def set_span_attribute_string(span, key, value) do
add(:set_span_attribute_string, {span, key, value})
Nif.set_span_attribute_string(span, key, value)
end

def set_span_attribute_int(span, key, value) do
add(:set_span_attribute_int, {span, key, value})
Nif.set_span_attribute_int(span, key, value)
end

def set_span_attribute_bool(span, key, value) do
add(:set_span_attribute_bool, {span, key, value})
Nif.set_span_attribute_bool(span, key, value)
end

def set_span_attribute_double(span, key, value) do
add(:set_span_attribute_double, {span, key, value})
Nif.set_span_attribute_double(span, key, value)
end

def set_span_attribute_sql_string(span, key, value) do
add(:set_span_attribute_sql_string, {span, key, value})
Nif.set_span_attribute_sql_string(span, key, value)
end

def set_span_sample_data(span, key, value) do
add(:set_span_sample_data, {span, key, value})
Nif.set_span_sample_data(span, key, value)
end

def set_span_sample_data_if_nil(span, key, value) do
add(:set_span_sample_data_if_nil, {span, key, value})
Nif.set_span_sample_data_if_nil(span, key, value)
end

def close_span(reference) do
add(:close_span, {reference})
Nif.close_span(reference)
Expand Down
Loading