Skip to content

Commit

Permalink
Add set_current_span function
Browse files Browse the repository at this point in the history
We have the option to get the current span but not to set it, we can
also use the current_span function a bit more throughout Tracing.
  • Loading branch information
MvanDiemen committed Jul 12, 2024
1 parent 8f85e6e commit f21bbd1
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions lib/tracing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ defmodule Tracing do
defmacro __using__(_) do
quote do
require Tracing
require OpenTelemetry.Tracer
use Tracing.Decorator
end
end
Expand All @@ -38,7 +39,7 @@ defmodule Tracing do
"""
@spec trace_id() :: String.t()
def trace_id do
case OpenTelemetry.Tracer.current_span_ctx() do
case current_span() do
:undefined ->
"span_not_found"

Expand All @@ -57,6 +58,13 @@ defmodule Tracing do
@spec current_span() :: any()
def current_span, do: OpenTelemetry.Tracer.current_span_ctx()

@doc """
Sets the current span to current_span() or what is provided
"""
def set_current_span(span \\ current_span()) do
OpenTelemetry.Tracer.set_current_span(span)
end

@doc """
Simple wrapper around `OpenTelemetry.Span.set_attributes/2`
"""
Expand All @@ -71,7 +79,8 @@ defmodule Tracing do
Simple wrapper around `OpenTelemetry.Span.add_event/2`
"""
def add_event(name, attrs) do
OpenTelemetry.Tracer.add_event(
OpenTelemetry.Span.add_event(
current_span(),
name,
attrs
)
Expand Down Expand Up @@ -99,18 +108,12 @@ defmodule Tracing do
@doc """
Helper function for propogating opentelemetry
"""
def inject(keywords \\ []) do
keywords
|> :otel_propagator_text_map.inject()
end
def inject(keywords \\ []), do: :otel_propagator_text_map.inject(keywords)

@doc """
Helper function for propogating opentelemetry
"""
def extract(keywords) do
keywords
|> :otel_propagator_text_map.extract()
end
def extract(keywords), do: :otel_propagator_text_map.extract(keywords)

@doc """
Helper function to start Tracing.Monitor
Expand Down

0 comments on commit f21bbd1

Please sign in to comment.