Skip to content

Commit

Permalink
[EXPORTER] Support empty arrays in OtlpRecordable attributes (#2166)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrobbel authored May 31, 2023
1 parent 8b7f9b4 commit 0eca570
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
45 changes: 30 additions & 15 deletions exporters/otlp/src/otlp_populate_attribute_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,58 +67,66 @@ void OtlpPopulateAttributeUtils::PopulateAnyValue(
}
else if (nostd::holds_alternative<nostd::span<const uint8_t>>(value))
{
auto array_value = proto_value->mutable_array_value();
for (const auto &val : nostd::get<nostd::span<const uint8_t>>(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<nostd::span<const bool>>(value))
{
auto array_value = proto_value->mutable_array_value();
for (const auto &val : nostd::get<nostd::span<const bool>>(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<nostd::span<const int>>(value))
{
auto array_value = proto_value->mutable_array_value();
for (const auto &val : nostd::get<nostd::span<const int>>(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<nostd::span<const int64_t>>(value))
{
auto array_value = proto_value->mutable_array_value();
for (const auto &val : nostd::get<nostd::span<const int64_t>>(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<nostd::span<const unsigned int>>(value))
{
auto array_value = proto_value->mutable_array_value();
for (const auto &val : nostd::get<nostd::span<const unsigned int>>(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<nostd::span<const uint64_t>>(value))
{
auto array_value = proto_value->mutable_array_value();
for (const auto &val : nostd::get<nostd::span<const uint64_t>>(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<nostd::span<const double>>(value))
{
auto array_value = proto_value->mutable_array_value();
for (const auto &val : nostd::get<nostd::span<const double>>(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<nostd::span<const nostd::string_view>>(value))
{
auto array_value = proto_value->mutable_array_value();
for (const auto &val : nostd::get<nostd::span<const nostd::string_view>>(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());
}
}
}
Expand Down Expand Up @@ -168,51 +176,58 @@ void OtlpPopulateAttributeUtils::PopulateAnyValue(
}
else if (nostd::holds_alternative<std::vector<bool>>(value))
{
auto array_value = proto_value->mutable_array_value();
for (const auto val : nostd::get<std::vector<bool>>(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<std::vector<int32_t>>(value))
{
auto array_value = proto_value->mutable_array_value();
for (const auto &val : nostd::get<std::vector<int32_t>>(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<std::vector<uint32_t>>(value))
{
auto array_value = proto_value->mutable_array_value();
for (const auto &val : nostd::get<std::vector<uint32_t>>(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<std::vector<int64_t>>(value))
{
auto array_value = proto_value->mutable_array_value();
for (const auto &val : nostd::get<std::vector<int64_t>>(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<std::vector<uint64_t>>(value))
{
auto array_value = proto_value->mutable_array_value();
for (const auto &val : nostd::get<std::vector<uint64_t>>(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<std::vector<double>>(value))
{
auto array_value = proto_value->mutable_array_value();
for (const auto &val : nostd::get<std::vector<double>>(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<std::vector<std::string>>(value))
{
auto array_value = proto_value->mutable_array_value();
for (const auto &val : nostd::get<std::vector<std::string>>(value))
{
proto_value->mutable_array_value()->add_values()->set_string_value(val);
array_value->add_values()->set_string_value(val);
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions exporters/otlp/test/otlp_recordable_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,29 @@ TEST(OtlpRecordable, SetArrayAttribute)
}
}

template <typename T>
struct EmptyArrayAttributeTest : public testing::Test
{
using ElementType = T;
};

using ArrayElementTypes =
testing::Types<bool, double, nostd::string_view, uint8_t, int, int64_t, unsigned int, uint64_t>;
TYPED_TEST_SUITE(EmptyArrayAttributeTest, ArrayElementTypes);

// Test empty arrays.
TYPED_TEST(EmptyArrayAttributeTest, SetEmptyArrayAttribute)
{
using ArrayElementType = typename TestFixture::ElementType;
OtlpRecordable rec;

nostd::span<const ArrayElementType> 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
Expand Down

0 comments on commit 0eca570

Please sign in to comment.