@@ -3,7 +3,7 @@ use crate::metrics::enhanced::{
33 statfs:: statfs_info,
44} ;
55use crate :: proc:: { self , CPUData , NetworkData } ;
6- use crate :: telemetry:: events:: ReportMetrics ;
6+ use crate :: telemetry:: events:: { ReportMetrics , RuntimeDoneMetrics } ;
77use dogstatsd:: metric;
88use dogstatsd:: metric:: { Metric , MetricValue } ;
99use dogstatsd:: { aggregator:: Aggregator , metric:: SortedTags } ;
@@ -127,13 +127,13 @@ impl Lambda {
127127 }
128128 }
129129
130- pub fn set_runtime_duration_metric ( & self , duration_ms : f64 , timestamp : i64 ) {
130+ pub fn set_runtime_done_metrics ( & self , metrics : & RuntimeDoneMetrics , timestamp : i64 ) {
131131 if !self . config . enhanced_metrics {
132132 return ;
133133 }
134134 let metric = Metric :: new (
135135 constants:: RUNTIME_DURATION_METRIC . into ( ) ,
136- MetricValue :: distribution ( duration_ms) ,
136+ MetricValue :: distribution ( metrics . duration_ms ) ,
137137 // Datadog expects this value as milliseconds, not seconds
138138 self . get_dynamic_value_tags ( ) ,
139139 Some ( timestamp) ,
@@ -146,6 +146,24 @@ impl Lambda {
146146 {
147147 error ! ( "failed to insert runtime duration metric: {}" , e) ;
148148 }
149+
150+ if let Some ( produced_bytes) = metrics. produced_bytes {
151+ let metric = Metric :: new (
152+ constants:: PRODUCED_BYTES_METRIC . into ( ) ,
153+ MetricValue :: distribution ( produced_bytes as f64 ) ,
154+ // Datadog expects this value as milliseconds, not seconds
155+ self . get_dynamic_value_tags ( ) ,
156+ Some ( timestamp) ,
157+ ) ;
158+ if let Err ( e) = self
159+ . aggregator
160+ . lock ( )
161+ . expect ( "lock poisoned" )
162+ . insert ( metric)
163+ {
164+ error ! ( "failed to insert produced bytes metric: {}" , e) ;
165+ }
166+ }
149167 }
150168
151169 pub fn set_post_runtime_duration_metric ( & self , duration_ms : f64 , timestamp : i64 ) {
@@ -842,7 +860,13 @@ mod tests {
842860 lambda. increment_errors_metric ( now) ;
843861 lambda. increment_timeout_metric ( now) ;
844862 lambda. set_init_duration_metric ( 100.0 , now) ;
845- lambda. set_runtime_duration_metric ( 100.0 , now) ;
863+ lambda. set_runtime_done_metrics (
864+ & RuntimeDoneMetrics {
865+ duration_ms : 100.0 ,
866+ produced_bytes : Some ( 42 as u64 ) ,
867+ } ,
868+ now,
869+ ) ;
846870 lambda. set_post_runtime_duration_metric ( 100.0 , now) ;
847871 lambda. set_report_log_metrics (
848872 & ReportMetrics {
@@ -871,6 +895,9 @@ mod tests {
871895 assert ! ( aggr
872896 . get_entry_by_id( constants:: RUNTIME_DURATION_METRIC . into( ) , & None , now)
873897 . is_none( ) ) ;
898+ assert ! ( aggr
899+ . get_entry_by_id( constants:: PRODUCED_BYTES_METRIC . into( ) , & None , now)
900+ . is_none( ) ) ;
874901 assert ! ( aggr
875902 . get_entry_by_id( constants:: POST_RUNTIME_DURATION_METRIC . into( ) , & None , now)
876903 . is_none( ) ) ;
@@ -949,6 +976,31 @@ mod tests {
949976 . is_none( ) ) ;
950977 }
951978
979+ #[ test]
980+ fn test_set_runtime_done_metrics ( ) {
981+ let ( metrics_aggr, my_config) = setup ( ) ;
982+ let lambda = Lambda :: new ( metrics_aggr. clone ( ) , my_config) ;
983+ let runtime_done_metrics = RuntimeDoneMetrics {
984+ duration_ms : 100.0 ,
985+ produced_bytes : Some ( 42 as u64 ) ,
986+ } ;
987+ let now: i64 = std:: time:: UNIX_EPOCH
988+ . elapsed ( )
989+ . expect ( "unable to poll clock, unrecoverable" )
990+ . as_secs ( )
991+ . try_into ( )
992+ . unwrap_or_default ( ) ;
993+ lambda. set_runtime_done_metrics ( & runtime_done_metrics, now) ;
994+
995+ assert_sketch (
996+ & metrics_aggr,
997+ constants:: RUNTIME_DURATION_METRIC ,
998+ 100.0 ,
999+ now,
1000+ ) ;
1001+ assert_sketch ( & metrics_aggr, constants:: PRODUCED_BYTES_METRIC , 42.0 , now) ;
1002+ }
1003+
9521004 #[ test]
9531005 fn test_set_report_log_metrics ( ) {
9541006 let ( metrics_aggr, my_config) = setup ( ) ;
0 commit comments