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

Small tweaks to support tracing in tests #52

Open
wants to merge 2 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
13 changes: 9 additions & 4 deletions lib/spandex_phoenix/plug/finish_trace.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ defmodule SpandexPhoenix.Plug.FinishTrace do

@init_opts Optimal.schema(
opts: [
tracer: :atom
tracer: :atom,
finish_trace?: :boolean
],
defaults: [
tracer: Application.get_env(:spandex_phoenix, :tracer)
tracer: Application.get_env(:spandex_phoenix, :tracer),
finish_trace?: Application.get_env(:spandex_phoenix, :finish_trace?, true)
],
describe: [
tracer: "The tracing module to be used to start the trace."
tracer: "The tracing module to be used to start the trace.",
finish_trace?:
"If we should finish traces, set to false if you're tracing your tests since your test middleware should finish the test trace."
]
)

Expand All @@ -24,8 +28,9 @@ defmodule SpandexPhoenix.Plug.FinishTrace do
@impl Plug
def call(conn, opts) do
tracer = opts[:tracer]
finish_trace? = opts[:finish_trace?]

if tracer.current_trace_id() do
if tracer.current_trace_id() && finish_trace? do
tracer.finish_trace()
end

Expand Down
9 changes: 8 additions & 1 deletion lib/spandex_phoenix/plug/start_trace.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ defmodule SpandexPhoenix.Plug.StartTrace do
tracer.continue_trace(opts[:span_name], span_context)

{:error, _} ->
tracer.start_trace(opts[:span_name])
# check to ensure we're not already in a trace
# before we start another trace
if {:error, :no_trace_context} == tracer.current_context() do
tracer.start_trace(opts[:span_name])
else
# start a span if we're already in a trace
tracer.start_span(opts[:span_name])
end
end

conn
Expand Down