Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EXPORTER] Support empty arrays in OtlpRecordable attributes #2166

Merged
merged 2 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 13 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,19 @@ TEST(OtlpRecordable, SetArrayAttribute)
}
}

// Test empty array.
TEST(OtlpRecordable, SetEmptyArrayAttribute)
{
OtlpRecordable rec;

std::vector<int64_t> empty_array = {};
nostd::span<const int64_t> 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
Expand Down