glotel/span
Types
An OpenTelemetry span context
pub type SpanContext
Functions
pub fn extract_values(values: List(#(String, String))) -> Nil
Extract OpenTelemetry attributes like traceparent
from request headers.
Examples
pub fn trace_middleware(request: Request, handler: fn() -> Response) -> Response {
extract_values(request.headers)
}
pub fn new(
name: String,
attributes: List(#(String, String)),
apply: fn(SpanContext) -> a,
) -> a
Creates a new span context, automatically linked to its immediate parent span, if present. Default to a internal span kind.
Examples
fn foo() {
use span_ctx <- new("foo", [#("attribute", "value")])
}
pub fn new_of_kind(
kind: SpanKind,
name: String,
attributes: List(#(String, String)),
apply: fn(SpanContext) -> a,
) -> a
Same as new
, but allows specifying span kind.
Examples
fn server_foo() {
use span_ctx <- new_of_kind(span_kind.Server, "server_foo", [
#("attribute", "value"),
])
}
pub fn set_attribute(
span: SpanContext,
key: String,
value: String,
) -> Nil
Sets an additional attribute on a given span.
Examples
fn foo() {
use span_ctx <- new("foo", [])
set_attribute(span_ctx, "bar", "baz")
}
pub fn set_error(span: SpanContext) -> Nil
Sets error = true
on a given span.
Examples
case status_code >= 500 {
True -> set_error(span_ctx)
False -> Nil
}
pub fn set_error_message(
span: SpanContext,
message: String,
) -> Nil
Same as set_error
, but allows specifying an explicit error message.
Examples
case status_code >= 500 {
True ->
set_error_message(
span_ctx,
"Request error: " <> int.to_string(status_code),
)
False -> Nil
}
pub fn try(
span: SpanContext,
result: Result(a, b),
apply fun: fn(a) -> Result(c, b),
) -> Result(c, b)
Same as standard library result.try()
,
but automatically calls set_error(span)
on an Error.
pub fn try_with_message(
span: SpanContext,
message: String,
result: Result(a, b),
apply fun: fn(a) -> Result(c, b),
) -> Result(c, b)
Same as standard library result.try()
,
but automatically calls set_error_message(span, message)
on an Error.