// Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 #include "opentelemetry/exporters/otlp/otlp_populate_attribute_utils.h" namespace nostd = opentelemetry::nostd; OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter { namespace otlp { // // See `attribute_value.h` for details. // const int kAttributeValueSize = 16; const int kOwnedAttributeValueSize = 15; void OtlpPopulateAttributeUtils::PopulateAttribute( opentelemetry::proto::common::v1::KeyValue *attribute, nostd::string_view key, const opentelemetry::common::AttributeValue &value) noexcept { if (nullptr == attribute) { return; } // Assert size of variant to ensure that this method gets updated if the variant // definition changes static_assert( nostd::variant_size::value == kAttributeValueSize, "AttributeValue contains unknown type"); attribute->set_key(key.data(), key.size()); if (nostd::holds_alternative(value)) { attribute->mutable_value()->set_bool_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { attribute->mutable_value()->set_int_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { attribute->mutable_value()->set_int_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { attribute->mutable_value()->set_int_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { attribute->mutable_value()->set_int_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { attribute->mutable_value()->set_double_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { attribute->mutable_value()->set_string_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { attribute->mutable_value()->set_string_value(nostd::get(value).data(), nostd::get(value).size()); } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { attribute->mutable_value()->mutable_array_value()->add_values()->set_bool_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { attribute->mutable_value()->mutable_array_value()->add_values()->set_double_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { attribute->mutable_value()->mutable_array_value()->add_values()->set_string_value(val.data(), val.size()); } } } /** Maps from C++ attribute into OTLP proto attribute. */ void OtlpPopulateAttributeUtils::PopulateAttribute( opentelemetry::proto::common::v1::KeyValue *attribute, nostd::string_view key, const opentelemetry::sdk::common::OwnedAttributeValue &value) noexcept { if (nullptr == attribute) { return; } // Assert size of variant to ensure that this method gets updated if the variant // definition changes static_assert(nostd::variant_size::value == kOwnedAttributeValueSize, "OwnedAttributeValue contains unknown type"); attribute->set_key(key.data(), key.size()); if (nostd::holds_alternative(value)) { attribute->mutable_value()->set_bool_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { attribute->mutable_value()->set_int_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { attribute->mutable_value()->set_int_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { attribute->mutable_value()->set_int_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { attribute->mutable_value()->set_int_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { attribute->mutable_value()->set_double_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { attribute->mutable_value()->set_string_value(nostd::get(value)); } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { attribute->mutable_value()->mutable_array_value()->add_values()->set_bool_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { attribute->mutable_value()->mutable_array_value()->add_values()->set_double_value(val); } } else if (nostd::holds_alternative>(value)) { for (const auto &val : nostd::get>(value)) { attribute->mutable_value()->mutable_array_value()->add_values()->set_string_value(val); } } } void OtlpPopulateAttributeUtils::PopulateAttribute( opentelemetry::proto::resource::v1::Resource *proto, const opentelemetry::sdk::resource::Resource &resource) noexcept { if (nullptr == proto) { return; } for (const auto &kv : resource.GetAttributes()) { OtlpPopulateAttributeUtils::PopulateAttribute(proto->add_attributes(), kv.first, kv.second); } } } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE