Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Optimal #124

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
- checkout

# specify any bash command here prefixed with `run: `
- run: mix format --check-formatted
- run: mix local.hex --force
- run: mix local.rebar --force
- run: mix deps.get
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
[![Inline docs](http://inch-ci.org/github/spandex-project/spandex.svg)](http://inch-ci.org/github/spandex-project/spandex)
[![Coverage Status](https://coveralls.io/repos/github/spandex-project/spandex/badge.svg)](https://coveralls.io/github/spandex-project/spandex)
[![Hex pm](http://img.shields.io/hexpm/v/spandex.svg?style=flat)](https://hex.pm/packages/spandex)
[![SourceLevel](https://app.sourcelevel.io/github/spandex-project/spandex.svg)](https://app.sourcelevel.io/github/spandex-project/spandex)
[![Total Download](https://img.shields.io/hexpm/dt/spandex.svg)](https://hex.pm/packages/spandex)
[![License](https://img.shields.io/hexpm/l/spandex.svg)](https://github.com/spandex-project/spandex/blob/master/LICENSE)

View the [documentation](https://hexdocs.pm/spandex)

Expand All @@ -15,8 +16,8 @@ runtime of your system. Using distributed tracing, you can also get a view of
how requests make their way through your entire ecosystem of microservices or
applications. Currently, Spandex only supports integrating with
[datadog](https://www.datadoghq.com/), but it is built to be agnostic to what
platform you choose to view your trace data. Eventually it should support open
zipkin, Stackdriver, and any other trace viewer/aggregation tool you'd like to
platform you choose to view your trace data. Eventually it should support Open
Zipkin, Stackdriver, and any other trace viewer/aggregation tool you'd like to
integrate with. We are still under active development, working on moving to a
more standard/generic implementation of the internals. Contributions welcome!

Expand All @@ -31,7 +32,7 @@ This is Datadog-specific since that's currently the only adapter.
* Include the adapter as a dependency (see below).
* Replace any occurrences of `Spandex.Adapters.Datadog` with
`SpandexDatadog.Adapter` in your code.
* Replace any occurences of `Spandex.Adapters.ApiSender` with
* Replace any occurrences of `Spandex.Adapters.ApiSender` with
`SpandexDatadog.ApiSender` in your code.

## Adapters
Expand Down Expand Up @@ -100,9 +101,9 @@ For adapter configuration, see the documentation for that adapter

There are 3 plugs provided for usage w/ Phoenix:

* `Spandex.Plug.StartTrace` - See moduledocs for options. Goes as early in your
* `Spandex.Plug.StartTrace` - See module docs for options. Goes as early in your
pipeline as possible.
* `Spandex.Plug.AddContext` - See moduledocs for options. Either after the
* `Spandex.Plug.AddContext` - See moduledoc for options. Either after the
router, or inside a pipeline in the router.
* `Spandex.Plug.EndTrace` - Must go *after* your router.

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