Current section
Files
Jump to
Current section
Files
src/glotel@span.erl
-module(glotel@span).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new_of_kind/4, new/3, set_attribute/3, set_error/1, set_error_message/2, 'try'/3, try_with_message/4, extract_values/1]).
-export_type([application/0, tracer/0, span_context/0, otel_context/0, context_token/0]).
-type application() :: any().
-type tracer() :: any().
-type span_context() :: any().
-type otel_context() :: any().
-type context_token() :: any().
-spec new_of_kind(
glotel@span_kind:span_kind(),
binary(),
list({binary(), binary()}),
fun((span_context()) -> FKN)
) -> FKN.
new_of_kind(Kind, Name, Attributes, Apply) ->
Otel_context = otel_ctx:get_current(),
Application = application:get_application(),
Tracer = opentelemetry:get_application_tracer(Application),
Span = glotel_ffi:do_start_span(
Tracer,
Name,
Kind,
maps:from_list(Attributes)
),
Otel_context2 = otel_tracer:set_current_span(Otel_context, Span),
Token = otel_ctx:attach(Otel_context2),
Result = Apply(Span),
otel_span:end_span(Span),
otel_ctx:detach(Token),
Result.
-spec new(binary(), list({binary(), binary()}), fun((span_context()) -> FKL)) -> FKL.
new(Name, Attributes, Apply) ->
new_of_kind(internal, Name, Attributes, Apply).
-spec set_attribute(span_context(), binary(), binary()) -> nil.
set_attribute(Span, Key, Value) ->
otel_span:set_attribute(Span, Key, Value).
-spec set_error(span_context()) -> nil.
set_error(Span) ->
otel_span:set_status(Span, error).
-spec set_error_message(span_context(), binary()) -> nil.
set_error_message(Span, Message) ->
otel_span:set_status(Span, error, Message).
-spec 'try'(
span_context(),
{ok, FKO} | {error, FKP},
fun((FKO) -> {ok, FKS} | {error, FKP})
) -> {ok, FKS} | {error, FKP}.
'try'(Span, Result, Fun) ->
case Result of
{ok, X} ->
Fun(X);
{error, E} ->
set_error(Span),
{error, E}
end.
-spec try_with_message(
span_context(),
binary(),
{ok, FKX} | {error, FKY},
fun((FKX) -> {ok, FLB} | {error, FKY})
) -> {ok, FLB} | {error, FKY}.
try_with_message(Span, Message, Result, Fun) ->
case Result of
{ok, X} ->
Fun(X);
{error, E} ->
set_error_message(Span, Message),
{error, E}
end.
-spec extract_values(list({binary(), binary()})) -> nil.
extract_values(Values) ->
otel_propagator_text_map:extract(Values).