Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion router/src/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,25 @@ use text_embeddings_core::infer::{
use text_embeddings_core::tokenization::{into_tokens, SimpleToken as CoreSimpleToken};
use text_embeddings_core::TextEmbeddingsError;
use tokio::sync::OwnedSemaphorePermit;
use std::sync::OnceLock;
use tower_http::cors::{AllowOrigin, CorsLayer};
use tracing::instrument;
use tracing_opentelemetry::OpenTelemetrySpanExt;
use utoipa::OpenApi;
use utoipa_swagger_ui::SwaggerUi;

static OTEL_EXCLUDED_PATHS: OnceLock<Vec<String>> = OnceLock::new();

fn is_path_excluded(path: &str, excluded: &[String]) -> bool {
excluded.iter().any(|p| p == path)
}

fn path_filter(path: &str) -> bool {
OTEL_EXCLUDED_PATHS
.get()
.map_or(true, |excluded| !is_path_excluded(path, excluded))
}

///Text Embeddings Inference endpoint info
#[utoipa::path(
get,
Expand Down Expand Up @@ -1632,6 +1645,7 @@ pub async fn run(
payload_limit: usize,
api_key: Option<String>,
cors_allow_origin: Option<Vec<String>>,
otlp_tracing_filter: Option<Vec<String>>,
) -> Result<(), anyhow::Error> {
// OpenAPI documentation
#[derive(OpenApi)]
Expand Down Expand Up @@ -1858,14 +1872,21 @@ pub async fn run(
routes = routes.layer(axum::middleware::from_fn(auth));
}

let otel_layer = if let Some(excluded_paths) = otlp_tracing_filter {
OTEL_EXCLUDED_PATHS.set(excluded_paths).ok();
OtelAxumLayer::default().filter(path_filter)
} else {
OtelAxumLayer::default()
};

let app = Router::new()
.merge(SwaggerUi::new("/docs").url("/api-doc/openapi.json", doc))
.merge(routes)
.merge(public_routes)
.layer(Extension(infer))
.layer(Extension(info))
.layer(Extension(prom_handle.clone()))
.layer(OtelAxumLayer::default())
.layer(otel_layer)
.layer(axum::middleware::from_fn(
logging::http::trace_context_middleware,
))
Expand Down Expand Up @@ -1932,3 +1953,43 @@ impl From<serde_json::Error> for ErrorResponse {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

fn paths(strs: &[&str]) -> Vec<String> {
strs.iter().map(|s| s.to_string()).collect()
}

#[test]
fn path_filter_allows_all_when_no_exclusions() {
let excluded: Vec<String> = vec![];
assert!(!is_path_excluded("/health", &excluded));
assert!(!is_path_excluded("/metrics", &excluded));
assert!(!is_path_excluded("/embed", &excluded));
}

#[test]
fn path_filter_blocks_excluded_paths() {
let excluded = paths(&["/health", "/metrics", "/ping"]);
assert!(is_path_excluded("/health", &excluded), "/health should be excluded");
assert!(is_path_excluded("/metrics", &excluded), "/metrics should be excluded");
assert!(is_path_excluded("/ping", &excluded), "/ping should be excluded");
}

#[test]
fn path_filter_allows_non_excluded_paths() {
let excluded = paths(&["/health", "/metrics", "/ping"]);
assert!(!is_path_excluded("/embed", &excluded), "/embed should not be excluded");
assert!(!is_path_excluded("/info", &excluded), "/info should not be excluded");
assert!(!is_path_excluded("/rerank", &excluded), "/rerank should not be excluded");
}

#[test]
fn path_filter_exact_match_only() {
let excluded = paths(&["/health"]);
assert!(!is_path_excluded("/healthz", &excluded), "/healthz should not be excluded");
assert!(!is_path_excluded("/health/check", &excluded), "/health/check should not be excluded");
}
}
2 changes: 2 additions & 0 deletions router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub async fn run(
otlp_service_name: String,
prometheus_port: u16,
cors_allow_origin: Option<Vec<String>>,
otlp_tracing_filter: Option<Vec<String>>,
) -> Result<()> {
let model_id_path = Path::new(&model_id);
let (model_root, api_repo) = if model_id_path.exists() && model_id_path.is_dir() {
Expand Down Expand Up @@ -377,6 +378,7 @@ pub async fn run(
payload_limit,
api_key,
cors_allow_origin,
otlp_tracing_filter,
)
.await
}
Expand Down
7 changes: 7 additions & 0 deletions router/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ struct Args {
#[clap(default_value = "text-embeddings-inference.server", long, env)]
otlp_service_name: String,

/// HTTP paths to exclude from OpenTelemetry tracing.
/// Can be specified multiple times or as a comma-separated list.
/// e.g. `--otlp-tracing-filter /health --otlp-tracing-filter /metrics`
#[clap(long, env, value_delimiter = ',')]
otlp_tracing_filter: Option<Vec<String>>,

/// The Prometheus port to listen on.
#[clap(default_value = "9000", long, env)]
prometheus_port: u16,
Expand Down Expand Up @@ -266,6 +272,7 @@ async fn main() -> Result<()> {
args.otlp_service_name,
args.prometheus_port,
args.cors_allow_origin,
args.otlp_tracing_filter,
)
.await?;

Expand Down
1 change: 1 addition & 0 deletions router/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub async fn start_server(model_id: String, revision: Option<String>, dtype: DTy
"text-embeddings-inference.server".to_owned(),
9000,
None,
None,
)
});

Expand Down