@@ -34,6 +34,12 @@ impl ResourceDetector for K8sResourceDetector {
3434 name,
3535 )
3636 } ) ,
37+ env:: var ( "NODE_NAME" ) . ok ( ) . map ( |name| {
38+ KeyValue :: new (
39+ opentelemetry_semantic_conventions:: attribute:: K8S_NODE_NAME ,
40+ name,
41+ )
42+ } ) ,
3743 ]
3844 . into_iter ( )
3945 . flatten ( ) ,
@@ -49,27 +55,45 @@ mod tests {
4955
5056 #[ test]
5157 fn test_k8s_resource_detector_with_env_vars ( ) {
52- temp_env:: with_vars ( [ ( "HOSTNAME" , Some ( "test-pod" ) ) ] , || {
53- let resource = K8sResourceDetector . detect ( ) ;
58+ temp_env:: with_vars (
59+ [
60+ ( "HOSTNAME" , Some ( "test-pod" ) ) ,
61+ ( "NODE_NAME" , Some ( "test-node" ) ) ,
62+ ] ,
63+ || {
64+ let resource = K8sResourceDetector . detect ( ) ;
5465
55- assert_eq ! ( resource. len( ) , 1 ) ;
66+ assert_eq ! ( resource. len( ) , 2 ) ;
5667
57- assert_eq ! (
58- resource. get( & Key :: from_static_str(
59- opentelemetry_semantic_conventions:: attribute:: K8S_POD_NAME
60- ) ) ,
61- Some ( Value :: from( "test-pod" ) )
62- )
63- } ) ;
68+ assert_eq ! (
69+ resource. get( & Key :: from_static_str(
70+ opentelemetry_semantic_conventions:: attribute:: K8S_POD_NAME
71+ ) ) ,
72+ Some ( Value :: from( "test-pod" ) )
73+ ) ;
74+
75+ assert_eq ! (
76+ resource. get( & Key :: from_static_str(
77+ opentelemetry_semantic_conventions:: attribute:: K8S_NODE_NAME
78+ ) ) ,
79+ Some ( Value :: from( "test-node" ) )
80+ )
81+ } ,
82+ ) ;
6483 }
6584
6685 #[ test]
6786 fn test_k8s_resource_detector_with_missing_env_vars ( ) {
6887 // make sure no env var is accidentally set
69- temp_env:: with_vars_unset ( [ "HOSTNAME" ] , || {
88+ temp_env:: with_vars_unset ( [ "HOSTNAME" , "NODE_NAME" ] , || {
7089 let resource = K8sResourceDetector . detect ( ) ;
7190
7291 assert_eq ! ( resource. len( ) , 0 ) ;
92+ assert ! ( resource
93+ . get( & Key :: from_static_str(
94+ opentelemetry_semantic_conventions:: attribute:: K8S_NODE_NAME
95+ ) )
96+ . is_none( ) )
7397 } ) ;
7498 }
7599}
0 commit comments