Skip to content

Commit

Permalink
Remove opts validation and Optimal as a dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkowalski committed Jan 18, 2021
1 parent 61fe08b commit abf3632
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 391 deletions.
52 changes: 20 additions & 32 deletions lib/plug/add_context.ex
Original file line number Diff line number Diff line change
@@ -1,51 +1,39 @@
defmodule Spandex.Plug.AddContext do
@moduledoc """
Adds request context to the top span of the trace, setting
the resource, method, url, service, type and env
the resource, method, url, service, type and env.
## Options
This plug accepts the following options:
* `:tracer` - The tracing module to be used to start the trace. Required.
* `:allowed_route_replacements` - A list of route parts that may be replaced with their actual value.
If not set or set to nil, then all will be allowed, unless they are disallowed.
* `:disallowed_route_replacements` - A list of route parts that may *not* be replaced with their actual value.
* `:query_params` - A list of query params who's value will be included in the resource name.
* `:tracer_opts` - Any opts to be passed to the tracer when starting or continuing the trace.
"""
@behaviour Plug

alias Spandex.Plug.Utils

@init_opts Optimal.schema(
opts: [
allowed_route_replacements: [{:list, :atom}, nil],
disallowed_route_replacements: {:list, :atom},
query_params: {:list, :atom},
tracer: :atom,
tracer_opts: :keyword
],
defaults: [
allowed_route_replacements: nil,
disallowed_route_replacements: [],
query_params: [],
tracer_opts: []
],
required: [:tracer],
describe: [
tracer: "The tracing module to be used to start the trace.",
tracer_opts: "Any opts to be passed to the tracer when starting or continuing the trace.",
allowed_route_replacements:
"A list of route parts that may be replaced with their actual value. " <>
"If not set or set to nil, then all will be allowed, unless they are disallowed.",
disallowed_route_replacements:
"A list of route parts that may *not* be replaced with their actual value.",
query_params: "A list of query params who's value will be included in the resource name."
]
)
@default_opts [
allowed_route_replacements: nil,
disallowed_route_replacements: [],
query_params: [],
tracer_opts: []
]

@doc """
Starts a trace, considering the filters/parameters in the provided options.
#{Optimal.Doc.document(@init_opts)}
You would generally not use `allowed_route_replacements` and `disallowed_route_replacements` together.
"""
@spec init(opts :: Keyword.t()) :: Keyword.t()
def init(opts) do
opts = Optimal.validate!(opts, @init_opts)

opts
@default_opts
|> Keyword.merge(opts || [])
|> Keyword.update!(:allowed_route_replacements, fn config ->
if config do
Enum.map(config, &Atom.to_string/1)
Expand Down
28 changes: 10 additions & 18 deletions lib/plug/end_trace.ex
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
defmodule Spandex.Plug.EndTrace do
@moduledoc """
Finishes a trace, setting status and error based on the HTTP status.
## Options
This plug accepts the following options:
* `:tracer` - The tracing module to be used to start the trace. Required.
* `:tracer_opts` - Any opts to be passed to the tracer when starting or continuing the trace.
"""
@behaviour Plug

alias Spandex.Plug.Utils

@init_opts Optimal.schema(
opts: [
tracer: :atom,
tracer_opts: :keyword
],
defaults: [
tracer_opts: []
],
required: [:tracer],
describe: [
tracer: "The tracing module to be used to start the trace.",
tracer_opts: "Any opts to be passed to the tracer when starting or continuing the trace."
]
)
@default_opts [tracer_opts: []]

@doc """
Accepts and validates opts for the plug, and underlying tracer.
#{Optimal.Doc.document(@init_opts)}
Accepts opts for the plug, and underlying tracer.
"""
@spec init(opts :: Keyword.t()) :: Keyword.t()
def init(opts) do
Optimal.validate!(opts, @init_opts)
Keyword.merge(@default_opts, opts)
end

@spec call(conn :: Plug.Conn.t(), _opts :: Keyword.t()) :: Plug.Conn.t()
Expand Down
46 changes: 18 additions & 28 deletions lib/plug/start_trace.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,34 @@ defmodule Spandex.Plug.StartTrace do
@moduledoc """
Starts a trace, skipping ignored routes or methods.
Store info in Conn assigns if we actually trace the request.
## Options
This plug accepts the following options:
* `:tracer` - The tracing module to be used to start the trace. Required.
* `:ignored_methods` - A list of strings representing methods to ignore. A good example would be `["OPTIONS"]`.
* `:ignored_routes` - A list of strings or regexes. If it is a string, it must match exactly.
* `:tracer_opts` - Any opts to be passed to the tracer when starting or continuing the trace.
* `:span_name` - The name to be used for the top level span.
"""
@behaviour Plug

alias Spandex.Plug.Utils
alias Spandex.SpanContext

@init_opts Optimal.schema(
opts: [
ignored_methods: {:list, :string},
ignored_routes: {:list, [:regex, :string]},
tracer: :atom,
tracer_opts: :keyword,
span_name: :string
],
defaults: [
ignored_methods: [],
ignored_routes: [],
tracer_opts: [],
span_name: "request"
],
required: [:tracer],
describe: [
ignored_methods:
"A list of strings representing methods to ignore. A good example would be `[\"OPTIONS\"]`",
ignored_routes: "A list of strings or regexes. If it is a string, it must match exactly.",
tracer: "The tracing module to be used to start the trace.",
tracer_opts: "Any opts to be passed to the tracer when starting or continuing the trace.",
span_name: "The name to be used for the top level span."
]
)
@default_opts [
ignored_methods: [],
ignored_routes: [],
tracer_opts: [],
span_name: "request"
]

@doc """
Accepts and validates opts for the plug, and underlying tracer.
#{Optimal.Doc.document(@init_opts)}
Accepts opts for the plug, and underlying tracer.
"""
@spec init(opts :: Keyword.t()) :: Keyword.t()
def init(opts), do: Optimal.validate!(opts, @init_opts)
def init(opts), do: Keyword.merge(@default_opts, opts)

@spec call(conn :: Plug.Conn.t(), opts :: Keyword.t()) :: Plug.Conn.t()
def call(conn, opts) do
Expand Down
Loading

0 comments on commit abf3632

Please sign in to comment.