O11y.SpanAttributes protocol (O11y v0.2.10)

Defines a protocol for returning opentelemetry span attributes from an object. The easiest way to use this is to use the @derive attribute on a struct as shown below, but you can also implement the protocol manually if you need to define custom behavior.

With the basic derive, all fields in the struct will be returned as attributes:

defmodule Basic do
  @derive O11y.SpanAttributes
  defstruct [:id, :name]
end

You can also use the only and except options to include or exclude specific fields:

defmodule Only do
  @derive {O11y.SpanAttributes, only: [:id, :name]}
  defstruct [:id, :name, :email, :password]
end
defmodule Except do
  @derive {O11y.SpanAttributes, except: [:email, :password]}
  defstruct [:id, :name, :email, :password]
end

Or you can manually implement the protocol for a struct:

defmodule Fancy do
  defstruct [:url, :token]
end

defimpl O11y.SpanAttributes, for: Fancy do
  def get(%{url: url, token: token}) do
    masked_token = token |> String.slice(-4, 4) |> String.pad_leading(String.length(token), "*")
    [{"url", url}, {"token", masked_token}]
  end
end

Summary

Types

t()

All the types that implement this protocol.

Functions

Returns the opentelemetry span attributes for the given object as a list of tuples.

Types

otlp_value()

@type otlp_value() :: String.t() | integer() | float() | boolean()

t()

@type t() :: term()

All the types that implement this protocol.

Functions

get(thing)

Returns the opentelemetry span attributes for the given object as a list of tuples.