Skip to content

Commit

Permalink
Remove usage of the transitive dependency Optimal (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkowalski authored Nov 18, 2021
1 parent 64dfaf3 commit 33d3fcf
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 105 deletions.
12 changes: 0 additions & 12 deletions lib/spandex_phoenix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ defmodule SpandexPhoenix do
StartTrace
}

@use_opts_schema StartTrace.__schema__()
|> Optimal.merge(AddContext.__schema__())
|> Optimal.merge(FinishTrace.__schema__())

defmacro __using__(opts) do
tracer = Keyword.get(opts, :tracer, Application.get_env(:spandex_phoenix, :tracer))
if is_nil(tracer), do: raise("You must configure a :tracer for :spandex_phoenix")
Expand All @@ -135,7 +131,6 @@ defmodule SpandexPhoenix do
finish_opts: finish_opts
] do
@before_compile SpandexPhoenix
@after_compile SpandexPhoenix
@use_opts use_opts
@tracer tracer
@start_opts StartTrace.init(start_opts)
Expand Down Expand Up @@ -169,13 +164,6 @@ defmodule SpandexPhoenix do
end
end

# This needs to happen in __after_compile__ so that it's possible to
# reference a callback function referenced in the module itself without
# getting a validation error.
defmacro __after_compile__(%{module: module}, _bytecode) do
Optimal.validate!(Module.get_attribute(module, :use_opts), @use_opts_schema)
end

@doc """
"""
@spec default_metadata(Plug.Conn.t()) :: Keyword.t()
Expand Down
31 changes: 5 additions & 26 deletions lib/spandex_phoenix/plug/add_context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,13 @@ defmodule SpandexPhoenix.Plug.AddContext do

@behaviour Plug

@init_opts Optimal.schema(
opts: [
customize_metadata: {:function, 1},
tracer: :atom
],
defaults: [
customize_metadata: &SpandexPhoenix.default_metadata/1,
tracer: Application.get_env(:spandex_phoenix, :tracer)
],
describe: [
customize_metadata: """
A function that takes the Plug.Conn for the current request
and returns the desired span options to apply to the top-level
span in the trace. The Plug.Conn is normally evaluated just
before the response is sent to the client, to ensure that the
most-accurate metadata can be collected. In cases where there
is an unhandled error, it may only represent the initial
request without any response information.
""",
tracer: "The tracing module to be used to start the trace."
]
)

@doc false
def __schema__, do: @init_opts
@default_opts [
customize_metadata: &SpandexPhoenix.default_metadata/1,
tracer: Application.get_env(:spandex_phoenix, :tracer)
]

@impl Plug
def init(opts), do: Optimal.validate!(opts, @init_opts)
def init(opts), do: Keyword.merge(@default_opts, opts)

@impl Plug
def call(conn, opts) do
Expand Down
19 changes: 4 additions & 15 deletions lib/spandex_phoenix/plug/finish_trace.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,12 @@ defmodule SpandexPhoenix.Plug.FinishTrace do

@behaviour Plug

@init_opts Optimal.schema(
opts: [
tracer: :atom
],
defaults: [
tracer: Application.get_env(:spandex_phoenix, :tracer)
],
describe: [
tracer: "The tracing module to be used to start the trace."
]
)

@doc false
def __schema__, do: @init_opts
@default_opts [
tracer: Application.get_env(:spandex_phoenix, :tracer)
]

@impl Plug
def init(opts), do: Optimal.validate!(opts, @init_opts)
def init(opts), do: Keyword.merge(@default_opts, opts)

@impl Plug
def call(conn, opts) do
Expand Down
27 changes: 6 additions & 21 deletions lib/spandex_phoenix/plug/start_trace.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,14 @@ defmodule SpandexPhoenix.Plug.StartTrace do

alias Spandex.SpanContext

@init_opts Optimal.schema(
opts: [
filter_traces: {:function, 1},
span_name: :string,
tracer: :atom
],
defaults: [
filter_traces: &SpandexPhoenix.trace_all_requests/1,
span_name: "request",
tracer: Application.get_env(:spandex_phoenix, :tracer)
],
describe: [
filter_traces: "A function that takes a Plug.Conn and returns true for requests to be traced.",
span_name: "The name to be used for the top level span.",
tracer: "The tracing module to be used to start the trace."
]
)

@doc false
def __schema__, do: @init_opts
@default_opts [
filter_traces: &SpandexPhoenix.trace_all_requests/1,
span_name: "request",
tracer: Application.get_env(:spandex_phoenix, :tracer)
]

@impl Plug
def init(opts), do: Optimal.validate!(opts, @init_opts)
def init(opts), do: Keyword.merge(@default_opts, opts)

@impl Plug
def call(conn, opts) do
Expand Down
6 changes: 0 additions & 6 deletions test/plug/add_context_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,5 @@ defmodule AddContextPlugTest do
TestTracer.finish_span()
assert %Spandex.Span{name: "request", resource: "GET /"} = TestTracer.current_span()
end

test "raises an exception when unexpected options are set" do
assert_raise ArgumentError, "Opt Validation Error: tr4c3r - is not allowed (no extra keys)", fn ->
call(AddContext, :get, "/", tr4c3r: AnotherTracer)
end
end
end
end
6 changes: 0 additions & 6 deletions test/plug/finish_trace_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,5 @@ defmodule FinishTracePlugTest do
}
}
end

test "raises an exception when unexpected options are set" do
assert_raise ArgumentError, "Opt Validation Error: tr4c3r - is not allowed (no extra keys)", fn ->
call(FinishTrace, :get, "/", tr4c3r: AnotherTracer)
end
end
end
end
6 changes: 0 additions & 6 deletions test/plug/start_trace_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,5 @@ defmodule StartTracePlugTest do
call(StartTrace, :get, "/", span_name: "my root span name")
assert %Spandex.Span{name: "my root span name"} = TestTracer.current_span()
end

test "raises an exception when unexpected options are set" do
assert_raise ArgumentError, "Opt Validation Error: spam_name - is not allowed (no extra keys)", fn ->
call(StartTrace, :get, "/", spam_name: "Eggs.Sausage.Spam")
end
end
end
end
13 changes: 0 additions & 13 deletions test/tracer_integration/phoenix/endpoint_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -315,18 +315,5 @@ defmodule TracerWithPhoenixEndpointTest do

assert Keyword.get(http, :url) == "/hello/+🤯"
end

test "validates the options passed to the use macro" do
Application.put_env(:spandex_phoenix, __MODULE__.EndpointWithInvalidFilterTraces, [])

assert_raise ArgumentError, "Opt Validation Error: filter_traces - must be of type {:function, 1}", fn ->
defmodule EndpointWithInvalidFilterTraces do
use Phoenix.Endpoint, otp_app: :spandex_phoenix
use SpandexPhoenix, filter_traces: &__MODULE__.customize_metadata/2

def customize_metadata(conn, _extra_arg), do: []
end
end
end
end
end

0 comments on commit 33d3fcf

Please sign in to comment.