Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 21 additions & 2 deletions lib/appsignal/span.ex
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,32 @@ 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)
def set_namespace(span, namespace) when is_binary(namespace) do
set_attribute(span, "appsignal.namespace", namespace)
span
end

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)
span
end

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 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