-
Notifications
You must be signed in to change notification settings - Fork 11
chore(antithesis): split intake HTTP modules #1892
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,77 +1,25 @@ | ||
| //! Axum HTTP surface for the intake. | ||
| //! | ||
| //! The `/api/v2/series` route stacks measurement middleware ahead of the | ||
| //! decompression layer so Pyld05 (compressed size), Pyld06 (uncompressed size), | ||
| //! and Pyld22 (content-length) can read both the on-the-wire and decompressed | ||
| //! body lengths, recorded as request extensions before `RequestDecompressionLayer` | ||
| //! consumes the encoding headers. | ||
| //! This module composes the intake router while submodules keep protocol groups | ||
| //! and middleware separate. | ||
|
|
||
| use std::sync::Arc; | ||
|
|
||
| use axum::{ | ||
| body::Body, | ||
| extract::{DefaultBodyLimit, Request}, | ||
| http::StatusCode, | ||
| middleware::{from_fn, Next}, | ||
| response::{IntoResponse, Response}, | ||
| routing::post, | ||
| Router, | ||
| }; | ||
| use headers::{ContentEncoding, ContentLength, HeaderMapExt}; | ||
| use tower::ServiceBuilder; | ||
| use tower_http::decompression::RequestDecompressionLayer; | ||
| use axum::{http::StatusCode, Router}; | ||
|
|
||
| use crate::intake; | ||
| mod datadog; | ||
| pub(crate) mod middleware; | ||
| mod state; | ||
|
|
||
| /// Memory backstop on the compressed body buffered before decompression, above any Pyld05 spec limit | ||
| const MAX_COMPRESSED_BODY_BYTES: usize = 64 * 1024 * 1024; | ||
| use self::state::AppState; | ||
|
|
||
| /// Wire measurements recorded before decompression, attached as a request extension for Pyld05/Pyld06/Pyld22 | ||
| #[derive(Clone, Copy, Debug)] | ||
| pub(crate) struct Measurements { | ||
| /// Compressed, on-the-wire body length, read before decompression. | ||
| pub(crate) compressed_len: u64, | ||
| /// Whether the request entered the decompression path. | ||
| pub(crate) decompression_applied: bool, | ||
| /// The declared `Content-Length`, or `None` when the header was absent. | ||
| pub(crate) declared_content_length: Option<u64>, | ||
| } | ||
| /// Memory backstop on the compressed body buffered before decompression. Sits above any Pyld05 spec limit. | ||
| const MAX_COMPRESSED_BODY_BYTES: usize = 64 * 1024 * 1024; | ||
|
|
||
| /// Build the intake router, `/api/v2/series` for payload assertions, others return 200 OK | ||
| /// Build the intake router, `/api/v2/series` for payload assertions, others return 200 OK. | ||
| pub fn build_router(hostname: Arc<str>) -> Router { | ||
| // Pyld01-Pyld06 and Pyld22 need the compressed body and raw headers, so the series | ||
| // route runs `measure_compressed_size` outermost, then decompresses, then | ||
| // lifts the body limit (the middleware's own cap is the backstop). | ||
| let series = post(intake::handle_series).layer( | ||
| ServiceBuilder::new() | ||
| .layer(from_fn(measure_compressed_size)) | ||
| .layer(RequestDecompressionLayer::new().pass_through_unaccepted(true)) | ||
| .layer(DefaultBodyLimit::disable()), | ||
| ); | ||
|
|
||
| Router::new() | ||
| .route("/api/v2/series", series) | ||
| .merge(datadog::routes()) | ||
| .fallback(|| async { StatusCode::OK }) | ||
| .with_state(hostname) | ||
| } | ||
|
|
||
| /// Buffer the body and record compressed size, encoding, and content-length before decompression | ||
| async fn measure_compressed_size(req: Request, next: Next) -> Response { | ||
| let (parts, body) = req.into_parts(); | ||
| let Ok(bytes) = axum::body::to_bytes(body, MAX_COMPRESSED_BODY_BYTES).await else { | ||
| return StatusCode::PAYLOAD_TOO_LARGE.into_response(); | ||
| }; | ||
| let len = bytes.len() as u64; | ||
| let applied = parts | ||
| .headers | ||
| .typed_get::<ContentEncoding>() | ||
| .is_some_and(|enc| enc.contains("deflate") || enc.contains("gzip") || enc.contains("zstd")); | ||
| let declared = parts.headers.typed_get::<ContentLength>().map(|cl| cl.0); | ||
| let mut req = Request::from_parts(parts, Body::from(bytes)); | ||
| req.extensions_mut().insert(Measurements { | ||
| compressed_len: len, | ||
| decompression_applied: applied, | ||
| declared_content_length: declared, | ||
| }); | ||
| next.run(req).await | ||
| .with_state(AppState { hostname }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| //! Datadog-compatible intake routes. | ||
|
|
||
| use axum::{extract::DefaultBodyLimit, middleware::from_fn, routing::post, Router}; | ||
| use tower::ServiceBuilder; | ||
| use tower_http::decompression::RequestDecompressionLayer; | ||
|
|
||
| use self::metrics::handle_series; | ||
| use super::middleware::measure_compressed_size; | ||
| use super::state::AppState; | ||
|
|
||
| mod metrics; | ||
|
|
||
| /// Build Datadog-compatible intake routes. | ||
| pub(crate) fn routes() -> Router<AppState> { | ||
| // Pyld01-Pyld06 and Pyld22 need the compressed body and raw headers, so the series | ||
| // route runs `measure_compressed_size` outermost, then decompresses, then | ||
| // lifts the body limit (the middleware's own cap is the backstop). | ||
| let series = post(handle_series).layer( | ||
| ServiceBuilder::new() | ||
| .layer(from_fn(measure_compressed_size)) | ||
| .layer(RequestDecompressionLayer::new().pass_through_unaccepted(true)) | ||
| .layer(DefaultBodyLimit::disable()), | ||
| ); | ||
|
|
||
| Router::new().route("/api/v2/series", series) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| //! HTTP middleware for intake request measurement. | ||
| //! | ||
| //! The `/api/v2/series` route stacks measurement middleware ahead of the | ||
| //! decompression layer so Pyld05 (compressed size), Pyld06 (uncompressed size), | ||
| //! and Pyld22 (content-length) can read both the on-the-wire and decompressed | ||
| //! body lengths, recorded as request extensions before `RequestDecompressionLayer` | ||
| //! consumes the encoding headers. | ||
|
|
||
| use axum::{ | ||
| body::Body, | ||
| extract::Request, | ||
| http::StatusCode, | ||
| middleware::Next, | ||
| response::{IntoResponse, Response}, | ||
| }; | ||
| use headers::{ContentEncoding, ContentLength, HeaderMapExt}; | ||
|
|
||
| use super::MAX_COMPRESSED_BODY_BYTES; | ||
|
|
||
| /// Wire measurements recorded before decompression, attached as a request extension for Pyld05/Pyld06/Pyld22. | ||
| #[derive(Clone, Copy, Debug)] | ||
| pub(crate) struct Measurements { | ||
| /// Compressed, on-the-wire body length, read before decompression. | ||
| pub(crate) compressed_len: u64, | ||
| /// Whether the request entered the decompression path. | ||
| pub(crate) decompression_applied: bool, | ||
| /// The declared `Content-Length`, or `None` when the header was absent. | ||
| pub(crate) declared_content_length: Option<u64>, | ||
| } | ||
|
|
||
| /// Buffer the body and record compressed size, encoding, and content-length before decompression. | ||
| pub(crate) async fn measure_compressed_size(req: Request, next: Next) -> Response { | ||
| let (parts, body) = req.into_parts(); | ||
| let Ok(bytes) = axum::body::to_bytes(body, MAX_COMPRESSED_BODY_BYTES).await else { | ||
| return StatusCode::PAYLOAD_TOO_LARGE.into_response(); | ||
| }; | ||
| let len = bytes.len() as u64; | ||
| let applied = parts | ||
| .headers | ||
| .typed_get::<ContentEncoding>() | ||
| .is_some_and(|enc| enc.contains("deflate") || enc.contains("gzip") || enc.contains("zstd")); | ||
| let declared = parts.headers.typed_get::<ContentLength>().map(|cl| cl.0); | ||
| let mut req = Request::from_parts(parts, Body::from(bytes)); | ||
| req.extensions_mut().insert(Measurements { | ||
| compressed_len: len, | ||
| decompression_applied: applied, | ||
| declared_content_length: declared, | ||
| }); | ||
| next.run(req).await | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| //! Shared state carried by each HTTP router. | ||
|
|
||
| use std::sync::Arc; | ||
|
|
||
| /// Shared application state for one target's HTTP router. | ||
| #[derive(Clone, Debug)] | ||
| pub(crate) struct AppState { | ||
| /// Configured Agent hostname. Pyld17 checks each series host against it. | ||
| pub(crate) hostname: Arc<str>, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,5 @@ | |
|
|
||
| pub mod http; | ||
|
|
||
| mod intake; | ||
| mod properties; | ||
| mod series_observation; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.