44#include < gtest/gtest.h>
55#include < stdint.h>
66#include < chrono>
7+ #include < cstdint>
78#include < map>
89#include < string>
10+ #include < type_traits>
911#include < utility>
1012#include < vector>
1113
@@ -256,22 +258,93 @@ TEST(OtlpRecordable, AddLink)
256258TEST (OtlpRecordable, SetResource)
257259{
258260 OtlpRecordable rec;
259- const std::string service_name_key = " service.name" ;
260- std::string service_name = " test-otlp" ;
261- auto resource = resource::Resource::Create ({{service_name_key, service_name}});
261+ bool array_bool[] = {true , false , true };
262+ int32_t array_int[] = {1 , 2 , 3 };
263+ double array_double[] = {1.1 , 2.2 , 3.3 };
264+ opentelemetry::nostd::string_view array_string[] = {" str1" , " str2" , " str3" };
265+
266+ resource::ResourceAttributes attributes{
267+ {" service.name" , opentelemetry::nostd::string_view{" test-otlp" }},
268+ {" bool_value" , true },
269+ {" int_value" , 3 },
270+ {" double_value" , static_cast <double >(1.4 )},
271+ {" bytes_value" ,
272+ opentelemetry::nostd::span<const uint8_t >{reinterpret_cast <const uint8_t *>(" \1\0\3 abc" ),
273+ 6 }},
274+ {" bool_array" , opentelemetry::nostd::span<const bool >{array_bool}},
275+ {" int_array" , opentelemetry::nostd::span<const int32_t >{array_int}},
276+ {" double_array" , opentelemetry::nostd::span<const double >{array_double}},
277+ {" string_array" ,
278+ opentelemetry::nostd::span<const opentelemetry::nostd::string_view>{array_string}}};
279+
280+ auto resource = resource::Resource::Create (attributes);
262281 rec.SetResource (resource);
263282
264- auto proto_resource = rec.ProtoResource ();
265- bool found_service_name = false ;
283+ auto proto_resource = rec.ProtoResource ();
284+ size_t found_attribute_count = 0 ;
266285 for (int i = 0 ; i < proto_resource.attributes_size (); i++)
267286 {
268287 const auto &attr = proto_resource.attributes (static_cast <int >(i));
269- if (attr.key () == service_name_key && attr. value (). string_value () == service_name )
288+ if (attr.key () == " service.name " )
270289 {
271- found_service_name = true ;
290+ EXPECT_EQ (attr.value ().string_value (), std::string{" test-otlp" });
291+ ++found_attribute_count;
292+ }
293+ else if (attr.key () == " bool_value" )
294+ {
295+ EXPECT_EQ (attr.value ().bool_value (), true );
296+ ++found_attribute_count;
297+ }
298+ else if (attr.key () == " int_value" )
299+ {
300+ EXPECT_EQ (attr.value ().int_value (), 3 );
301+ ++found_attribute_count;
302+ }
303+ else if (attr.key () == " double_value" )
304+ {
305+ EXPECT_EQ (attr.value ().double_value (), static_cast <double >(1.4 ));
306+ ++found_attribute_count;
307+ }
308+ else if (attr.key () == " bytes_value" )
309+ {
310+ EXPECT_TRUE (0 == memcmp (attr.value ().bytes_value ().data (), " \1\0\3 abc" , 6 ));
311+ EXPECT_EQ (attr.value ().bytes_value ().size (), 6 );
312+ ++found_attribute_count;
313+ }
314+ else if (attr.key () == " bool_array" )
315+ {
316+ EXPECT_EQ (attr.value ().array_value ().values_size (), 3 );
317+ EXPECT_EQ (attr.value ().array_value ().values (0 ).bool_value (), true );
318+ EXPECT_EQ (attr.value ().array_value ().values (1 ).bool_value (), false );
319+ EXPECT_EQ (attr.value ().array_value ().values (2 ).bool_value (), true );
320+ ++found_attribute_count;
321+ }
322+ else if (attr.key () == " int_array" )
323+ {
324+ EXPECT_EQ (attr.value ().array_value ().values_size (), 3 );
325+ EXPECT_EQ (attr.value ().array_value ().values (0 ).int_value (), 1 );
326+ EXPECT_EQ (attr.value ().array_value ().values (1 ).int_value (), 2 );
327+ EXPECT_EQ (attr.value ().array_value ().values (2 ).int_value (), 3 );
328+ ++found_attribute_count;
329+ }
330+ else if (attr.key () == " double_array" )
331+ {
332+ EXPECT_EQ (attr.value ().array_value ().values_size (), 3 );
333+ EXPECT_EQ (attr.value ().array_value ().values (0 ).double_value (), 1.1 );
334+ EXPECT_EQ (attr.value ().array_value ().values (1 ).double_value (), 2.2 );
335+ EXPECT_EQ (attr.value ().array_value ().values (2 ).double_value (), 3.3 );
336+ ++found_attribute_count;
337+ }
338+ else if (attr.key () == " string_array" )
339+ {
340+ EXPECT_EQ (attr.value ().array_value ().values_size (), 3 );
341+ EXPECT_EQ (attr.value ().array_value ().values (0 ).string_value (), " str1" );
342+ EXPECT_EQ (attr.value ().array_value ().values (1 ).string_value (), " str2" );
343+ EXPECT_EQ (attr.value ().array_value ().values (2 ).string_value (), " str3" );
344+ ++found_attribute_count;
272345 }
273346 }
274- EXPECT_TRUE (found_service_name );
347+ EXPECT_EQ (found_attribute_count, attributes. size () );
275348}
276349
277350TEST (OtlpRecordable, SetResourceWithSchemaURL)
@@ -303,6 +376,12 @@ TEST(OtlpRecordable, SetSingleAttribute)
303376 common::AttributeValue str_val (nostd::string_view (" Test" ));
304377 rec.SetAttribute (str_key, str_val);
305378
379+ nostd::string_view byte_key = " byte_attr" ;
380+ uint8_t byte_arr[] = {' T' , ' e' , ' s' , ' t' };
381+ common::AttributeValue byte_val (
382+ nostd::span<const uint8_t >{reinterpret_cast <const uint8_t *>(byte_arr), 4 });
383+ rec.SetAttribute (byte_key, byte_val);
384+
306385 EXPECT_EQ (rec.span ().attributes (0 ).key (), bool_key);
307386 EXPECT_EQ (rec.span ().attributes (0 ).value ().bool_value (), nostd::get<bool >(bool_val));
308387
@@ -312,6 +391,12 @@ TEST(OtlpRecordable, SetSingleAttribute)
312391 EXPECT_EQ (rec.span ().attributes (2 ).key (), str_key);
313392 EXPECT_EQ (rec.span ().attributes (2 ).value ().string_value (),
314393 nostd::get<nostd::string_view>(str_val).data ());
394+
395+ EXPECT_EQ (rec.span ().attributes (3 ).key (), byte_key);
396+ EXPECT_EQ (rec.span ().attributes (3 ).value ().bytes_value ().size (), 4 );
397+ EXPECT_TRUE (0 == memcmp (reinterpret_cast <const void *>(
398+ rec.span ().attributes (3 ).value ().bytes_value ().data ()),
399+ reinterpret_cast <const void *>(byte_arr), 4 ));
315400}
316401
317402// Test non-int array types. Int array types are tested using templates (see IntAttributeTest)
0 commit comments