-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmain.rs
More file actions
1435 lines (1325 loc) · 54 KB
/
main.rs
File metadata and controls
1435 lines (1325 loc) · 54 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
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#![deny(clippy::all)]
#![deny(clippy::pedantic)]
#![deny(clippy::unwrap_used)]
#![deny(unused_extern_crates)]
#![deny(unused_allocation)]
#![deny(unused_assignments)]
#![deny(unused_comparisons)]
#![deny(unreachable_pub)]
#![deny(missing_copy_implementations)]
#![deny(missing_debug_implementations)]
#[cfg(not(target_env = "msvc"))]
use tikv_jemallocator::Jemalloc;
#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;
use bottlecap::{
DOGSTATSD_PORT, LAMBDA_RUNTIME_SLUG,
appsec::processor::{
Error::FeatureDisabled as AppSecFeatureDisabled, Processor as AppSecProcessor,
},
config::{
self, Config,
aws::{AwsConfig, build_lambda_function_arn},
flush_strategy::{FlushStrategy, PeriodicStrategy},
log_level::LogLevel,
},
event_bus::{Event, EventBus},
extension::{
self, EXTENSION_HOST, EXTENSION_HOST_IP, ExtensionError, NextEventResponse,
RegisterResponse,
telemetry::{
self, TELEMETRY_PORT,
events::{TelemetryEvent, TelemetryRecord},
listener::TelemetryListener,
},
},
fips::{log_fips_status, prepare_client_provider},
flushing::FlushingService,
lifecycle::{
flush_control::{DEFAULT_CONTINUOUS_FLUSH_INTERVAL, FlushControl, FlushDecision},
invocation::processor_service::{InvocationProcessorHandle, InvocationProcessorService},
listener::Listener as LifecycleListener,
},
logger,
logs::{
agent::LogsAgent,
aggregator_service::{
AggregatorHandle as LogsAggregatorHandle, AggregatorService as LogsAggregatorService,
},
flusher::LogsFlusher,
lambda::DurableContextUpdate,
},
otlp::{agent::Agent as OtlpAgent, should_enable_otlp_agent},
proxy::{interceptor, should_start_proxy},
secrets::decrypt,
tags::{
lambda::{self, tags::EXTENSION_VERSION},
provider::Provider as TagProvider,
},
traces::{
http_client as trace_http_client,
propagation::DatadogCompositePropagator,
proxy_aggregator,
proxy_flusher::Flusher as ProxyFlusher,
span_dedup_service,
stats_aggregator::StatsAggregator,
stats_concentrator_service::{StatsConcentratorHandle, StatsConcentratorService},
stats_flusher,
stats_generator::StatsGenerator,
stats_processor, trace_agent,
trace_aggregator::SendDataBuilderInfo,
trace_aggregator_service::{
AggregatorHandle as TraceAggregatorHandle, AggregatorService as TraceAggregatorService,
},
trace_flusher,
trace_processor::{self, SendingTraceProcessor},
},
};
use datadog_fips::reqwest_adapter::create_reqwest_client_builder;
use decrypt::resolve_secrets;
use dogstatsd::{
aggregator::{
AggregatorHandle as MetricsAggregatorHandle, AggregatorService as MetricsAggregatorService,
},
api_key::ApiKeyFactory,
constants::CONTEXTS,
datadog::{
DdDdUrl, DdUrl, MetricsIntakeUrlPrefix, MetricsIntakeUrlPrefixOverride,
RetryStrategy as DsdRetryStrategy, Site as MetricsSite,
},
dogstatsd::{DogStatsD, DogStatsDConfig},
flusher::{Flusher as MetricsFlusher, FlusherConfig as MetricsFlusherConfig},
metric::{EMPTY_TAGS, SortedTags},
};
use libdd_trace_obfuscation::obfuscation_config;
use reqwest::Client;
use std::{collections::hash_map, env, path::Path, str::FromStr, sync::Arc};
use tokio::time::Instant;
use tokio::{sync::Mutex as TokioMutex, sync::mpsc::Sender};
use tokio_util::sync::CancellationToken;
use tracing::{debug, error, warn};
use tracing_subscriber::EnvFilter;
use ustr::Ustr;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let start_time = Instant::now();
init_ustr();
enable_logging_subsystem();
let aws_config = AwsConfig::from_env(start_time);
log_fips_status(&aws_config.region);
let version_without_next = EXTENSION_VERSION.split('-').next().unwrap_or("NA");
debug!("Starting Datadog Extension v{version_without_next}");
// Debug: Wait for debugger to attach if DD_DEBUG_WAIT_FOR_ATTACH is set
if let Ok(wait_secs) = env::var("DD_DEBUG_WAIT_FOR_ATTACH")
&& let Ok(secs) = wait_secs.parse::<u64>()
{
debug!("DD_DEBUG_WAIT_FOR_ATTACH: Waiting {secs} seconds for debugger to attach...");
debug!("Connect your debugger to port 2345 now!");
tokio::time::sleep(tokio::time::Duration::from_secs(secs)).await;
debug!("DD_DEBUG_WAIT_FOR_ATTACH: Continuing execution...");
}
prepare_client_provider()?;
let client = create_reqwest_client_builder()
.map_err(|e| anyhow::anyhow!("Failed to create client builder: {e:?}"))?
.no_proxy()
.build()
.map_err(|e| anyhow::anyhow!("Failed to create client: {e:?}"))?;
let cloned_client = client.clone();
let runtime_api = aws_config.runtime_api.clone();
let managed_instance_mode = aws_config.is_managed_instance_mode();
let response = tokio::task::spawn(async move {
extension::register(
&cloned_client,
&runtime_api,
extension::EXTENSION_NAME,
managed_instance_mode,
)
.await
});
// First load the AWS configuration
let lambda_directory: String =
env::var("LAMBDA_TASK_ROOT").unwrap_or_else(|_| "/var/task".to_string());
let config = Arc::new(config::get_config(Path::new(&lambda_directory)));
let aws_config = Arc::new(aws_config);
let api_key_factory = create_api_key_factory(&config, &aws_config);
let r = response
.await
.map_err(|e| anyhow::anyhow!("Failed to join task: {e:?}"))?
.map_err(|e| anyhow::anyhow!("Failed to register extension: {e:?}"))?;
match extension_loop_active(
Arc::clone(&aws_config),
&config,
&client,
&r,
Arc::clone(&api_key_factory),
start_time,
)
.await
{
Ok(()) => {
debug!("Extension loop completed successfully");
Ok(())
}
Err(e) => {
error!("Extension loop failed: {e:?}, Calling /next without Datadog instrumentation");
extension_loop_idle(&client, &r, &aws_config).await
}
}
}
// Ustr initialization can take 10+ ms.
// Start it early in a separate thread so it won't become a bottleneck later when SortedTags::parse() is called.
fn init_ustr() {
tokio::spawn(async {
Ustr::from("");
});
}
fn enable_logging_subsystem() {
let log_level = LogLevel::from_str(
std::env::var("DD_LOG_LEVEL")
.unwrap_or("info".to_string())
.as_str(),
)
.unwrap_or(LogLevel::Info);
let env_filter = format!(
"h2=off,hyper=off,reqwest=off,rustls=off,datadog-trace-mini-agent=off,{log_level:?}",
);
let subscriber = tracing_subscriber::fmt::Subscriber::builder()
.with_env_filter(
EnvFilter::try_new(env_filter).expect("could not parse log level in configuration"),
)
.with_level(true)
.with_thread_names(false)
.with_thread_ids(false)
.with_line_number(false)
.with_file(false)
.with_target(false)
.without_time()
.event_format(logger::Formatter)
.finish();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
debug!("Logging subsystem enabled");
}
/// Returns the appropriate flush strategy for the given mode.
/// In managed instance mode, continuous flush strategy is required for optimal performance.
/// If a different strategy is configured, this function will override it and log an info message.
fn get_flush_strategy_for_mode(
aws_config: &AwsConfig,
configured_strategy: FlushStrategy,
) -> FlushStrategy {
if !aws_config.is_managed_instance_mode() {
return configured_strategy;
}
// Check if flush strategy needs to be enforced and log if so
if let FlushStrategy::Continuously(_) = configured_strategy {
configured_strategy
} else {
// Only log if the user explicitly configured a non-default strategy
if !matches!(configured_strategy, FlushStrategy::Default) {
warn!(
"Managed Instance mode detected. Flush strategy '{}' is not compatible with managed instance mode. \
Enforcing continuous flush strategy with {}ms interval for optimal performance.",
configured_strategy.name(),
DEFAULT_CONTINUOUS_FLUSH_INTERVAL
);
}
FlushStrategy::Continuously(PeriodicStrategy {
interval: DEFAULT_CONTINUOUS_FLUSH_INTERVAL,
})
}
}
fn create_api_key_factory(config: &Arc<Config>, aws_config: &Arc<AwsConfig>) -> Arc<ApiKeyFactory> {
let config = Arc::clone(config);
let aws_config = Arc::clone(aws_config);
let api_key_secret_reload_interval = config.api_key_secret_reload_interval;
Arc::new(ApiKeyFactory::new_from_resolver(
Arc::new(move || {
let config = Arc::clone(&config);
let aws_config = Arc::clone(&aws_config);
Box::pin(async move { resolve_secrets(config, aws_config).await })
}),
api_key_secret_reload_interval,
))
}
async fn extension_loop_idle(
client: &Client,
r: &RegisterResponse,
aws_config: &AwsConfig,
) -> anyhow::Result<()> {
loop {
match extension::next_event(client, &r.extension_id, &aws_config.runtime_api).await {
Ok(_) => {
debug!("Extension is idle, skipping next event");
}
Err(e) => {
error!("Error getting next event: {e:?}");
return Err(e.into());
}
}
}
}
#[allow(clippy::too_many_lines)]
async fn extension_loop_active(
aws_config: Arc<AwsConfig>,
config: &Arc<Config>,
client: &Client,
r: &RegisterResponse,
api_key_factory: Arc<ApiKeyFactory>,
start_time: Instant,
) -> anyhow::Result<()> {
let (mut event_bus, event_bus_tx) = EventBus::run();
let account_id = r.account_id.as_ref().unwrap_or(&"none".to_string()).clone();
let tags_provider = setup_tag_provider(&Arc::clone(&aws_config), config, &account_id);
// Build one shared reqwest::Client for metrics, logs, and trace proxy flushing.
// reqwest::Client is Arc-based internally, so cloning just increments a refcount
// and shares the connection pool.
let shared_client = bottlecap::http::get_client(config);
let (
logs_agent_channel,
logs_flusher,
logs_agent_cancel_token,
logs_aggregator_handle,
durable_context_tx,
) = start_logs_agent(
config,
Arc::clone(&api_key_factory),
&tags_provider,
event_bus_tx.clone(),
aws_config.is_managed_instance_mode(),
&shared_client,
);
let (metrics_flushers, metrics_aggregator_handle, dogstatsd_cancel_token) = start_dogstatsd(
tags_provider.clone(),
Arc::clone(&api_key_factory),
config,
&shared_client,
)
.await;
let propagator = Arc::new(DatadogCompositePropagator::new(Arc::clone(config)));
// Lifecycle Invocation Processor
let (invocation_processor_handle, invocation_processor_service) =
InvocationProcessorService::new(
Arc::clone(&tags_provider),
Arc::clone(config),
Arc::clone(&aws_config),
metrics_aggregator_handle.clone(),
Arc::clone(&propagator),
durable_context_tx,
);
tokio::spawn(async move {
invocation_processor_service.run().await;
});
// AppSec processor (if enabled)
let appsec_processor = match AppSecProcessor::new(config) {
Ok(p) => Some(Arc::new(TokioMutex::new(p))),
Err(AppSecFeatureDisabled) => None,
Err(e) => {
error!(
"AAP | error creating App & API Protection processor, the feature will be disabled: {e}"
);
None
}
};
let (
trace_agent_channel,
trace_flusher,
trace_processor,
stats_flusher,
proxy_flusher,
trace_agent_shutdown_token,
stats_concentrator,
trace_aggregator_handle,
) = start_trace_agent(
config,
&api_key_factory,
&tags_provider,
invocation_processor_handle.clone(),
appsec_processor.clone(),
&shared_client,
);
let api_runtime_proxy_shutdown_signal = start_api_runtime_proxy(
config,
Arc::clone(&aws_config),
&invocation_processor_handle,
appsec_processor.as_ref(),
Arc::clone(&propagator),
);
let lifecycle_listener =
LifecycleListener::new(invocation_processor_handle.clone(), Arc::clone(&propagator));
let lifecycle_listener_shutdown_token = lifecycle_listener.get_shutdown_token();
// TODO(astuyve): deprioritize this task after the first request
tokio::spawn(async move {
if let Err(e) = lifecycle_listener.start().await {
error!("Error starting lifecycle listener: {e:?}");
}
});
let telemetry_listener_cancel_token = setup_telemetry_client(
client,
&r.extension_id,
&aws_config.runtime_api,
logs_agent_channel,
event_bus_tx.clone(),
config.serverless_logs_enabled,
aws_config.is_managed_instance_mode(),
)
.await?;
let otlp_cancel_token = start_otlp_agent(
config,
tags_provider.clone(),
trace_processor.clone(),
trace_agent_channel.clone(),
stats_concentrator.clone(),
);
// Validate and get the appropriate flush strategy for the current mode
let flush_strategy = get_flush_strategy_for_mode(&aws_config, config.serverless_flush_strategy);
debug!("Flush strategy: {:?}", flush_strategy);
let mut flush_control = FlushControl::new(flush_strategy, config.flush_timeout);
debug!(
"Datadog Next-Gen Extension ready in {:}ms",
start_time.elapsed().as_millis().to_string()
);
if aws_config.is_managed_instance_mode() {
// Clone Arc references for the background flusher task
let logs_flusher_clone = logs_flusher.clone();
let metrics_flushers_clone = Arc::clone(&metrics_flushers);
let trace_flusher_clone = Arc::clone(&trace_flusher);
let stats_flusher_clone = Arc::clone(&stats_flusher);
let proxy_flusher_clone = proxy_flusher.clone();
let metrics_aggr_handle_clone = metrics_aggregator_handle.clone();
// In Managed Instance mode, create a separate interval for the background flusher task.
// We don't reuse race_flush_interval because we need to configure the missed tick
// behavior before discarding the first tick. While creating a new interval may seem
// redundant, it keeps Managed Instance and OnDemand mode flush intervals properly isolated,
// making the code easier to maintain and less error-prone.
//
// Use Skip behavior to prevent accumulating missed ticks if flushes take longer
// than the interval. This ensures we maintain a steady flush cadence without
// bursts of catch-up ticks, which is important since flushes are non-blocking.
let mut managed_instance_mode_flush_interval = flush_control.get_flush_interval();
managed_instance_mode_flush_interval
.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
managed_instance_mode_flush_interval.tick().await; // discard first tick
let cancel_token_clone = telemetry_listener_cancel_token.clone();
// Spawn a background task for continuous periodic flushing in Managed Instance mode.
// A background task continuously flushes metrics, logs,
// traces, and stats at regular intervals (configured by flush_control). This ensures
// data is sent to Datadog even while concurrent invocations are being processed.
// The flushing happens independently of invocation lifecycle events.
// This background task runs until shutdown is signaled via cancel_token_clone.
let flush_task_handle = tokio::spawn(async move {
let mut flushing_service = FlushingService::new(
logs_flusher_clone,
trace_flusher_clone,
stats_flusher_clone,
proxy_flusher_clone,
metrics_flushers_clone,
metrics_aggr_handle_clone,
);
loop {
tokio::select! {
_ = managed_instance_mode_flush_interval.tick() => {
if !flushing_service.has_pending_handles() {
// Only spawn new flush if no pending flushes to prevent resource buildup
flushing_service.spawn_non_blocking().await;
}
}
() = cancel_token_clone.cancelled() => {
debug!("Managed Instance mode: periodic flusher task cancelled, waiting for pending flushes");
// Wait for any pending flushes
flushing_service.await_handles().await;
// Final flush to capture any data that accumulated since the last
// spawn_non_blocking(). This is our last opportunity to send data.
flushing_service.flush_blocking_final().await;
break;
}
}
}
});
// Spawn a separate task to handle the SHUTDOWN event from /next endpoint.
// This task waits for the Lambda runtime to signal shutdown via the Extensions API.
// When shutdown is received, it cancels the background flusher and signals the main
// event loop to begin graceful shutdown.
let shutdown_cancel_token = CancellationToken::new();
let shutdown_cancel_token_clone = shutdown_cancel_token.clone();
let invocation_processor_handle_clone = invocation_processor_handle.clone();
let runtime_api_clone = aws_config.runtime_api.clone();
let extension_id_clone = r.extension_id.clone();
let client_clone = client.clone();
// Main event loop for Managed Instance mode: process telemetry events until shutdown
//
// the extension registers for SHUTDOWN events ONLY (not INVOKE events),
// 1. A separate task waits for the SHUTDOWN event from next_event()
// 2. This loop processes telemetry events from event_bus
// 3. When SHUTDOWN is received (detected via cancel token), break the loop
// 4. Invocation lifecycle events (START, REPORT, etc.) come via Telemetry API,
// not via next_event() responses
//
// This allows Managed Instance mode to handle concurrent invocations while OnDemand mode
// processes invocations sequentially, one at a time.
tokio::spawn(async move {
// In Managed Instance mode, the only event we can subscribe to is SHUTDOWN, meaning that
// this call will block until the shutdown event is received.
// We can use this to signal other tasks to shutdown and wait for them to complete.
// Therefore, we need to have it in a separate task to avoid blocking the main loop.
debug!("Managed Instance mode: waiting for shutdown event");
loop {
let next_response =
extension::next_event(&client_clone, &runtime_api_clone, &extension_id_clone)
.await;
match next_response {
Ok(NextEventResponse::Shutdown { .. }) => {
debug!("Shutdown event received, stopping extension loop");
// Notify the invocation processor about shutdown
if let Err(e) = invocation_processor_handle_clone.on_shutdown_event().await
{
error!("Failed to send shutdown event to processor: {}", e);
}
// Signal all other tasks to shutdown
shutdown_cancel_token_clone.cancel();
break;
}
Ok(NextEventResponse::Invoke { .. }) => {
error!(
"Received unexpected Invoke event in Managed Instance mode - this should not happen. \
Managed Instance mode should only subscribe to SHUTDOWN events."
);
shutdown_cancel_token_clone.cancel();
break;
}
Err(ExtensionError::HttpError(e)) if e.is_timeout() || e.is_connect() => {
debug!(
"Transient network error waiting for shutdown event: {}. Retrying...",
e
);
}
Err(e) => {
error!(
"Unrecoverable error waiting for shutdown event: {:?}. \
Initiating emergency shutdown.",
e
);
shutdown_cancel_token_clone.cancel();
break;
}
}
}
debug!("Shutdown task completed");
});
'managed_instance_event_loop: loop {
tokio::select! {
biased;
// Process telemetry events (platform.start, platform.report, etc.) sent from
// the Telemetry API listener. These events provide invocation lifecycle information
// without requiring next_event() calls. The biased ordering ensures we prioritize
// processing telemetry events before checking for shutdown.
Some(event) = event_bus.rx.recv() => {
handle_event_bus_event(event,
invocation_processor_handle.clone(),
appsec_processor.clone(),
tags_provider.clone(),
trace_processor.clone(),
trace_agent_channel.clone(),
stats_concentrator.clone()).
await;
}
// Detect when shutdown has been signaled by the shutdown task.
// This happens when the /next endpoint returns a SHUTDOWN event.
() = shutdown_cancel_token.cancelled() => {
debug!("Shutdown signal received, exiting event loop");
break 'managed_instance_event_loop;
}
}
}
// Shutdown sequence
debug!("Initiating shutdown sequence");
// Wait for tombstone event from telemetry listener to ensure all events are processed
// This is the result of code refactoring which is shared by OnDemand mode as well.
wait_for_tombstone_event(
&mut event_bus,
&invocation_processor_handle,
appsec_processor.clone(),
tags_provider.clone(),
trace_processor.clone(),
trace_agent_channel.clone(),
stats_concentrator.clone(),
300,
)
.await;
// Cancel background tasks
cancel_background_services(
api_runtime_proxy_shutdown_signal.as_ref(),
otlp_cancel_token.as_ref(),
&trace_agent_shutdown_token,
&dogstatsd_cancel_token,
&telemetry_listener_cancel_token,
&lifecycle_listener_shutdown_token,
);
// Wait for background flusher to complete gracefully.
// The background task performs the final flush before exiting, so we just need to wait.
if let Err(e) = flush_task_handle.await {
error!("Error waiting for background flush task: {e:?}");
}
return Ok(());
}
// Below is for On-Demand mode only
let mut race_flush_interval = flush_control.get_flush_interval();
race_flush_interval.tick().await; // discard first tick, which is instantaneous
let next_lambda_response =
extension::next_event(client, &aws_config.runtime_api, &r.extension_id).await;
// first invoke we must call next
let mut flushing_service = FlushingService::new(
logs_flusher.clone(),
Arc::clone(&trace_flusher),
Arc::clone(&stats_flusher),
proxy_flusher.clone(),
Arc::clone(&metrics_flushers),
metrics_aggregator_handle.clone(),
);
handle_next_invocation(next_lambda_response, &invocation_processor_handle).await;
loop {
let maybe_shutdown_event;
let current_flush_decision = flush_control.evaluate_flush_decision();
match current_flush_decision {
FlushDecision::End => {
// break loop after runtime done
// flush everything
// call next
// optionally flush after tick for long running invos
'flush_end: loop {
tokio::select! {
biased;
Some(event) = event_bus.rx.recv() => {
if let Some(telemetry_event) = handle_event_bus_event(event, invocation_processor_handle.clone(), appsec_processor.clone(), tags_provider.clone(), trace_processor.clone(), trace_agent_channel.clone(), stats_concentrator.clone()).await
&& let TelemetryRecord::PlatformRuntimeDone{ .. } = telemetry_event.record {
break 'flush_end;
}
}
_ = race_flush_interval.tick() => {
flushing_service.flush_blocking().await;
race_flush_interval.reset();
}
}
}
// flush
flushing_service.flush_blocking().await;
race_flush_interval.reset();
let next_response =
extension::next_event(client, &aws_config.runtime_api, &r.extension_id).await;
maybe_shutdown_event =
handle_next_invocation(next_response, &invocation_processor_handle).await;
}
FlushDecision::Continuous | FlushDecision::Periodic | FlushDecision::Dont => {
match current_flush_decision {
//Periodic flush scenario, flush at top of invocation
FlushDecision::Continuous => {
if !flushing_service.has_pending_handles() {
flushing_service.spawn_non_blocking().await;
race_flush_interval.reset();
}
}
FlushDecision::Periodic => {
flushing_service.flush_blocking().await;
race_flush_interval.reset();
}
_ => {
// No specific flush logic for Dont or End (End already handled above)
}
}
// NO FLUSH SCENARIO
// JUST LOOP OVER PIPELINE AND WAIT FOR NEXT EVENT
// If we get platform.runtimeDone or platform.runtimeReport
// That's fine, we still wait to break until we get the response from next
// and then we break to determine if we'll flush or not
let next_lambda_response =
extension::next_event(client, &aws_config.runtime_api, &r.extension_id);
tokio::pin!(next_lambda_response);
'next_invocation: loop {
tokio::select! {
biased;
next_response = &mut next_lambda_response => {
maybe_shutdown_event = handle_next_invocation(next_response, &invocation_processor_handle).await;
// Need to break here to re-call next
break 'next_invocation;
}
Some(event) = event_bus.rx.recv() => {
handle_event_bus_event(event, invocation_processor_handle.clone(), appsec_processor.clone(), tags_provider.clone(), trace_processor.clone(), trace_agent_channel.clone(), stats_concentrator.clone()).await;
}
_ = race_flush_interval.tick() => {
if flush_control.flush_strategy == FlushStrategy::Default {
flushing_service.flush_blocking().await;
race_flush_interval.reset();
}
}
}
}
}
}
if let NextEventResponse::Shutdown { .. } = maybe_shutdown_event {
// Cancel Telemetry API listener
// Important to do this first, so we can receive the Tombstone event which signals
// that there are no more Telemetry events to process
telemetry_listener_cancel_token.cancel();
// Cancel Logs Agent which might have Telemetry API events to process
logs_agent_cancel_token.cancel();
// Drop the event bus sender to allow the channel to close properly
drop(event_bus_tx);
// Redrive/block on any failed payloads
flushing_service.await_handles().await;
// Wait for tombstone event from telemetry listener to ensure all events are processed
wait_for_tombstone_event(
&mut event_bus,
&invocation_processor_handle,
appsec_processor.clone(),
tags_provider.clone(),
trace_processor.clone(),
trace_agent_channel.clone(),
stats_concentrator.clone(),
300,
)
.await;
// Cancel background services
cancel_background_services(
api_runtime_proxy_shutdown_signal.as_ref(),
otlp_cancel_token.as_ref(),
&trace_agent_shutdown_token,
&dogstatsd_cancel_token,
&telemetry_listener_cancel_token,
&lifecycle_listener_shutdown_token,
);
// Final flush - this is our last opportunity to send data before shutdown
flushing_service.flush_blocking_final().await;
// Even though we're shutting down, we need to reset the flush interval to prevent any future flushes
race_flush_interval.reset();
// Shutdown aggregator services
if let Err(e) = logs_aggregator_handle.shutdown() {
error!("Failed to shutdown logs aggregator: {e}");
}
if let Err(e) = trace_aggregator_handle.shutdown() {
error!("Failed to shutdown trace aggregator: {e}");
}
return Ok(());
}
}
}
/// Wait for the `Tombstone` event from telemetry listener to ensure all events are processed.
/// This function will timeout after the specified duration to prevent hanging indefinitely.
#[allow(clippy::too_many_arguments)]
async fn wait_for_tombstone_event(
event_bus: &mut EventBus,
invocation_processor_handle: &InvocationProcessorHandle,
appsec_processor: Option<Arc<TokioMutex<AppSecProcessor>>>,
tags_provider: Arc<TagProvider>,
trace_processor: Arc<trace_processor::ServerlessTraceProcessor>,
trace_agent_channel: Sender<SendDataBuilderInfo>,
stats_concentrator: StatsConcentratorHandle,
timeout_ms: u64,
) {
'shutdown: loop {
tokio::select! {
Some(event) = event_bus.rx.recv() => {
if let Event::Tombstone = event {
debug!("Received tombstone event, proceeding with shutdown");
break 'shutdown;
}
handle_event_bus_event(
event,
invocation_processor_handle.clone(),
appsec_processor.clone(),
tags_provider.clone(),
trace_processor.clone(),
trace_agent_channel.clone(),
stats_concentrator.clone(),
).await;
}
() = tokio::time::sleep(tokio::time::Duration::from_millis(timeout_ms)) => {
debug!("Timeout waiting for tombstone event, proceeding with shutdown");
break 'shutdown;
}
}
}
}
/// Cancel all background service tasks in preparation for shutdown.
fn cancel_background_services(
api_runtime_proxy_shutdown_signal: Option<&CancellationToken>,
otlp_cancel_token: Option<&CancellationToken>,
trace_agent_shutdown_token: &CancellationToken,
dogstatsd_cancel_token: &CancellationToken,
telemetry_listener_cancel_token: &CancellationToken,
lifecycle_listener_shutdown_token: &CancellationToken,
) {
if let Some(token) = api_runtime_proxy_shutdown_signal {
token.cancel();
}
if let Some(token) = otlp_cancel_token {
token.cancel();
}
trace_agent_shutdown_token.cancel();
dogstatsd_cancel_token.cancel();
telemetry_listener_cancel_token.cancel();
lifecycle_listener_shutdown_token.cancel();
}
#[allow(clippy::too_many_lines)]
async fn handle_event_bus_event(
event: Event,
invocation_processor_handle: InvocationProcessorHandle,
appsec_processor: Option<Arc<TokioMutex<AppSecProcessor>>>,
tags_provider: Arc<TagProvider>,
trace_processor: Arc<trace_processor::ServerlessTraceProcessor>,
trace_agent_channel: Sender<SendDataBuilderInfo>,
stats_concentrator: StatsConcentratorHandle,
) -> Option<TelemetryEvent> {
match event {
Event::OutOfMemory(event_timestamp) => {
if let Err(e) = invocation_processor_handle
.on_out_of_memory_error(event_timestamp)
.await
{
error!("Failed to send out of memory error to processor: {}", e);
}
}
Event::Telemetry(event) => {
debug!("Telemetry event received: {:?}", event);
match event.record {
TelemetryRecord::PlatformInitStart {
runtime_version, ..
} => {
if let Err(e) = invocation_processor_handle
.on_platform_init_start(event.time, runtime_version)
.await
{
error!("Failed to send platform init start to processor: {}", e);
}
}
TelemetryRecord::PlatformInitReport {
metrics,
initialization_type,
..
} => {
if let Err(e) = invocation_processor_handle
.on_platform_init_report(
initialization_type,
metrics.duration_ms,
event.time.timestamp(),
)
.await
{
error!("Failed to send platform init report to processor: {}", e);
}
}
TelemetryRecord::PlatformRestoreStart { .. } => {
if let Err(e) = invocation_processor_handle
.on_platform_restore_start(event.time)
.await
{
error!("Failed to send platform restore start to processor: {}", e);
}
}
TelemetryRecord::PlatformRestoreReport { metrics, .. } => {
if let Some(m) = metrics {
if let Err(e) = invocation_processor_handle
.on_platform_restore_report(m.duration_ms, event.time.timestamp())
.await
{
error!("Failed to send platform restore report to processor: {}", e);
}
} else {
error!(
"Missing SnapStart RestoreReportMetric. Not creating SnapStart span."
);
}
}
TelemetryRecord::PlatformStart { request_id, .. } => {
if let Err(e) = invocation_processor_handle
.on_platform_start(request_id, event.time)
.await
{
error!("Failed to send platform start to processor: {}", e);
}
}
TelemetryRecord::PlatformRuntimeDone {
ref request_id,
metrics: Some(metrics),
status,
ref error_type,
..
} => {
if let Err(e) = invocation_processor_handle
.on_platform_runtime_done(
request_id.clone(),
metrics,
status,
error_type.clone(),
tags_provider.clone(),
Arc::new(SendingTraceProcessor {
appsec: appsec_processor.clone(),
processor: trace_processor.clone(),
trace_tx: trace_agent_channel.clone(),
stats_generator: Arc::new(StatsGenerator::new(
stats_concentrator.clone(),
)),
}),
event.time.timestamp(),
)
.await
{
error!("Failed to send platform runtime done to processor: {}", e);
}
return Some(event);
}
TelemetryRecord::PlatformReport {
ref request_id,
metrics,
status,
ref error_type,
ref spans,
} => {
if let Err(e) = invocation_processor_handle
.on_platform_report(
request_id,
metrics,
event.time.timestamp(),
status,
error_type,
spans,
tags_provider.clone(),
Arc::new(SendingTraceProcessor {
appsec: appsec_processor.clone(),
processor: trace_processor.clone(),
trace_tx: trace_agent_channel.clone(),
stats_generator: Arc::new(StatsGenerator::new(
stats_concentrator.clone(),
)),
}),
)
.await
{
error!("Failed to send platform runtime report to processor: {}", e);
}
return Some(event);
}
_ => {
debug!("Unforwarded Telemetry event: {:?}", event);
}
}
}
// Nothing to do with Tombstone event
Event::Tombstone => {}
}
None
}
async fn handle_next_invocation(
next_response: Result<NextEventResponse, ExtensionError>,
invocation_processor_handle: &InvocationProcessorHandle,
) -> NextEventResponse {
match next_response {
Ok(NextEventResponse::Invoke {
ref request_id,
deadline_ms,
ref invoked_function_arn,
}) => {
debug!(
"Invoke event {}; deadline: {}, invoked_function_arn: {}",
request_id.clone(),
deadline_ms,
invoked_function_arn.clone()
);
if let Err(e) = invocation_processor_handle
.on_invoke_event(request_id.into())
.await
{
error!("Failed to send invoke event to processor: {}", e);
}
}
Ok(NextEventResponse::Shutdown {
ref shutdown_reason,