Skip to content

Commit

Permalink
[PLATFORM-1004]: Debug 1.2.0 failing to start (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpiemontese authored Mar 13, 2023
1 parent ef53de5 commit fb6d895
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
67 changes: 43 additions & 24 deletions lib/telepoison.ex
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ defmodule Telepoison do
end

# see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#name
def default_span_name(request), do: request.method |> Atom.to_string() |> String.upcase()
defp default_span_name(request), do: request.method |> Atom.to_string() |> String.upcase()

@ctx_key {__MODULE__, :parent_ctx}
defp save_parent_ctx do
Expand Down Expand Up @@ -259,7 +259,7 @@ defmodule Telepoison do
end

defp get_ot_attributes(opts) do
default_ot_attributes = get_defaults(:ot_attributes)
default_ot_attributes = get_defaults!(:ot_attributes)

default_ot_attributes
|> Enum.concat(Keyword.get(opts, :ot_attributes, []))
Expand All @@ -276,7 +276,7 @@ defmodule Telepoison do
end

defp get_resource_route([ot_resource_route: :infer], request) do
get_defaults(:infer_fn).(request)
get_defaults!(:infer_fn).(request)
end

defp get_resource_route([ot_resource_route: :ignore], _) do
Expand All @@ -292,34 +292,53 @@ defmodule Telepoison do
nil
end

defp get_defaults(:infer_fn) do
Agent.get(
__MODULE__,
fn
{infer_fn, _} when is_function(infer_fn, 1) ->
infer_fn
defp get_defaults!(key) do
case get_defaults(key) do
{:ok, value} -> value
{:error, error} -> raise ArgumentError, error
end
end

_ ->
raise ArgumentError,
"The configured :infer_route keyword option value must be a function with an arity of 1"
end
)
defp get_defaults(:infer_fn) do
if agent_started?() do
Agent.get(
__MODULE__,
fn
{infer_fn, _} when is_function(infer_fn, 1) ->
{:ok, infer_fn}

_ ->
{:error, "The configured :infer_route keyword option value must be a function with an arity of 1"}
end
)
else
{:error, "Route inference function hasn't been configured"}
end
end

defp get_defaults(:ot_attributes) do
Agent.get(
__MODULE__,
fn
{_, ot_attributes} when is_list(ot_attributes) ->
ot_attributes
if agent_started?() do
attributes =
Agent.get(
__MODULE__,
fn
{_, ot_attributes} when is_list(ot_attributes) ->
ot_attributes

_ ->
[]
end
)
_ ->
[]
end
)

{:ok, attributes}
else
{:ok, []}
end
end

def strip_uri_credentials(uri) do
defp agent_started?, do: Process.whereis(__MODULE__) != nil

defp strip_uri_credentials(uri) do
uri |> URI.parse() |> Map.put(:userinfo, nil) |> Map.put(:authority, nil) |> URI.to_string()
end
end
8 changes: 8 additions & 0 deletions test/telepoison_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,14 @@ defmodule TelepoisonTest do
end
end

test "Telepoison works if setup is not called" do
Telepoison.get!("http://localhost:8000/user/edit/24", [], ot_attributes: [{"some_attribute", "some value"}])

assert_receive {:span, span(attributes: attributes)}, 1000

assert confirm_attributes(attributes, {"some_attribute", "some value"})
end

def flush_mailbox do
receive do
_ -> flush_mailbox()
Expand Down

0 comments on commit fb6d895

Please sign in to comment.