-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathfluentd_exporter.cc
304 lines (269 loc) · 7.99 KB
/
fluentd_exporter.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#include "opentelemetry/exporters/fluentd/trace/fluentd_exporter.h"
#include "opentelemetry/exporters/fluentd/trace/recordable.h"
#include "opentelemetry/ext/http/common/url_parser.h"
#include "opentelemetry/exporters/fluentd/common/fluentd_logging.h"
#include "nlohmann/json.hpp"
#include <cassert>
using UrlParser = opentelemetry::ext::http::common::UrlParser;
using namespace nlohmann;
OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter {
namespace fluentd {
namespace trace {
/**
* @brief Scheme for tcp:// stream
*/
constexpr const char *kTCP = "tcp";
/**
* @brief Scheme for udp:// datagram
*/
constexpr const char *kUDP = "udp";
/**
* @brief Scheme for unix:// domain socket
*/
constexpr const char *kUNIX = "unix";
/**
* @brief Create FluentD exporter with options
* @param options
*/
FluentdExporter::FluentdExporter(
const fluentd_common::FluentdExporterOptions &options)
: options_(options) {
Initialize();
}
/**
* @brief Create FluentD exporter with default options
*/
FluentdExporter::FluentdExporter()
: options_(fluentd_common::FluentdExporterOptions()) {
Initialize();
}
/**
* @brief Create new Recordable
* @return Recordable
*/
std::unique_ptr<sdk::trace::Recordable>
FluentdExporter::MakeRecordable() noexcept {
return std::unique_ptr<sdk::trace::Recordable>(new opentelemetry::exporter::fluentd::trace::Recordable(FLUENT_VALUE_SPAN, options_.include_trace_state_for_span));
}
/**
* @brief Export spans.
* @param spans
* @return Export result.
*/
sdk::common::ExportResult FluentdExporter::Export(
const nostd::span<std::unique_ptr<sdk::trace::Recordable>>
&spans) noexcept {
// Return failure if this exporter has been shutdown
if (is_shutdown_) {
return sdk::common::ExportResult::kFailure;
}
// If no spans in Recordable, then return error.
if (spans.size() == 0) {
return sdk::common::ExportResult::kFailure;
}
json events = {};
{
// Write all Spans first
json obj = json::array();
obj.push_back(FLUENT_VALUE_SPAN);
json spanevents = json::array();
for (auto &recordable : spans) {
auto rec = std::unique_ptr<Recordable>(
static_cast<Recordable *>(recordable.release()));
if (rec != nullptr) {
auto span = rec->span();
// Emit "Span" as fluentd event
json record = json::array();
record.push_back(span["options"][FLUENT_FIELD_ENDTTIME]);
json fields = {};
for (auto &kv : span["options"].items()) {
fields[kv.key()] = kv.value();
}
record.push_back(fields);
spanevents.push_back(record);
// Iterate over all events added on this span
for (auto &v : span["events"]) {
auto &event = v[1];
std::string name = event[FLUENT_FIELD_NAME];
event[FLUENT_FIELD_SPAN_ID] = span["options"][FLUENT_FIELD_SPAN_ID];
event[FLUENT_FIELD_TRACE_ID] = span["options"][FLUENT_FIELD_TRACE_ID];
// Event Time flows as a separate field in fluentd.
// However, if we may need to consider addding
// span["options"][FLUENT_FIELD_TIME]
//
// Complete list of all Span attributes :
// for (auto &kv : span["options"].items()) { ... }
// Group all events by matching event name in array.
// This array is translated into FluentD forward payload.
if (!events.contains(name)) {
events[name] = json::array();
}
events[name].push_back(v);
}
}
}
obj.push_back(spanevents);
LOG_TRACE("sending %zu Span event(s)", obj[1].size());
std::vector<uint8_t> msg = nlohmann::json::to_msgpack(obj);
// Immediately send the Span event(s)
bool result = Send(msg);
if (!result) {
return sdk::common::ExportResult::kFailure;
}
}
if (options_.convert_event_to_trace) {
for (auto &kv : events.items()) {
json obj = json::array();
obj.push_back(kv.key());
json otherevents = json::array();
for (auto &v : kv.value()) {
otherevents.push_back(v);
}
obj.push_back(otherevents);
LOG_TRACE("sending %zu %s events", obj[1].size(), kv.key().c_str());
std::vector<uint8_t> msg = nlohmann::json::to_msgpack(obj);
// Immediately send the Span event(s)
bool result = Send(msg);
if (!result) {
return sdk::common::ExportResult::kFailure;
}
}
}
// At this point we always return success because there is no way
// to know if delivery is gonna succeed with multiple retries.
return sdk::common::ExportResult::kSuccess;
}
/**
* @brief Initialize FluentD exporter socket.
* @return true if end-point settings have been accepted.
*/
bool FluentdExporter::Initialize() {
UrlParser url(options_.endpoint);
bool is_unix_domain = false;
if (url.scheme_ == kTCP) {
socketparams_ = {AF_INET, SOCK_STREAM, 0};
} else if (url.scheme_ == kUDP) {
socketparams_ = {AF_INET, SOCK_DGRAM, 0};
}
#ifdef HAVE_UNIX_DOMAIN
else if (url.scheme_ == kUNIX) {
socketparams_ = {AF_UNIX, SOCK_STREAM, 0};
is_unix_domain = true;
}
#endif
else {
#if defined(__EXCEPTIONS)
// Customers MUST specify valid end-point configuration
throw std::runtime_error("Invalid endpoint!");
#endif
return false;
}
addr_.reset(
new SocketTools::SocketAddr(options_.endpoint.c_str(), is_unix_domain));
if (addr_->m_data_in.sin_family != AF_UNIX &&
addr_->m_data_in.sin_family != AF_INET &&
addr_->m_data_in.sin_family != AF_INET6)
{
LOG_ERROR("Invalid endpoint! %s", options_.endpoint.c_str());
return false;
}
LOG_TRACE("connecting to %s", addr_->toString().c_str());
return true;
}
/**
* @brief Establish connection to FluentD
* @return true if connected successfully.
*/
bool FluentdExporter::Connect() {
if (!connected_) {
socket_ = SocketTools::Socket(socketparams_);
connected_ = socket_.connect(*addr_);
if (!connected_) {
LOG_ERROR("Unable to connect to %s", options_.endpoint.c_str());
return false;
}
}
// Connected or already connected
return true;
}
/**
* @brief Try to upload fluentd forward protocol packet.
* This method respects the retry options for connects
* and upload retries.
*
* @param packet
* @return true if packet got delivered.
*/
bool FluentdExporter::Send(std::vector<uint8_t> &packet) {
size_t retryCount = options_.retry_count;
auto retrySleepMs = std::chrono::milliseconds(options_.retry_delay_ms);
while (retryCount--) {
int error_code = 0;
// Check if socket is Okay
if (connected_) {
socket_.getsockopt(SOL_SOCKET, SO_ERROR, error_code);
if (error_code != 0) {
connected_ = false;
}
}
// Reconnect if not Okay
if (!connected_) {
Initialize(); // try a DNS lookup, etc
// Establishing socket connection may take time
if (!Connect()) {
if (retryCount) {
std::this_thread::sleep_for(retrySleepMs);
}
continue;
}
LOG_DEBUG("socket connected");
}
// Try to write
size_t sentSize = socket_.writeall(packet);
if (packet.size() == sentSize) {
LOG_DEBUG("send successful");
Disconnect();
LOG_DEBUG("socket disconnected");
return true;
}
LOG_WARN("send failed, retrying %u ...", (unsigned int)retryCount);
Disconnect();
// Retry to connect and/or send
if (retryCount) {
std::this_thread::sleep_for(retrySleepMs);
}
}
LOG_ERROR("send failed!");
Disconnect();
return false;
}
/**
* @brief Disconnect FluentD socket or datagram.
* @return
*/
bool FluentdExporter::Disconnect() {
if (connected_) {
connected_ = false;
if (!socket_.invalid()) {
socket_.close();
return true;
}
}
return false;
}
/**
* @brief Shutdown FluentD exporter
* @param
* @return
*/
bool FluentdExporter::Shutdown(std::chrono::microseconds) noexcept {
is_shutdown_ = true;
return true;
}
} // namespace trace
} // namespace fluentd
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE