@@ -55,20 +55,11 @@ pub mod tonic {
5555 ) ,
5656 ) -> Self {
5757 let ( library, target) = data;
58- if let Some ( t) = target {
59- InstrumentationScope {
60- name : t. to_string ( ) ,
61- version : String :: new ( ) ,
62- attributes : vec ! [ ] ,
63- ..Default :: default ( )
64- }
65- } else {
66- InstrumentationScope {
67- name : library. name ( ) . to_owned ( ) ,
68- version : library. version ( ) . map ( ToOwned :: to_owned) . unwrap_or_default ( ) ,
69- attributes : Attributes :: from ( library. attributes ( ) . cloned ( ) ) . 0 ,
70- ..Default :: default ( )
71- }
58+ InstrumentationScope {
59+ name : target. map_or_else ( || library. name ( ) . to_owned ( ) , Cow :: into_owned) ,
60+ version : library. version ( ) . unwrap_or_default ( ) . to_owned ( ) ,
61+ attributes : Attributes :: from ( library. attributes ( ) . cloned ( ) ) . 0 ,
62+ ..Default :: default ( )
7263 }
7364 }
7465 }
@@ -86,20 +77,11 @@ pub mod tonic {
8677 ) ,
8778 ) -> Self {
8879 let ( library, target) = data;
89- if let Some ( t) = target {
90- InstrumentationScope {
91- name : t. to_string ( ) ,
92- version : String :: new ( ) ,
93- attributes : vec ! [ ] ,
94- ..Default :: default ( )
95- }
96- } else {
97- InstrumentationScope {
98- name : library. name ( ) . to_owned ( ) ,
99- version : library. version ( ) . map ( ToOwned :: to_owned) . unwrap_or_default ( ) ,
100- attributes : Attributes :: from ( library. attributes ( ) . cloned ( ) ) . 0 ,
101- ..Default :: default ( )
102- }
80+ InstrumentationScope {
81+ name : target. map_or_else ( || library. name ( ) . to_owned ( ) , Cow :: into_owned) ,
82+ version : library. version ( ) . unwrap_or_default ( ) . to_owned ( ) ,
83+ attributes : Attributes :: from ( library. attributes ( ) . cloned ( ) ) . 0 ,
84+ ..Default :: default ( )
10385 }
10486 }
10587 }
@@ -178,4 +160,53 @@ pub mod tonic {
178160 . collect :: < Vec < _ > > ( )
179161 . into ( )
180162 }
163+
164+ #[ cfg( test) ]
165+ mod tests {
166+ use super :: * ;
167+ use opentelemetry:: KeyValue ;
168+ use std:: borrow:: Cow ;
169+
170+ fn assert_scope_fields (
171+ proto_scope : & InstrumentationScope ,
172+ expected_name : & str ,
173+ expected_version : & str ,
174+ expected_attr_key : & str ,
175+ ) {
176+ assert_eq ! ( proto_scope. name, expected_name) ;
177+ assert_eq ! ( proto_scope. version, expected_version) ;
178+ assert_eq ! ( proto_scope. attributes. len( ) , 1 ) ;
179+ assert_eq ! ( proto_scope. attributes[ 0 ] . key, expected_attr_key) ;
180+ }
181+
182+ #[ test]
183+ fn instrumentation_scope_with_target_overrides_name_but_preserves_version_and_attributes ( ) {
184+ let scope = opentelemetry:: InstrumentationScope :: builder ( "my-lib" )
185+ . with_version ( "1.0.0" )
186+ . with_attributes ( [ KeyValue :: new ( "feature" , "metrics" ) ] )
187+ . build ( ) ;
188+ let target: Option < Cow < ' static , str > > = Some ( Cow :: Borrowed ( "my_app::handlers" ) ) ;
189+
190+ let from_owned = InstrumentationScope :: from ( ( scope. clone ( ) , target. clone ( ) ) ) ;
191+ let from_ref = InstrumentationScope :: from ( ( & scope, target) ) ;
192+
193+ assert_scope_fields ( & from_owned, "my_app::handlers" , "1.0.0" , "feature" ) ;
194+ assert_scope_fields ( & from_ref, "my_app::handlers" , "1.0.0" , "feature" ) ;
195+ }
196+
197+ #[ test]
198+ fn instrumentation_scope_without_target_preserves_all_fields ( ) {
199+ let scope = opentelemetry:: InstrumentationScope :: builder ( "my-lib" )
200+ . with_version ( "1.0.0" )
201+ . with_attributes ( [ KeyValue :: new ( "feature" , "metrics" ) ] )
202+ . build ( ) ;
203+ let target: Option < Cow < ' static , str > > = None ;
204+
205+ let from_owned = InstrumentationScope :: from ( ( scope. clone ( ) , target. clone ( ) ) ) ;
206+ let from_ref = InstrumentationScope :: from ( ( & scope, target) ) ;
207+
208+ assert_scope_fields ( & from_owned, "my-lib" , "1.0.0" , "feature" ) ;
209+ assert_scope_fields ( & from_ref, "my-lib" , "1.0.0" , "feature" ) ;
210+ }
211+ }
181212}
0 commit comments