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..f1b1c73741 100644 --- a/exporters/otlp/test/otlp_recordable_test.cc +++ b/exporters/otlp/test/otlp_recordable_test.cc @@ -284,6 +284,29 @@ TEST(OtlpRecordable, SetArrayAttribute) } } +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; + + nostd::span span = {}; + 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