-
Notifications
You must be signed in to change notification settings - Fork 126
Add source code attributes to Elixir tracer macros #550
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
Changes from 8 commits
0f19904
b01b62d
92ab754
c5a1a2b
3033ab6
1cd3445
a7076e5
cde2961
528b84f
7967902
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,17 +16,51 @@ defmodule OpenTelemetry.Tracer do | |
end | ||
""" | ||
|
||
require OpenTelemetry.SemanticConventions.Trace | ||
alias OpenTelemetry.SemanticConventions.Trace, as: Conventions | ||
|
||
defmacrop code_attributes do | ||
quote do | ||
code_function = | ||
case __CALLER__.function do | ||
{func_name, func_arity} -> "#{func_name}/#{func_arity}" | ||
nil -> nil | ||
end | ||
|
||
source_attrs = %{ | ||
Conventions.code_function() => code_function, | ||
Conventions.code_namespace() => __CALLER__.module, | ||
Conventions.code_filepath() => __CALLER__.file, | ||
Conventions.code_lineno() => __CALLER__.line | ||
} | ||
end | ||
end | ||
|
||
@doc """ | ||
Starts a new span and does not make it the current active span of the current process. | ||
|
||
The current active Span is used as the parent of the created Span. | ||
""" | ||
defmacro start_span(name, opts \\ quote(do: %{})) do | ||
quote bind_quoted: [name: name, start_opts: opts] do | ||
code_attrs = code_attributes() |> Macro.escape() | ||
thread_id_key = Conventions.thread_id() | ||
|
||
quote bind_quoted: [ | ||
name: name, | ||
start_opts: opts, | ||
code_attrs: code_attrs, | ||
thread_id_key: thread_id_key | ||
] do | ||
code_attrs = Map.put(code_attrs, thread_id_key, :erlang.system_info(:scheduler_id)) | ||
|
||
start_opts = | ||
Map.new(start_opts) | ||
|> Map.update(:attributes, code_attrs, &Map.merge(&1, code_attrs)) | ||
|
||
:otel_tracer.start_span( | ||
:opentelemetry.get_application_tracer(__MODULE__), | ||
name, | ||
Map.new(start_opts) | ||
start_opts | ||
) | ||
end | ||
end | ||
|
@@ -37,7 +71,22 @@ defmodule OpenTelemetry.Tracer do | |
The current active Span is used as the parent of the created Span. | ||
""" | ||
defmacro start_span(ctx, name, opts) do | ||
quote bind_quoted: [ctx: ctx, name: name, start_opts: opts] do | ||
code_attrs = code_attributes() |> Macro.escape() | ||
thread_id_key = Conventions.thread_id() | ||
|
||
quote bind_quoted: [ | ||
ctx: ctx, | ||
name: name, | ||
start_opts: opts, | ||
code_attrs: code_attrs, | ||
thread_id_key: thread_id_key | ||
] do | ||
code_attrs = Map.put(code_attrs, thread_id_key, :erlang.system_info(:scheduler_id)) | ||
|
||
start_opts = | ||
Map.new(start_opts) | ||
|> Map.update(:attributes, code_attrs, &Map.merge(&1, code_attrs)) | ||
|
||
:otel_tracer.start_span( | ||
ctx, | ||
:opentelemetry.get_application_tracer(__MODULE__), | ||
|
@@ -70,11 +119,21 @@ defmodule OpenTelemetry.Tracer do | |
See `start_span/2` and `end_span/0`. | ||
""" | ||
defmacro with_span(name, start_opts \\ quote(do: %{}), do: block) do | ||
code_attrs = code_attributes() |> Macro.escape() | ||
thread_id_key = Conventions.thread_id() | ||
|
||
quote do | ||
code_attrs = | ||
Map.put(unquote(code_attrs), unquote(thread_id_key), :erlang.system_info(:scheduler_id)) | ||
|
||
start_opts = | ||
Map.new(unquote(start_opts)) | ||
|> Map.update(:attributes, code_attrs, &Map.merge(&1, code_attrs)) | ||
|
||
:otel_tracer.with_span( | ||
:opentelemetry.get_application_tracer(__MODULE__), | ||
unquote(name), | ||
Map.new(unquote(start_opts)), | ||
start_opts, | ||
fn _ -> unquote(block) end | ||
) | ||
end | ||
|
@@ -88,12 +147,22 @@ defmodule OpenTelemetry.Tracer do | |
See `start_span/2` and `end_span/0`. | ||
""" | ||
defmacro with_span(ctx, name, start_opts, do: block) do | ||
code_attrs = code_attributes() |> Macro.escape() | ||
thread_id_key = Conventions.thread_id() | ||
|
||
quote do | ||
code_attrs = | ||
Map.put(unquote(code_attrs), unquote(thread_id_key), :erlang.system_info(:scheduler_id)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like thread_id is still in here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My bad, I haven't had a chance to properly sit down with this and get any context back. I promise I will get to that soon. |
||
|
||
start_opts = | ||
Map.new(unquote(start_opts)) | ||
|> Map.update(:attributes, code_attrs, &Map.merge(&1, code_attrs)) | ||
|
||
:otel_tracer.with_span( | ||
unquote(ctx), | ||
:opentelemetry.get_application_tracer(__MODULE__), | ||
unquote(name), | ||
Map.new(unquote(start_opts)), | ||
start_opts, | ||
fn _ -> unquote(block) end | ||
) | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know you mentioned not thinking it should be the scheduler id but the pid before. I wonder if we shouldn't just leave out the thread attribute for now. I'd like to get the pid in but not sure what attribute key to use for it yet and don't want to have our hands tied by backwards compatibility if we add this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Cantido do you think you'll have time to update this PR to remove this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can certainly do that!