OpentelemetryFunction (opentelemetry_function v0.1.0)

Documentation for OpentelemetryFunction.

This package provides functions to help propagating OpenTelemetry context across functions that are executed asynchronously.

Link to this section Summary

Functions

Accepts a function and wraps it in a function which propagates OpenTelemetry context.

Link to this section Functions

Link to this function

wrap(fun_or_mfa, span_name \\ "Function.wrap")

Specs

wrap((... -> any()), binary()) :: (... -> any())
wrap({module(), atom(), [term()]}, binary()) :: (... -> any())

Accepts a function and wraps it in a function which propagates OpenTelemetry context.

This function supports functions with arity up to 9.

Example

# Before
task = Task.async(func)
Task.await(task, timeout)

# With explicit context propagation
ctx = OpenTelemetry.Ctx.get_current()
task = Task.async(fn ->
  OpenTelemetry.Ctx.attach(ctx)
  func.()
end)
Task.await(task, timeout)

# With this helper function
task = Task.async(OpentelemetryFunction.wrap(func))
Task.await(task, timeout)

It is also possible to use this with MFA:

# Before
:jobs.enqueue(:tasks_queue, {mod, fun, args})

# After
wrapped_fun = OpenTelemetry.Function.wrap({mod, fun, args})
:jobs.enqueue(:tasks_queue, wrapped_fun)