From ef4abd9c9b279f729164e807933f041bf47f2369 Mon Sep 17 00:00:00 2001 From: Lalit Date: Thu, 5 Dec 2024 23:01:51 -0800 Subject: [PATCH] changes --- exporters/fluentd/CMakeLists.txt | 4 + .../exporters/fluentd/common/fluentd_common.h | 88 +++++++++++++++++++ .../exporters/fluentd/log/recordable.h | 5 +- exporters/fluentd/src/log/recordable.cc | 15 ++++ 4 files changed, 111 insertions(+), 1 deletion(-) diff --git a/exporters/fluentd/CMakeLists.txt b/exporters/fluentd/CMakeLists.txt index 3e9bc6866..f869fda09 100644 --- a/exporters/fluentd/CMakeLists.txt +++ b/exporters/fluentd/CMakeLists.txt @@ -56,6 +56,10 @@ endif() include_directories(include) +if(OPENTELEMETRY_ENABLE_FLUENT_RESOURCE_PUBLISH_EXPERIMENTAL) + add_definitions(-DOPENTELEMETRY_ENABLE_FLUENT_RESOURCE_PUBLISH_EXPERIMENTAL) +endif() + # create fluentd trace exporter add_library(opentelemetry_exporter_geneva_trace src/trace/fluentd_exporter.cc src/trace/recordable.cc) diff --git a/exporters/fluentd/include/opentelemetry/exporters/fluentd/common/fluentd_common.h b/exporters/fluentd/include/opentelemetry/exporters/fluentd/common/fluentd_common.h index 4fc91c596..8b21d82a7 100644 --- a/exporters/fluentd/include/opentelemetry/exporters/fluentd/common/fluentd_common.h +++ b/exporters/fluentd/include/opentelemetry/exporters/fluentd/common/fluentd_common.h @@ -4,6 +4,7 @@ #pragma once #include "opentelemetry/common/attribute_value.h" +#include "opentelemetry/sdk/common/attribute_utils.h" #include "opentelemetry/exporters/fluentd/common/fluentd_logging.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/version.h" @@ -99,6 +100,93 @@ void inline PopulateAttribute( } } +void inline PopulateOwnedAttribute( + nlohmann::json &attribute, nostd::string_view key, + const opentelemetry::sdk::common::OwnedAttributeValue &value) { + // Assert size of variant to ensure that this method gets updated if the + // variant definition changes + static_assert( + nostd::variant_size::value == + kAttributeValueSize + 1, + "AttributeValue contains unknown type"); + + namespace common = opentelemetry::sdk::common; + switch(value.index()) { + case common::kTypeBool: + attribute[key.data()] = nostd::get(value); + break; + case common::kTypeInt: + attribute[key.data()] = nostd::get(value); + break; + case common::kTypeUInt: + attribute[key.data()] = nostd::get(value); + break; + case common::kTypeInt64: + attribute[key.data()] = nostd::get(value); + break; + case common::kTypeDouble: + attribute[key.data()] = nostd::get(value); + break; + case common::kTypeString: + attribute[key.data()] = nostd::get(value); + break; + case common::kTypeSpanBool: + attribute[key.data()] = {}; + for (const auto &val : nostd::get>(value)) { + attribute[key.data()].push_back(val); + } + break; + case common::kTypeSpanInt: + attribute[key.data()] = {}; + for (const auto &val : nostd::get>(value)) { + attribute[key.data()].push_back(val); + } + break; + case common::kTypeSpanUInt: + attribute[key.data()] = {}; + for (const auto &val : nostd::get>(value)) { + attribute[key.data()].push_back(val); + } + break; + case common::kTypeSpanInt64: + attribute[key.data()] = {}; + for (const auto &val : nostd::get>(value)) { + attribute[key.data()].push_back(val); + } + break; + case common::kTypeSpanDouble: + attribute[key.data()] = {}; + for (const auto &val : nostd::get>(value)) { + attribute[key.data()].push_back(val); + } + break; + case common::kTypeSpanString: + attribute[key.data()] = {}; + for (const auto &val : + nostd::get>(value)) { + attribute[key.data()].push_back(val); + } + break; + case common::kTypeUInt64: + attribute[key.data()] = nostd::get(value); + break; + case common::kTypeSpanUInt64: + attribute[key.data()] = {}; + for (const auto &val : nostd::get>(value)) { + attribute[key.data()].push_back(val); + } + break; + case common::kTypeSpanByte: + attribute[key.data()] = {}; + for (const auto &val : nostd::get>(value)) { + attribute[key.data()].push_back(val); + } + break; + default: + break; + } +} + inline std::string AttributeValueToString( const opentelemetry::common::AttributeValue &value) { std::string result; diff --git a/exporters/fluentd/include/opentelemetry/exporters/fluentd/log/recordable.h b/exporters/fluentd/include/opentelemetry/exporters/fluentd/log/recordable.h index 9687f03e9..f8082d975 100644 --- a/exporters/fluentd/include/opentelemetry/exporters/fluentd/log/recordable.h +++ b/exporters/fluentd/include/opentelemetry/exporters/fluentd/log/recordable.h @@ -44,8 +44,11 @@ class Recordable final : public opentelemetry::sdk::logs::Recordable { * @param Resource the resource to set */ void SetResource(const opentelemetry::sdk::resource::Resource +#ifdef OPENTELEMETRY_ENABLE_FLUENT_RESOURCE_PUBLISH_EXPERIMENTAL + &resource) noexcept override; +#else &resource) noexcept override {} // Not Supported - +#endif /** * Set an attribute of a log. * @param key the key of the attribute diff --git a/exporters/fluentd/src/log/recordable.cc b/exporters/fluentd/src/log/recordable.cc index 657f924d1..831a2de78 100644 --- a/exporters/fluentd/src/log/recordable.cc +++ b/exporters/fluentd/src/log/recordable.cc @@ -5,6 +5,7 @@ #include "opentelemetry/exporters/fluentd/common/fluentd_common.h" #include "opentelemetry/exporters/fluentd/common/fluentd_logging.h" +#include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/logs/severity.h" #include "opentelemetry/trace/span_id.h" #include "opentelemetry/trace/trace_id.h" @@ -54,6 +55,20 @@ void Recordable::SetSpanId(const opentelemetry::trace::SpanId &span_id) noexcept json_[FLUENT_FIELD_SPAN_ID] = std::string(span_id_lower_base16, 16); } +#ifdef OPENTELEMETRY_ENABLE_FLUENT_RESOURCE_PUBLISH_EXPERIMENTAL +void Recordable::SetResource(const opentelemetry::sdk::resource::Resource + &resource) noexcept { + if(resource.GetAttributes().size() > 0) { + if (!json_.contains(FLUENT_FIELD_PROPERTIES)) { + json_[FLUENT_FIELD_PROPERTIES] = nlohmann::json::object(); + } + } + for(const auto& [key, value] : resource.GetAttributes()) { + fluentd_common::PopulateOwnedAttribute(json_[FLUENT_FIELD_PROPERTIES], key, value); + } +} +#endif + void Recordable::SetAttribute( nostd::string_view key, const opentelemetry::common::AttributeValue &value) noexcept {