-module(automata@event@metadata).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/automata/event/metadata.gleam").
-export([empty/0, with_correlation_id/2, with_causation_id/2, with_trace_id/2, with_attribute/3, attribute/2, has_attribute/2, correlation_id/1, causation_id/1, trace_id/1, attributes/1]).
-export_type([metadata/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type metadata() :: {metadata,
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@dict:dict(binary(), binary())}.
-file("src/automata/event/metadata.gleam", 25).
?DOC(" Empty metadata with no ids and no attributes.\n").
-spec empty() -> metadata().
empty() ->
{metadata, none, none, none, maps:new()}.
-file("src/automata/event/metadata.gleam", 36).
?DOC(
" Tag this metadata with the correlation id that groups events\n"
" belonging to the same business transaction.\n"
).
-spec with_correlation_id(metadata(), binary()) -> metadata().
with_correlation_id(Metadata, Id) ->
{metadata,
{some, Id},
erlang:element(3, Metadata),
erlang:element(4, Metadata),
erlang:element(5, Metadata)}.
-file("src/automata/event/metadata.gleam", 44).
?DOC(" Set the causation id pointing at the immediate parent event.\n").
-spec with_causation_id(metadata(), binary()) -> metadata().
with_causation_id(Metadata, Id) ->
{metadata,
erlang:element(2, Metadata),
{some, Id},
erlang:element(4, Metadata),
erlang:element(5, Metadata)}.
-file("src/automata/event/metadata.gleam", 49).
?DOC(" Set the W3C/OpenTelemetry-style trace id.\n").
-spec with_trace_id(metadata(), binary()) -> metadata().
with_trace_id(Metadata, Id) ->
{metadata,
erlang:element(2, Metadata),
erlang:element(3, Metadata),
{some, Id},
erlang:element(5, Metadata)}.
-file("src/automata/event/metadata.gleam", 54).
?DOC(" Insert (or overwrite) a free-form attribute pair.\n").
-spec with_attribute(metadata(), binary(), binary()) -> metadata().
with_attribute(Metadata, Key, Value) ->
{metadata,
erlang:element(2, Metadata),
erlang:element(3, Metadata),
erlang:element(4, Metadata),
gleam@dict:insert(erlang:element(5, Metadata), Key, Value)}.
-file("src/automata/event/metadata.gleam", 63).
?DOC(" Look up an attribute by key. Returns `None` when no value is set.\n").
-spec attribute(metadata(), binary()) -> gleam@option:option(binary()).
attribute(Metadata, Key) ->
case gleam_stdlib:map_get(erlang:element(5, Metadata), Key) of
{ok, Value} ->
{some, Value};
{error, _} ->
none
end.
-file("src/automata/event/metadata.gleam", 72).
?DOC(
" `True` when `key` is present in the attribute map (regardless of\n"
" its value).\n"
).
-spec has_attribute(metadata(), binary()) -> boolean().
has_attribute(Metadata, Key) ->
gleam@dict:has_key(erlang:element(5, Metadata), Key).
-file("src/automata/event/metadata.gleam", 80).
?DOC(
" Return the configured `correlation_id`, if any.\n"
"\n"
" Symmetric with `with_correlation_id/2` so callers can read and write\n"
" without reaching into the `Metadata` record's public fields directly.\n"
).
-spec correlation_id(metadata()) -> gleam@option:option(binary()).
correlation_id(Metadata) ->
erlang:element(2, Metadata).
-file("src/automata/event/metadata.gleam", 85).
?DOC(" Return the configured `causation_id`, if any.\n").
-spec causation_id(metadata()) -> gleam@option:option(binary()).
causation_id(Metadata) ->
erlang:element(3, Metadata).
-file("src/automata/event/metadata.gleam", 90).
?DOC(" Return the configured `trace_id`, if any.\n").
-spec trace_id(metadata()) -> gleam@option:option(binary()).
trace_id(Metadata) ->
erlang:element(4, Metadata).
-file("src/automata/event/metadata.gleam", 95).
?DOC(" Return the full attribute map.\n").
-spec attributes(metadata()) -> gleam@dict:dict(binary(), binary()).
attributes(Metadata) ->
erlang:element(5, Metadata).