Wraps some :opencensus
capabilities for Elixir users so
they don't have to learn them some Erlang in order to get
OpenCensus distributed tracing.
Add opencensus_elixir
to your deps
in mix.exs
:
def deps do
[
{:opencensus, "~> 0.9"},
{:opencensus_elixir, "~> 0.3.0"}
]
end
Wrap your code with the
Opencensus.Trace.with_child_span/3
macro to
execute it in a fresh span:
import Opencensus.Trace
def traced_fn(arg) do
with_child_span "traced" do
:YOUR_CODE_HERE
end
end
If you prefer driving Erlang packages directly (see also :telemetry
), copy
what you need from lib/opencensus/trace.ex
and call
Logger.set_logger_metadata/0
if there's any chance of Logger use within
the span.
def traced_fn() do
try do
:ocp.with_child_span("name", %{fn: :traced_fn, mod: __MODULE__})
Logger.set_logger_metadata()
:YOUR_CODE_HERE
after
:ocp.finish_span()
Logger.set_logger_metadata()
end
end
If try .. after .. end
feels too bulky and you're sure you won't need
Logger, try :ocp.with_child_span/3
:
def traced_fn() do
:ocp.with_child_span("traced", %{fn: :traced_fn, mod: __MODULE__}, fn () ->
:YOUR_CODE_HERE
end)
end
To see your spans, use the :oc_reporter_stdout
reporter, either in config:
config :opencensus, reporters: [{:oc_reporter_stdout, []}]
... or at the iex
prompt:
iex> :oc_reporter.register(:oc_reporter_stdout)