@@ -44,6 +44,8 @@ const SERVICE_KEY: &str = "service";
4444const COMPUTE_STATS_KEY : & str = "_dd.compute_stats" ;
4545// ComputeStatsValue is the tag value indicating trace stats should be computed
4646const COMPUTE_STATS_VALUE : & str = "1" ;
47+ // FunctionTagsKey is the tag key for a function's tags to be set on the top level tracepayload
48+ const FUNCTION_TAGS_KEY : & str = "_dd.tags.function" ;
4749// TODO(astuyve) decide what to do with the version
4850const EXTENSION_VERSION_KEY : & str = "dd_extension_version" ;
4951// TODO(duncanista) figure out a better way to not hardcode this
@@ -249,6 +251,17 @@ impl Lambda {
249251 pub fn get_tags_map ( & self ) -> & hash_map:: HashMap < String , String > {
250252 & self . tags_map
251253 }
254+
255+ #[ must_use]
256+ pub fn get_function_tags_map ( & self ) -> hash_map:: HashMap < String , String > {
257+ let tags = self
258+ . tags_map
259+ . iter ( )
260+ . map ( |( k, v) | format ! ( "{k}:{v}" ) )
261+ . collect :: < Vec < String > > ( )
262+ . join ( "," ) ;
263+ hash_map:: HashMap :: from_iter ( [ ( FUNCTION_TAGS_KEY . to_string ( ) , tags) ] )
264+ }
252265}
253266
254267#[ cfg( test) ]
@@ -371,4 +384,43 @@ mod tests {
371384 fs:: remove_file ( path) . unwrap ( ) ;
372385 assert_eq ! ( runtime, "provided.al2023" ) ;
373386 }
387+
388+ #[ test]
389+ fn test_get_function_tags_map ( ) {
390+ let mut metadata = hash_map:: HashMap :: new ( ) ;
391+ metadata. insert (
392+ FUNCTION_ARN_KEY . to_string ( ) ,
393+ "arn:aws:lambda:us-west-2:123456789012:function:my-function" . to_string ( ) ,
394+ ) ;
395+ let config = Arc :: new ( Config {
396+ service : Some ( "my-service" . to_string ( ) ) ,
397+ tags : Some ( "key1:value1,key2:value2" . to_string ( ) ) ,
398+ env : Some ( "test" . to_string ( ) ) ,
399+ version : Some ( "1.0.0" . to_string ( ) ) ,
400+ ..Config :: default ( )
401+ } ) ;
402+ let tags = Lambda :: new_from_config ( config, & metadata) ;
403+ let function_tags = tags. get_function_tags_map ( ) ;
404+ assert_eq ! ( function_tags. len( ) , 1 ) ;
405+ let fn_tags_map: hash_map:: HashMap < String , String > = hash_map:: HashMap :: from_iter (
406+ function_tags
407+ . get ( FUNCTION_TAGS_KEY )
408+ . unwrap ( )
409+ . split ( ',' )
410+ . map ( |tag| {
411+ let parts = tag. split ( ':' ) . collect :: < Vec < & str > > ( ) ;
412+ ( parts[ 0 ] . to_string ( ) , parts[ 1 ] . to_string ( ) )
413+ } ) ,
414+ ) ;
415+ assert_eq ! ( fn_tags_map. len( ) , 14 ) ;
416+ assert_eq ! ( fn_tags_map. get( "key1" ) . unwrap( ) , "value1" ) ;
417+ assert_eq ! ( fn_tags_map. get( "key2" ) . unwrap( ) , "value2" ) ;
418+ assert_eq ! ( fn_tags_map. get( ACCOUNT_ID_KEY ) . unwrap( ) , "123456789012" ) ;
419+ assert_eq ! ( fn_tags_map. get( ENV_KEY ) . unwrap( ) , "test" ) ;
420+ assert_eq ! ( fn_tags_map. get( FUNCTION_ARN_KEY ) . unwrap( ) , "arn" ) ;
421+ assert_eq ! ( fn_tags_map. get( FUNCTION_NAME_KEY ) . unwrap( ) , "my-function" ) ;
422+ assert_eq ! ( fn_tags_map. get( REGION_KEY ) . unwrap( ) , "us-west-2" ) ;
423+ assert_eq ! ( fn_tags_map. get( SERVICE_KEY ) . unwrap( ) , "my-service" ) ;
424+ assert_eq ! ( fn_tags_map. get( VERSION_KEY ) . unwrap( ) , "1.0.0" ) ;
425+ }
374426}
0 commit comments