Skip to content

Instantly share code, notes, and snippets.

@garthk

garthk/tracer.ex Secret

Last active March 27, 2021 05:57
Show Gist options
  • Save garthk/fddffdb2880b665c296cab435f216267 to your computer and use it in GitHub Desktop.
Save garthk/fddffdb2880b665c296cab435f216267 to your computer and use it in GitHub Desktop.
Experiment adding OpenTelemetry Python style exception recording to OpenTelemetry Elixir
defmodule OpenTelemetry.Tracer do
# ...
quote do
span_ctx = unquote(span_ctx)
{exception_opts, start_opts} =
unquote(start_opts)
|> Map.new()
|> Map.split([:record_exception, :set_status_on_exception])
start_opts =
Map.merge(start_opts, %{record_exception: false, set_status_on_exception: false})
try do
:otel_tracer.with_span(
span_ctx,
:opentelemetry.get_tracer(__MODULE__),
unquote(name),
start_opts,
fn _ -> unquote(block) end
)
rescue
exception ->
if not match?(%{record_exception: false}, exception_opts) do
OpenTelemetry.Span.record_exception(span_ctx, exception, __STACKTRACE__)
end
if not match?(%{set_status_on_exception: false}, exception_opts) do
status = OpenTelemetry.status(:error, Exception.message(exception))
OpenTelemetry.Span.set_status(span_ctx, status)
end
reraise exception, __STACKTRACE__
end
end
# ...
end
@garthk
Copy link
Author

garthk commented Mar 27, 2021

Experiment adding OT Python style opt-out record_exception and set_status_on_exception start options to OT Elixir's with_span/4. Most of the mess is in the capabilities being conditional, and turning both off for the inner otel_tracer:with_span/5 so exceptions don't get recorded twice.

@garthk
Copy link
Author

garthk commented Mar 27, 2021

Removed delegation to Tracer. with_span_exception/4. Contains if, which'll have some maintainers breaking out in a rash, but it strikes me as closer to the house code style.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment