Skip to content

Commit

Permalink
Stop overriding completion_time when finishing span (#60)
Browse files Browse the repository at this point in the history
* Stop overriding completion_time when finishing span
  • Loading branch information
Christophe Marchal authored and zachdaniel committed Aug 21, 2018
1 parent b8779e1 commit ae15db6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/spandex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ defmodule Spandex do
{:error, :no_trace_context}

%Trace{spans: spans, stack: stack} ->
unfinished_spans = Enum.map(stack, &update_or_keep(&1, completion_time: adapter.now()))
unfinished_spans = Enum.map(stack, &ensure_completion_time_set(&1, adapter))

sender = opts[:sender] || adapter.default_sender()

Expand All @@ -157,13 +157,19 @@ defmodule Spandex do
{:error, :no_span_context}

%Trace{stack: [span | tail], spans: spans} = trace ->
finished_span = update_or_keep(span, completion_time: adapter.now())
finished_span = ensure_completion_time_set(span, adapter)

strategy.put_trace(opts[:tracer], %{trace | stack: tail, spans: [finished_span | spans]})
{:ok, finished_span}
end
end

defp ensure_completion_time_set(%Span{completion_time: nil} = span, adapter) do
update_or_keep(span, completion_time: adapter.now())
end

defp ensure_completion_time_set(%Span{} = span, _adapter), do: span

def span_error(_error, _stacktrace, :disabled), do: {:error, :disabled}

def span_error(exception, stacktrace, opts) do
Expand Down
20 changes: 20 additions & 0 deletions test/span_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,24 @@ defmodule Spandex.Test.SpanTest do

assert(span.service == :special_service)
end

test "finishing a span does not override the completion time" do
completion_time = :os.system_time(:nano_seconds)
Tracer.start_trace("my_trace")
Tracer.update_span(service: :my_app, type: :web, completion_time: completion_time)
Tracer.finish_span()
Tracer.finish_trace()

span = Util.find_span("my_trace")
assert(span.completion_time == completion_time)
end

test "unfinished spans should have a completion time after trace finishes" do
Tracer.start_trace("my_trace")
Tracer.update_span(service: :my_app, type: :web)
Tracer.finish_trace()
span = Util.find_span("my_trace")

assert(span.completion_time != nil)
end
end

0 comments on commit ae15db6

Please sign in to comment.