Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(agent-harness): add HTTP request observability (#5918)

Wolf Mermelstein9608a01b476e
diff --git a/Cargo.lock b/Cargo.lock
index 5204f42e93..5269c97fd3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -272,6 +272,7 @@ dependencies = [
"macro_event_broker",
"macro_queues",
"macro_service_urls",
+ "macro_tower_layers",
"macro_user_id",
"macro_uuid",
"notification",
@@ -9595,6 +9596,7 @@ dependencies = [
name = "macro_tower_layers"
version = "0.1.0"
dependencies = [
+ "axum",
"http 1.4.0",
"opentelemetry",
"opentelemetry-http",
diff --git a/crates/agent_session/src/inbound/axum_router.rs b/crates/agent_session/src/inbound/axum_router.rs
index 10e8d18b5c..4fa1c675d8 100644
--- a/crates/agent_session/src/inbound/axum_router.rs
+++ b/crates/agent_session/src/inbound/axum_router.rs
@@ -252,7 +252,6 @@ impl IntoResponse for AgentSessionApiError {
if let AgentSessionError::InvalidName(message) = error {
return (StatusCode::BAD_REQUEST, Json(message)).into_response();
}
- tracing::error!(error = ?error, "agent session request failed");
(StatusCode::INTERNAL_SERVER_ERROR, "internal server error").into_response()
}
}
@@ -471,7 +470,11 @@ pub async fn rename_agent_session_handler<
/// Perform a control operation on a live agent session.
#[tracing::instrument(
skip_all,
- fields(actor = %caller.acting_entity(), session_id = %session_id),
+ fields(
+ actor = %caller.acting_entity(),
+ session_id = %session_id,
+ agent.action.name = req.action.as_ref(),
+ ),
err(Debug)
)]
pub async fn control_agent_session_handler<
diff --git a/crates/macro_tower_layers/Cargo.toml b/crates/macro_tower_layers/Cargo.toml
index 9009e24a8b..9e14d78413 100644
--- a/crates/macro_tower_layers/Cargo.toml
+++ b/crates/macro_tower_layers/Cargo.toml
@@ -5,6 +5,7 @@ publish = false
version = "0.1.0"
[dependencies]
+axum = { workspace = true }
http = { workspace = true }
opentelemetry = { workspace = true, features = ["trace"] }
opentelemetry-http = { workspace = true }
diff --git a/crates/macro_tower_layers/src/lib.rs b/crates/macro_tower_layers/src/lib.rs
index 009577f384..e03a3ec338 100644
--- a/crates/macro_tower_layers/src/lib.rs
+++ b/crates/macro_tower_layers/src/lib.rs
@@ -11,6 +11,7 @@ use std::{
time::Duration,
};
+use axum::extract::MatchedPath;
use http::{HeaderValue, Request, Response};
use opentelemetry::trace::TraceContextExt;
use tokio::time::MissedTickBehavior;
@@ -107,19 +108,23 @@ impl<B> MakeSpan<B> for MakeHttpRequestSpan {
.get("x-request-id")
.and_then(|value| value.to_str().ok())
.unwrap_or_default();
-
- tracing::info_span!(
+ let span = tracing::info_span!(
target: HTTP_REQUEST_SPAN_TARGET,
"http.request",
otel.kind = "server",
"http.request.method" = %request.method(),
+ "http.route" = tracing::field::Empty,
"url.path" = request.uri().path(),
"request.id" = request_id,
"http.response.status_code" = tracing::field::Empty,
latency_ms = tracing::field::Empty,
otel.status_code = tracing::field::Empty,
otel.status_description = tracing::field::Empty,
- )
+ );
+ if let Some(route) = request.extensions().get::<MatchedPath>() {
+ span.record("http.route", route.as_str());
+ }
+ span
}
}
diff --git a/crates/macro_tower_layers/src/test.rs b/crates/macro_tower_layers/src/test.rs
index 655b5a9ea7..edbe59c8b8 100644
--- a/crates/macro_tower_layers/src/test.rs
+++ b/crates/macro_tower_layers/src/test.rs
@@ -1,8 +1,10 @@
use super::*;
+use axum::{Router, body::Body, routing::get};
use std::{
collections::{HashMap, HashSet},
sync::{Arc, Mutex},
};
+use tower::ServiceExt;
use tower_http::trace::{MakeSpan, OnFailure, OnResponse};
use tracing::{
Level,
@@ -120,6 +122,7 @@ fn request_span_uses_info_and_safe_structured_fields() {
let declared_fields = captured.declared_span_fields.lock().unwrap();
assert!(declared_fields.contains("http.request.method"));
+ assert!(declared_fields.contains("http.route"));
assert!(declared_fields.contains("url.path"));
assert!(declared_fields.contains("request.id"));
assert!(declared_fields.contains("http.response.status_code"));
@@ -134,6 +137,34 @@ fn request_span_uses_info_and_safe_structured_fields() {
assert!(fields.values().all(|value| !value.contains("secret")));
}
+#[tokio::test]
+async fn request_span_records_the_matched_route() {
+ let (subscriber, captured) = TracingCapture::new();
+ let _guard = set_default(subscriber);
+ let app = Router::new()
+ .route("/documents/{id}", get(|| async {}))
+ .layer(MacroRequestIdAndTracingLayer::new(Duration::from_millis(200)).into_inner());
+
+ app.oneshot(
+ Request::builder()
+ .uri("/documents/123")
+ .body(Body::empty())
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(
+ captured
+ .recorded_span_fields
+ .lock()
+ .unwrap()
+ .get("http.route")
+ .unwrap(),
+ "\"/documents/{id}\""
+ );
+}
+
#[test]
fn successful_response_records_telemetry_without_completion_event() {
let (subscriber, captured) = TracingCapture::new();
diff --git a/services/agent_harness_service/Cargo.toml b/services/agent_harness_service/Cargo.toml
index 25e1bf8ad3..0012da61fd 100644
--- a/services/agent_harness_service/Cargo.toml
+++ b/services/agent_harness_service/Cargo.toml
@@ -33,6 +33,7 @@ macro_event_broker = { path = "../../crates/macro_event_broker", features = ["ou
macro_queues = { path = "../../crates/macro_queues" }
lexical_client = { path = "../../crates/lexical_client" }
macro_service_urls = { path = "../../crates/macro_service_urls" }
+macro_tower_layers = { path = "../../crates/macro_tower_layers" }
notification = { path = "../../crates/notification" }
rdkafka = { workspace = true }
channels = { path = "../../crates/channels", features = ["ports", "outbound"] }
diff --git a/services/agent_harness_service/src/api.rs b/services/agent_harness_service/src/api.rs
index d62a3cd0d0..3daed36cb9 100644
--- a/services/agent_harness_service/src/api.rs
+++ b/services/agent_harness_service/src/api.rs
@@ -3,6 +3,8 @@
//! This process owns the complete agent-session HTTP API: durable metadata and
//! log reads as well as operations against the live in-memory transport.
+use std::time::Duration;
+
use agent_harness::inbound::runtime_gateway::{RuntimeGatewayState, runtime_gateway_router};
use agent_session::domain::ports::{
AgentSessionNotificationRecipient, BotDirectory, SessionOpener,
@@ -18,6 +20,7 @@ use axum::Router;
use axum::routing::get;
use entity_access::domain::ports::EntityAccessService;
use macro_authorization::MacroAuthorizationService;
+use macro_tower_layers::MacroRequestIdAndTracingLayer;
use utoipa::OpenApi;
use utoipa_swagger_ui::SwaggerUi;
@@ -41,6 +44,8 @@ where
Auth: MacroAuthorizationService,
{
let app = api_router(read_state, control_state, create_state, gateway_state)
+ .layer(MacroRequestIdAndTracingLayer::new(Duration::from_millis(200)).into_inner())
+ .merge(Router::new().route("/health", get(health)))
.layer(macro_cors::cors_layer())
.merge(SwaggerUi::new("/docs").url("/api-doc/openapi.json", swagger::ApiDoc::openapi()));
@@ -74,7 +79,6 @@ where
.merge(agent_session_control_router(control_state))
.merge(agent_session_create_router(create_state));
Router::new()
- .route("/health", get(health))
.nest("/agent-sessions", agent_sessions)
.merge(agent_sandbox_size_router(read_state))
.nest("/runtime", runtime_gateway_router(gateway_state))