|
| 1 | +# Attaches an Open Telemetry span that abides by the semantic conventions for |
| 2 | +# HTTP clients to the request, including the associated W3C trace context |
| 3 | +# headers. |
| 4 | +# |
| 5 | +# See: https://opentelemetry.io/docs/specs/semconv/http/http-spans/#http-client-span |
| 6 | +req_with_span <- function( |
| 7 | + req, |
| 8 | + resend_count = 0, |
| 9 | + tracer = NULL, |
| 10 | + scope = parent.frame() |
| 11 | +) { |
| 12 | + if (!is_installed("otel")) { |
| 13 | + return(req) |
| 14 | + } |
| 15 | + tracer <- tracer %||% otel::get_tracer("httr2") |
| 16 | + if (!tracer$is_enabled()) { |
| 17 | + return(req) |
| 18 | + } |
| 19 | + parsed <- tryCatch(url_parse(req$url), error = function(cnd) NULL) |
| 20 | + if (is.null(parsed)) { |
| 21 | + # Don't create spans for invalid URLs. |
| 22 | + return(req) |
| 23 | + } |
| 24 | + if (!req_has_user_agent(req)) { |
| 25 | + req <- req_user_agent(req) |
| 26 | + } |
| 27 | + default_port <- 443L |
| 28 | + if (parsed$scheme == "http") { |
| 29 | + default_port <- 80L |
| 30 | + } |
| 31 | + # Follow the semantic conventions and redact credentials in the URL, when |
| 32 | + # present. |
| 33 | + if (!is.null(parsed$username)) { |
| 34 | + parsed$username <- "REDACTED" |
| 35 | + } |
| 36 | + if (!is.null(parsed$password)) { |
| 37 | + parsed$password <- "REDACTED" |
| 38 | + } |
| 39 | + method <- req_method_get(req) |
| 40 | + span <- tracer$start_span( |
| 41 | + name = method, |
| 42 | + options = list(kind = "CLIENT"), |
| 43 | + # Ensure we set attributes relevant to sampling at span creation time. |
| 44 | + attributes = compact(list( |
| 45 | + "http.request.method" = method, |
| 46 | + "server.address" = parsed$hostname, |
| 47 | + "server.port" = parsed$port %||% default_port, |
| 48 | + "url.full" = url_build(parsed), |
| 49 | + "http.request.resend_count" = if (resend_count > 1) resend_count, |
| 50 | + "user_agent.original" = req$options$useragent |
| 51 | + )), |
| 52 | + scope = scope |
| 53 | + ) |
| 54 | + ctx <- span$get_context() |
| 55 | + req <- req_headers(req, !!!ctx$to_http_headers()) |
| 56 | + req$state$span <- span |
| 57 | + req |
| 58 | +} |
| 59 | + |
| 60 | +# Ends the Open Telemetry span associated with this request, if any. |
| 61 | +req_end_span <- function(req, resp = NULL) { |
| 62 | + span <- req$state$span |
| 63 | + if (is.null(span) || !span$is_recording()) { |
| 64 | + return() |
| 65 | + } |
| 66 | + if (is.null(resp)) { |
| 67 | + span$end() |
| 68 | + return() |
| 69 | + } |
| 70 | + if (is_error(resp)) { |
| 71 | + span$record_exception(resp) |
| 72 | + span$set_status("error") |
| 73 | + # Surface the underlying curl error class. |
| 74 | + span$set_attribute("error.type", class(resp$parent)[1]) |
| 75 | + span$end() |
| 76 | + return() |
| 77 | + } |
| 78 | + span$set_attribute("http.response.status_code", resp_status(resp)) |
| 79 | + if (error_is_error(req, resp)) { |
| 80 | + desc <- resp_status_desc(resp) |
| 81 | + if (is.na(desc)) { |
| 82 | + desc <- NULL |
| 83 | + } |
| 84 | + span$set_status("error", desc) |
| 85 | + # The semantic conventions recommend using the status code as a string for |
| 86 | + # these cases. |
| 87 | + span$set_attribute("error.type", as.character(resp_status(resp))) |
| 88 | + } else { |
| 89 | + span$set_status("ok") |
| 90 | + } |
| 91 | + span$end() |
| 92 | +} |
| 93 | + |
| 94 | +# Replaces the existing Open Telemetry span on a request with a new one. Used |
| 95 | +# for retries. |
| 96 | +req_reset_span <- function( |
| 97 | + req, |
| 98 | + handle, |
| 99 | + resend_count = 0, |
| 100 | + tracer = NULL, |
| 101 | + scope = parent.frame() |
| 102 | +) { |
| 103 | + req <- req_with_span(req, resend_count, tracer, scope) |
| 104 | + if (is.null(req$state$span)) { |
| 105 | + return(req) |
| 106 | + } |
| 107 | + # Because the headers have changed, we need to re-sign the request and update |
| 108 | + # stateful components (like the handle). |
| 109 | + req <- auth_sign(req) |
| 110 | + curl::handle_setheaders(handle, .list = headers_flatten(req$headers)) |
| 111 | + req$state$headers <- req$headers |
| 112 | + req |
| 113 | +} |
0 commit comments