Skip to content
Merged
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
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ members = [
"integrations/nvtx/example",
"integrations/nvtx/analyzer",
"integrations/nvtx/ui",
"integrations/nvtx/server",
# Domain-specific crates
"domains/query_engine/analyzer",
"domains/query_engine/model",
Expand Down
53 changes: 48 additions & 5 deletions crates/io/src/filesystem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ impl TryFrom<&str> for Format {
}

impl Format {
/// Detect the format of a context directory from the first recognized
/// `*.<ext>` event stream in any of its per-entity subdirectories. Returns
/// `None` if no readable stream with a known extension is present.
/// Detect the common format of a context's event streams. Returns `None` if
/// no recognized stream is present or streams use different formats.
pub fn detect(context_dir: &std::path::Path) -> Option<Self> {
let mut detected = None;
for entry in std::fs::read_dir(context_dir).ok()?.flatten() {
if !entry.file_type().map(|t| t.is_dir()).unwrap_or(false) {
continue;
Expand All @@ -48,10 +48,53 @@ impl Format {
.and_then(|ext| ext.to_str())
.and_then(|ext| Self::try_from(ext).ok())
{
return Some(format);
match detected {
Some(existing) if existing != format => return None,
None => detected = Some(format),
_ => {}
}
}
}
}
None
detected
}
}

#[cfg(all(test, feature = "ndjson"))]
mod tests {
use std::path::PathBuf;

use super::*;
use uuid::Uuid;

fn context_with_streams(streams: &[(&str, &str)]) -> PathBuf {
let context = std::env::temp_dir().join(format!("quent-format-{}", Uuid::now_v7()));
for &(entity, extension) in streams {
let stream_dir = context.join(entity);
std::fs::create_dir_all(&stream_dir).unwrap();
std::fs::write(stream_dir.join(format!("events.{extension}")), []).unwrap();
}
context
}

#[test]
fn detects_one_context_wide_format() {
let context =
context_with_streams(&[("EngineEvent", "ndjson"), ("NvtxEventEntity", "ndjson")]);
let detected = Format::detect(&context);
std::fs::remove_dir_all(context).unwrap();

assert_eq!(detected, Some(Format::Ndjson));
}

#[test]
#[cfg(feature = "msgpack")]
fn rejects_mixed_context_formats() {
let context =
context_with_streams(&[("EngineEvent", "ndjson"), ("NvtxEventEntity", "msgpack")]);
let detected = Format::detect(&context);
std::fs::remove_dir_all(context).unwrap();

assert_eq!(detected, None);
}
}
85 changes: 82 additions & 3 deletions domains/query_engine/server/src/analyzer_cache.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

use std::collections::{BTreeSet, HashMap, HashSet};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::path::Path;
use std::{sync::Arc, time::Duration};

Expand Down Expand Up @@ -42,6 +42,8 @@ pub struct EngineIndex {
contexts: HashMap<Uuid, BTreeSet<Uuid>>,
/// Engine id to its workers and the context each was found in.
workers: HashMap<Uuid, Vec<(Uuid, Uuid)>>,
/// Resource-group entities observed in each context, by engine ID.
context_resources: HashMap<Uuid, BTreeMap<Uuid, BTreeSet<Uuid>>>,
}

impl EngineIndex {
Expand All @@ -53,13 +55,23 @@ impl EngineIndex {
}

fn add_worker(&mut self, engine_id: Uuid, worker_id: Uuid, context_id: Uuid) {
self.attribute_context(engine_id, context_id);
self.attribute_resource(engine_id, context_id, worker_id);
self.workers
.entry(engine_id)
.or_default()
.push((worker_id, context_id));
}

fn attribute_resource(&mut self, engine_id: Uuid, context_id: Uuid, resource_id: Uuid) {
self.attribute_context(engine_id, context_id);
self.context_resources
.entry(engine_id)
.or_default()
.entry(context_id)
.or_default()
.insert(resource_id);
}

/// All known engine ids.
pub fn engine_ids(&self) -> impl Iterator<Item = Uuid> + '_ {
self.contexts.keys().copied()
Expand All @@ -78,6 +90,21 @@ impl EngineIndex {
pub fn workers_of(&self, engine_id: Uuid) -> &[(Uuid, Uuid)] {
self.workers.get(&engine_id).map_or(&[], Vec::as_slice)
}

/// Resource-group entity IDs observed in each context of `engine_id`.
pub fn context_resources_of(&self, engine_id: Uuid) -> BTreeMap<Uuid, Vec<Uuid>> {
let mut resources: BTreeMap<Uuid, Vec<Uuid>> = self
.contexts_of(engine_id)
.into_iter()
.map(|context_id| (context_id, Vec::new()))
.collect();
if let Some(by_context) = self.context_resources.get(&engine_id) {
for (&context_id, resource_ids) in by_context {
resources.insert(context_id, resource_ids.iter().copied().collect());
}
}
resources
}
}

/// A dumb lister for query-engine-domain models: scan every `<output_dir>/<ctx>/`
Expand Down Expand Up @@ -116,7 +143,7 @@ pub fn index_query_engines(output_dir: &Path) -> ServerResult<EngineIndex> {
for event in importer {
let event = event?;
if seen.insert(event.id) {
index.attribute_context(event.id, context_id);
index.attribute_resource(event.id, context_id, event.id);
}
}
}
Expand Down Expand Up @@ -202,6 +229,20 @@ where
Ok((self.lister)()?.engine_ids().collect())
}

/// List an engine's contributing contexts without blocking the async executor.
pub(crate) async fn contexts(&self, engine_id: Uuid) -> ServerResult<ui::EngineContexts> {
let lister = Arc::clone(&self.lister);
tokio::task::spawn_blocking(move || {
let index = lister()?;
Ok(ui::EngineContexts {
engine_id,
context_resources: index.context_resources_of(engine_id),
})
})
.await
.map_err(|error| ServerError::Cache(format!("blocking task panicked: {error}")))?
}

pub(crate) async fn list_with_metadata(&self) -> ServerResult<Vec<ui::Engine>> {
let lister = Arc::clone(&self.lister);
let importer = Arc::clone(&self.importer);
Expand Down Expand Up @@ -241,3 +282,41 @@ where
.map_err(|e: Arc<ServerError>| ServerError::Cache(format!("{e:?}")))
}
}

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

#[test]
fn engine_context_inventory_is_deduplicated_and_sorted() {
let engine_id = Uuid::from_u128(1);
let earlier = Uuid::from_u128(2);
let later = Uuid::from_u128(3);
let mut index = EngineIndex::default();
index.attribute_context(engine_id, later);
index.attribute_context(engine_id, earlier);
index.attribute_context(engine_id, later);

assert_eq!(index.contexts_of(engine_id), vec![earlier, later]);
assert!(index.contexts_of(Uuid::from_u128(4)).is_empty());
}

#[test]
fn context_inventory_retains_resource_ownership() {
let engine_id = Uuid::from_u128(1);
let engine_context = Uuid::from_u128(2);
let worker_context = Uuid::from_u128(3);
let worker_id = Uuid::from_u128(4);
let mut index = EngineIndex::default();
index.attribute_resource(engine_id, engine_context, engine_id);
index.add_worker(engine_id, worker_id, worker_context);

assert_eq!(
index.context_resources_of(engine_id),
BTreeMap::from([
(engine_context, vec![engine_id]),
(worker_context, vec![worker_id]),
])
);
}
}
19 changes: 18 additions & 1 deletion domains/query_engine/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ pub fn analyzer_service_router<A>(
lister: Box<analyzer_cache::ListerFn>,
cors: Option<String>,
) -> Result<AxumRouter, Box<dyn std::error::Error>>
where
A: UiAnalyzer + Send + Sync + 'static,
<A as UiAnalyzer>::EntityRef: serde::Serialize,
{
analyzer_service_router_with_routes::<A>(importer, lister, cors, AxumRouter::new())
}

/// Build the analyzer router and merge integration-owned routes before common
/// CORS and embedded-UI fallback layers are installed.
pub fn analyzer_service_router_with_routes<A>(
importer: Box<analyzer_cache::ImporterFn<A>>,
lister: Box<analyzer_cache::ListerFn>,
cors: Option<String>,
additional_routes: AxumRouter,
) -> Result<AxumRouter, Box<dyn std::error::Error>>
where
A: UiAnalyzer + Send + Sync + 'static,
<A as UiAnalyzer>::EntityRef: serde::Serialize,
Expand All @@ -64,7 +79,9 @@ where
timelines: TimelineCache::new(),
};

let mut http_routes = axum::Router::new().nest("/api/engines", ui::routes(state));
let mut http_routes = axum::Router::new()
.nest("/api/engines", ui::routes(state))
.merge(additional_routes);

#[cfg(feature = "swagger")]
{
Expand Down
29 changes: 29 additions & 0 deletions domains/query_engine/server/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,33 @@ where
Ok(Json(analyzer.query_engine_model().engine()?.to_ui()?))
}

/// List every Quent context attributed to an engine.
#[cfg_attr(feature = "swagger", utoipa::path(
get,
path = "/api/engines/{engine_id}/contexts",
tag = "engines",
params(
("engine_id" = Uuid, Path, description = "The engine ID")
),
responses(
(status = 200, description = "Contexts contributing telemetry to the engine", body = Object)
)
))]
#[tracing::instrument(skip_all, err)]
async fn engine_contexts<A>(
State(state): State<ServiceState<A>>,
Path(engine_id): Path<Uuid>,
) -> ServerResult<Json<ui::EngineContexts>>
where
A: UiAnalyzer + Send + Sync + 'static,
{
let contexts = state.analyzers.contexts(engine_id).await.map_err(|error| {
tracing::error!(%error, %engine_id, "engine context inventory failed");
crate::error::ServerError::Cache("engine context inventory could not be loaded".to_owned())
})?;
Ok(Json(contexts))
}

// TODO(johanpel): pagination
/// List all query groups for a given engine.
#[cfg_attr(feature = "swagger", utoipa::path(
Expand Down Expand Up @@ -329,6 +356,7 @@ where
paths(
list_engines,
engine,
engine_contexts,
list_query_groups,
list_queries,
query,
Expand All @@ -353,6 +381,7 @@ where
Router::new()
.route("/", get(list_engines))
.route("/{engine_id}", get(engine))
.route("/{engine_id}/contexts", get(engine_contexts))
.route("/{engine_id}/query-groups", get(list_query_groups))
.route(
"/{engine_id}/query_group/{query_group_id}/queries",
Expand Down
13 changes: 12 additions & 1 deletion domains/query_engine/ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,21 @@ use quent_ui::{
quantity::QuantitySpec,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::collections::{BTreeMap, HashMap};
use ts_rs::TS;
use uuid::Uuid;

/// Quent contexts whose streams contribute to one engine view.
///
/// This is intentionally an inventory only. It does not claim that every
/// context contains a particular query or NVTX stream.
#[derive(TS, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EngineContexts {
pub engine_id: Uuid,
/// Resource-group entities observed in each context, keyed by context ID.
pub context_resources: BTreeMap<Uuid, Vec<Uuid>>,
}

/// Global timeline-request parameter identifying the query to report on.
#[derive(TS, Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct QueryFilter {
Expand Down
31 changes: 31 additions & 0 deletions integrations/nvtx/server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

[package]
name = "nvtx-server"
version.workspace = true
edition.workspace = true
publish.workspace = true

[features]
default = []
real-capture-tests = ["nvtx-analyzer/real-capture-tests"]

[dependencies]
axum = { version = "0.8.7" }
moka = { version = "0.12.13", features = ["future", "sync"] }
nvtx-analyzer = { path = "../analyzer" }
nvtx-bridge = { path = "../bridge" }
nvtx-ui = { path = "../ui" }
quent-events = { path = "../../../crates/events" }
quent-io = { path = "../../../crates/io" }
serde.workspace = true
tokio = { workspace = true, features = ["rt-multi-thread", "sync"] }
tracing.workspace = true
uuid.workspace = true

[dev-dependencies]
nvtx-events = { path = "../events" }
serde_json.workspace = true
tempfile = "3"
tower = { version = "0.5", features = ["util"] }
Loading