// Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 #include #include #include #include #include #include #include "opentelemetry/common/attribute_value.h" #include "opentelemetry/exporters/otlp/otlp_populate_attribute_utils.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/nostd/utility.h" #include "opentelemetry/nostd/variant.h" #include "opentelemetry/sdk/common/attribute_utils.h" #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/version.h" // clang-format off #include "opentelemetry/exporters/otlp/protobuf_include_prefix.h" // IWYU pragma: keep #include "opentelemetry/proto/common/v1/common.pb.h" #include "opentelemetry/proto/resource/v1/resource.pb.h" #include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" // IWYU pragma: keep // clang-format on 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::PopulateAnyValue( opentelemetry::proto::common::v1::AnyValue *proto_value, const opentelemetry::common::AttributeValue &value) noexcept { if (nullptr == proto_value) { 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"); if (nostd::holds_alternative(value)) { proto_value->set_bool_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { proto_value->set_int_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { proto_value->set_int_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { proto_value->set_int_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { proto_value->set_int_value( nostd::get(value)); // NOLINT(cppcoreguidelines-narrowing-conversions) } else if (nostd::holds_alternative(value)) { proto_value->set_double_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { proto_value->set_string_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { proto_value->set_string_value(nostd::get(value).data(), nostd::get(value).size()); } else if (nostd::holds_alternative>(value)) { auto array_value = proto_value->mutable_array_value(); for (const auto &val : nostd::get>(value)) { 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)) { 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)) { 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)) { 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)) { 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)) { array_value->add_values()->set_int_value( val); // NOLINT(cppcoreguidelines-narrowing-conversions) } } else if (nostd::holds_alternative>(value)) { auto array_value = proto_value->mutable_array_value(); for (const auto &val : nostd::get>(value)) { 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)) { array_value->add_values()->set_string_value(val.data(), val.size()); } } } void OtlpPopulateAttributeUtils::PopulateAnyValue( opentelemetry::proto::common::v1::AnyValue *proto_value, const opentelemetry::sdk::common::OwnedAttributeValue &value) noexcept { if (nullptr == proto_value) { 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"); if (nostd::holds_alternative(value)) { proto_value->set_bool_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { proto_value->set_int_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { proto_value->set_int_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { proto_value->set_int_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { proto_value->set_int_value( nostd::get(value)); // NOLINT(cppcoreguidelines-narrowing-conversions) } else if (nostd::holds_alternative(value)) { proto_value->set_double_value(nostd::get(value)); } else if (nostd::holds_alternative(value)) { proto_value->set_string_value(nostd::get(value)); } else if (nostd::holds_alternative>(value)) { auto array_value = proto_value->mutable_array_value(); for (const auto val : nostd::get>(value)) { 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)) { 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)) { array_value->add_values()->set_int_value( val); // NOLINT(cppcoreguidelines-narrowing-conversions) } } else if (nostd::holds_alternative>(value)) { auto array_value = proto_value->mutable_array_value(); for (const auto &val : nostd::get>(value)) { 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)) { array_value->add_values()->set_int_value( val); // NOLINT(cppcoreguidelines-narrowing-conversions) } } else if (nostd::holds_alternative>(value)) { auto array_value = proto_value->mutable_array_value(); for (const auto &val : nostd::get>(value)) { 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)) { array_value->add_values()->set_string_value(val); } } } 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()); PopulateAnyValue(attribute->mutable_value(), value); } /** 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()); PopulateAnyValue(attribute->mutable_value(), value); } 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