1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! StatsD collector with bounded, sequential batch handling and no reply traffic.
pub mod actions;
pub mod codec;
use crate::protocol::{Event, SpawnContext};
use crate::server::connection::ConnectionId;
use crate::state::{
server::{ConnectionState, ConnectionStatus, ProtocolConnectionInfo},
AccessLogOwner,
};
use crate::{console_debug, console_error, console_info};
use anyhow::Result;
use codec::{Dialect, DEFAULT_DIALECT, DEFAULT_LLM_FALLBACK, MAX_DATAGRAM_BYTES};
use serde_json::json;
use tokio::net::UdpSocket;
pub struct StatsdServer;
impl StatsdServer {
pub async fn spawn(ctx: SpawnContext) -> Result<std::net::SocketAddr> {
let dialect = Dialect::parse(
&ctx.startup_params
.as_ref()
.map(|p| p.get_optional_string("dialect"))
.transpose()?
.flatten()
.unwrap_or_else(|| DEFAULT_DIALECT.into()),
)?;
let llm_fallback = ctx
.startup_params
.as_ref()
.map(|p| p.get_optional_bool("llm_fallback"))
.transpose()?
.flatten()
.unwrap_or(DEFAULT_LLM_FALLBACK);
let socket = UdpSocket::bind(ctx.legacy_listen_addr()).await?;
let local_addr = socket.local_addr()?;
console_info!(
ctx.status_tx,
"StatsD listening on {} ({}, llm_fallback={})",
local_addr,
dialect.as_str(),
llm_fallback
);
let registrar = ctx.state.clone();
let server_id = ctx.server_id;
let handle = tokio::spawn(async move {
// +1 detects a truncated datagram: anything above the cap is rejected,
// even if its prefix would be a valid message. No unbounded task queue.
let mut buffer = vec![0u8; MAX_DATAGRAM_BYTES + 1];
let protocol = actions::StatsdProtocol::new();
loop {
let (n, peer) = match socket.recv_from(&mut buffer).await {
Ok(received) => received,
Err(error) => {
console_error!(ctx.status_tx, "StatsD receive failed: {}", error);
break;
}
};
let records = match codec::parse_datagram(&buffer[..n], dialect) {
Ok(records) => records,
Err(error) => {
console_debug!(
ctx.status_tx,
"StatsD rejected datagram from {}: {}",
peer,
error
);
ctx.state
.record_access_log(
AccessLogOwner::Server(server_id.as_u32()),
"StatsD",
None,
"statsd_invalid_datagram",
json!({"source_addr":peer.to_string(),"received_bytes":n}),
vec![json!({"error":error.to_string()})],
)
.await;
continue;
}
};
// Only the batch currently being handled needs a peer row. Remove it
// afterward rather than accumulate one connection per telemetry packet.
let id = ConnectionId::new(ctx.state.get_next_unified_id().await);
let now = crate::utils::clock::Instant::now();
ctx.state
.add_connection_to_server(
server_id,
ConnectionState {
id,
remote_addr: peer,
local_addr,
bytes_sent: 0,
bytes_received: n as u64,
packets_sent: 0,
packets_received: 1,
last_activity: now,
status: ConnectionStatus::Active,
status_changed_at: now,
protocol_info: ProtocolConnectionInfo::empty(),
},
)
.await;
let event = Event::new(
&actions::STATSD_BATCH_EVENT,
json!({"records":records,"record_count":records.len(),"source_addr":peer.to_string(),"dialect":dialect.as_str()}),
);
let configured = ctx
.state
.get_event_handler_config(server_id)
.await
.is_some_and(|config| config.find_handler("statsd_batch").is_some());
if llm_fallback || configured {
match crate::llm::action_helper::call_llm(
&ctx.llm_client,
&ctx.state,
server_id,
Some(id),
&event,
&protocol,
)
.await
{
Ok(result) => {
for message in result.messages {
console_info!(ctx.status_tx, "{}", message);
}
}
Err(error) => {
console_error!(
ctx.status_tx,
"StatsD decision=fail_closed_handler_error from {}: {}",
peer,
error
);
}
}
} else {
ctx.state
.record_access_log(
AccessLogOwner::Server(server_id.as_u32()),
"StatsD",
Some(id.as_u32()),
"statsd_batch",
event.data,
vec![json!({"type":"collect_statsd_batch"})],
)
.await;
}
ctx.state.remove_connection_from_server(server_id, id).await;
let _ = ctx.status_tx.send("__UPDATE_UI__".into());
}
});
registrar.register_server_task(server_id, handle).await;
Ok(local_addr)
}
}