From 0ecceaab031a9a9ce10a08cffa5309c2f02bd4c4 Mon Sep 17 00:00:00 2001 From: Matthijs Brobbel Date: Wed, 31 May 2023 11:58:21 +0200 Subject: [PATCH 1/2] Support empty arrays in `OtlpRecordable` attributes --- .../otlp/src/otlp_populate_attribute_utils.cc | 45 ++++++++++++------- exporters/otlp/test/otlp_recordable_test.cc | 13 ++++++ 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/exporters/otlp/src/otlp_populate_attribute_utils.cc b/exporters/otlp/src/otlp_populate_attribute_utils.cc index d86953506c..69a284835a 100644 --- a/exporters/otlp/src/otlp_populate_attribute_utils.cc +++ b/exporters/otlp/src/otlp_populate_attribute_utils.cc @@ -67,58 +67,66 @@ void OtlpPopulateAttributeUtils::PopulateAnyValue( } else if (nostd::holds_alternative>(value)) { + auto array_value = proto_value->mutable_array_value(); for (const auto &val : nostd::get>(value)) { - proto_value->mutable_array_value()->add_values()->set_int_value(val); + array_value->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { + auto array_value = proto_value->mutable_array_value(); for (const auto &val : nostd::get>(value)) { - proto_value->mutable_array_value()->add_values()->set_bool_value(val); + array_value->add_values()->set_bool_value(val); } } else if (nostd::holds_alternative>(value)) { + auto array_value = proto_value->mutable_array_value(); for (const auto &val : nostd::get>(value)) { - proto_value->mutable_array_value()->add_values()->set_int_value(val); + array_value->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { + auto array_value = proto_value->mutable_array_value(); for (const auto &val : nostd::get>(value)) { - proto_value->mutable_array_value()->add_values()->set_int_value(val); + array_value->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { + auto array_value = proto_value->mutable_array_value(); for (const auto &val : nostd::get>(value)) { - proto_value->mutable_array_value()->add_values()->set_int_value(val); + array_value->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { + auto array_value = proto_value->mutable_array_value(); for (const auto &val : nostd::get>(value)) { - proto_value->mutable_array_value()->add_values()->set_int_value(val); + array_value->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { + auto array_value = proto_value->mutable_array_value(); for (const auto &val : nostd::get>(value)) { - proto_value->mutable_array_value()->add_values()->set_double_value(val); + array_value->add_values()->set_double_value(val); } } else if (nostd::holds_alternative>(value)) { + auto array_value = proto_value->mutable_array_value(); for (const auto &val : nostd::get>(value)) { - proto_value->mutable_array_value()->add_values()->set_string_value(val.data(), val.size()); + array_value->add_values()->set_string_value(val.data(), val.size()); } } } @@ -168,51 +176,58 @@ void OtlpPopulateAttributeUtils::PopulateAnyValue( } else if (nostd::holds_alternative>(value)) { + auto array_value = proto_value->mutable_array_value(); for (const auto val : nostd::get>(value)) { - proto_value->mutable_array_value()->add_values()->set_bool_value(val); + array_value->add_values()->set_bool_value(val); } } else if (nostd::holds_alternative>(value)) { + auto array_value = proto_value->mutable_array_value(); for (const auto &val : nostd::get>(value)) { - proto_value->mutable_array_value()->add_values()->set_int_value(val); + array_value->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { + auto array_value = proto_value->mutable_array_value(); for (const auto &val : nostd::get>(value)) { - proto_value->mutable_array_value()->add_values()->set_int_value(val); + array_value->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { + auto array_value = proto_value->mutable_array_value(); for (const auto &val : nostd::get>(value)) { - proto_value->mutable_array_value()->add_values()->set_int_value(val); + array_value->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { + auto array_value = proto_value->mutable_array_value(); for (const auto &val : nostd::get>(value)) { - proto_value->mutable_array_value()->add_values()->set_int_value(val); + array_value->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { + auto array_value = proto_value->mutable_array_value(); for (const auto &val : nostd::get>(value)) { - proto_value->mutable_array_value()->add_values()->set_double_value(val); + array_value->add_values()->set_double_value(val); } } else if (nostd::holds_alternative>(value)) { + auto array_value = proto_value->mutable_array_value(); for (const auto &val : nostd::get>(value)) { - proto_value->mutable_array_value()->add_values()->set_string_value(val); + array_value->add_values()->set_string_value(val); } } } diff --git a/exporters/otlp/test/otlp_recordable_test.cc b/exporters/otlp/test/otlp_recordable_test.cc index abad2991b7..e8af9f13b4 100644 --- a/exporters/otlp/test/otlp_recordable_test.cc +++ b/exporters/otlp/test/otlp_recordable_test.cc @@ -284,6 +284,19 @@ TEST(OtlpRecordable, SetArrayAttribute) } } +// Test empty array. +TEST(OtlpRecordable, SetEmptyArrayAttribute) +{ + OtlpRecordable rec; + + std::vector empty_array = {}; + nostd::span span(empty_array); + rec.SetAttribute("empty_arr_attr", span); + + EXPECT_TRUE(rec.span().attributes(0).value().has_array_value()); + EXPECT_TRUE(rec.span().attributes(0).value().array_value().values().empty()); +} + /** * AttributeValue can contain different int types, such as int, int64_t, * unsigned int, and uint64_t. To avoid writing test cases for each, we can From c6dfea75fb3b7cef6f6bb1c4572b3c0dc0fb2c91 Mon Sep 17 00:00:00 2001 From: Matthijs Brobbel Date: Wed, 31 May 2023 15:42:19 +0200 Subject: [PATCH 2/2] Use typed test for `OtlpRecordable` empty array test --- exporters/otlp/test/otlp_recordable_test.cc | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/exporters/otlp/test/otlp_recordable_test.cc b/exporters/otlp/test/otlp_recordable_test.cc index e8af9f13b4..f1b1c73741 100644 --- a/exporters/otlp/test/otlp_recordable_test.cc +++ b/exporters/otlp/test/otlp_recordable_test.cc @@ -284,13 +284,23 @@ TEST(OtlpRecordable, SetArrayAttribute) } } -// Test empty array. -TEST(OtlpRecordable, SetEmptyArrayAttribute) +template +struct EmptyArrayAttributeTest : public testing::Test +{ + using ElementType = T; +}; + +using ArrayElementTypes = + testing::Types; +TYPED_TEST_SUITE(EmptyArrayAttributeTest, ArrayElementTypes); + +// Test empty arrays. +TYPED_TEST(EmptyArrayAttributeTest, SetEmptyArrayAttribute) { + using ArrayElementType = typename TestFixture::ElementType; OtlpRecordable rec; - std::vector empty_array = {}; - nostd::span span(empty_array); + nostd::span span = {}; rec.SetAttribute("empty_arr_attr", span); EXPECT_TRUE(rec.span().attributes(0).value().has_array_value());