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, inline]).
-define(FILEPATH, "src/glotel/span.gleam").
-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]).
-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 application() :: any().
-type tracer() :: any().
-type span_context() :: any().
-type otel_context() :: any().
-type context_token() :: any().
-file("src/glotel/span.gleam", 77).
?DOC(
" Same as `new`, but allows specifying span kind.\n"
" \n"
" ## Examples\n"
" \n"
" ```gleam\n"
" fn server_foo() {\n"
" use span_ctx <- new_of_kind(span_kind.Server, \"server_foo\", [\n"
" #(\"attribute\", \"value\"),\n"
" ])\n"
" }\n"
" ```\n"
).
-spec new_of_kind(
glotel@span_kind:span_kind(),
binary(),
list({binary(), binary()}),
fun((span_context()) -> DQW)
) -> DQW.
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.
-file("src/glotel/span.gleam", 57).
?DOC(
" Creates a new span context, automatically linked to its immediate parent span, if present.\n"
" Default to a internal span kind.\n"
" \n"
" ## Examples\n"
" \n"
" ```gleam\n"
" fn foo() {\n"
" use span_ctx <- new(\"foo\", [#(\"attribute\", \"value\")])\n"
" }\n"
" ```\n"
).
-spec new(binary(), list({binary(), binary()}), fun((span_context()) -> DQU)) -> DQU.
new(Name, Attributes, Apply) ->
new_of_kind(internal, Name, Attributes, Apply).
-file("src/glotel/span.gleam", 110).
?DOC(
" Sets an additional attribute on a given span.\n"
" \n"
" ## Examples\n"
" \n"
" ```gleam\n"
" fn foo() {\n"
" use span_ctx <- new(\"foo\", [])\n"
" set_attribute(span_ctx, \"bar\", \"baz\")\n"
" }\n"
" ```\n"
).
-spec set_attribute(span_context(), binary(), binary()) -> nil.
set_attribute(Span, Key, Value) ->
otel_span:set_attribute(Span, Key, Value).
-file("src/glotel/span.gleam", 126).
?DOC(
" Sets `error = true` on a given span.\n"
" \n"
" ## Examples\n"
" \n"
" ```gleam\n"
" case status_code >= 500 {\n"
" True -> set_error(span_ctx)\n"
" False -> Nil\n"
" }\n"
" ```\n"
).
-spec set_error(span_context()) -> nil.
set_error(Span) ->
otel_span:set_status(Span, error).
-file("src/glotel/span.gleam", 152).
?DOC(
" Same as `set_error`, but allows specifying an explicit error message.\n"
" \n"
" ## Examples\n"
" \n"
" ```gleam\n"
" case status_code >= 500 {\n"
" True ->\n"
" set_error_message(\n"
" span_ctx,\n"
" \"Request error: \" <> int.to_string(status_code),\n"
" )\n"
" False -> Nil\n"
" }\n"
" ```\n"
).
-spec set_error_message(span_context(), binary()) -> nil.
set_error_message(Span, Message) ->
otel_span:set_status(Span, error, Message).
-file("src/glotel/span.gleam", 159).
?DOC(
" Same as standard library `result.try()`,\n"
" but automatically calls `set_error(span)` on an Error.\n"
).
-spec 'try'(
span_context(),
{ok, DQX} | {error, DQY},
fun((DQX) -> {ok, DRB} | {error, DQY})
) -> {ok, DRB} | {error, DQY}.
'try'(Span, Result, Fun) ->
case Result of
{ok, X} ->
Fun(X);
{error, E} ->
set_error(Span),
{error, E}
end.
-file("src/glotel/span.gleam", 176).
?DOC(
" Same as standard library `result.try()`,\n"
" but automatically calls `set_error_message(span, message)` on an Error.\n"
).
-spec try_with_message(
span_context(),
binary(),
{ok, DRG} | {error, DRH},
fun((DRG) -> {ok, DRK} | {error, DRH})
) -> {ok, DRK} | {error, DRH}.
try_with_message(Span, Message, Result, Fun) ->
case Result of
{ok, X} ->
Fun(X);
{error, E} ->
set_error_message(Span, Message),
{error, E}
end.
-file("src/glotel/span.gleam", 202).
?DOC(
" Extract OpenTelemetry attributes like `traceparent` from request headers.\n"
" \n"
" ## Examples\n"
" \n"
" ```gleam\n"
" pub fn trace_middleware(request: Request, handler: fn() -> Response) -> Response {\n"
" extract_values(request.headers)\n"
" }\n"
" ```\n"
).
-spec extract_values(list({binary(), binary()})) -> nil.
extract_values(Values) ->
otel_propagator_text_map:extract(Values).