Skip to main content

http_span

Function http_span 

Source
pub fn http_span<T>(
    method: &str,
    url: &str,
    data: HashMap<String, Value>,
    f: impl FnOnce(Option<&str>) -> T,
) -> T
Expand description

Times an outgoing HTTP call as an http span named "<METHOD> <host>" (never the path or query, which can carry ids or tokens) and hands f the traceparent header value to send with that request: its parent id is this span’s own id, so the called service’s root span nests under it when it continues the trace. Set it however your HTTP client does:

let url = "https://api.example.com/orders";
let response = forge_ops_tracker::http_span("POST", url, HashMap::new(), |traceparent| {
    let mut request = ureq::post(url);
    if let Some(value) = traceparent {
        request = request.set(forge_ops_tracker::TRACEPARENT_HEADER, value);
    }
    request.send_string("{}")
});

f gets None outside a trace, when Configuration.propagate_traces is off, or when the URL’s host isn’t in Configuration.trace_propagation_targets; outside a trace no span is recorded either. Recorded even if f panics. This crate doesn’t instrument any HTTP client on its own, so a call made without this helper carries no header.