Repository navigation
refactor(proxy): inject tracing receiver and access context #44035
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
Show all changes
12 commits
Select commit
Hold shift + click to select a range
30e0e80
refactor(proxy): inject tracing receiver and access context
yujonglee-berri 011a655
refactor(proxy): own tracing resources through FastAPI lifespan
yujonglee-berri 94fde13
test(proxy): pass tracing dependency in Lens lifecycle
yujonglee-berri f1d664b
refactor(proxy): stop tracing logger cooperatively
yujonglee-berri 6acbd7c
refactor(proxy): derive tracing permissions in one place
yujonglee-berri 0ca3c8b
refactor(proxy): compose application lifespan state
yujonglee-berri 1e7094b
refactor(proxy): give Lens tracing storage directly
yujonglee-berri d02d44b
refactor(tracing): name shared ClickHouse storage explicitly
yujonglee-berri b4420d8
refactor(tracing): extract shared ClickHouse storage crate
yujonglee-berri 54cb272
merge: sync tracing dependencies with main
yujonglee-berri 560e173
test(proxy): isolate db push timeout from Lens safety check
yujonglee-berri 4921a1c
fix(tracing): drain spend retries during shutdown
yujonglee-berri 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,20 @@ | ||
| [package] | ||
| name = "litellm-storage-clickhouse" | ||
| version = "0.1.0" | ||
| description = "Shared ClickHouse connection and HTTP storage for LiteLLM features" | ||
| edition.workspace = true | ||
| license.workspace = true | ||
| repository.workspace = true | ||
|
|
||
| [dependencies] | ||
| flate2.workspace = true | ||
| litellm-http.workspace = true | ||
| serde.workspace = true | ||
| serde_json.workspace = true | ||
| thiserror.workspace = true | ||
| url.workspace = true | ||
|
|
||
| [dev-dependencies] | ||
| litellm-http = { workspace = true, features = ["test-support"] } | ||
| rstest.workspace = true | ||
| tokio.workspace = true |
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,5 @@ | ||
| # ClickHouse storage | ||
|
|
||
| `litellm-storage-clickhouse` exports `Storage`, a shared writer connection and optional reader connection for one ClickHouse database. It also exports bounded HTTP read and insert execution | ||
|
|
||
| The crate has no trace tables, OTLP types, or named trace queries. `litellm-traces` supplies those rules and uses this storage for both trace rows and spend rows |
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,29 @@ | ||
| #[derive(Debug, thiserror::Error)] | ||
| pub enum Error { | ||
| #[error("invalid ClickHouse insert row")] | ||
| InvalidRow, | ||
| #[error("invalid ClickHouse insert table")] | ||
| InvalidTable, | ||
| #[error("invalid ClickHouse HTTP URL")] | ||
| InvalidUrl, | ||
| #[error("database must be a nonempty SQL identifier and retention must be positive")] | ||
| InvalidSchema, | ||
| #[error("SQL query must not be empty")] | ||
| EmptySql, | ||
| #[error("unknown ClickHouse read query")] | ||
| InvalidQuery, | ||
| #[error("ClickHouse query failed with HTTP status {0}")] | ||
| QueryFailed(u16), | ||
| #[error("ClickHouse insert failed with HTTP status {0}")] | ||
| InsertFailed(u16), | ||
| #[error("ClickHouse insert exceeds the encoded size limit")] | ||
| InsertTooLarge, | ||
| #[error("ClickHouse schema setup failed with HTTP status {0}")] | ||
| SchemaFailed(u16), | ||
| #[error("ClickHouse query exceeded the response size limit")] | ||
| ResponseTooLarge, | ||
| #[error("ClickHouse returned an invalid or failed JSON query response")] | ||
| InvalidResponse, | ||
| #[error("ClickHouse query transport failed")] | ||
| Transport, | ||
| } |
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,70 @@ | ||
| use std::{io::Write, time::Duration}; | ||
|
|
||
| use flate2::{Compression, write::GzEncoder}; | ||
| use litellm_http::Client; | ||
|
|
||
| use crate::{Connection, Error, valid_identifier}; | ||
|
|
||
| const INSERT_TIMEOUT: Duration = Duration::from_secs(30); | ||
|
|
||
| pub async fn insert_encoded_rows( | ||
| client: &Client, | ||
| connection: &Connection, | ||
| database: &str, | ||
| table: &str, | ||
| token: &str, | ||
| encoded: &str, | ||
| ) -> Result<(), Error> { | ||
| if !valid_identifier(database) { | ||
| return Err(Error::InvalidSchema); | ||
| } | ||
| if !valid_identifier(table) { | ||
| return Err(Error::InvalidTable); | ||
| } | ||
| let mut encoder = GzEncoder::new(Vec::new(), Compression::default()); | ||
| encoder | ||
| .write_all(encoded.as_bytes()) | ||
| .map_err(|_| Error::InvalidRow)?; | ||
| let body = encoder.finish().map_err(|_| Error::InvalidRow)?; | ||
| let mut url = connection.url().clone(); | ||
| let existing_pairs: Vec<(String, String)> = url | ||
| .query_pairs() | ||
| .filter(|(key, _)| { | ||
| !matches!( | ||
| key.as_ref(), | ||
| "query" | ||
| | "async_insert" | ||
| | "async_insert_deduplicate" | ||
| | "wait_for_async_insert" | ||
| | "input_format_skip_unknown_fields" | ||
| | "date_time_input_format" | ||
| ) | ||
| }) | ||
| .map(|(key, value)| (key.into_owned(), value.into_owned())) | ||
| .collect(); | ||
| url.query_pairs_mut() | ||
| .clear() | ||
| .extend_pairs(existing_pairs) | ||
| .append_pair( | ||
| "query", | ||
| &format!("INSERT INTO `{database}`.{} FORMAT JSONEachRow", table), | ||
| ) | ||
| .append_pair("insert_deduplication_token", token) | ||
| .append_pair("async_insert", "1") | ||
| .append_pair("async_insert_deduplicate", "1") | ||
| .append_pair("wait_for_async_insert", "1") | ||
| .append_pair("input_format_skip_unknown_fields", "0") | ||
| .append_pair("date_time_input_format", "best_effort"); | ||
| let response = client | ||
| .post(url) | ||
| .timeout(INSERT_TIMEOUT) | ||
| .header("Content-Encoding", "gzip") | ||
| .body(body) | ||
| .send() | ||
| .await | ||
| .map_err(|_| Error::Transport)?; | ||
| if !response.status().is_success() { | ||
| return Err(Error::InsertFailed(response.status().as_u16())); | ||
| } | ||
| Ok(()) | ||
| } | ||
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,127 @@ | ||
| mod error; | ||
| mod insert; | ||
| mod read; | ||
|
|
||
| pub use error::Error; | ||
| pub use insert::insert_encoded_rows; | ||
| pub use read::{Parameter, execute_read}; | ||
| use url::Url; | ||
|
|
||
| #[derive(Clone)] | ||
| pub struct Connection { | ||
| url: Url, | ||
| } | ||
|
|
||
| impl Connection { | ||
| pub fn parse(value: &str) -> Result<Self, Error> { | ||
| let url = Url::parse(value).map_err(|_| Error::InvalidUrl)?; | ||
| if !matches!(url.scheme(), "http" | "https") || url.host().is_none() { | ||
| return Err(Error::InvalidUrl); | ||
| } | ||
| Ok(Self { url }) | ||
| } | ||
|
|
||
| pub fn configured( | ||
| url: &str, | ||
| database: &str, | ||
| user: &str, | ||
| password: &str, | ||
| ) -> Result<Self, Error> { | ||
| let mut connection = Self::parse(url)?; | ||
| connection | ||
| .url | ||
| .set_username(user) | ||
| .map_err(|_| Error::InvalidUrl)?; | ||
| connection | ||
| .url | ||
| .set_password(Some(password)) | ||
| .map_err(|_| Error::InvalidUrl)?; | ||
| let pairs: Vec<_> = connection | ||
| .url | ||
| .query_pairs() | ||
| .filter(|(key, _)| !matches!(key.as_ref(), "database" | "user" | "password")) | ||
| .map(|(key, value)| (key.into_owned(), value.into_owned())) | ||
| .collect(); | ||
| connection | ||
| .url | ||
| .query_pairs_mut() | ||
| .clear() | ||
| .extend_pairs(pairs) | ||
| .append_pair("database", database); | ||
| Ok(connection) | ||
| } | ||
|
|
||
| pub fn writer(url: &str) -> Result<Self, Error> { | ||
| let mut connection = Self::parse(url)?; | ||
| let pairs: Vec<_> = connection | ||
| .url | ||
| .query_pairs() | ||
| .filter(|(key, _)| !matches!(key.as_ref(), "database" | "readonly" | "query")) | ||
| .map(|(key, value)| (key.into_owned(), value.into_owned())) | ||
| .collect(); | ||
| connection.url.query_pairs_mut().clear().extend_pairs(pairs); | ||
| Ok(connection) | ||
| } | ||
|
|
||
| pub fn reader(url: &str, database: &str) -> Result<Self, Error> { | ||
| let mut connection = Self::parse(url)?; | ||
| let pairs: Vec<_> = connection | ||
| .url | ||
| .query_pairs() | ||
| .filter(|(key, _)| key != "database") | ||
| .map(|(key, value)| (key.into_owned(), value.into_owned())) | ||
| .collect(); | ||
| connection | ||
| .url | ||
| .query_pairs_mut() | ||
| .clear() | ||
| .extend_pairs(pairs) | ||
| .append_pair("database", database); | ||
| Ok(connection) | ||
| } | ||
|
|
||
| pub fn url(&self) -> &Url { | ||
| &self.url | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone)] | ||
| pub struct Storage { | ||
| database: String, | ||
| writer: Connection, | ||
| reader: Option<Connection>, | ||
| } | ||
|
|
||
| impl Storage { | ||
| pub fn new(database: String, url: &str, reader_url: Option<&str>) -> Result<Self, Error> { | ||
| if !valid_identifier(&database) { | ||
| return Err(Error::InvalidSchema); | ||
| } | ||
| Ok(Self { | ||
| writer: Connection::writer(url)?, | ||
| reader: reader_url | ||
| .map(|value| Connection::reader(value, &database)) | ||
| .transpose()?, | ||
| database, | ||
| }) | ||
| } | ||
|
|
||
| pub fn database(&self) -> &str { | ||
| &self.database | ||
| } | ||
|
|
||
| pub fn writer(&self) -> &Connection { | ||
| &self.writer | ||
| } | ||
|
|
||
| pub fn reader(&self) -> Option<&Connection> { | ||
| self.reader.as_ref() | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn valid_identifier(value: &str) -> bool { | ||
| !value.is_empty() | ||
| && value | ||
| .bytes() | ||
| .all(|c| c.is_ascii_alphanumeric() || c == b'_') | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.