@@ -213,3 +213,67 @@ mod other {
213213pub use linux:: * ;
214214#[ cfg( not( target_os = "linux" ) ) ]
215215pub use other:: * ;
216+
217+ #[ cfg( test) ]
218+ mod tests {
219+ use super :: * ;
220+ use libdd_trace_protobuf:: opentelemetry:: proto:: common:: v1:: {
221+ any_value, AnyValue , ProcessContext ,
222+ } ;
223+
224+ fn find_attr < ' a > ( ctx : & ' a ProcessContext , key : & str ) -> Option < & ' a AnyValue > {
225+ ctx. resource
226+ . as_ref ( ) ?
227+ . attributes
228+ . iter ( )
229+ . find ( |kv| kv. key == key) ?
230+ . value
231+ . as_ref ( )
232+ }
233+
234+ #[ test]
235+ fn threadlocal_attrs_absent_when_keys_empty ( ) {
236+ let ctx = TracerMetadata :: default ( ) . to_otel_process_ctx ( ) ;
237+
238+ assert ! ( find_attr( & ctx, "threadlocal.schema_version" ) . is_none( ) ) ;
239+ assert ! ( find_attr( & ctx, "threadlocal.attribute_key_map" ) . is_none( ) ) ;
240+ }
241+
242+ #[ test]
243+ fn threadlocal_attrs_present_with_correct_values ( ) {
244+ let ctx = TracerMetadata {
245+ threadlocal_attribute_keys : vec ! [
246+ "span.id" . to_owned( ) ,
247+ "trace.id" . to_owned( ) ,
248+ "custom.key" . to_owned( ) ,
249+ ] ,
250+ ..Default :: default ( )
251+ }
252+ . to_otel_process_ctx ( ) ;
253+
254+ // Schema version attribute
255+ let schema_version = find_attr ( & ctx, "threadlocal.schema_version" )
256+ . expect ( "threadlocal.schema_version should be present" ) ;
257+ assert_eq ! (
258+ schema_version. value,
259+ Some ( any_value:: Value :: StringValue ( "tlsdesc_v1_dev" . to_owned( ) ) )
260+ ) ;
261+
262+ // Key map attribute: ordered array of key name strings
263+ let key_map = find_attr ( & ctx, "threadlocal.attribute_key_map" )
264+ . expect ( "threadlocal.attribute_key_map should be present" ) ;
265+ let array = match & key_map. value {
266+ Some ( any_value:: Value :: ArrayValue ( a) ) => a,
267+ other => panic ! ( "expected ArrayValue, got {:?}" , other) ,
268+ } ;
269+ let keys: Vec < & str > = array
270+ . values
271+ . iter ( )
272+ . map ( |v| match & v. value {
273+ Some ( any_value:: Value :: StringValue ( s) ) => s. as_str ( ) ,
274+ other => panic ! ( "expected StringValue, got {:?}" , other) ,
275+ } )
276+ . collect ( ) ;
277+ assert_eq ! ( keys, [ "span.id" , "trace.id" , "custom.key" ] ) ;
278+ }
279+ }
0 commit comments