@@ -111,16 +111,8 @@ pub struct Processor {
111111 /// on `platform.report`. This flag ensures whichever event arrives first wins and the other is skipped,
112112 /// preventing double counting.
113113 init_duration_metric_emitted : bool ,
114- /// Timestamp of the invocation metric for the sandbox's first (cold start) invocation in
115- /// On-Demand mode, held back from `enhanced_metrics` until `platform.initStart` is processed.
116- ///
117- /// The Extensions API's `/next` poll (which drives `on_invoke_event`) has no artificial
118- /// delay, while `platform.initStart` is delivered through the buffered Telemetry API. In
119- /// On-Demand mode the Invoke event consistently reaches the processor first, so emitting the
120- /// invocation metric immediately would race `set_durable_function_tag` and miss the
121- /// `durable_function:true` tag on invocation #1. Holding the metric here until
122- /// `on_platform_init_start` (or the next invocation, or shutdown) flushes it ensures the tag
123- /// is applied before the metric is counted.
114+ /// Timestamp of the On-Demand cold-start invocation metric, held back until
115+ /// `platform.initStart` sets the durable tag so it's not missed on invocation #1.
124116 pending_first_invocation_metric : Option < i64 > ,
125117}
126118
@@ -172,14 +164,10 @@ impl Processor {
172164 /// Given a `request_id`, creates the context and adds the enhanced metric offsets to the context buffer.
173165 ///
174166 pub fn on_invoke_event ( & mut self , request_id : String ) {
175- // Flush any invocation metric held back from a previous cold start. This is a fallback
176- // for the case where `platform.initStart` never arrived (or arrived very late) before
177- // this next invocation; see `pending_first_invocation_metric` for why it's held back.
167+ // Fallback flush in case `platform.initStart` never arrived before this invocation.
178168 self . flush_pending_first_invocation_metric ( ) ;
179169
180- // In On-Demand mode, the very first invocation of a cold sandbox races
181- // `platform.initStart` (see `pending_first_invocation_metric`); hold its invocation
182- // metric back instead of emitting it inline below.
170+ // See `pending_first_invocation_metric`.
183171 let is_on_demand_cold_start =
184172 !self . aws_config . is_managed_instance_mode ( ) && self . context_buffer . is_empty ( ) ;
185173
@@ -247,8 +235,7 @@ impl Processor {
247235 . add_enhanced_metric_data ( & request_id, enhanced_metric_offsets) ;
248236 }
249237
250- // Increment the invocation metric, unless it's an On-Demand cold start, in which case
251- // it's held back until `platform.initStart` (or a fallback) flushes it.
238+ // Hold back the metric for an On-Demand cold start; see `pending_first_invocation_metric`.
252239 if is_on_demand_cold_start {
253240 self . pending_first_invocation_metric = Some ( timestamp) ;
254241 } else {
@@ -285,8 +272,7 @@ impl Processor {
285272 }
286273 }
287274
288- /// Emits the invocation metric held back by `on_invoke_event` for an On-Demand cold start,
289- /// if one is pending. See `pending_first_invocation_metric` for why it's held back.
275+ /// Flushes the metric held back by `on_invoke_event`, if any.
290276 fn flush_pending_first_invocation_metric ( & mut self ) {
291277 if let Some ( timestamp) = self . pending_first_invocation_metric . take ( ) {
292278 self . enhanced_metrics . increment_invocation_metric ( timestamp) ;
@@ -375,8 +361,7 @@ impl Processor {
375361 {
376362 self . enhanced_metrics . set_durable_function_tag ( ) ;
377363 }
378- // Flush the held-back invocation metric now that the durable_function tag (if any) has
379- // been set, so invocation #1 is counted with the correct tag.
364+ // Flush the held-back metric now that the durable tag (if any) has been set.
380365 self . flush_pending_first_invocation_metric ( ) ;
381366
382367 let start_time: i64 = SystemTime :: from ( time)
@@ -1030,8 +1015,7 @@ impl Processor {
10301015 }
10311016
10321017 pub fn on_shutdown_event ( & mut self ) {
1033- // Don't drop the invocation metric if the sandbox shuts down before
1034- // `platform.initStart` (or a second invocation) had a chance to flush it.
1018+ // Fallback flush in case the sandbox shuts down before it's otherwise flushed.
10351019 self . flush_pending_first_invocation_metric ( ) ;
10361020
10371021 let now = std:: time:: SystemTime :: now ( )
0 commit comments