-
Notifications
You must be signed in to change notification settings - Fork 3
feat(budgateway): add OTLP telemetry proxy for SDK observability #1289
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| use axum::body::Body; | ||
| use axum::extract::State; | ||
| use axum::http::{HeaderMap, StatusCode, Uri}; | ||
| use axum::response::{IntoResponse, Response}; | ||
| use std::time::Duration; | ||
|
|
||
| use crate::gateway_util::AppStateData; | ||
|
|
||
| const OTLP_PROXY_TIMEOUT: Duration = Duration::from_secs(10); | ||
|
|
||
| /// POST /v1/traces, /v1/metrics, /v1/logs | ||
| /// Transparent proxy to the internal OTEL collector. | ||
| /// Auth is handled by the require_api_key_telemetry middleware. | ||
| pub async fn otlp_proxy_handler( | ||
| State(app_state): State<AppStateData>, | ||
| uri: Uri, | ||
| headers: HeaderMap, | ||
| body: Body, | ||
| ) -> Result<Response, Response> { | ||
| let collector_endpoint = &app_state.config.gateway.otlp_proxy.collector_endpoint; | ||
| let url = format!("{}{}", collector_endpoint, uri.path()); | ||
|
|
||
| let mut req = app_state | ||
| .http_client | ||
| .post(&url) | ||
| .timeout(OTLP_PROXY_TIMEOUT) | ||
| .body(reqwest::Body::wrap_stream(body.into_data_stream())); | ||
|
|
||
| for (name, value) in headers.iter() { | ||
| if name != "host" && name != "connection" && name != "authorization" { | ||
| req = req.header(name, value); | ||
| } | ||
| } | ||
|
|
||
| match req.send().await { | ||
| Ok(resp) => { | ||
| let status = StatusCode::from_u16(resp.status().as_u16()) | ||
| .unwrap_or(StatusCode::BAD_GATEWAY); | ||
| let resp_headers = resp.headers().clone(); | ||
| let resp_body = resp.bytes().await.unwrap_or_default(); | ||
|
|
||
| let mut response = (status, resp_body).into_response(); | ||
| for (name, value) in resp_headers.iter() { | ||
| response.headers_mut().insert(name, value.clone()); | ||
| } | ||
| Ok(response) | ||
| } | ||
|
Comment on lines
+36
to
+47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation buffers the entire upstream response body in memory using Streaming avoids holding the entire response in memory and handles network interruptions more gracefully. Additionally, the status code handling can be simplified by using Ok(resp) => {
let mut response_builder = Response::builder().status(resp.status());
// Copy headers from the upstream response.
if let Some(headers) = response_builder.headers_mut() {
headers.extend(resp.headers().clone());
}
// Stream the body from the upstream response.
let body = Body::from_stream(resp.bytes_stream());
// It's safe to unwrap here as we've built a valid response.
Ok(response_builder.body(body).unwrap())
} |
||
| Err(e) => { | ||
| tracing::warn!(error = %e, url = %url, "OTLP proxy: failed to reach OTEL collector"); | ||
| Err((StatusCode::BAD_GATEWAY, "OTEL collector unavailable").into_response()) | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for extracting and validating the API key can be made more idiomatic and efficient. The current implementation involves an extra
Stringallocation and a slightly verbosematchstatement.Refactoring this to use
ok_or_elseand process the&strslice directly will make the code cleaner and avoid the unnecessary allocation, improving performance slightly.