Skip to content

Commit

Permalink
improvement: set more logger metadata, and reset it after span closes
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Dec 5, 2022
1 parent a40c10a commit 81b80ea
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions lib/spandex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ defmodule Spandex do
sender = opts[:sender] || adapter.default_sender()
# TODO: We need to define a behaviour for the Sender API.
sender.send_trace(%Trace{trace | spans: spans ++ unfinished_spans, stack: []})
Logger.metadata(trace_id: nil, span_id: nil, service: nil, service_version: nil)
strategy.delete_trace(opts[:trace_key])

{:error, _} = error ->
Expand Down Expand Up @@ -222,6 +223,16 @@ defmodule Spandex do
spans: [finished_span | spans]
})

current = current_span(opts)

if current do
Logger.metadata(
span_id: to_string(current.id),
service: to_string(current.service),
service_version: to_string(current.service_version)
)
end

{:ok, finished_span}

{:error, _} = error ->
Expand Down Expand Up @@ -453,7 +464,12 @@ defmodule Spandex do
adapter = opts[:adapter]

with {:ok, top_span} <- span(name, opts, span_context, adapter) do
Logger.metadata(trace_id: to_string(span_context.trace_id), span_id: to_string(top_span.id))
Logger.metadata(
trace_id: to_string(span_context.trace_id),
span_id: to_string(top_span.id),
service: to_string(top_span.service),
service_version: to_string(top_span.service_version)
)

trace = %Trace{
id: span_context.trace_id,
Expand Down Expand Up @@ -483,7 +499,13 @@ defmodule Spandex do

with {:ok, span} <- Span.child_of(current_span, name, adapter.span_id(), adapter.now(), opts),
{:ok, _trace} <- strategy.put_trace(opts[:trace_key], %{trace | stack: [span | trace.stack]}) do
Logger.metadata(span_id: to_string(span.id), trace_id: to_string(trace.id))
Logger.metadata(
span_id: to_string(span.id),
trace_id: to_string(trace.id),
service: to_string(span.service),
service_version: to_string(span.service_version)
)

{:ok, span}
end
end
Expand All @@ -495,7 +517,13 @@ defmodule Spandex do

with {:ok, span} <- span(name, opts, span_context, adapter),
{:ok, _trace} <- strategy.put_trace(opts[:trace_key], %{trace | stack: [span]}) do
Logger.metadata(span_id: to_string(span.id), trace_id: to_string(trace_id))
Logger.metadata(
span_id: to_string(span.id),
trace_id: to_string(trace_id),
service: to_string(span.service),
service_version: to_string(span.service_version)
)

{:ok, span}
end
end
Expand All @@ -507,7 +535,13 @@ defmodule Spandex do
span_context = %SpanContext{trace_id: trace_id}

with {:ok, span} <- span(name, opts, span_context, adapter) do
Logger.metadata(trace_id: to_string(trace_id), span_id: to_string(span.id))
Logger.metadata(
trace_id: to_string(trace_id),
span_id: to_string(span.id),
service: to_string(span.service),
service_version: to_string(span.service_version)
)

trace = %Trace{spans: [], stack: [span], id: trace_id}
strategy.put_trace(opts[:trace_key], trace)
end
Expand Down

0 comments on commit 81b80ea

Please sign in to comment.