OpenTelemetryDecorator v0.1.0 OpenTelemetryDecorator
A function decorator for OpenTelemetry traces.
Usage
defmodule MyApp.Worker do
use OpenTelemetryDecorator
@decorate trace([:my_app, :worker, :do_work])
def do_work(arg1, arg2) do
...doing work
do_more_work(arg1)
end
@decorate trace([:my_app, :worker, :do_more_work])
def do_more_work(arg1) do
...doing more work
end
end
We use OpenTelemetry.Tracer.current_span_ctx()
to automatically link new spans to the current trace (if it exists and is in the same process). So the above example will link the do_work
and do_more_work
spans for you by default.
You can provide span attributes by specifying a list of variable names as atoms.
This list can include...
Any variables (in the top level closure) available when the function exits:
defmodule MyApp.Worker do
use OpenTelemetryDecorator
@decorate trace([:my_app, :worker, :do_work], [:arg1, :arg2, :thing1])
def do_work(arg1, arg2) do
thing1 = arg1.count + arg2.count
{:ok, thing1}
end
end
The result of the function by including the atom :result
:
defmodule MyApp.Worker do
use OpenTelemetryDecorator
@decorate trace([:my_app, :worker, :do_work], [:result])
def do_work(arg1, arg2) do
thing1 = arg1.count + arg2.count
{:ok, thing1}
end
end
Map/struct properties using nested lists of atoms:
defmodule MyApp.Worker do
use OpenTelemetryDecorator
@decorate trace([:my_app, :worker, :do_work], [[:arg1, :count], [:arg2, :count]])
def do_work(arg1, arg2) do
thing1 = arg1.count + arg2.count
{:ok, thing1}
end
end
Link to this section Summary
Functions
This method has to be public because it's used within the macro, but it shouldn't be called directly.
Decorate a function to add an OpenTelemetry trace with a named span.
This method has to be public because it's used within the macro, but it shouldn't be called directly.
Link to this section Functions
get_reportable_attrs(bound_variables, reportable_attr_keys, result \\ nil)
This method has to be public because it's used within the macro, but it shouldn't be called directly.
trace(event_name, attr_keys \\ [], body, context)
Decorate a function to add an OpenTelemetry trace with a named span.
You can provide span attributes by specifying a list of variable names as atoms. This list can include:
- any variables (in the top level closure) available when the function exits,
-
the result of the function by including the atom
:result
, - map/struct properties using nested lists of atoms.
defmodule MyApp.Worker do
use OpenTelemetryDecorator
@decorate trace([:my_app, :worker, :do_work], [:arg1, [:arg2, :count], :thing1, :result])
def do_work(arg1, arg2) do
thing1 = arg1.count + arg2.count
{:ok, thing1}
end
end
validate_args(event_name, attr_keys)
This method has to be public because it's used within the macro, but it shouldn't be called directly.