-
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?
Conversation
Add /v1/traces, /v1/metrics, /v1/logs proxy endpoints that forward OTLP data from SDKs to the internal OTEL collector. Routes bypass analytics/blocking middleware to avoid recursive tracing. Includes lightweight Bearer auth, 5MB body limit, and Helm chart config. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary of ChangesHello @vsraccubits, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a new OTLP telemetry proxy within the budgateway service, enabling SDKs to send OpenTelemetry Protocol (OTLP) traces, metrics, and logs directly to an internal OpenTelemetry collector. The proxy is designed for efficiency and stability, bypassing standard middleware and incorporating its own lightweight authentication and body size limits to ensure secure and performant telemetry ingestion. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request introduces an OTLP telemetry proxy, a valuable feature for enhancing SDK observability. The implementation is well-structured, adding new endpoints, configuration options, and a dedicated authentication middleware. The routing logic correctly bypasses analytics and blocking middleware, which is crucial for preventing recursive tracing.
I have two main suggestions for improvement:
- In
auth.rs, I recommend refactoring the API key extraction logic for better readability and to avoid an unnecessary memory allocation. - In
otlp_proxy.rs, I suggest streaming the response body from the upstream collector instead of buffering it. This is a more memory-efficient and robust approach for a proxy.
Overall, the changes are solid, and with these refinements, the feature will be even more robust and maintainable.
| pub async fn require_api_key_telemetry( | ||
| State(auth): State<Auth>, | ||
| request: Request, | ||
| next: Next, | ||
| ) -> Result<Response, Response> { | ||
| let key = request | ||
| .headers() | ||
| .get("authorization") | ||
| .and_then(|v| v.to_str().ok()) | ||
| .map(|s| { | ||
| let s = s.trim(); | ||
| s.strip_prefix("Bearer ").unwrap_or(s).to_string() | ||
| }); | ||
|
|
||
| let key = match key { | ||
| Some(k) => k, | ||
| None => { | ||
| return Err(auth_error_response( | ||
| StatusCode::UNAUTHORIZED, | ||
| "Missing authorization header", | ||
| )) | ||
| } | ||
| }; | ||
|
|
||
| if auth.validate_api_key(&key).is_err() { | ||
| return Err(auth_error_response( | ||
| StatusCode::UNAUTHORIZED, | ||
| "Invalid API key", | ||
| )); | ||
| } | ||
|
|
||
| Ok(next.run(request).await) | ||
| } |
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 String allocation and a slightly verbose match statement.
Refactoring this to use ok_or_else and process the &str slice directly will make the code cleaner and avoid the unnecessary allocation, improving performance slightly.
pub async fn require_api_key_telemetry(
State(auth): State<Auth>,
request: Request,
next: Next,
) -> Result<Response, Response> {
let key = request
.headers()
.get("authorization")
.and_then(|v| v.to_str().ok())
.map(|s| s.strip_prefix("Bearer ").unwrap_or(s).trim())
.ok_or_else(|| {
auth_error_response(
StatusCode::UNAUTHORIZED,
"Missing or invalid authorization header",
)
})?;
if auth.validate_api_key(key).is_err() {
return Err(auth_error_response(
StatusCode::UNAUTHORIZED,
"Invalid API key",
));
}
Ok(next.run(request).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) | ||
| } |
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 current implementation buffers the entire upstream response body in memory using resp.bytes().await before sending it to the client. For a proxy handling potentially large payloads (even with a 5MB limit), it's more memory-efficient and robust to stream the response body directly.
Streaming avoids holding the entire response in memory and handles network interruptions more gracefully. Additionally, the status code handling can be simplified by using resp.status() directly.
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())
}
Add /v1/traces, /v1/metrics, /v1/logs proxy endpoints that forward OTLP data from SDKs to the internal OTEL collector. Routes bypass analytics/blocking middleware to avoid recursive tracing. Includes lightweight Bearer auth, 5MB body limit, and Helm chart config.