Snakepit.Telemetry.SafeMetadata (Snakepit v0.6.10)

View Source

Safe metadata handling for telemetry events.

This module ensures that metadata from Python workers doesn't create new atoms at runtime, which could exhaust the BEAM atom table.

Only keys from the allowlist are converted to atoms; everything else remains as strings.

Summary

Functions

Returns the list of allowed atom keys.

Enriches metadata from Python with Elixir context.

Validates and converts measurements map.

Merges two metadata maps safely.

Sanitizes a metadata map, converting only allowed keys to atoms.

Functions

allowed_atom_keys()

Returns the list of allowed atom keys.

enrich(python_metadata, elixir_context)

Enriches metadata from Python with Elixir context.

Only allowed keys are converted to atoms; unknown keys remain as strings.

Examples

iex> Snakepit.Telemetry.SafeMetadata.enrich(
...>   %{"tool" => "predict"},
...>   [node: :nonode@nohost, worker_id: "worker_1"]
...> )
{:ok, %{tool: "predict", node: :nonode@nohost, worker_id: "worker_1"}}

measurements(measurements)

Validates and converts measurements map.

All measurement keys must be from the allowlist (enforced by Naming module).

Examples

iex> Snakepit.Telemetry.SafeMetadata.measurements(%{"duration" => 1000})
{:ok, %{duration: 1000}}

merge(metadata1, metadata2)

Merges two metadata maps safely.

Examples

iex> Snakepit.Telemetry.SafeMetadata.merge(%{"tool" => "predict"}, %{node: :nonode@nohost})
{:ok, %{"tool" => "predict", node: :nonode@nohost}}

sanitize(metadata)

Sanitizes a metadata map, converting only allowed keys to atoms.

Examples

iex> Snakepit.Telemetry.SafeMetadata.sanitize(%{"node" => "test@host", "unknown" => "value"})
{:ok, %{node: "test@host", "unknown" => "value"}}