Skip to content

Commit 6581f31

Browse files
authored
chore(trace-agent): clean state handling (#730)
# What? Cleans the trace agent state handling # Motivation Easier to read in specific routes # Tests Profiled an AWS Lambda in Python to ensure traces + profiles worked through different endpoint states
1 parent 55a3b5d commit 6581f31

1 file changed

Lines changed: 104 additions & 89 deletions

File tree

bottlecap/src/traces/trace_agent.rs

Lines changed: 104 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,28 @@ const STATS_PAYLOAD_CHANNEL_BUFFER_SIZE: usize = 10;
5555
pub const MAX_CONTENT_LENGTH: usize = 10 * 1024 * 1024;
5656
const LAMBDA_LOAD_SPAN: &str = "aws.lambda.load";
5757

58-
type AgentState = (
59-
Arc<config::Config>,
60-
Arc<dyn trace_processor::TraceProcessor + Send + Sync>,
61-
Sender<SendData>,
62-
Arc<dyn stats_processor::StatsProcessor + Send + Sync>,
63-
Sender<pb::ClientStatsPayload>,
64-
Arc<Mutex<InvocationProcessor>>,
65-
Arc<provider::Provider>,
66-
reqwest::Client,
67-
String, // api_key
68-
);
58+
#[derive(Clone)]
59+
pub struct TraceState {
60+
pub config: Arc<config::Config>,
61+
pub trace_processor: Arc<dyn trace_processor::TraceProcessor + Send + Sync>,
62+
pub trace_tx: Sender<SendData>,
63+
pub invocation_processor: Arc<Mutex<InvocationProcessor>>,
64+
pub tags_provider: Arc<provider::Provider>,
65+
}
66+
67+
#[derive(Clone)]
68+
pub struct StatsState {
69+
pub stats_processor: Arc<dyn stats_processor::StatsProcessor + Send + Sync>,
70+
pub stats_tx: Sender<pb::ClientStatsPayload>,
71+
}
72+
73+
#[derive(Clone)]
74+
pub struct ProxyState {
75+
pub config: Arc<config::Config>,
76+
pub tags_provider: Arc<provider::Provider>,
77+
pub http_client: reqwest::Client,
78+
pub api_key: String,
79+
}
6980

7081
pub struct TraceAgent {
7182
pub config: Arc<config::Config>,
@@ -168,19 +179,27 @@ impl TraceAgent {
168179
}
169180

170181
fn make_router(&self, stats_tx: Sender<pb::ClientStatsPayload>) -> Router {
171-
let state: AgentState = (
172-
Arc::clone(&self.config),
173-
Arc::clone(&self.trace_processor),
174-
self.tx.clone(),
175-
Arc::clone(&self.stats_processor),
182+
let trace_state = TraceState {
183+
config: Arc::clone(&self.config),
184+
trace_processor: Arc::clone(&self.trace_processor),
185+
trace_tx: self.tx.clone(),
186+
invocation_processor: Arc::clone(&self.invocation_processor),
187+
tags_provider: Arc::clone(&self.tags_provider),
188+
};
189+
190+
let stats_state = StatsState {
191+
stats_processor: Arc::clone(&self.stats_processor),
176192
stats_tx,
177-
Arc::clone(&self.invocation_processor),
178-
Arc::clone(&self.tags_provider),
179-
self.http_client.clone(),
180-
self.api_key.clone(),
181-
);
193+
};
182194

183-
Router::new()
195+
let proxy_state = ProxyState {
196+
config: Arc::clone(&self.config),
197+
tags_provider: Arc::clone(&self.tags_provider),
198+
http_client: self.http_client.clone(),
199+
api_key: self.api_key.clone(),
200+
};
201+
202+
let trace_router = Router::new()
184203
.route(
185204
V4_TRACE_ENDPOINT_PATH,
186205
post(Self::v04_traces).put(Self::v04_traces),
@@ -189,7 +208,13 @@ impl TraceAgent {
189208
V5_TRACE_ENDPOINT_PATH,
190209
post(Self::v05_traces).put(Self::v05_traces),
191210
)
211+
.with_state(trace_state);
212+
213+
let stats_router = Router::new()
192214
.route(STATS_ENDPOINT_PATH, post(Self::stats).put(Self::stats))
215+
.with_state(stats_state);
216+
217+
let proxy_router = Router::new()
193218
.route(DSM_AGENT_PATH, post(Self::dsm_proxy))
194219
.route(PROFILING_ENDPOINT_PATH, post(Self::profiling_proxy))
195220
.route(
@@ -202,53 +227,55 @@ impl TraceAgent {
202227
)
203228
.route(LLM_OBS_SPANS_ENDPOINT_PATH, post(Self::llm_obs_spans_proxy))
204229
.route(DEBUGGER_ENDPOINT_PATH, post(Self::debugger_logs_proxy))
205-
.route(INFO_ENDPOINT_PATH, any(Self::info))
230+
.with_state(proxy_state);
231+
232+
let info_router = Router::new().route(INFO_ENDPOINT_PATH, any(Self::info));
233+
234+
Router::new()
235+
.merge(trace_router)
236+
.merge(stats_router)
237+
.merge(proxy_router)
238+
.merge(info_router)
206239
.fallback(handler_not_found)
207-
.with_state(state)
208240
}
209241

210242
async fn graceful_shutdown(shutdown_token: CancellationToken) {
211243
shutdown_token.cancelled().await;
212244
debug!("Trace Agent | Shutdown signal received, shutting down");
213245
}
214246

215-
async fn v04_traces(
216-
State((config, trace_processor, trace_tx, _, _, invocation_processor, tags_provider, _, _)): State<AgentState>,
217-
request: Request,
218-
) -> Response {
247+
async fn v04_traces(State(state): State<TraceState>, request: Request) -> Response {
219248
Self::handle_traces(
220-
config,
249+
state.config,
221250
request,
222-
trace_processor,
223-
trace_tx,
224-
invocation_processor,
225-
tags_provider,
251+
state.trace_processor,
252+
state.trace_tx,
253+
state.invocation_processor,
254+
state.tags_provider,
226255
ApiVersion::V04,
227256
)
228257
.await
229258
}
230259

231-
async fn v05_traces(
232-
State((config, trace_processor, trace_tx, _, _, invocation_processor, tags_provider, _, _)): State<AgentState>,
233-
request: Request,
234-
) -> Response {
260+
async fn v05_traces(State(state): State<TraceState>, request: Request) -> Response {
235261
Self::handle_traces(
236-
config,
262+
state.config,
237263
request,
238-
trace_processor,
239-
trace_tx,
240-
invocation_processor,
241-
tags_provider,
264+
state.trace_processor,
265+
state.trace_tx,
266+
state.invocation_processor,
267+
state.tags_provider,
242268
ApiVersion::V05,
243269
)
244270
.await
245271
}
246272

247-
async fn stats(
248-
State((_, _, _, stats_processor, stats_tx, _, _, _, _)): State<AgentState>,
249-
request: Request,
250-
) -> Response {
251-
match stats_processor.process_stats(request, stats_tx).await {
273+
async fn stats(State(state): State<StatsState>, request: Request) -> Response {
274+
match state
275+
.stats_processor
276+
.process_stats(request, state.stats_tx)
277+
.await
278+
{
252279
Ok(result) => result.into_response(),
253280
Err(err) => error_response(
254281
StatusCode::INTERNAL_SERVER_ERROR,
@@ -257,15 +284,12 @@ impl TraceAgent {
257284
}
258285
}
259286

260-
async fn dsm_proxy(
261-
State((config, _, _, _, _, _, tags_provider, client, api_key)): State<AgentState>,
262-
request: Request,
263-
) -> Response {
287+
async fn dsm_proxy(State(state): State<ProxyState>, request: Request) -> Response {
264288
Self::handle_proxy(
265-
config,
266-
client,
267-
api_key,
268-
tags_provider,
289+
state.config,
290+
state.http_client,
291+
state.api_key,
292+
state.tags_provider,
269293
request,
270294
"trace.agent",
271295
DSM_ENDPOINT_PATH,
@@ -274,15 +298,12 @@ impl TraceAgent {
274298
.await
275299
}
276300

277-
async fn profiling_proxy(
278-
State((config, _, _, _, _, _, tags_provider, client, api_key)): State<AgentState>,
279-
request: Request,
280-
) -> Response {
301+
async fn profiling_proxy(State(state): State<ProxyState>, request: Request) -> Response {
281302
Self::handle_proxy(
282-
config,
283-
client,
284-
api_key,
285-
tags_provider,
303+
state.config,
304+
state.http_client,
305+
state.api_key,
306+
state.tags_provider,
286307
request,
287308
"intake.profile",
288309
PROFILING_BACKEND_PATH,
@@ -292,14 +313,14 @@ impl TraceAgent {
292313
}
293314

294315
async fn llm_obs_eval_metric_proxy(
295-
State((config, _, _, _, _, _, tags_provider, client, api_key)): State<AgentState>,
316+
State(state): State<ProxyState>,
296317
request: Request,
297318
) -> Response {
298319
Self::handle_proxy(
299-
config,
300-
client,
301-
api_key,
302-
tags_provider,
320+
state.config,
321+
state.http_client,
322+
state.api_key,
323+
state.tags_provider,
303324
request,
304325
"api",
305326
LLM_OBS_EVAL_METRIC_INTAKE_PATH,
@@ -309,14 +330,14 @@ impl TraceAgent {
309330
}
310331

311332
async fn llm_obs_eval_metric_proxy_v2(
312-
State((config, _, _, _, _, _, tags_provider, client, api_key)): State<AgentState>,
333+
State(state): State<ProxyState>,
313334
request: Request,
314335
) -> Response {
315336
Self::handle_proxy(
316-
config,
317-
client,
318-
api_key,
319-
tags_provider,
337+
state.config,
338+
state.http_client,
339+
state.api_key,
340+
state.tags_provider,
320341
request,
321342
"api",
322343
LLM_OBS_EVAL_METRIC_INTAKE_PATH_V2,
@@ -325,15 +346,12 @@ impl TraceAgent {
325346
.await
326347
}
327348

328-
async fn llm_obs_spans_proxy(
329-
State((config, _, _, _, _, _, tags_provider, client, api_key)): State<AgentState>,
330-
request: Request,
331-
) -> Response {
349+
async fn llm_obs_spans_proxy(State(state): State<ProxyState>, request: Request) -> Response {
332350
Self::handle_proxy(
333-
config,
334-
client,
335-
api_key,
336-
tags_provider,
351+
state.config,
352+
state.http_client,
353+
state.api_key,
354+
state.tags_provider,
337355
request,
338356
"llmobs-intake",
339357
LLM_OBS_SPANS_INTAKE_PATH,
@@ -342,15 +360,12 @@ impl TraceAgent {
342360
.await
343361
}
344362

345-
async fn debugger_logs_proxy(
346-
State((config, _, _, _, _, _, tags_provider, client, api_key)): State<AgentState>,
347-
request: Request,
348-
) -> Response {
363+
async fn debugger_logs_proxy(State(state): State<ProxyState>, request: Request) -> Response {
349364
Self::handle_proxy(
350-
config,
351-
client,
352-
api_key,
353-
tags_provider,
365+
state.config,
366+
state.http_client,
367+
state.api_key,
368+
state.tags_provider,
354369
request,
355370
"http-intake.logs",
356371
DEBUGGER_LOGS_INTAKE_PATH,

0 commit comments

Comments
 (0)