Skip to content

Commit

Permalink
limit spans returned by traces list endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mellowagain committed Feb 19, 2025
1 parent 29feb50 commit 430687f
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions otel-worker-cli/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ impl Store for LibsqlStore {
async fn traces_list(
&self,
_tx: &Transaction,
// Future improvement could hold sort fields, limits, etc
limit: Option<u32>, // Future improvement could hold sort fields, limits, etc
) -> Result<Vec<otel_worker_core::data::models::Trace>> {
let traces = self
.connection
.query(&self.sql_builder.traces_list(None), ())
.query(&self.sql_builder.traces_list(limit), ())
.await?
.fetch_all()
.await?;
Expand Down
7 changes: 4 additions & 3 deletions otel-worker-core/src/api/handlers/traces.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::api::errors::{ApiServerError, CommonError};
use crate::api::models::TraceSummary;
use crate::data::models::HexEncodedId;
use crate::data::models::{HexEncodedId, QueryParams};
use crate::data::{BoxedStore, DbError};
use axum::extract::{Path, State};
use axum::extract::{Path, Query, State};
use axum::Json;
use http::StatusCode;
use otel_worker_macros::ApiError;
Expand All @@ -13,10 +13,11 @@ use tracing::error;
#[tracing::instrument(skip_all)]
pub async fn traces_list_handler(
State(store): State<BoxedStore>,
Query(QueryParams { limit }): Query<QueryParams>,
) -> Result<Json<Vec<TraceSummary>>, ApiServerError<TraceListError>> {
let tx = store.start_readonly_transaction().await?;

let traces = store.traces_list(&tx).await?;
let traces = store.traces_list(&tx, limit).await?;

let mut result = Vec::with_capacity(20);

Expand Down
2 changes: 1 addition & 1 deletion otel-worker-core/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub trait Store: Send + Sync {
async fn traces_list(
&self,
tx: &Transaction,
// Future improvement could hold sort fields, limits, etc
limit: Option<u32>, // Future improvement could hold sort fields, limits, etc
) -> Result<Vec<models::Trace>>;

/// Delete all spans with a specific trace_id.
Expand Down
5 changes: 5 additions & 0 deletions otel-worker-core/src/data/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ impl From<api::models::Span> for Span {
}
}

#[derive(Deserialize)]
pub struct QueryParams {
limit: Option<u32>,
}

#[derive(Clone, Debug, Serialize, PartialEq)]
pub struct HexEncodedId(String);

Expand Down
7 changes: 5 additions & 2 deletions otel-worker-core/src/data/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ impl SqlBuilder {
///
/// Query parameters:
/// - $1: trace_id
pub fn span_list_by_trace(&self) -> String {
String::from("SELECT * FROM spans WHERE trace_id=$1")
pub fn span_list_by_trace(&self, limit: Option<u32>) -> String {
format!(
"SELECT * FROM spans WHERE trace_id=$1 LIMIT {}",
limit.unwrap_or(100)
)
}

/// Create a new span.
Expand Down
4 changes: 3 additions & 1 deletion otel-worker/package.json
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"packageManager": "pnpm@9.12.3+sha512.cce0f9de9c5a7c95bef944169cc5dfe8741abfb145078c0d508b868056848a87c81e626246cb60967cbd7fd29a6c062ef73ff840d96b3c86c40ac92cf4a813ee"
}
4 changes: 2 additions & 2 deletions otel-worker/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ impl Store for D1Store {
async fn traces_list(
&self,
_tx: &Transaction,
// Future improvement could hold sort fields, limits, etc
limit: Option<u32>, // Future improvement could hold sort fields, limits, etc
) -> Result<Vec<models::Trace>> {
SendFuture::new(async {
let traces = self
.fetch_all(self.sql_builder.traces_list(None), &[])
.fetch_all(self.sql_builder.traces_list(limit), &[])
.await?;

Ok(traces)
Expand Down

0 comments on commit 430687f

Please sign in to comment.