Skip to content

Commit 4421243

Browse files
authored
Support for publishing resource attirbutes in fluent logger. (open-telemetry#503)
1 parent 6a11053 commit 4421243

File tree

4 files changed

+111
-1
lines changed

4 files changed

+111
-1
lines changed

exporters/fluentd/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ endif()
5656

5757
include_directories(include)
5858

59+
if(OPENTELEMETRY_ENABLE_FLUENT_RESOURCE_PUBLISH_EXPERIMENTAL)
60+
add_definitions(-DOPENTELEMETRY_ENABLE_FLUENT_RESOURCE_PUBLISH_EXPERIMENTAL)
61+
endif()
62+
5963
# create fluentd trace exporter
6064
add_library(opentelemetry_exporter_geneva_trace src/trace/fluentd_exporter.cc
6165
src/trace/recordable.cc)

exporters/fluentd/include/opentelemetry/exporters/fluentd/common/fluentd_common.h

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#pragma once
55

66
#include "opentelemetry/common/attribute_value.h"
7+
#include "opentelemetry/sdk/common/attribute_utils.h"
78
#include "opentelemetry/exporters/fluentd/common/fluentd_logging.h"
89
#include "opentelemetry/nostd/string_view.h"
910
#include "opentelemetry/version.h"
@@ -99,6 +100,93 @@ void inline PopulateAttribute(
99100
}
100101
}
101102

103+
void inline PopulateOwnedAttribute(
104+
nlohmann::json &attribute, nostd::string_view key,
105+
const opentelemetry::sdk::common::OwnedAttributeValue &value) {
106+
// Assert size of variant to ensure that this method gets updated if the
107+
// variant definition changes
108+
static_assert(
109+
nostd::variant_size<opentelemetry::common::AttributeValue>::value ==
110+
kAttributeValueSize + 1,
111+
"AttributeValue contains unknown type");
112+
113+
namespace common = opentelemetry::sdk::common;
114+
switch(value.index()) {
115+
case common::kTypeBool:
116+
attribute[key.data()] = nostd::get<bool>(value);
117+
break;
118+
case common::kTypeInt:
119+
attribute[key.data()] = nostd::get<int>(value);
120+
break;
121+
case common::kTypeUInt:
122+
attribute[key.data()] = nostd::get<unsigned int>(value);
123+
break;
124+
case common::kTypeInt64:
125+
attribute[key.data()] = nostd::get<int64_t>(value);
126+
break;
127+
case common::kTypeDouble:
128+
attribute[key.data()] = nostd::get<double>(value);
129+
break;
130+
case common::kTypeString:
131+
attribute[key.data()] = nostd::get<std::string>(value);
132+
break;
133+
case common::kTypeSpanBool:
134+
attribute[key.data()] = {};
135+
for (const auto &val : nostd::get<std::vector<bool>>(value)) {
136+
attribute[key.data()].push_back(val);
137+
}
138+
break;
139+
case common::kTypeSpanInt:
140+
attribute[key.data()] = {};
141+
for (const auto &val : nostd::get<std::vector<int>>(value)) {
142+
attribute[key.data()].push_back(val);
143+
}
144+
break;
145+
case common::kTypeSpanUInt:
146+
attribute[key.data()] = {};
147+
for (const auto &val : nostd::get<std::vector<unsigned int>>(value)) {
148+
attribute[key.data()].push_back(val);
149+
}
150+
break;
151+
case common::kTypeSpanInt64:
152+
attribute[key.data()] = {};
153+
for (const auto &val : nostd::get<std::vector<int64_t>>(value)) {
154+
attribute[key.data()].push_back(val);
155+
}
156+
break;
157+
case common::kTypeSpanDouble:
158+
attribute[key.data()] = {};
159+
for (const auto &val : nostd::get<std::vector<double>>(value)) {
160+
attribute[key.data()].push_back(val);
161+
}
162+
break;
163+
case common::kTypeSpanString:
164+
attribute[key.data()] = {};
165+
for (const auto &val :
166+
nostd::get<std::vector<std::string>>(value)) {
167+
attribute[key.data()].push_back(val);
168+
}
169+
break;
170+
case common::kTypeUInt64:
171+
attribute[key.data()] = nostd::get<uint64_t>(value);
172+
break;
173+
case common::kTypeSpanUInt64:
174+
attribute[key.data()] = {};
175+
for (const auto &val : nostd::get<std::vector<uint64_t>>(value)) {
176+
attribute[key.data()].push_back(val);
177+
}
178+
break;
179+
case common::kTypeSpanByte:
180+
attribute[key.data()] = {};
181+
for (const auto &val : nostd::get<std::vector<uint8_t>>(value)) {
182+
attribute[key.data()].push_back(val);
183+
}
184+
break;
185+
default:
186+
break;
187+
}
188+
}
189+
102190
inline std::string AttributeValueToString(
103191
const opentelemetry::common::AttributeValue &value) {
104192
std::string result;

exporters/fluentd/include/opentelemetry/exporters/fluentd/log/recordable.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ class Recordable final : public opentelemetry::sdk::logs::Recordable {
4444
* @param Resource the resource to set
4545
*/
4646
void SetResource(const opentelemetry::sdk::resource::Resource
47+
#ifdef OPENTELEMETRY_ENABLE_FLUENT_RESOURCE_PUBLISH_EXPERIMENTAL
48+
&resource) noexcept override;
49+
#else
4750
&resource) noexcept override {} // Not Supported
48-
51+
#endif
4952
/**
5053
* Set an attribute of a log.
5154
* @param key the key of the attribute

exporters/fluentd/src/log/recordable.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "opentelemetry/exporters/fluentd/common/fluentd_common.h"
66
#include "opentelemetry/exporters/fluentd/common/fluentd_logging.h"
77

8+
#include "opentelemetry/sdk/resource/resource.h"
89
#include "opentelemetry/logs/severity.h"
910
#include "opentelemetry/trace/span_id.h"
1011
#include "opentelemetry/trace/trace_id.h"
@@ -54,6 +55,20 @@ void Recordable::SetSpanId(const opentelemetry::trace::SpanId &span_id) noexcept
5455
json_[FLUENT_FIELD_SPAN_ID] = std::string(span_id_lower_base16, 16);
5556
}
5657

58+
#ifdef OPENTELEMETRY_ENABLE_FLUENT_RESOURCE_PUBLISH_EXPERIMENTAL
59+
void Recordable::SetResource(const opentelemetry::sdk::resource::Resource
60+
&resource) noexcept {
61+
if(resource.GetAttributes().size() > 0) {
62+
if (!json_.contains(FLUENT_FIELD_PROPERTIES)) {
63+
json_[FLUENT_FIELD_PROPERTIES] = nlohmann::json::object();
64+
}
65+
}
66+
for(const auto& [key, value] : resource.GetAttributes()) {
67+
fluentd_common::PopulateOwnedAttribute(json_[FLUENT_FIELD_PROPERTIES], key, value);
68+
}
69+
}
70+
#endif
71+
5772
void Recordable::SetAttribute(
5873
nostd::string_view key,
5974
const opentelemetry::common::AttributeValue &value) noexcept {

0 commit comments

Comments
 (0)