Emit tracing inside record_event and drop duplicate feeder match
record_event now logs at the matching tracing level (error/warn/info) using the persisted message — giving every app uniform event logs for free. Feeder's handle_control_event collapses from a 60-line match (which just duplicated the persisted message with less-readable UUIDs) to a single if-let for UnitStateChanged, which is the only AppEvent that is intentionally not persisted. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
parent
1c646dfaa7
commit
3e0d4c242b
|
|
@ -92,71 +92,19 @@ async fn handle_control_event(
|
|||
pool: &sqlx::PgPool,
|
||||
ws_manager: Option<&std::sync::Arc<WebSocketManager>>,
|
||||
) {
|
||||
persist_event_if_needed(&event, pool, ws_manager).await;
|
||||
|
||||
match event {
|
||||
AppEvent::EquipmentStartCommandSent {
|
||||
equipment_id,
|
||||
unit_id,
|
||||
point_id,
|
||||
} => {
|
||||
tracing::info!(
|
||||
"Equipment start command sent: equipment={}, unit={:?}, point={}",
|
||||
equipment_id,
|
||||
unit_id,
|
||||
point_id
|
||||
);
|
||||
}
|
||||
AppEvent::EquipmentStopCommandSent {
|
||||
equipment_id,
|
||||
unit_id,
|
||||
point_id,
|
||||
} => {
|
||||
tracing::info!(
|
||||
"Equipment stop command sent: equipment={}, unit={:?}, point={}",
|
||||
equipment_id,
|
||||
unit_id,
|
||||
point_id
|
||||
);
|
||||
}
|
||||
AppEvent::AutoControlStarted { unit_id } => {
|
||||
tracing::info!("Auto control started for unit {}", unit_id);
|
||||
}
|
||||
AppEvent::AutoControlStopped { unit_id } => {
|
||||
tracing::info!("Auto control stopped for unit {}", unit_id);
|
||||
}
|
||||
AppEvent::FaultLocked {
|
||||
unit_id,
|
||||
equipment_id,
|
||||
} => {
|
||||
tracing::warn!("Fault locked: unit={}, equipment={}", unit_id, equipment_id);
|
||||
}
|
||||
AppEvent::FaultAcked { unit_id } => {
|
||||
tracing::info!("Fault acked for unit {}", unit_id);
|
||||
}
|
||||
AppEvent::CommLocked { unit_id } => {
|
||||
tracing::warn!("Comm locked for unit {}", unit_id);
|
||||
}
|
||||
AppEvent::CommRecovered { unit_id } => {
|
||||
tracing::info!("Comm recovered for unit {}", unit_id);
|
||||
}
|
||||
AppEvent::RemLocal {
|
||||
unit_id,
|
||||
equipment_id,
|
||||
} => {
|
||||
tracing::warn!("REM local: unit={}, equipment={}", unit_id, equipment_id);
|
||||
}
|
||||
AppEvent::RemRecovered { unit_id } => {
|
||||
tracing::info!("REM recovered for unit {}", unit_id);
|
||||
}
|
||||
AppEvent::UnitStateChanged {
|
||||
unit_id,
|
||||
from_state,
|
||||
to_state,
|
||||
} => {
|
||||
tracing::info!("Unit {} state: {} -> {}", unit_id, from_state, to_state);
|
||||
}
|
||||
// UnitStateChanged is high-frequency and intentionally not persisted;
|
||||
// it still needs tracing for local observability. All other events are
|
||||
// persisted via record_event, which emits tracing automatically.
|
||||
if let AppEvent::UnitStateChanged {
|
||||
unit_id,
|
||||
from_state,
|
||||
to_state,
|
||||
} = &event
|
||||
{
|
||||
tracing::info!("Unit {} state: {} -> {}", unit_id, from_state, to_state);
|
||||
}
|
||||
|
||||
persist_event_if_needed(&event, pool, ws_manager).await;
|
||||
}
|
||||
|
||||
async fn fetch_unit_code(pool: &sqlx::PgPool, id: Uuid) -> String {
|
||||
|
|
|
|||
|
|
@ -64,6 +64,13 @@ pub async fn record_event(
|
|||
event: EventInsert,
|
||||
) {
|
||||
let event_type = event.event_type;
|
||||
|
||||
match event.level {
|
||||
"error" => tracing::error!(event_type, "{}", event.message),
|
||||
"warn" => tracing::warn!(event_type, "{}", event.message),
|
||||
_ => tracing::info!(event_type, "{}", event.message),
|
||||
}
|
||||
|
||||
let envelope = EventEnvelope::new(event_type, event.payload);
|
||||
|
||||
let inserted = sqlx::query_as::<_, EventRecord>(
|
||||
|
|
|
|||
Loading…
Reference in New Issue