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

Permit keyword list start_opts in Tracer macros. #228

Merged
merged 2 commits into from
Mar 24, 2021
Merged
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 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