Skip to content

Commit

Permalink
Permit keyword list start_opts in Tracer macros.
Browse files Browse the repository at this point in the history
Ergonomic for Elixir developers.

Also:

* Repair test name to reflect test code

* Provide tested examples of `with_span/3` with `start_opts` expressed
  as both a map and keyword list
  • Loading branch information
garthk committed Mar 22, 2021
1 parent 9f0d961 commit ae9062c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
13 changes: 9 additions & 4 deletions apps/opentelemetry_api/lib/open_telemetry/tracer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule OpenTelemetry.Tracer do
"""
defmacro start_span(name, opts \\ quote(do: %{})) do
quote bind_quoted: [name: name, start_opts: opts] do
:otel_tracer.start_span(:opentelemetry.get_tracer(__MODULE__), name, start_opts)
:otel_tracer.start_span(:opentelemetry.get_tracer(__MODULE__), name, Map.new(start_opts))
end
end

Expand All @@ -45,7 +45,12 @@ defmodule OpenTelemetry.Tracer do
"""
defmacro start_span(ctx, name, opts) do
quote bind_quoted: [ctx: ctx, name: name, start_opts: opts] do
:otel_tracer.start_span(ctx, :opentelemetry.get_tracer(__MODULE__), name, start_opts)
:otel_tracer.start_span(
ctx,
:opentelemetry.get_tracer(__MODULE__),
name,
Map.new(start_opts)
)
end
end

Expand Down Expand Up @@ -76,7 +81,7 @@ defmodule OpenTelemetry.Tracer do
:otel_tracer.with_span(
:opentelemetry.get_tracer(__MODULE__),
unquote(name),
unquote(start_opts),
Map.new(unquote(start_opts)),
fn _ -> unquote(block) end
)
end
Expand All @@ -95,7 +100,7 @@ defmodule OpenTelemetry.Tracer do
unquote(ctx),
:opentelemetry.get_tracer(__MODULE__),
unquote(name),
unquote(start_opts),
Map.new(unquote(start_opts)),
fn _ -> unquote(block) end
)
end
Expand Down
26 changes: 25 additions & 1 deletion apps/opentelemetry_api/test/open_telemetry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ defmodule OpenTelemetryTest do
assert [{"attr-1", "value-1"}] == a
end

test "macro start_span" do
test "macro with_span" do
Tracer.with_span "span-1" do
Tracer.with_span "span-2" do
Tracer.set_attribute("attr-1", "value-1")
Expand All @@ -55,6 +55,30 @@ defmodule OpenTelemetryTest do
end
end

test "macro with_span start_opts (map)" do
Tracer.with_span "span-1", %{
attributes: %{"thread.id" => inspect(self())},
is_recording: true,
kind: :internal,
links: [],
start_time: :opentelemetry.timestamp()
# sampler
} do
:ok
end
end

test "macro with_span start_opts (keyword list)" do
Tracer.with_span "span-1",
attributes: %{"thread.id" => inspect(self())},
is_recording: true,
kind: :internal,
links: [],
start_time: :opentelemetry.timestamp() do
:ok
end
end

test "can deconstruct a span context" do
Tracer.with_span "span-1" do
span = Tracer.current_span_ctx()
Expand Down

0 comments on commit ae9062c

Please sign in to comment.