-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtrace_agent.rs
More file actions
767 lines (696 loc) · 27.1 KB
/
trace_agent.rs
File metadata and controls
767 lines (696 loc) · 27.1 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0
use axum::{
Router,
extract::{DefaultBodyLimit, Request, State},
http::StatusCode,
response::{IntoResponse, Response},
routing::{any, post},
};
use serde_json::json;
use std::collections::VecDeque;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::{
Mutex,
mpsc::{self, Receiver, Sender},
};
use tokio_util::sync::CancellationToken;
use tower_http::limit::RequestBodyLimitLayer;
use tracing::{debug, error, warn};
use crate::traces::trace_processor::SendingTraceProcessor;
use crate::{
appsec::processor::Processor as AppSecProcessor,
config,
http::{extract_request_body, handler_not_found},
lifecycle::invocation::{
context::ReparentingInfo, processor_service::InvocationProcessorHandle,
},
tags::{self, provider},
traces::{
INVOCATION_SPAN_RESOURCE,
proxy_aggregator::{self, ProxyRequest},
span_dedup::DedupKey,
span_dedup_service::DedupHandle,
stats_aggregator,
stats_generator::StatsGenerator,
stats_processor,
trace_aggregator::SendDataBuilderInfo,
trace_aggregator_service::AggregatorHandle,
trace_processor,
},
};
use libdd_common::http_common;
use libdd_trace_protobuf::pb;
use libdd_trace_utils::trace_utils::{self};
use crate::traces::stats_concentrator_service::StatsConcentratorHandle;
const TRACE_AGENT_PORT: usize = 8126;
// Agent endpoints
const V4_TRACE_ENDPOINT_PATH: &str = "/v0.4/traces";
const V5_TRACE_ENDPOINT_PATH: &str = "/v0.5/traces";
const STATS_ENDPOINT_PATH: &str = "/v0.6/stats";
const DSM_AGENT_PATH: &str = "/v0.1/pipeline_stats";
const PROFILING_ENDPOINT_PATH: &str = "/profiling/v1/input";
const LLM_OBS_EVAL_METRIC_ENDPOINT_PATH: &str = "/evp_proxy/v2/api/intake/llm-obs/v1/eval-metric";
const LLM_OBS_EVAL_METRIC_ENDPOINT_PATH_V2: &str =
"/evp_proxy/v2/api/intake/llm-obs/v2/eval-metric";
const LLM_OBS_SPANS_ENDPOINT_PATH: &str = "/evp_proxy/v2/api/v2/llmobs";
const INFO_ENDPOINT_PATH: &str = "/info";
const V1_DEBUGGER_ENDPOINT_PATH: &str = "/debugger/v1/input";
const V2_DEBUGGER_ENDPOINT_PATH: &str = "/debugger/v2/input";
const DEBUGGER_DIAGNOSTICS_ENDPOINT_PATH: &str = "/debugger/v1/diagnostics";
const INSTRUMENTATION_ENDPOINT_PATH: &str = "/telemetry/proxy/api/v2/apmtelemetry";
// Intake endpoints
const DSM_INTAKE_PATH: &str = "/api/v0.1/pipeline_stats";
const LLM_OBS_SPANS_INTAKE_PATH: &str = "/api/v2/llmobs";
const LLM_OBS_EVAL_METRIC_INTAKE_PATH: &str = "/api/intake/llm-obs/v1/eval-metric";
const LLM_OBS_EVAL_METRIC_INTAKE_PATH_V2: &str = "/api/intake/llm-obs/v2/eval-metric";
const PROFILING_INTAKE_PATH: &str = "/api/v2/profile";
const V1_DEBUGGER_LOGS_INTAKE_PATH: &str = "/api/v2/logs";
const V2_DEBUGGER_INTAKE_PATH: &str = "/api/v2/debugger";
const INSTRUMENTATION_INTAKE_PATH: &str = "/api/v2/apmtelemetry";
const TRACER_PAYLOAD_CHANNEL_BUFFER_SIZE: usize = 10;
const STATS_PAYLOAD_CHANNEL_BUFFER_SIZE: usize = 10;
pub const TRACE_REQUEST_BODY_LIMIT: usize = 50 * 1024 * 1024;
pub const DEFAULT_REQUEST_BODY_LIMIT: usize = 2 * 1024 * 1024;
pub const MAX_CONTENT_LENGTH: usize = 50 * 1024 * 1024;
const LAMBDA_LOAD_SPAN: &str = "aws.lambda.load";
#[derive(Clone)]
pub struct TraceState {
pub config: Arc<config::Config>,
pub trace_sender: Arc<trace_processor::SendingTraceProcessor>,
pub invocation_processor_handle: InvocationProcessorHandle,
pub tags_provider: Arc<provider::Provider>,
pub span_deduper: DedupHandle,
}
#[derive(Clone)]
pub struct StatsState {
pub stats_processor: Arc<dyn stats_processor::StatsProcessor + Send + Sync>,
pub stats_tx: Sender<pb::ClientStatsPayload>,
}
#[derive(Clone)]
pub struct ProxyState {
pub config: Arc<config::Config>,
pub proxy_aggregator: Arc<Mutex<proxy_aggregator::Aggregator>>,
}
pub struct TraceAgent {
pub config: Arc<config::Config>,
pub trace_processor: Arc<dyn trace_processor::TraceProcessor + Send + Sync>,
pub stats_aggregator: Arc<Mutex<stats_aggregator::StatsAggregator>>,
pub stats_processor: Arc<dyn stats_processor::StatsProcessor + Send + Sync>,
pub proxy_aggregator: Arc<Mutex<proxy_aggregator::Aggregator>>,
pub tags_provider: Arc<provider::Provider>,
invocation_processor_handle: InvocationProcessorHandle,
appsec_processor: Option<Arc<Mutex<AppSecProcessor>>>,
shutdown_token: CancellationToken,
tx: Sender<SendDataBuilderInfo>,
stats_concentrator: StatsConcentratorHandle,
span_deduper: DedupHandle,
}
#[derive(Clone, Copy)]
pub enum ApiVersion {
V04,
V05,
}
impl TraceAgent {
#[must_use]
#[allow(clippy::too_many_arguments)]
pub fn new(
config: Arc<config::Config>,
aggregator_handle: AggregatorHandle,
trace_processor: Arc<dyn trace_processor::TraceProcessor + Send + Sync>,
stats_aggregator: Arc<Mutex<stats_aggregator::StatsAggregator>>,
stats_processor: Arc<dyn stats_processor::StatsProcessor + Send + Sync>,
proxy_aggregator: Arc<Mutex<proxy_aggregator::Aggregator>>,
invocation_processor_handle: InvocationProcessorHandle,
appsec_processor: Option<Arc<Mutex<AppSecProcessor>>>,
tags_provider: Arc<provider::Provider>,
stats_concentrator: StatsConcentratorHandle,
span_deduper: DedupHandle,
) -> TraceAgent {
// Set up a channel to send processed traces to our trace aggregator. tx is passed through each
// endpoint_handler to the trace processor, which uses it to send de-serialized
// processed trace payloads to our trace aggregator.
let (trace_tx, mut trace_rx): (Sender<SendDataBuilderInfo>, Receiver<SendDataBuilderInfo>) =
mpsc::channel(TRACER_PAYLOAD_CHANNEL_BUFFER_SIZE);
// Start the trace aggregator, which receives and buffers trace payloads to be consumed by the trace flusher.
tokio::spawn(async move {
while let Some(tracer_payload_info) = trace_rx.recv().await {
if let Err(e) = aggregator_handle.insert_payload(tracer_payload_info) {
error!("TRACE_AGENT | Failed to insert payload into aggregator: {e}");
}
}
});
TraceAgent {
config: config.clone(),
trace_processor,
stats_aggregator,
stats_processor,
proxy_aggregator,
invocation_processor_handle,
appsec_processor,
tags_provider,
tx: trace_tx,
shutdown_token: CancellationToken::new(),
stats_concentrator,
span_deduper,
}
}
#[allow(clippy::cast_possible_truncation)]
pub async fn start(&self) -> Result<(), Box<dyn std::error::Error>> {
let now = Instant::now();
// Set up a channel to send processed stats to our stats aggregator.
let (stats_tx, mut stats_rx): (
Sender<pb::ClientStatsPayload>,
Receiver<pb::ClientStatsPayload>,
) = mpsc::channel(STATS_PAYLOAD_CHANNEL_BUFFER_SIZE);
// Start the stats aggregator, which receives and buffers stats payloads to be consumed by the stats flusher.
let stats_aggregator = self.stats_aggregator.clone();
tokio::spawn(async move {
while let Some(stats_payload) = stats_rx.recv().await {
let mut aggregator = stats_aggregator.lock().await;
aggregator.add(stats_payload);
}
});
let router = self.make_router(stats_tx);
let port = u16::try_from(TRACE_AGENT_PORT).expect("TRACE_AGENT_PORT is too large");
let socket = SocketAddr::from(([127, 0, 0, 1], port));
let listener = tokio::net::TcpListener::bind(&socket).await?;
debug!("TRACE AGENT | Listening on port {TRACE_AGENT_PORT}");
debug!(
"TRACE AGENT | Time taken to start: {} ms",
now.elapsed().as_millis()
);
let shutdown_token_clone = self.shutdown_token.clone();
axum::serve(listener, router)
.with_graceful_shutdown(Self::graceful_shutdown(shutdown_token_clone))
.await?;
Ok(())
}
fn make_router(&self, stats_tx: Sender<pb::ClientStatsPayload>) -> Router {
let stats_generator = Arc::new(StatsGenerator::new(self.stats_concentrator.clone()));
let trace_state = TraceState {
config: Arc::clone(&self.config),
trace_sender: Arc::new(SendingTraceProcessor {
appsec: self.appsec_processor.clone(),
processor: Arc::clone(&self.trace_processor),
trace_tx: self.tx.clone(),
stats_generator,
}),
invocation_processor_handle: self.invocation_processor_handle.clone(),
tags_provider: Arc::clone(&self.tags_provider),
span_deduper: self.span_deduper.clone(),
};
let stats_state = StatsState {
stats_processor: Arc::clone(&self.stats_processor),
stats_tx,
};
let proxy_state = ProxyState {
config: Arc::clone(&self.config),
proxy_aggregator: Arc::clone(&self.proxy_aggregator),
};
let trace_router = Router::new()
.route(
V4_TRACE_ENDPOINT_PATH,
post(Self::v04_traces).put(Self::v04_traces),
)
.route(
V5_TRACE_ENDPOINT_PATH,
post(Self::v05_traces).put(Self::v05_traces),
)
.layer(RequestBodyLimitLayer::new(TRACE_REQUEST_BODY_LIMIT))
.with_state(trace_state);
let stats_router = Router::new()
.route(STATS_ENDPOINT_PATH, post(Self::stats).put(Self::stats))
.layer(RequestBodyLimitLayer::new(DEFAULT_REQUEST_BODY_LIMIT))
.with_state(stats_state);
let proxy_router = Router::new()
.route(DSM_AGENT_PATH, post(Self::dsm_proxy))
.route(PROFILING_ENDPOINT_PATH, post(Self::profiling_proxy))
.route(
LLM_OBS_EVAL_METRIC_ENDPOINT_PATH,
post(Self::llm_obs_eval_metric_proxy),
)
.route(
LLM_OBS_EVAL_METRIC_ENDPOINT_PATH_V2,
post(Self::llm_obs_eval_metric_proxy_v2),
)
.route(LLM_OBS_SPANS_ENDPOINT_PATH, post(Self::llm_obs_spans_proxy))
.route(V1_DEBUGGER_ENDPOINT_PATH, post(Self::debugger_logs_proxy))
.route(V2_DEBUGGER_ENDPOINT_PATH, post(Self::debugger_intake_proxy))
.route(
DEBUGGER_DIAGNOSTICS_ENDPOINT_PATH,
post(Self::debugger_intake_proxy),
)
.route(
INSTRUMENTATION_ENDPOINT_PATH,
post(Self::instrumentation_proxy),
)
.layer(RequestBodyLimitLayer::new(DEFAULT_REQUEST_BODY_LIMIT))
.with_state(proxy_state);
let info_router = Router::new().route(INFO_ENDPOINT_PATH, any(Self::info));
Router::new()
.merge(trace_router)
.merge(stats_router)
.merge(proxy_router)
.merge(info_router)
.fallback(handler_not_found)
// Disable the default body limit so we can use our own limit
.layer(DefaultBodyLimit::disable())
}
async fn graceful_shutdown(shutdown_token: CancellationToken) {
shutdown_token.cancelled().await;
debug!("TRACE_AGENT | Shutdown signal received, shutting down");
}
async fn v04_traces(State(state): State<TraceState>, request: Request) -> Response {
Self::handle_traces(
state.config,
request,
state.trace_sender,
state.invocation_processor_handle,
state.tags_provider,
state.span_deduper,
ApiVersion::V04,
)
.await
}
async fn v05_traces(State(state): State<TraceState>, request: Request) -> Response {
Self::handle_traces(
state.config,
request,
state.trace_sender,
state.invocation_processor_handle,
state.tags_provider,
state.span_deduper,
ApiVersion::V05,
)
.await
}
async fn stats(State(state): State<StatsState>, request: Request) -> Response {
match state
.stats_processor
.process_stats(request, state.stats_tx)
.await
{
Ok(result) => result.into_response(),
Err(err) => error_response(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Error processing trace stats: {err}"),
),
}
}
async fn dsm_proxy(State(state): State<ProxyState>, request: Request) -> Response {
Self::handle_proxy(
state.config,
state.proxy_aggregator,
request,
"trace.agent",
DSM_INTAKE_PATH,
"DSM",
)
.await
}
async fn profiling_proxy(State(state): State<ProxyState>, request: Request) -> Response {
Self::handle_proxy(
state.config,
state.proxy_aggregator,
request,
"intake.profile",
PROFILING_INTAKE_PATH,
"profiling",
)
.await
}
async fn llm_obs_eval_metric_proxy(
State(state): State<ProxyState>,
request: Request,
) -> Response {
Self::handle_proxy(
state.config,
state.proxy_aggregator,
request,
"api",
LLM_OBS_EVAL_METRIC_INTAKE_PATH,
"llm_obs_eval_metric",
)
.await
}
async fn llm_obs_eval_metric_proxy_v2(
State(state): State<ProxyState>,
request: Request,
) -> Response {
Self::handle_proxy(
state.config,
state.proxy_aggregator,
request,
"api",
LLM_OBS_EVAL_METRIC_INTAKE_PATH_V2,
"llm_obs_eval_metric",
)
.await
}
async fn llm_obs_spans_proxy(State(state): State<ProxyState>, request: Request) -> Response {
Self::handle_proxy(
state.config,
state.proxy_aggregator,
request,
"llmobs-intake",
LLM_OBS_SPANS_INTAKE_PATH,
"llm_obs_spans",
)
.await
}
// Used for `/debugger/v1/input` in Exception Replay
async fn debugger_logs_proxy(State(state): State<ProxyState>, request: Request) -> Response {
Self::handle_proxy(
state.config,
state.proxy_aggregator,
request,
"http-intake.logs",
V1_DEBUGGER_LOGS_INTAKE_PATH,
"debugger_logs",
)
.await
}
// Used for `/debugger/v1/diagnostics` and `/debugger/v2/input` in Exception Replay
async fn debugger_intake_proxy(State(state): State<ProxyState>, request: Request) -> Response {
Self::handle_proxy(
state.config,
state.proxy_aggregator,
request,
"debugger-intake",
V2_DEBUGGER_INTAKE_PATH,
"debugger",
)
.await
}
async fn instrumentation_proxy(State(state): State<ProxyState>, request: Request) -> Response {
Self::handle_proxy(
state.config,
state.proxy_aggregator,
request,
"instrumentation-telemetry-intake",
INSTRUMENTATION_INTAKE_PATH,
"instrumentation",
)
.await
}
#[allow(clippy::unused_async)]
async fn info() -> Response {
let response_json = json!(
{
"endpoints": [
V4_TRACE_ENDPOINT_PATH,
V5_TRACE_ENDPOINT_PATH,
STATS_ENDPOINT_PATH,
DSM_AGENT_PATH,
PROFILING_ENDPOINT_PATH,
INFO_ENDPOINT_PATH,
LLM_OBS_EVAL_METRIC_ENDPOINT_PATH,
LLM_OBS_EVAL_METRIC_ENDPOINT_PATH_V2,
LLM_OBS_SPANS_ENDPOINT_PATH,
V1_DEBUGGER_ENDPOINT_PATH,
V2_DEBUGGER_ENDPOINT_PATH,
DEBUGGER_DIAGNOSTICS_ENDPOINT_PATH,
],
"client_drop_p0s": false,
}
);
(StatusCode::OK, response_json.to_string()).into_response()
}
#[allow(clippy::too_many_arguments)]
#[allow(clippy::too_many_lines)]
async fn handle_traces(
config: Arc<config::Config>,
request: Request,
trace_sender: Arc<SendingTraceProcessor>,
invocation_processor_handle: InvocationProcessorHandle,
tags_provider: Arc<provider::Provider>,
deduper: DedupHandle,
version: ApiVersion,
) -> Response {
let start = Instant::now();
let (parts, body) = match extract_request_body(request).await {
Ok(r) => r,
Err(e) => {
return error_response(
StatusCode::INTERNAL_SERVER_ERROR,
format!("TRACE_AGENT | handle_traces | Error extracting request body: {e}"),
);
}
};
if let Some(content_length) = parts
.headers
.get("content-length")
.and_then(|h| h.to_str().ok())
.and_then(|h| h.parse::<usize>().ok())
.filter(|l| *l > MAX_CONTENT_LENGTH)
{
return error_response(
StatusCode::PAYLOAD_TOO_LARGE,
format!(
"Content-Length {content_length} exceeds maximum allowed size {MAX_CONTENT_LENGTH}"
),
);
}
let tracer_header_tags = (&parts.headers).into();
let (body_size, mut traces): (usize, Vec<Vec<pb::Span>>) = match version {
ApiVersion::V04 => {
match trace_utils::get_traces_from_request_body(http_common::Body::from_bytes(body))
.await
{
Ok(result) => result,
Err(err) => {
return error_response(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Error deserializing trace from request body: {err}"),
);
}
}
}
ApiVersion::V05 => match trace_utils::get_v05_traces_from_request_body(
http_common::Body::from_bytes(body),
)
.await
{
Ok(result) => result,
Err(err) => {
return error_response(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Error deserializing trace from request body: {err}"),
);
}
},
};
let mut reparenting_info = match invocation_processor_handle.get_reparenting_info().await {
Ok(info) => info,
Err(e) => {
error!("Failed to get reparenting info: {e}");
VecDeque::new()
}
};
for chunk in &mut traces {
let original_chunk = std::mem::take(chunk);
for mut span in original_chunk {
// Check for duplicates
let key = DedupKey::new(span.trace_id, span.span_id);
let should_keep = match deduper.check_and_add(key, config.span_dedup_timeout).await
{
Ok(should_keep) => {
if !should_keep {
debug!(
"Dropping duplicate span with trace_id: {}, span_id: {}",
span.trace_id, span.span_id
);
}
should_keep
}
Err(e) => {
// Not using warn or error level to avoid confusion for customers.
// No action is needed on customer side.
debug!("Failed to check span in deduper, keeping span: {e}");
true
}
};
if !should_keep {
continue;
}
// If the aws.lambda.load span is found, we're in Python or Node.
// We need to update the trace ID of the cold start span, reparent the `aws.lambda.load`
// span to the cold start span, and eventually send the cold start span.
if span.name == LAMBDA_LOAD_SPAN {
match invocation_processor_handle
.set_cold_start_span_trace_id(span.trace_id)
.await
{
Ok(Some(cold_start_span_id)) => {
span.parent_id = cold_start_span_id;
}
Ok(None) => {}
Err(e) => {
error!("Failed to set cold start span trace ID: {e}");
}
}
}
if span.resource == INVOCATION_SPAN_RESOURCE
&& let Err(e) = invocation_processor_handle
.add_tracer_span(span.clone())
.await
{
error!("Failed to add tracer span to processor: {}", e);
}
if span.name == "aws.lambda"
&& let (Some(request_id), Some(execution_id), Some(execution_name)) = (
span.meta.get(tags::lambda::tags::REQUEST_ID_KEY),
span.meta.get(tags::lambda::tags::DURABLE_EXECUTION_ID_KEY),
span.meta
.get(tags::lambda::tags::DURABLE_EXECUTION_NAME_KEY),
)
{
let first_invocation = span
.meta
.get(tags::lambda::tags::DURABLE_FUNCTION_FIRST_INVOCATION_KEY)
.map(|v| v == "true");
let execution_status = span
.meta
.get(tags::lambda::tags::DURABLE_FUNCTION_EXECUTION_STATUS_KEY)
.cloned();
if let Err(e) = invocation_processor_handle
.forward_durable_context(
request_id.clone(),
execution_id.clone(),
execution_name.clone(),
first_invocation,
execution_status,
)
.await
{
error!("Failed to forward durable context to processor: {e}");
}
}
handle_reparenting(&mut reparenting_info, &mut span);
// Keep the span
chunk.push(span);
}
}
// Remove empty chunks
traces.retain(|chunk| !chunk.is_empty());
match invocation_processor_handle
.update_reparenting(reparenting_info)
.await
{
Ok(contexts_to_send) => {
for ctx_to_send in contexts_to_send {
debug!("Invocation span is now ready. Sending: {ctx_to_send:?}");
if let Err(e) = invocation_processor_handle
.send_ctx_spans(&tags_provider, &trace_sender, ctx_to_send)
.await
{
error!("Failed to send context spans to processor: {}", e);
}
}
}
Err(e) => {
error!("Failed to update reparenting: {e}");
}
}
if let Err(err) = trace_sender
.send_processed_traces(
config,
tags_provider,
tracer_header_tags,
traces,
body_size,
None,
)
.await
{
return error_response(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Error sending traces to the trace aggregator: {err:?}"),
);
}
debug!(
"TRACE_AGENT | Processing traces took: {} ms",
start.elapsed().as_millis()
);
success_response("Successfully buffered traces to be aggregated.")
}
#[allow(clippy::too_many_arguments)]
async fn handle_proxy(
config: Arc<config::Config>,
proxy_aggregator: Arc<Mutex<proxy_aggregator::Aggregator>>,
request: Request,
backend_domain: &str,
backend_path: &str,
context: &str,
) -> Response {
debug!("TRACE_AGENT | Proxied request for {context}");
let (parts, body) = match extract_request_body(request).await {
Ok(r) => r,
Err(e) => {
return warn_response(
StatusCode::INTERNAL_SERVER_ERROR,
format!("TRACE_AGENT | handle_proxy | Error extracting request body: {e}"),
);
}
};
let target_url = format!("https://{}.{}{}", backend_domain, config.site, backend_path);
let proxy_request = ProxyRequest {
headers: parts.headers,
body,
target_url,
};
let mut proxy_aggregator = proxy_aggregator.lock().await;
proxy_aggregator.add(proxy_request);
(
StatusCode::OK,
format!("Acknowledged request for {context}"),
)
.into_response()
}
#[must_use]
pub fn get_sender_copy(&self) -> Sender<SendDataBuilderInfo> {
self.tx.clone()
}
#[must_use]
pub fn shutdown_token(&self) -> CancellationToken {
self.shutdown_token.clone()
}
}
fn handle_reparenting(reparenting_info: &mut VecDeque<ReparentingInfo>, span: &mut pb::Span) {
for rep_info in reparenting_info {
if rep_info.needs_trace_id {
rep_info.guessed_trace_id = span.trace_id;
rep_info.needs_trace_id = false;
debug!(
"Guessed trace ID: {} for reparenting {rep_info:?}",
span.trace_id
);
}
if span.trace_id == rep_info.guessed_trace_id
&& span.parent_id == rep_info.parent_id_to_reparent
{
debug!(
"Reparenting span {} with parent id {}",
span.span_id, rep_info.invocation_span_id
);
span.parent_id = rep_info.invocation_span_id;
}
}
}
fn error_response<E: std::fmt::Display>(status: StatusCode, error: E) -> Response {
error!("{}", error);
(status, error.to_string()).into_response()
}
/// Like [`error_response`], but logs at WARN level. Use when the failure is caused by an
/// external event (e.g. client disconnected) rather than a bug in the extension itself.
fn warn_response<E: std::fmt::Display>(status: StatusCode, error: E) -> Response {
warn!("{}", error);
(status, error.to_string()).into_response()
}
fn success_response(message: &str) -> Response {
debug!("{}", message);
(StatusCode::OK, json!({"rate_by_service": {}}).to_string()).into_response()
}