Skip to content

Commit df52371

Browse files
authored
fix(guard, pb): add top level ray id, fix actor wf error handling (#3236)
1 parent ca4eed0 commit df52371

File tree

10 files changed

+168
-138
lines changed

10 files changed

+168
-138
lines changed

Cargo.lock

Lines changed: 4 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/common/api-builder/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ axum-extra.workspace = true
1212
gas.workspace = true
1313
chrono.workspace = true
1414
http.workspace = true
15-
hyper = { workspace = true, features = ["full"] }
15+
# TODO: Make this use workspace version
16+
hyper = { version = "1.6.0", features = ["full"] }
1617
lazy_static.workspace = true
1718
opentelemetry.workspace = true
1819
rivet-cache.workspace = true

packages/common/api-builder/src/middleware.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use axum::{
88
middleware::Next,
99
response::Response,
1010
};
11+
use hyper::header::HeaderName;
1112
use opentelemetry::trace::TraceContextExt;
1213
use rivet_metrics::KeyValue;
1314
use tower_http::trace::TraceLayer;
@@ -16,6 +17,8 @@ use tracing_opentelemetry::OpenTelemetrySpanExt;
1617

1718
use crate::{ErrorExt, RequestIds, metrics};
1819

20+
pub const X_RIVET_RAY_ID: HeaderName = HeaderName::from_static("x-rivet-ray-id");
21+
1922
// TODO: Remove this since this is duplicate logs & traces, but this is just to see what Axum adds
2023
// natively vs our logging. We can add this once we're satisfied with our own logging.
2124
pub fn create_trace_layer()
@@ -46,11 +49,14 @@ pub async fn http_logging_middleware(
4649
.span_context()
4750
.clone();
4851

49-
// Generate request IDs
50-
let request_ids = RequestIds::new(config.dc_label());
51-
52-
// Add request IDs to request extensions so they can be accessed by handlers
53-
req.extensions_mut().insert(request_ids);
52+
// Add request IDs to request extensions if not already added by guard so they can be accessed by handlers
53+
let request_ids = if let Some(request_ids) = req.extensions().get::<RequestIds>() {
54+
*request_ids
55+
} else {
56+
let request_ids = RequestIds::new(config.dc_label());
57+
req.extensions_mut().insert(request_ids);
58+
request_ids
59+
};
5460

5561
// Create span for this request
5662
let req_span = tracing::info_span!(
@@ -118,7 +124,7 @@ pub async fn http_logging_middleware(
118124

119125
// Add ray_id to response headers
120126
if let Ok(ray_id_value) = request_ids.ray_id.to_string().parse() {
121-
response.headers_mut().insert("rvt-ray-id", ray_id_value);
127+
response.headers_mut().insert(X_RIVET_RAY_ID, ray_id_value);
122128
}
123129

124130
let status = response.status();
@@ -175,8 +181,6 @@ pub async fn http_logging_middleware(
175181
let duration = start.elapsed().as_secs_f64();
176182

177183
tracing::debug!(
178-
ray_id = %request_ids.ray_id,
179-
req_id = %request_ids.req_id,
180184
%remote_addr,
181185
%method,
182186
%uri,

packages/common/api-util/src/lib.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anyhow::{Context, Result};
22
use axum::{body::Body, response::Response};
33
use futures_util::StreamExt;
4-
use rivet_api_builder::{ApiCtx, ErrorResponse, RawErrorResponse};
4+
use rivet_api_builder::{ApiCtx, ErrorResponse, RawErrorResponse, X_RIVET_RAY_ID};
55
use serde::{Serialize, de::DeserializeOwned};
66
use std::future::Future;
77

@@ -43,7 +43,7 @@ pub async fn request_remote_datacenter_raw(
4343
let res = request
4444
.send()
4545
.await
46-
.context("failed sending request to remote dc")?;
46+
.context("failed parsing response from remote dc")?;
4747
reqwest_to_axum_response(res)
4848
.await
4949
.context("failed parsing response from remote dc")
@@ -85,7 +85,7 @@ where
8585
let res = request
8686
.send()
8787
.await
88-
.context("failed sending request to remote dc")?;
88+
.context("failed parsing response from remote dc")?;
8989
parse_response::<T>(res)
9090
.await
9191
.context("failed parsing response from remote dc")
@@ -159,7 +159,7 @@ where
159159
// Error only if all requests failed
160160
if result_count == errors.len() {
161161
if let Some(res) = errors.into_iter().next() {
162-
return Err(res).context("all datacenter requests failed");
162+
return Err(res).with_context(|| "all datacenter requests failed");
163163
}
164164
}
165165

@@ -184,15 +184,25 @@ pub async fn reqwest_to_axum_response(reqwest_response: reqwest::Response) -> Re
184184
#[tracing::instrument(skip_all)]
185185
pub async fn parse_response<T: DeserializeOwned>(reqwest_response: reqwest::Response) -> Result<T> {
186186
let status = reqwest_response.status();
187+
let headers = reqwest_response.headers();
188+
let ray_id = headers
189+
.get(X_RIVET_RAY_ID)
190+
.and_then(|v| v.to_str().ok())
191+
.map(|x| x.to_string());
187192
let response_text = reqwest_response.text().await?;
188193

189194
if status.is_success() {
190195
serde_json::from_str::<T>(&response_text).map_err(Into::into)
191196
} else {
192-
Err(RawErrorResponse(
197+
let error = RawErrorResponse(
193198
status,
194199
serde_json::from_str::<ErrorResponse>(&response_text)?,
195-
)
196-
.into())
200+
);
201+
202+
if let Some(ray_id) = ray_id {
203+
Err(error).with_context(|| format!("remote request failed (ray_id: {ray_id})"))
204+
} else {
205+
Err(error.into())
206+
}
197207
}
198208
}

packages/core/guard/core/src/analytics.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use serde::{Deserialize, Serialize};
2-
use uuid::Uuid;
32

43
// Properties not currently collected but should be added in future iterations:
54
// - client_ssl_cipher: Requires TLS connection introspection
@@ -18,7 +17,9 @@ use uuid::Uuid;
1817

1918
#[derive(Debug, Clone, Serialize, Deserialize)]
2019
pub struct GuardHttpRequest {
21-
pub request_id: Uuid,
20+
// TODO:
21+
// pub request_id: Id,
22+
// pub ray_id: Id,
2223
pub client_ip: String,
2324
pub client_request_body_bytes: u64,
2425
pub client_request_host: String,
@@ -31,9 +32,6 @@ pub struct GuardHttpRequest {
3132
pub client_request_user_agent: String,
3233
pub client_src_port: u16,
3334
pub client_x_requested_with: String,
34-
pub guard_datacenter_id: Uuid,
35-
pub guard_cluster_id: Uuid,
36-
pub guard_server_id: Uuid,
3735
pub guard_end_timestamp: u64,
3836
pub guard_response_body_bytes: u64,
3937
pub guard_response_content_type: String,

0 commit comments

Comments
 (0)