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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
// SPDX-License-Identifier: GPL-3.0-or-later
#define EXPORTING_INTERNALS
#include "mongodb.h"
#define CONFIG_FILE_LINE_MAX ((CONFIG_MAX_NAME + CONFIG_MAX_VALUE + 1024) * 2)
/**
* Initialize MongoDB connector specific data, including a ring buffer
*
* @param instance an instance data structure.
* @return Returns 0 on success, 1 on failure.
*/
int mongodb_init(struct instance *instance)
{
struct mongodb_specific_config *connector_specific_config = instance->config.connector_specific_config;
mongoc_uri_t *uri;
bson_error_t bson_error;
if (unlikely(!connector_specific_config->collection || !*connector_specific_config->collection)) {
error("EXPORTING: collection name is a mandatory MongoDB parameter, but it is not configured");
return 1;
}
uri = mongoc_uri_new_with_error(instance->config.destination, &bson_error);
if (unlikely(!uri)) {
error(
"EXPORTING: failed to parse URI: %s. Error message: %s", instance->config.destination, bson_error.message);
return 1;
}
int32_t socket_timeout =
mongoc_uri_get_option_as_int32(uri, MONGOC_URI_SOCKETTIMEOUTMS, instance->config.timeoutms);
if (!mongoc_uri_set_option_as_int32(uri, MONGOC_URI_SOCKETTIMEOUTMS, socket_timeout)) {
error("EXPORTING: failed to set %s to the value %d", MONGOC_URI_SOCKETTIMEOUTMS, socket_timeout);
return 1;
};
struct mongodb_specific_data *connector_specific_data =
(struct mongodb_specific_data *)instance->connector_specific_data;
connector_specific_data->client = mongoc_client_new_from_uri(uri);
if (unlikely(!connector_specific_data->client)) {
error("EXPORTING: failed to create a new client");
return 1;
}
if (!mongoc_client_set_appname(connector_specific_data->client, "netdata")) {
error("EXPORTING: failed to set client appname");
};
connector_specific_data->collection = mongoc_client_get_collection(
connector_specific_data->client, connector_specific_config->database, connector_specific_config->collection);
mongoc_uri_destroy(uri);
// create a ring buffer
struct bson_buffer *first_buffer = NULL;
if (instance->config.buffer_on_failures < 2)
instance->config.buffer_on_failures = 1;
else
instance->config.buffer_on_failures -= 1;
for (int i = 0; i < instance->config.buffer_on_failures; i++) {
struct bson_buffer *current_buffer = callocz(1, sizeof(struct bson_buffer));
if (!connector_specific_data->first_buffer)
first_buffer = current_buffer;
else
current_buffer->next = connector_specific_data->first_buffer;
connector_specific_data->first_buffer = current_buffer;
}
first_buffer->next = connector_specific_data->first_buffer;
connector_specific_data->last_buffer = connector_specific_data->first_buffer;
return 0;
}
/**
* Initialize a MongoDB connector instance
*
* @param instance an instance data structure.
* @return Returns 0 on success, 1 on failure.
*/
int init_mongodb_instance(struct instance *instance)
{
instance->worker = mongodb_connector_worker;
instance->start_batch_formatting = NULL;
instance->start_host_formatting = format_host_labels_json_plaintext;
instance->start_chart_formatting = NULL;
if (EXPORTING_OPTIONS_DATA_SOURCE(instance->config.options) == EXPORTING_SOURCE_DATA_AS_COLLECTED)
instance->metric_formatting = format_dimension_collected_json_plaintext;
else
instance->metric_formatting = format_dimension_stored_json_plaintext;
instance->end_chart_formatting = NULL;
instance->variables_formatting = NULL;
instance->end_host_formatting = flush_host_labels;
instance->end_batch_formatting = format_batch_mongodb;
instance->prepare_header = NULL;
instance->check_response = NULL;
instance->buffer = (void *)buffer_create(0);
if (!instance->buffer) {
error("EXPORTING: cannot create buffer for MongoDB exporting connector instance %s", instance->config.name);
return 1;
}
if (uv_mutex_init(&instance->mutex))
return 1;
if (uv_cond_init(&instance->cond_var))
return 1;
struct mongodb_specific_data *connector_specific_data = callocz(1, sizeof(struct mongodb_specific_data));
instance->connector_specific_data = (void *)connector_specific_data;
instance->config.timeoutms =
(instance->config.update_every >= 2) ? (instance->engine->config.update_every * MSEC_PER_SEC - 500) : 1000;
if (!instance->engine->mongoc_initialized) {
mongoc_init();
instance->engine->mongoc_initialized = 1;
}
if (unlikely(mongodb_init(instance))) {
error("EXPORTING: cannot initialize MongoDB exporting connector");
return 1;
}
return 0;
}
/**
* Free an array of BSON structures
*
* @param insert an array of documents.
* @param documents_inserted the number of documents inserted.
*/
void free_bson(bson_t **insert, size_t documents_inserted)
{
size_t i;
for (i = 0; i < documents_inserted; i++)
bson_destroy(insert[i]);
freez(insert);
}
/**
* Format a batch for the MongoDB connector
*
* @param instance an instance data structure.
* @return Returns 0 on success, 1 on failure.
*/
int format_batch_mongodb(struct instance *instance)
{
struct mongodb_specific_data *connector_specific_data =
(struct mongodb_specific_data *)instance->connector_specific_data;
struct stats *stats = &instance->stats;
bson_t **insert = connector_specific_data->last_buffer->insert;
if (insert) {
// ring buffer is full, reuse the oldest element
connector_specific_data->first_buffer = connector_specific_data->first_buffer->next;
free_bson(insert, connector_specific_data->last_buffer->documents_inserted);
connector_specific_data->total_documents_inserted -= connector_specific_data->last_buffer->documents_inserted;
stats->buffered_bytes -= connector_specific_data->last_buffer->buffered_bytes;
}
insert = callocz((size_t)stats->buffered_metrics, sizeof(bson_t *));
connector_specific_data->last_buffer->insert = insert;
BUFFER *buffer = (BUFFER *)instance->buffer;
char *start = (char *)buffer_tostring(buffer);
char *end = start;
size_t documents_inserted = 0;
while (*end && documents_inserted <= (size_t)stats->buffered_metrics) {
while (*end && *end != '\n')
end++;
if (likely(*end)) {
*end = '\0';
end++;
} else {
break;
}
bson_error_t bson_error;
insert[documents_inserted] = bson_new_from_json((const uint8_t *)start, -1, &bson_error);
if (unlikely(!insert[documents_inserted])) {
error(
"EXPORTING: Failed creating a BSON document from a JSON string \"%s\" : %s", start, bson_error.message);
free_bson(insert, documents_inserted);
return 1;
}
start = end;
documents_inserted++;
}
stats->buffered_bytes += connector_specific_data->last_buffer->buffered_bytes = buffer_strlen(buffer);
buffer_flush(buffer);
// The stats->buffered_metrics is used in the MongoDB batch formatting as a variable for the number
// of metrics, added in the current iteration, so we are clearing it here. We will use the
// connector_specific_data->total_documents_inserted in the worker to show the statistics.
stats->buffered_metrics = 0;
connector_specific_data->total_documents_inserted += documents_inserted;
connector_specific_data->last_buffer->documents_inserted = documents_inserted;
connector_specific_data->last_buffer = connector_specific_data->last_buffer->next;
return 0;
}
/**
* Clean a MongoDB connector instance up
*
* @param instance an instance data structure.
*/
void mongodb_cleanup(struct instance *instance)
{
info("EXPORTING: cleaning up instance %s ...", instance->config.name);
struct mongodb_specific_data *connector_specific_data =
(
|