Delete memory mode "map" and "save". (#16604)
* Delete memory modes "map" and "save". * Remove unmaintained exporting tests * Remove references of map/save modes in docs. * Remove more references to map/save from docs.
vkalintiris committed
Jan 11, 2024 at 19:38 UTC
2165279a87314fb74e8c4de735231a78de73aa54
26 files changed
+27
-4092
collectors/plugins.d/pluginsd_parser.c
+1
-1
@@ -856,7 +856,7 @@ static inline PARSER_RC pluginsd_begin_v2(char **words, size_t num_words, PARSER
856
st->counter++;
857
st->counter_done++;
858
859
- // these are only needed for db mode RAM, SAVE, MAP, ALLOC
859
+ // these are only needed for db mode RAM, ALLOC
860
st->db.current_entry++;
861
if(st->db.current_entry >= st->db.entries)
862
st->db.current_entry -= st->db.entries;
collectors/plugins.d/pluginsd_replication.c
+1
-1
@@ -71,7 +71,7 @@ PARSER_RC pluginsd_replay_begin(char **words, size_t num_words, PARSER *parser)
71
st->counter++;
72
st->counter_done++;
73
74
- // these are only needed for db mode RAM, SAVE, MAP, ALLOC
74
+ // these are only needed for db mode RAM, ALLOC
75
st->db.current_entry++;
76
if(st->db.current_entry >= st->db.entries)
77
st->db.current_entry -= st->db.entries;
daemon/buildinfo.c
-20
@@ -57,8 +57,6 @@ typedef enum __attribute__((packed)) {
57
BIB_DB_DBENGINE,
58
BIB_DB_ALLOC,
59
BIB_DB_RAM,
60
- BIB_DB_MAP,
61
- BIB_DB_SAVE,
60
BIB_DB_NONE,
61
BIB_CONNECTIVITY_ACLK,
62
BIB_CONNECTIVITY_HTTPD_STATIC,
@@ -554,22 +552,6 @@ static struct {
552
.json = "ram",
553
.value = NULL,
554
},
557
- [BIB_DB_MAP] = {
558
- .category = BIC_DATABASE,
559
- .type = BIT_BOOLEAN,
560
- .analytics = NULL,
561
- .print = "map",
562
- .json = "map",
563
- .value = NULL,
564
- },
565
- [BIB_DB_SAVE] = {
566
- .category = BIC_DATABASE,
567
- .type = BIT_BOOLEAN,
568
- .analytics = NULL,
569
- .print = "save",
570
- .json = "save",
571
- .value = NULL,
572
- },
555
[BIB_DB_NONE] = {
556
.category = BIC_DATABASE,
557
.type = BIT_BOOLEAN,
@@ -1101,8 +1083,6 @@ __attribute__((constructor)) void initialize_build_info(void) {
1083
#endif
1084
build_info_set_status(BIB_DB_ALLOC, true);
1085
build_info_set_status(BIB_DB_RAM, true);
1104
- build_info_set_status(BIB_DB_MAP, true);
1105
- build_info_set_status(BIB_DB_SAVE, true);
1086
build_info_set_status(BIB_DB_NONE, true);
1087
1088
build_info_set_status(BIB_CONNECTIVITY_HTTPD_STATIC, true);
daemon/commands.c
-16
@@ -36,7 +36,6 @@ struct command_context {
36
/* Forward declarations */
37
static cmd_status_t cmd_help_execute(char *args, char **message);
38
static cmd_status_t cmd_reload_health_execute(char *args, char **message);
39
-static cmd_status_t cmd_save_database_execute(char *args, char **message);
39
static cmd_status_t cmd_reopen_logs_execute(char *args, char **message);
40
static cmd_status_t cmd_exit_execute(char *args, char **message);
41
static cmd_status_t cmd_fatal_execute(char *args, char **message);
@@ -52,7 +51,6 @@ static cmd_status_t cmd_dumpconfig(char *args, char **message);
51
static command_info_t command_info_array[] = {
52
{"help", cmd_help_execute, CMD_TYPE_HIGH_PRIORITY}, // show help menu
53
{"reload-health", cmd_reload_health_execute, CMD_TYPE_ORTHOGONAL}, // reload health configuration
55
- {"save-database", cmd_save_database_execute, CMD_TYPE_ORTHOGONAL}, // save database for memory mode save
54
{"reopen-logs", cmd_reopen_logs_execute, CMD_TYPE_ORTHOGONAL}, // Close and reopen log files
55
{"shutdown-agent", cmd_exit_execute, CMD_TYPE_EXCLUSIVE}, // exit cleanly
56
{"fatal-agent", cmd_fatal_execute, CMD_TYPE_HIGH_PRIORITY}, // exit with fatal error
@@ -150,20 +148,6 @@ static cmd_status_t cmd_reload_health_execute(char *args, char **message)
148
return CMD_STATUS_SUCCESS;
149
}
150
153
-static cmd_status_t cmd_save_database_execute(char *args, char **message)
154
-{
155
- (void)args;
156
- (void)message;
157
-
158
- nd_log_limits_unlimited();
159
- netdata_log_info("COMMAND: Saving databases.");
160
- rrdhost_save_all();
161
- netdata_log_info("COMMAND: Databases saved.");
162
- nd_log_limits_reset();
163
-
164
- return CMD_STATUS_SUCCESS;
165
-}
166
-
151
static cmd_status_t cmd_reopen_logs_execute(char *args, char **message)
152
{
153
(void)args;
daemon/commands.h
-1
@@ -9,7 +9,6 @@
9
typedef enum cmd {
10
CMD_HELP = 0,
11
CMD_RELOAD_HEALTH,
12
- CMD_SAVE_DATABASE,
12
CMD_REOPEN_LOGS,
13
CMD_EXIT,
14
CMD_FATAL,
daemon/config/README.md
+2
-2
@@ -88,8 +88,8 @@ Please note that your data history will be lost if you have modified `history` p
88
89
| setting | default | info |
90
|:---------------------------------------------:|:----------:|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
91
-| mode | `dbengine` | `dbengine`: The default for long-term metrics storage with efficient RAM and disk usage. Can be extended with `dbengine page cache size MB` and `dbengine disk space MB`. <br />`save`: Netdata will save its round robin database on exit and load it on startup. <br />`map`: Cache files will be updated in real-time. Not ideal for systems with high load or slow disks (check `man mmap`). <br />`ram`: The round-robin database will be temporary and it will be lost when Netdata exits. <br />`alloc`: Similar to `ram`, but can significantly reduce memory usage, when combined with a low retention and does not support KSM. <br />`none`: Disables the database at this host, and disables health monitoring entirely, as that requires a database of metrics. Not to be used together with streaming. |
92
-| retention | `3600` | Used with `mode = save/map/ram/alloc`, not the default `mode = dbengine`. This number reflects the number of entries the `netdata` daemon will by default keep in memory for each chart dimension. Check [Memory Requirements](https://github.com/netdata/netdata/blob/master/database/README.md) for more information. |
91
+| mode | `dbengine` | `dbengine`: The default for long-term metrics storage with efficient RAM and disk usage. Can be extended with `dbengine page cache size MB` and `dbengine disk space MB`. <br />`ram`: The round-robin database will be temporary and it will be lost when Netdata exits. <br />`alloc`: Similar to `ram`, but can significantly reduce memory usage, when combined with a low retention and does not support KSM. <br />`none`: Disables the database at this host, and disables health monitoring entirely, as that requires a database of metrics. Not to be used together with streaming. |
92
+| retention | `3600` | Used with `mode = ram/alloc`, not the default `mode = dbengine`. This number reflects the number of entries the `netdata` daemon will by default keep in memory for each chart dimension. Check [Memory Requirements](https://github.com/netdata/netdata/blob/master/database/README.md) for more information. |
93
| storage tiers | `1` | The number of storage tiers you want to have in your dbengine. Check the tiering mechanism in the [dbengine's reference](https://github.com/netdata/netdata/blob/master/database/engine/README.md#tiering). You can have up to 5 tiers of data (including the _Tier 0_). This number ranges between 1 and 5. |
94
| dbengine page cache size MB | `32` | Determines the amount of RAM in MiB that is dedicated to caching for _Tier 0_ Netdata metric values. |
95
| dbengine tier **`N`** page cache size MB | `32` | Determines the amount of RAM in MiB that is dedicated for caching Netdata metric values of the **`N`** tier. <br /> `N belongs to [1..4]` |
daemon/main.c
+1
-5
@@ -404,10 +404,6 @@ void netdata_cleanup_and_exit(int ret, const char *action, const char *action_re
404
405
web_client_cache_destroy();
406
407
- delta_shutdown_time("clean rrdhost database");
408
-
409
- rrdhost_cleanup_all();
410
-
407
delta_shutdown_time("stop aclk threads");
408
409
timeout = !service_wait_exit(
@@ -1229,7 +1225,7 @@ static void get_netdata_configured_variables() {
1225
#else
1226
if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
1227
error_report("RRD_MEMORY_MODE_DBENGINE is not supported in this platform. The agent will use db mode 'save' instead.");
1232
- default_rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
1228
+ default_rrd_memory_mode = RRD_MEMORY_MODE_RAM;
1229
}
1230
#endif
1231
daemon/service.c
+1
-44
@@ -16,11 +16,7 @@
16
#define WORKER_JOB_CLEANUP_ORPHAN_HOSTS 6
17
#define WORKER_JOB_CLEANUP_OBSOLETE_CHARTS_ON_HOSTS 7
18
#define WORKER_JOB_FREE_HOST 9
19
-#define WORKER_JOB_SAVE_HOST_CHARTS 10
20
-#define WORKER_JOB_DELETE_HOST_CHARTS 11
19
#define WORKER_JOB_FREE_CHART 12
22
-#define WORKER_JOB_SAVE_CHART 13
23
-#define WORKER_JOB_DELETE_CHART 14
20
#define WORKER_JOB_FREE_DIMENSION 15
21
#define WORKER_JOB_PGC_MAIN_EVICT 16
22
#define WORKER_JOB_PGC_MAIN_FLUSH 17
@@ -38,13 +34,6 @@ static void svc_rrddim_obsolete_to_archive(RRDDIM *rd) {
34
rrddim_flag_set(rd, RRDDIM_FLAG_ARCHIVED);
35
rrddim_flag_clear(rd, RRDDIM_FLAG_OBSOLETE);
36
41
- const char *cache_filename = rrddim_cache_filename(rd);
42
- if(cache_filename) {
43
- netdata_log_info("Deleting dimension file '%s'.", cache_filename);
44
- if (unlikely(unlink(cache_filename) == -1))
45
- netdata_log_error("Cannot delete dimension file '%s'", cache_filename);
46
- }
47
-
37
if (rd->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
38
rrddimvar_delete_all(rd);
39
@@ -133,17 +122,6 @@ static void svc_rrdset_obsolete_to_free(RRDSET *st) {
122
// has to be run after all dimensions are archived - or use-after-free will occur
123
rrdvar_delete_all(st->rrdvars);
124
136
- if(st->rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE) {
137
- if(rrdhost_option_check(st->rrdhost, RRDHOST_OPTION_DELETE_OBSOLETE_CHARTS)) {
138
- worker_is_busy(WORKER_JOB_DELETE_CHART);
139
- rrdset_delete_files(st);
140
- }
141
- else {
142
- worker_is_busy(WORKER_JOB_SAVE_CHART);
143
- rrdset_save(st);
144
- }
145
- }
146
-
125
rrdset_free(st);
126
}
127
@@ -269,28 +247,11 @@ restart_after_removal:
247
if(!rrdhost_should_be_removed(host, protected_host, now))
248
continue;
249
272
- bool is_archived = rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED);
273
- if (!is_archived) {
274
- netdata_log_info("Host '%s' with machine guid '%s' is obsolete - cleaning up.", rrdhost_hostname(host), host->machine_guid);
275
-
276
- if (rrdhost_option_check(host, RRDHOST_OPTION_DELETE_ORPHAN_HOST)
277
- /* don't delete multi-host DB host files */
278
- && !(host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE && is_storage_engine_shared(host->db[0].si))
279
- ) {
280
- worker_is_busy(WORKER_JOB_DELETE_HOST_CHARTS);
281
- rrdhost_delete_charts(host);
282
- }
283
- else {
284
- worker_is_busy(WORKER_JOB_SAVE_HOST_CHARTS);
285
- rrdhost_save_charts(host);
286
- }
287
- }
288
-
250
bool force = false;
290
-
251
if (rrdhost_option_check(host, RRDHOST_OPTION_EPHEMERAL_HOST) && now - host->last_connected > rrdhost_free_ephemeral_time_s)
252
force = true;
253
254
+ bool is_archived = rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED);
255
if (!force && is_archived)
256
continue;
257
@@ -339,11 +300,7 @@ void *service_main(void *ptr)
300
worker_register_job_name(WORKER_JOB_CLEANUP_ORPHAN_HOSTS, "cleanup orphan hosts");
301
worker_register_job_name(WORKER_JOB_CLEANUP_OBSOLETE_CHARTS_ON_HOSTS, "cleanup obsolete charts on all hosts");
302
worker_register_job_name(WORKER_JOB_FREE_HOST, "free host");
342
- worker_register_job_name(WORKER_JOB_SAVE_HOST_CHARTS, "save host charts");
343
- worker_register_job_name(WORKER_JOB_DELETE_HOST_CHARTS, "delete host charts");
303
worker_register_job_name(WORKER_JOB_FREE_CHART, "free chart");
345
- worker_register_job_name(WORKER_JOB_SAVE_CHART, "save chart");
346
- worker_register_job_name(WORKER_JOB_DELETE_CHART, "delete chart");
304
worker_register_job_name(WORKER_JOB_FREE_DIMENSION, "free dimension");
305
worker_register_job_name(WORKER_JOB_PGC_MAIN_EVICT, "main cache evictions");
306
worker_register_job_name(WORKER_JOB_PGC_MAIN_FLUSH, "main cache flushes");
daemon/signals.c
-9
@@ -6,7 +6,6 @@ typedef enum signal_action {
6
NETDATA_SIGNAL_END_OF_LIST,
7
NETDATA_SIGNAL_IGNORE,
8
NETDATA_SIGNAL_EXIT_CLEANLY,
9
- NETDATA_SIGNAL_SAVE_DATABASE,
9
NETDATA_SIGNAL_REOPEN_LOGS,
10
NETDATA_SIGNAL_RELOAD_HEALTH,
11
NETDATA_SIGNAL_FATAL,
@@ -24,7 +23,6 @@ static struct {
23
{ SIGQUIT, "SIGQUIT", 0, NETDATA_SIGNAL_EXIT_CLEANLY },
24
{ SIGTERM, "SIGTERM", 0, NETDATA_SIGNAL_EXIT_CLEANLY },
25
{ SIGHUP, "SIGHUP", 0, NETDATA_SIGNAL_REOPEN_LOGS },
27
- { SIGUSR1, "SIGUSR1", 0, NETDATA_SIGNAL_SAVE_DATABASE },
26
{ SIGUSR2, "SIGUSR2", 0, NETDATA_SIGNAL_RELOAD_HEALTH },
27
{ SIGBUS, "SIGBUS", 0, NETDATA_SIGNAL_FATAL },
28
{ SIGCHLD, "SIGCHLD", 0, NETDATA_SIGNAL_CHILD },
@@ -209,13 +207,6 @@ void signals_handle(void) {
207
execute_command(CMD_RELOAD_HEALTH, NULL, NULL);
208
break;
209
212
- case NETDATA_SIGNAL_SAVE_DATABASE:
213
- nd_log_limits_unlimited();
214
- netdata_log_info("SIGNAL: Received %s. Saving databases...", name);
215
- nd_log_limits_reset();
216
- execute_command(CMD_SAVE_DATABASE, NULL, NULL);
217
- break;
218
-
210
case NETDATA_SIGNAL_REOPEN_LOGS:
211
nd_log_limits_unlimited();
212
netdata_log_info("SIGNAL: Received %s. Reopening all log files...", name);
daemon/unit_test.c
-2
@@ -2238,7 +2238,6 @@ int test_dbengine(void)
2238
2239
rrd_wrlock();
2240
rrdeng_prepare_exit((struct rrdengine_instance *)host->db[0].si);
2241
- rrdhost_delete_charts(host);
2241
rrdeng_exit((struct rrdengine_instance *)host->db[0].si);
2242
rrdeng_enq_cmd(NULL, RRDENG_OPCODE_SHUTDOWN_EVLOOP, NULL, NULL, STORAGE_PRIORITY_BEST_EFFORT, NULL, NULL);
2243
rrd_unlock();
@@ -2648,7 +2647,6 @@ void dbengine_stress_test(unsigned TEST_DURATION_SEC, unsigned DSET_CHARTS, unsi
2647
freez(query_threads);
2648
rrd_wrlock();
2649
rrdeng_prepare_exit((struct rrdengine_instance *)host->db[0].si);
2651
- rrdhost_delete_charts(host);
2650
rrdeng_exit((struct rrdengine_instance *)host->db[0].si);
2651
rrdeng_enq_cmd(NULL, RRDENG_OPCODE_SHUTDOWN_EVLOOP, NULL, NULL, STORAGE_PRIORITY_BEST_EFFORT, NULL, NULL);
2652
rrd_unlock();
database/README.md
+3
-12
@@ -12,19 +12,10 @@ Netdata is fully capable of long-term metrics storage, at per-second granularity
12
13
2. `ram`, data are purely in memory. Data are never saved on disk. This mode uses `mmap()` and supports [KSM](#ksm).
14
15
-3. `save`, data are only in RAM while Netdata runs and are saved to / loaded from disk on Netdata restart. It also
16
- uses `mmap()` and supports [KSM](#ksm).
17
-
18
-4. `map`, data are in memory mapped files. This works like the swap. When Netdata writes data on its memory, the Linux
19
- kernel marks the related memory pages as dirty and automatically starts updating them on disk. Unfortunately we
20
- cannot control how frequently this works. The Linux kernel uses exactly the same algorithm it uses for its swap
21
- memory. This mode uses `mmap()` but does not support [KSM](#ksm). _Keep in mind though, this option will have a
22
- constant write on your disk._
23
-
24
-5. `alloc`, like `ram` but it uses `calloc()` and does not support [KSM](#ksm). This mode is the fallback for all others
15
+3. `alloc`, like `ram` but it uses `calloc()` and does not support [KSM](#ksm). This mode is the fallback for all others
16
except `none`.
17
27
-6. `none`, without a database (collected metrics can only be streamed to another Netdata).
18
+4. `none`, without a database (collected metrics can only be streamed to another Netdata).
19
20
## Which database mode to use
21
@@ -47,7 +38,7 @@ You can select the database mode by editing `netdata.conf` and setting:
38
39
```conf
40
[db]
50
- # dbengine (default), ram, save (the default if dbengine not available), map (swap like), none, alloc
41
+ # dbengine (default), ram (the default if dbengine not available), alloc, none
42
mode = dbengine
43
```
44
database/rrd.c
+3
-31
@@ -19,7 +19,7 @@ int default_rrd_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
19
#ifdef ENABLE_DBENGINE
20
RRD_MEMORY_MODE default_rrd_memory_mode = RRD_MEMORY_MODE_DBENGINE;
21
#else
22
-RRD_MEMORY_MODE default_rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
22
+RRD_MEMORY_MODE default_rrd_memory_mode = RRD_MEMORY_MODE_RAM;
23
#endif
24
int gap_when_lost_iterations_above = 1;
25
@@ -32,15 +32,9 @@ inline const char *rrd_memory_mode_name(RRD_MEMORY_MODE id) {
32
case RRD_MEMORY_MODE_RAM:
33
return RRD_MEMORY_MODE_RAM_NAME;
34
35
- case RRD_MEMORY_MODE_MAP:
36
- return RRD_MEMORY_MODE_MAP_NAME;
37
-
35
case RRD_MEMORY_MODE_NONE:
36
return RRD_MEMORY_MODE_NONE_NAME;
37
41
- case RRD_MEMORY_MODE_SAVE:
42
- return RRD_MEMORY_MODE_SAVE_NAME;
43
-
38
case RRD_MEMORY_MODE_ALLOC:
39
return RRD_MEMORY_MODE_ALLOC_NAME;
40
@@ -53,7 +47,7 @@ inline const char *rrd_memory_mode_name(RRD_MEMORY_MODE id) {
47
return eng->name;
48
}
49
56
- return RRD_MEMORY_MODE_SAVE_NAME;
50
+ return RRD_MEMORY_MODE_RAM_NAME;
51
}
52
53
RRD_MEMORY_MODE rrd_memory_mode_id(const char *name) {
@@ -62,7 +56,7 @@ RRD_MEMORY_MODE rrd_memory_mode_id(const char *name) {
56
return eng->id;
57
}
58
65
- return RRD_MEMORY_MODE_SAVE;
59
+ return RRD_MEMORY_MODE_RAM;
60
}
61
62
@@ -132,28 +126,6 @@ const char *rrdset_type_name(RRDSET_TYPE chart_type) {
126
}
127
}
128
135
-// ----------------------------------------------------------------------------
136
-// RRD - cache directory
137
-
138
-char *rrdhost_cache_dir_for_rrdset_alloc(RRDHOST *host, const char *id) {
139
- char *ret = NULL;
140
-
141
- char b[FILENAME_MAX + 1];
142
- char n[FILENAME_MAX + 1];
143
- rrdset_strncpyz_name(b, id, FILENAME_MAX);
144
-
145
- snprintfz(n, FILENAME_MAX, "%s/%s", host->cache_dir, b);
146
- ret = strdupz(n);
147
-
148
- if(host->rrd_memory_mode == RRD_MEMORY_MODE_MAP || host->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
149
- int r = mkdir(ret, 0775);
150
- if(r != 0 && errno != EEXIST)
151
- netdata_log_error("Cannot create directory '%s'", ret);
152
- }
153
-
154
- return ret;
155
-}
156
-
129
// ----------------------------------------------------------------------------
130
// RRD - string management
131
database/rrd.h
+1
-36
@@ -79,8 +79,6 @@ struct pg_cache_page_index;
79
typedef enum __attribute__ ((__packed__)) rrd_memory_mode {
80
RRD_MEMORY_MODE_NONE = 0,
81
RRD_MEMORY_MODE_RAM = 1,
82
- RRD_MEMORY_MODE_MAP = 2,
83
- RRD_MEMORY_MODE_SAVE = 3,
82
RRD_MEMORY_MODE_ALLOC = 4,
83
RRD_MEMORY_MODE_DBENGINE = 5,
84
@@ -89,8 +87,6 @@ typedef enum __attribute__ ((__packed__)) rrd_memory_mode {
87
88
#define RRD_MEMORY_MODE_NONE_NAME "none"
89
#define RRD_MEMORY_MODE_RAM_NAME "ram"
92
-#define RRD_MEMORY_MODE_MAP_NAME "map"
93
-#define RRD_MEMORY_MODE_SAVE_NAME "save"
90
#define RRD_MEMORY_MODE_ALLOC_NAME "alloc"
91
#define RRD_MEMORY_MODE_DBENGINE_NAME "dbengine"
92
@@ -325,13 +321,12 @@ struct rrddim {
321
#endif
322
323
// ------------------------------------------------------------------------
328
- // db mode RAM, SAVE, MAP, ALLOC, NONE specifics
324
+ // db mode RAM, ALLOC, NONE specifics
325
// TODO - they should be managed by storage engine
326
// (RRDDIM_DB_STATE ptr to an undefined structure, and a call to clean this up during destruction)
327
328
struct {
329
size_t memsize; // the memory allocated for this dimension (without RRDDIM)
334
- void *rd_on_file; // pointer to the header written on disk
330
storage_number *data; // the array of values
331
} db;
332
@@ -381,22 +376,6 @@ size_t rrddim_size(void);
376
#define rrddim_set_updated(rd) (rd)->collector.options |= RRDDIM_OPTION_UPDATED
377
#define rrddim_clear_updated(rd) (rd)->collector.options &= ~RRDDIM_OPTION_UPDATED
378
384
-// returns the RRDDIM cache filename, or NULL if it does not exist
385
-const char *rrddim_cache_filename(RRDDIM *rd);
386
-
387
-// updated the header with the latest RRDDIM value, for memory mode MAP and SAVE
388
-void rrddim_memory_file_update(RRDDIM *rd);
389
-
390
-// free the memory file structures for memory mode MAP and SAVE
391
-void rrddim_memory_file_free(RRDDIM *rd);
392
-
393
-bool rrddim_memory_load_or_create_map_save(RRDSET *st, RRDDIM *rd, RRD_MEMORY_MODE memory_mode);
394
-
395
-// return the v019 header size of RRDDIM files
396
-size_t rrddim_memory_file_header_size(void);
397
-
398
-void rrddim_memory_file_save(RRDDIM *rd);
399
-
379
// ------------------------------------------------------------------------
380
// DATA COLLECTION STORAGE OPS
381
@@ -960,12 +939,6 @@ STRING *rrd_string_strdupz(const char *s);
939
#define rrdset_number_of_dimensions(st) \
940
dictionary_entries((st)->rrddim_root_index)
941
963
-void rrdset_memory_file_save(RRDSET *st);
964
-void rrdset_memory_file_free(RRDSET *st);
965
-void rrdset_memory_file_update(RRDSET *st);
966
-const char *rrdset_cache_filename(RRDSET *st);
967
-bool rrdset_memory_load_or_create_map_save(RRDSET *st_on_file, RRD_MEMORY_MODE memory_mode);
968
-
942
#include "rrdcollector.h"
943
#include "rrdfunctions.h"
944
@@ -1488,13 +1461,9 @@ RRDSET *rrdset_create_custom(RRDHOST *host
1461
rrdset_create(localhost, type, id, name, family, context, title, units, plugin, module, priority, update_every, chart_type)
1462
1463
void rrdhost_free_all(void);
1491
-void rrdhost_save_all(void);
1492
-void rrdhost_cleanup_all(void);
1464
1465
void rrdhost_system_info_free(struct rrdhost_system_info *system_info);
1466
void rrdhost_free___while_having_rrd_wrlock(RRDHOST *host, bool force);
1496
-void rrdhost_save_charts(RRDHOST *host);
1497
-void rrdhost_delete_charts(RRDHOST *host);
1467
1468
int rrdhost_should_be_removed(RRDHOST *host, RRDHOST *protected_host, time_t now_s);
1469
@@ -1623,8 +1592,6 @@ void rrdhost_set_is_parent_label(void);
1592
// ----------------------------------------------------------------------------
1593
// RRD internal functions
1594
1626
-void rrdset_delete_files(RRDSET *st);
1627
-void rrdset_save(RRDSET *st);
1595
void rrdset_free(RRDSET *st);
1596
1597
void rrddim_free(RRDSET *st, RRDDIM *rd);
@@ -1632,10 +1599,8 @@ void rrddim_free(RRDSET *st, RRDDIM *rd);
1599
#ifdef NETDATA_RRD_INTERNALS
1600
1601
char *rrdhost_cache_dir_for_rrdset_alloc(RRDHOST *host, const char *id);
1635
-const char *rrdset_cache_dir(RRDSET *st);
1602
1603
void rrdset_reset(RRDSET *st);
1638
-void rrdset_delete_obsolete_dimensions(RRDSET *st);
1604
1605
#endif /* NETDATA_RRD_INTERNALS */
1606
database/rrddim.c
-193
@@ -58,13 +58,6 @@ static void rrddim_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, v
58
if(rrdset_flag_check(st, RRDSET_FLAG_STORE_FIRST))
59
rd->collector.counter = 1;
60
61
- if(ctr->memory_mode == RRD_MEMORY_MODE_MAP || ctr->memory_mode == RRD_MEMORY_MODE_SAVE) {
62
- if(!rrddim_memory_load_or_create_map_save(st, rd, ctr->memory_mode)) {
63
- netdata_log_info("Failed to use memory mode %s for chart '%s', dimension '%s', falling back to ram", (ctr->memory_mode == RRD_MEMORY_MODE_MAP)?"map":"save", rrdset_name(st), rrddim_name(rd));
64
- ctr->memory_mode = RRD_MEMORY_MODE_RAM;
65
- }
66
- }
67
-
61
if(ctr->memory_mode == RRD_MEMORY_MODE_RAM) {
62
size_t entries = st->db.entries;
63
if(!entries) entries = 5;
@@ -212,15 +205,6 @@ static void rrddim_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, v
205
206
rrddimvar_delete_all(rd);
207
215
- // free(rd->annotations);
216
- //#ifdef ENABLE_ACLK
217
- // if (!netdata_exit)
218
- // aclk_send_dimension_update(rd);
219
- //#endif
220
-
221
- // this will free MEMORY_MODE_SAVE and MEMORY_MODE_MAP structures
222
- rrddim_memory_file_free(rd);
223
-
208
for(size_t tier = 0; tier < storage_tiers ;tier++) {
209
if(!rd->tiers[tier].smh) continue;
210
@@ -589,180 +573,3 @@ collected_number rrddim_set(RRDSET *st, const char *id, collected_number value)
573
574
return rrddim_set_by_pointer(st, rd, value);
575
}
592
-
593
-
594
-// ----------------------------------------------------------------------------
595
-// compatibility layer for RRDDIM files v019
596
-
597
-#define RRDDIMENSION_MAGIC_V019 "NETDATA RRD DIMENSION FILE V019"
598
-
599
-struct avl_element_v019 {
600
- void *avl_link[2];
601
- signed char avl_balance;
602
-};
603
-
604
-struct rrddim_map_save_v019 {
605
- struct avl_element_v019 avl; // ignored
606
- void *id; // ignored
607
- void *name; // ignored
608
- uint32_t algorithm; // print warning on mismatch - update on load
609
- uint32_t rrd_memory_mode; // ignored
610
- long long multiplier; // print warning on mismatch - update on load
611
- long long divisor; // print warning on mismatch - update on load
612
- uint32_t flags; // ignored
613
- uint32_t hash; // ignored
614
- uint32_t hash_name; // ignored
615
- void *cache_filename; // ignored - we use it to keep the filename to save back
616
- size_t collections_counter; // ignored
617
- void *state; // ignored
618
- size_t unused[8]; // ignored
619
- long long collected_value_max; // ignored
620
- unsigned int updated:1; // ignored
621
- unsigned int exposed:1; // ignored
622
- struct timeval last_collected_time; // check to reset all - ignored after load
623
- long double calculated_value; // ignored
624
- long double last_calculated_value; // ignored
625
- long double last_stored_value; // ignored
626
- long long collected_value; // ignored
627
- long long last_collected_value; // load and save
628
- long double collected_volume; // ignored
629
- long double stored_volume; // ignored
630
- void *next; // ignored
631
- void *rrdset; // ignored
632
- long entries; // check to reset all - update on load
633
- int update_every; // check to reset all - update on load
634
- size_t memsize; // check to reset all - update on load
635
- char magic[sizeof(RRDDIMENSION_MAGIC_V019) + 1];// check to reset all - update on load
636
- void *variables; // ignored
637
- storage_number values[]; // the array of values
638
-};
639
-
640
-size_t rrddim_memory_file_header_size(void) {
641
- return sizeof(struct rrddim_map_save_v019);
642
-}
643
-
644
-void rrddim_memory_file_update(RRDDIM *rd) {
645
- if(!rd || !rd->db.rd_on_file) return;
646
- struct rrddim_map_save_v019 *rd_on_file = rd->db.rd_on_file;
647
-
648
- rd_on_file->last_collected_time.tv_sec = rd->collector.last_collected_time.tv_sec;
649
- rd_on_file->last_collected_time.tv_usec = rd->collector.last_collected_time.tv_usec;
650
- rd_on_file->last_collected_value = rd->collector.last_collected_value;
651
-}
652
-
653
-void rrddim_memory_file_free(RRDDIM *rd) {
654
- if(!rd || !rd->db.rd_on_file) return;
655
-
656
- // needed for memory mode map, to save the latest state
657
- rrddim_memory_file_update(rd);
658
-
659
- struct rrddim_map_save_v019 *rd_on_file = rd->db.rd_on_file;
660
- __atomic_sub_fetch(&rrddim_db_memory_size, rd_on_file->memsize + strlen(rd_on_file->cache_filename), __ATOMIC_RELAXED);
661
- freez(rd_on_file->cache_filename);
662
- netdata_munmap(rd_on_file, rd_on_file->memsize);
663
-
664
- // remove the pointers from the RRDDIM
665
- rd->db.rd_on_file = NULL;
666
- rd->db.data = NULL;
667
-}
668
-
669
-const char *rrddim_cache_filename(RRDDIM *rd) {
670
- if(!rd || !rd->db.rd_on_file) return NULL;
671
- struct rrddim_map_save_v019 *rd_on_file = rd->db.rd_on_file;
672
- return rd_on_file->cache_filename;
673
-}
674
-
675
-void rrddim_memory_file_save(RRDDIM *rd) {
676
- if(!rd || !rd->db.rd_on_file) return;
677
-
678
- rrddim_memory_file_update(rd);
679
-
680
- struct rrddim_map_save_v019 *rd_on_file = rd->db.rd_on_file;
681
- if(rd_on_file->rrd_memory_mode != RRD_MEMORY_MODE_SAVE) return;
682
-
683
- memory_file_save(rd_on_file->cache_filename, rd_on_file, rd_on_file->memsize);
684
-}
685
-
686
-bool rrddim_memory_load_or_create_map_save(RRDSET *st, RRDDIM *rd, RRD_MEMORY_MODE memory_mode) {
687
- if(memory_mode != RRD_MEMORY_MODE_SAVE && memory_mode != RRD_MEMORY_MODE_MAP)
688
- return false;
689
-
690
- struct rrddim_map_save_v019 *rd_on_file = NULL;
691
-
692
- unsigned long size = sizeof(struct rrddim_map_save_v019) + (st->db.entries * sizeof(storage_number));
693
-
694
- char filename[FILENAME_MAX + 1];
695
- char fullfilename[FILENAME_MAX + 1];
696
- rrdset_strncpyz_name(filename, rrddim_id(rd), FILENAME_MAX);
697
- snprintfz(fullfilename, FILENAME_MAX, "%s/%s.db", rrdset_cache_dir(st), filename);
698
-
699
- rd_on_file = (struct rrddim_map_save_v019 *)netdata_mmap(
700
- fullfilename, size, ((memory_mode == RRD_MEMORY_MODE_MAP) ? MAP_SHARED : MAP_PRIVATE), 1, false, NULL);
701
-
702
- if(unlikely(!rd_on_file)) return false;
703
-
704
- struct timeval now;
705
- now_realtime_timeval(&now);
706
-
707
- int reset = 0;
708
- rd_on_file->magic[sizeof(RRDDIMENSION_MAGIC_V019)] = '\0';
709
- if(strcmp(rd_on_file->magic, RRDDIMENSION_MAGIC_V019) != 0) {
710
- netdata_log_info("Initializing file %s.", fullfilename);
711
- memset(rd_on_file, 0, size);
712
- reset = 1;
713
- }
714
- else if(rd_on_file->memsize != size) {
715
- netdata_log_error("File %s does not have the desired size, expected %lu but found %lu. Clearing it.", fullfilename, size, (unsigned long int) rd_on_file->memsize);
716
- memset(rd_on_file, 0, size);
717
- reset = 1;
718
- }
719
- else if(rd_on_file->update_every != st->update_every) {
720
- netdata_log_error("File %s does not have the same update frequency, expected %d but found %d. Clearing it.", fullfilename, st->update_every, rd_on_file->update_every);
721
- memset(rd_on_file, 0, size);
722
- reset = 1;
723
- }
724
- else if(dt_usec(&now, &rd_on_file->last_collected_time) > (rd_on_file->entries * rd_on_file->update_every * USEC_PER_SEC)) {
725
- netdata_log_info("File %s is too old (last collected %llu seconds ago, but the database is %ld seconds). Clearing it.", fullfilename, dt_usec(&now, &rd_on_file->last_collected_time) / USEC_PER_SEC, rd_on_file->entries * rd_on_file->update_every);
726
- memset(rd_on_file, 0, size);
727
- reset = 1;
728
- }
729
-
730
- if(!reset) {
731
- rd->collector.last_collected_value = rd_on_file->last_collected_value;
732
-
733
- if(rd_on_file->algorithm != rd->algorithm)
734
- netdata_log_info("File %s does not have the expected algorithm (expected %u '%s', found %u '%s'). Previous values may be wrong.",
735
- fullfilename, rd->algorithm, rrd_algorithm_name(rd->algorithm), rd_on_file->algorithm, rrd_algorithm_name(rd_on_file->algorithm));
736
-
737
- if(rd_on_file->multiplier != rd->multiplier)
738
- netdata_log_info("File %s does not have the expected multiplier (expected %d, found %ld). "
739
- "Previous values may be wrong.", fullfilename, rd->multiplier, (long)rd_on_file->multiplier);
740
-
741
- if(rd_on_file->divisor != rd->divisor)
742
- netdata_log_info("File %s does not have the expected divisor (expected %d, found %ld). "
743
- "Previous values may be wrong.", fullfilename, rd->divisor, (long)rd_on_file->divisor);
744
- }
745
-
746
- // zero the entire header
747
- memset(rd_on_file, 0, sizeof(struct rrddim_map_save_v019));
748
-
749
- // set the important fields
750
- strcpy(rd_on_file->magic, RRDDIMENSION_MAGIC_V019);
751
- rd_on_file->algorithm = rd->algorithm;
752
- rd_on_file->multiplier = rd->multiplier;
753
- rd_on_file->divisor = rd->divisor;
754
- rd_on_file->entries = st->db.entries;
755
- rd_on_file->update_every = rd->rrdset->update_every;
756
- rd_on_file->memsize = size;
757
- rd_on_file->rrd_memory_mode = memory_mode;
758
- rd_on_file->cache_filename = strdupz(fullfilename);
759
-
760
- __atomic_add_fetch(&rrddim_db_memory_size, rd_on_file->memsize + strlen(rd_on_file->cache_filename), __ATOMIC_RELAXED);
761
-
762
- rd->db.data = &rd_on_file->values[0];
763
- rd->db.rd_on_file = rd_on_file;
764
- rd->db.memsize = size;
765
- rrddim_memory_file_update(rd);
766
-
767
- return true;
768
-}
database/rrdhost.c
+2
-121
@@ -356,8 +356,6 @@ int is_legacy = 1;
356
switch(memory_mode) {
357
default:
358
case RRD_MEMORY_MODE_ALLOC:
359
- case RRD_MEMORY_MODE_MAP:
360
- case RRD_MEMORY_MODE_SAVE:
359
case RRD_MEMORY_MODE_RAM:
360
if(host->rrdpush_seconds_to_replicate > (time_t) host->rrd_history_entries * (time_t) host->rrd_update_every)
361
host->rrdpush_seconds_to_replicate = (time_t) host->rrd_history_entries * (time_t) host->rrd_update_every;
@@ -390,8 +388,8 @@ int is_legacy = 1;
388
host->cache_dir = strdupz(filename);
389
}
390
393
- if((host->rrd_memory_mode == RRD_MEMORY_MODE_MAP || host->rrd_memory_mode == RRD_MEMORY_MODE_SAVE ||
394
- (host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE && is_legacy))) {
391
+ if(host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE && is_legacy)
392
+ {
393
int r = mkdir(host->cache_dir, 0775);
394
if(r != 0 && errno != EEXIST)
395
nd_log(NDLS_DAEMON, NDLP_CRIT,
@@ -1358,26 +1356,6 @@ void rrd_finalize_collection_for_all_hosts(void) {
1356
dfe_done(host);
1357
}
1358
1361
-// ----------------------------------------------------------------------------
1362
-// RRDHOST - save host files
1363
-
1364
-void rrdhost_save_charts(RRDHOST *host) {
1365
- if(!host) return;
1366
-
1367
- nd_log(NDLS_DAEMON, NDLP_DEBUG,
1368
- "RRD: 'host:%s' saving / closing database...",
1369
- rrdhost_hostname(host));
1370
-
1371
- RRDSET *st;
1372
-
1373
- // we get a write lock
1374
- // to ensure only one thread is saving the database
1375
- rrdset_foreach_write(st, host) {
1376
- rrdset_save(st);
1377
- }
1378
- rrdset_foreach_done(st);
1379
-}
1380
-
1359
struct rrdhost_system_info *rrdhost_labels_to_system_info(RRDLABELS *labels) {
1360
struct rrdhost_system_info *info = callocz(1, sizeof(struct rrdhost_system_info));
1361
info->hops = 1;
@@ -1576,103 +1554,6 @@ void rrdhost_finalize_collection(RRDHOST *host) {
1554
rrdset_foreach_done(st);
1555
}
1556
1579
-// ----------------------------------------------------------------------------
1580
-// RRDHOST - delete host files
1581
-
1582
-void rrdhost_delete_charts(RRDHOST *host) {
1583
- if(!host) return;
1584
-
1585
- nd_log(NDLS_DAEMON, NDLP_DEBUG,
1586
- "RRD: 'host:%s' deleting disk files...",
1587
- rrdhost_hostname(host));
1588
-
1589
- RRDSET *st;
1590
-
1591
- if(host->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || host->rrd_memory_mode == RRD_MEMORY_MODE_MAP) {
1592
- // we get a write lock
1593
- // to ensure only one thread is saving the database
1594
- rrdset_foreach_write(st, host){
1595
- rrdset_delete_files(st);
1596
- }
1597
- rrdset_foreach_done(st);
1598
- }
1599
-
1600
- recursively_delete_dir(host->cache_dir, "left over host");
1601
-}
1602
-
1603
-// ----------------------------------------------------------------------------
1604
-// RRDHOST - cleanup host files
1605
-
1606
-void rrdhost_cleanup_charts(RRDHOST *host) {
1607
- if(!host) return;
1608
-
1609
- nd_log(NDLS_DAEMON, NDLP_DEBUG,
1610
- "RRD: 'host:%s' cleaning up disk files...",
1611
- rrdhost_hostname(host));
1612
-
1613
- RRDSET *st;
1614
- uint32_t rrdhost_delete_obsolete_charts = rrdhost_option_check(host, RRDHOST_OPTION_DELETE_OBSOLETE_CHARTS);
1615
-
1616
- // we get a write lock
1617
- // to ensure only one thread is saving the database
1618
- rrdset_foreach_write(st, host) {
1619
-
1620
- if(rrdhost_delete_obsolete_charts && rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE))
1621
- rrdset_delete_files(st);
1622
-
1623
- else if(rrdhost_delete_obsolete_charts && rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE_DIMENSIONS))
1624
- rrdset_delete_obsolete_dimensions(st);
1625
-
1626
- else
1627
- rrdset_save(st);
1628
-
1629
- }
1630
- rrdset_foreach_done(st);
1631
-}
1632
-
1633
-
1634
-// ----------------------------------------------------------------------------
1635
-// RRDHOST - save all hosts to disk
1636
-
1637
-void rrdhost_save_all(void) {
1638
- nd_log(NDLS_DAEMON, NDLP_DEBUG,
1639
- "RRD: saving databases [%zu hosts(s)]...",
1640
- rrdhost_hosts_available());
1641
-
1642
- rrd_rdlock();
1643
-
1644
- RRDHOST *host;
1645
- rrdhost_foreach_read(host)
1646
- rrdhost_save_charts(host);
1647
-
1648
- rrd_unlock();
1649
-}
1650
-
1651
-// ----------------------------------------------------------------------------
1652
-// RRDHOST - save or delete all hosts from disk
1653
-
1654
-void rrdhost_cleanup_all(void) {
1655
- nd_log(NDLS_DAEMON, NDLP_DEBUG,
1656
- "RRD: cleaning up database [%zu hosts(s)]...",
1657
- rrdhost_hosts_available());
1658
-
1659
- rrd_rdlock();
1660
-
1661
- RRDHOST *host;
1662
- rrdhost_foreach_read(host) {
1663
- if (host != localhost && rrdhost_option_check(host, RRDHOST_OPTION_DELETE_ORPHAN_HOST) && !host->receiver
1664
- /* don't delete multi-host DB host files */
1665
- && !(host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE && is_storage_engine_shared(host->db[0].si))
1666
- )
1667
- rrdhost_delete_charts(host);
1668
- else
1669
- rrdhost_cleanup_charts(host);
1670
- }
1671
-
1672
- rrd_unlock();
1673
-}
1674
-
1675
-
1557
// ----------------------------------------------------------------------------
1558
// RRDHOST - set system info from environment variables
1559
// system_info fields must be heap allocated or NULL
database/rrdset.c
+1
-289
@@ -270,13 +270,6 @@ static void rrdset_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, v
270
271
rw_spinlock_init(&st->alerts.spinlock);
272
273
- if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || st->rrd_memory_mode == RRD_MEMORY_MODE_MAP) {
274
- if(!rrdset_memory_load_or_create_map_save(st, st->rrd_memory_mode)) {
275
- netdata_log_info("Failed to use db mode %s for chart '%s', falling back to ram mode.", (st->rrd_memory_mode == RRD_MEMORY_MODE_MAP)?"map":"save", rrdset_name(st));
276
- st->rrd_memory_mode = RRD_MEMORY_MODE_RAM;
277
- }
278
- }
279
-
273
// initialize the db tiers
274
{
275
for(size_t tier = 0; tier < storage_tiers ; tier++) {
@@ -385,8 +378,6 @@ static void rrdset_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, v
378
// 8. destroy the ml handle
379
ml_chart_delete(st);
380
388
- rrdset_memory_file_free(st); // remove files of db mode save and map
389
-
381
// ------------------------------------------------------------------------
382
// free it
383
@@ -904,12 +895,9 @@ inline long align_entries_to_pagesize(RRD_MEMORY_MODE mode, long entries) {
895
if(entries < 5) entries = 5;
896
if(entries > RRD_HISTORY_ENTRIES_MAX) entries = RRD_HISTORY_ENTRIES_MAX;
897
907
- if(mode == RRD_MEMORY_MODE_MAP || mode == RRD_MEMORY_MODE_SAVE || mode == RRD_MEMORY_MODE_RAM) {
898
+ if(mode == RRD_MEMORY_MODE_RAM) {
899
long header_size = 0;
900
910
- if(mode == RRD_MEMORY_MODE_MAP || mode == RRD_MEMORY_MODE_SAVE)
911
- header_size = (long)rrddim_memory_file_header_size();
912
-
901
long page = (long)sysconf(_SC_PAGESIZE);
902
long size = (long)(header_size + entries * sizeof(storage_number));
903
if (unlikely(size % page)) {
@@ -946,62 +934,6 @@ void rrdset_free(RRDSET *st) {
934
rrdset_index_del(st->rrdhost, st);
935
}
936
949
-void rrdset_save(RRDSET *st) {
950
- rrdset_memory_file_save(st);
951
-
952
- RRDDIM *rd;
953
- rrddim_foreach_read(rd, st)
954
- rrddim_memory_file_save(rd);
955
- rrddim_foreach_done(rd);
956
-}
957
-
958
-void rrdset_delete_files(RRDSET *st) {
959
- RRDDIM *rd;
960
-
961
- netdata_log_info("Deleting chart '%s' ('%s') from disk...", rrdset_id(st), rrdset_name(st));
962
-
963
- if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || st->rrd_memory_mode == RRD_MEMORY_MODE_MAP) {
964
- const char *cache_filename = rrdset_cache_filename(st);
965
- if(cache_filename) {
966
- netdata_log_info("Deleting chart header file '%s'.", cache_filename);
967
- if (unlikely(unlink(cache_filename) == -1))
968
- netdata_log_error("Cannot delete chart header file '%s'", cache_filename);
969
- }
970
- else
971
- netdata_log_error("Cannot find the cache filename of chart '%s'", rrdset_id(st));
972
- }
973
-
974
- rrddim_foreach_read(rd, st) {
975
- const char *cache_filename = rrddim_cache_filename(rd);
976
- if(!cache_filename) continue;
977
-
978
- netdata_log_info("Deleting dimension file '%s'.", cache_filename);
979
- if(unlikely(unlink(cache_filename) == -1))
980
- netdata_log_error("Cannot delete dimension file '%s'", cache_filename);
981
- }
982
- rrddim_foreach_done(rd);
983
-
984
- if(st->db.cache_dir)
985
- recursively_delete_dir(st->db.cache_dir, "left-over chart");
986
-}
987
-
988
-void rrdset_delete_obsolete_dimensions(RRDSET *st) {
989
- RRDDIM *rd;
990
-
991
- netdata_log_info("Deleting dimensions of chart '%s' ('%s') from disk...", rrdset_id(st), rrdset_name(st));
992
-
993
- rrddim_foreach_read(rd, st) {
994
- if(rrddim_flag_check(rd, RRDDIM_FLAG_OBSOLETE)) {
995
- const char *cache_filename = rrddim_cache_filename(rd);
996
- if(!cache_filename) continue;
997
- netdata_log_info("Deleting dimension file '%s'.", cache_filename);
998
- if(unlikely(unlink(cache_filename) == -1))
999
- netdata_log_error("Cannot delete dimension file '%s'", cache_filename);
1000
- }
1001
- }
1002
- rrddim_foreach_done(rd);
1003
-}
1004
-
937
// ----------------------------------------------------------------------------
938
// RRDSET - create a chart
939
@@ -2100,18 +2032,6 @@ void rrdset_timed_done(RRDSET *st, struct timeval now, bool pending_rrdset_next)
2032
// ALL DONE ABOUT THE DATA UPDATE
2033
// --------------------------------------------------------------------
2034
2103
- if(unlikely(st->rrd_memory_mode == RRD_MEMORY_MODE_MAP)) {
2104
- // update the memory mapped files with the latest values
2105
-
2106
- rrdset_memory_file_update(st);
2107
-
2108
- for(dim_id = 0, rda = rda_base; dim_id < rda_slots ; ++dim_id, ++rda) {
2109
- rd = rda->rd;
2110
- if(unlikely(!rd)) continue;
2111
- rrddim_memory_file_update(rd);
2112
- }
2113
- }
2114
-
2035
for(dim_id = 0, rda = rda_base; dim_id < rda_slots ; ++dim_id, ++rda) {
2036
rd = rda->rd;
2037
if(unlikely(!rd)) continue;
@@ -2150,211 +2070,3 @@ time_t rrdset_set_update_every_s(RRDSET *st, time_t update_every_s) {
2070
2071
return prev_update_every_s;
2072
}
2153
-
2154
-// ----------------------------------------------------------------------------
2155
-// compatibility layer for RRDSET files v019
2156
-
2157
-#define RRDSET_MAGIC_V019 "NETDATA RRD SET FILE V019"
2158
-#define RRD_ID_LENGTH_MAX_V019 200
2159
-
2160
-struct avl_element_v019 {
2161
- void *avl_link[2];
2162
- signed char avl_balance;
2163
-};
2164
-struct avl_tree_type_v019 {
2165
- void *root;
2166
- int (*compar)(void *a, void *b);
2167
-};
2168
-struct avl_tree_lock_v019 {
2169
- struct avl_tree_type_v019 avl_tree;
2170
- pthread_rwlock_t rwlock;
2171
-};
2172
-struct rrdset_map_save_v019 {
2173
- struct avl_element_v019 avl; // ignored
2174
- struct avl_element_v019 avlname; // ignored
2175
- char id[RRD_ID_LENGTH_MAX_V019 + 1]; // check to reset all - update on load
2176
- void *name; // ignored
2177
- void *unused_ptr; // ignored
2178
- void *type; // ignored
2179
- void *family; // ignored
2180
- void *title; // ignored
2181
- void *units; // ignored
2182
- void *context; // ignored
2183
- uint32_t hash_context; // ignored
2184
- uint32_t chart_type; // ignored
2185
- int update_every; // check to reset all - update on load
2186
- long entries; // check to reset all - update on load
2187
- long current_entry; // NEEDS TO BE UPDATED - FIXED ON LOAD
2188
- uint32_t flags; // ignored
2189
- void *exporting_flags; // ignored
2190
- int gap_when_lost_iterations_above; // ignored
2191
- long priority; // ignored
2192
- uint32_t rrd_memory_mode; // ignored
2193
- void *cache_dir; // ignored
2194
- char cache_filename[FILENAME_MAX+1]; // ignored - update on load
2195
- pthread_rwlock_t rrdset_rwlock; // ignored
2196
- size_t counter; // NEEDS TO BE UPDATED - maintained on load
2197
- size_t counter_done; // ignored
2198
- union { //
2199
- time_t last_accessed_time_s; // ignored
2200
- time_t last_entry_s; // ignored
2201
- }; //
2202
- time_t upstream_resync_time; // ignored
2203
- void *plugin_name; // ignored
2204
- void *module_name; // ignored
2205
- void *chart_uuid; // ignored
2206
- void *state; // ignored
2207
- size_t unused[3]; // ignored
2208
- size_t rrddim_page_alignment; // ignored
2209
- uint32_t hash; // ignored
2210
- uint32_t hash_name; // ignored
2211
- usec_t usec_since_last_update; // NEEDS TO BE UPDATED - maintained on load
2212
- struct timeval last_updated; // NEEDS TO BE UPDATED - check to reset all - fixed on load
2213
- struct timeval last_collected_time; // ignored
2214
- long long collected_total; // ignored
2215
- long long last_collected_total; // ignored
2216
- void *rrdfamily; // ignored
2217
- void *rrdhost; // ignored
2218
- void *next; // ignored
2219
- long double green; // ignored
2220
- long double red; // ignored
2221
- struct avl_tree_lock_v019 rrdvar_root_index; // ignored
2222
- void *variables; // ignored
2223
- void *alarms; // ignored
2224
- unsigned long memsize; // check to reset all - update on load
2225
- char magic[sizeof(RRDSET_MAGIC_V019) + 1]; // check to reset all - update on load
2226
- struct avl_tree_lock_v019 dimensions_index; // ignored
2227
- void *dimensions; // ignored
2228
-};
2229
-
2230
-void rrdset_memory_file_update(RRDSET *st) {
2231
- if(!st->db.st_on_file) return;
2232
- struct rrdset_map_save_v019 *st_on_file = st->db.st_on_file;
2233
-
2234
- st_on_file->current_entry = st->db.current_entry;
2235
- st_on_file->counter = st->counter;
2236
- st_on_file->usec_since_last_update = st->usec_since_last_update;
2237
- st_on_file->last_updated.tv_sec = st->last_updated.tv_sec;
2238
- st_on_file->last_updated.tv_usec = st->last_updated.tv_usec;
2239
-}
2240
-
2241
-const char *rrdset_cache_filename(RRDSET *st) {
2242
- if(!st->db.st_on_file) return NULL;
2243
- struct rrdset_map_save_v019 *st_on_file = st->db.st_on_file;
2244
- return st_on_file->cache_filename;
2245
-}
2246
-
2247
-const char *rrdset_cache_dir(RRDSET *st) {
2248
- if(!st->db.cache_dir)
2249
- st->db.cache_dir = rrdhost_cache_dir_for_rrdset_alloc(st->rrdhost, rrdset_id(st));
2250
-
2251
- return st->db.cache_dir;
2252
-}
2253
-
2254
-void rrdset_memory_file_free(RRDSET *st) {
2255
- if(!st->db.st_on_file) return;
2256
-
2257
- // needed for memory mode map, to save the latest state
2258
- rrdset_memory_file_update(st);
2259
-
2260
- struct rrdset_map_save_v019 *st_on_file = st->db.st_on_file;
2261
- __atomic_sub_fetch(&rrddim_db_memory_size, st_on_file->memsize, __ATOMIC_RELAXED);
2262
- netdata_munmap(st_on_file, st_on_file->memsize);
2263
-
2264
- // remove the pointers from the RRDDIM
2265
- st->db.st_on_file = NULL;
2266
-}
2267
-
2268
-void rrdset_memory_file_save(RRDSET *st) {
2269
- if(!st->db.st_on_file) return;
2270
-
2271
- rrdset_memory_file_update(st);
2272
-
2273
- struct rrdset_map_save_v019 *st_on_file = st->db.st_on_file;
2274
- if(st_on_file->rrd_memory_mode != RRD_MEMORY_MODE_SAVE) return;
2275
-
2276
- memory_file_save(st_on_file->cache_filename, st->db.st_on_file, st_on_file->memsize);
2277
-}
2278
-
2279
-bool rrdset_memory_load_or_create_map_save(RRDSET *st, RRD_MEMORY_MODE memory_mode) {
2280
- if(memory_mode != RRD_MEMORY_MODE_SAVE && memory_mode != RRD_MEMORY_MODE_MAP)
2281
- return false;
2282
-
2283
- char fullfilename[FILENAME_MAX + 1];
2284
- snprintfz(fullfilename, FILENAME_MAX, "%s/main.db", rrdset_cache_dir(st));
2285
-
2286
- unsigned long size = sizeof(struct rrdset_map_save_v019);
2287
- struct rrdset_map_save_v019 *st_on_file = (struct rrdset_map_save_v019 *)netdata_mmap(
2288
- fullfilename, size, ((memory_mode == RRD_MEMORY_MODE_MAP) ? MAP_SHARED : MAP_PRIVATE), 0, false, NULL);
2289
-
2290
- if(!st_on_file) return false;
2291
-
2292
- time_t now_s = now_realtime_sec();
2293
-
2294
- st_on_file->magic[sizeof(RRDSET_MAGIC_V019)] = '\0';
2295
- if(strcmp(st_on_file->magic, RRDSET_MAGIC_V019) != 0) {
2296
- netdata_log_info("Initializing file '%s'.", fullfilename);
2297
- memset(st_on_file, 0, size);
2298
- }
2299
- else if(strncmp(st_on_file->id, rrdset_id(st), RRD_ID_LENGTH_MAX_V019) != 0) {
2300
- netdata_log_error("File '%s' contents are not for chart '%s'. Clearing it.", fullfilename, rrdset_id(st));
2301
- memset(st_on_file, 0, size);
2302
- }
2303
- else if(st_on_file->memsize != size || st_on_file->entries != st->db.entries) {
2304
- netdata_log_error("File '%s' does not have the desired size. Clearing it.", fullfilename);
2305
- memset(st_on_file, 0, size);
2306
- }
2307
- else if(st_on_file->update_every != st->update_every) {
2308
- netdata_log_error("File '%s' does not have the desired granularity. Clearing it.", fullfilename);
2309
- memset(st_on_file, 0, size);
2310
- }
2311
- else if((now_s - st_on_file->last_updated.tv_sec) > (long)st->update_every * (long)st->db.entries) {
2312
- netdata_log_info("File '%s' is too old. Clearing it.", fullfilename);
2313
- memset(st_on_file, 0, size);
2314
- }
2315
- else if(st_on_file->last_updated.tv_sec > now_s + st->update_every) {
2316
- netdata_log_error("File '%s' refers to the future by %zd secs. Resetting it to now.", fullfilename, (ssize_t)(st_on_file->last_updated.tv_sec - now_s));
2317
- st_on_file->last_updated.tv_sec = now_s;
2318
- }
2319
-
2320
- if(st_on_file->current_entry >= st_on_file->entries)
2321
- st_on_file->current_entry = 0;
2322
-
2323
- // make sure the database is aligned
2324
- bool align_last_updated = false;
2325
- if(st_on_file->last_updated.tv_sec) {
2326
- st_on_file->update_every = st->update_every;
2327
- align_last_updated = true;
2328
- }
2329
-
2330
- // copy the useful values to st
2331
- st->db.current_entry = st_on_file->current_entry;
2332
- st->counter = st_on_file->counter;
2333
- st->usec_since_last_update = st_on_file->usec_since_last_update;
2334
- st->last_updated.tv_sec = st_on_file->last_updated.tv_sec;
2335
- st->last_updated.tv_usec = st_on_file->last_updated.tv_usec;
2336
-
2337
- // link it to st
2338
- st->db.st_on_file = st_on_file;
2339
-
2340
- // clear everything
2341
- memset(st_on_file, 0, size);
2342
-
2343
- // set the values we need
2344
- strncpyz(st_on_file->id, rrdset_id(st), RRD_ID_LENGTH_MAX_V019);
2345
- strcpy(st_on_file->cache_filename, fullfilename);
2346
- strcpy(st_on_file->magic, RRDSET_MAGIC_V019);
2347
- st_on_file->memsize = size;
2348
- st_on_file->entries = st->db.entries;
2349
- st_on_file->update_every = st->update_every;
2350
- st_on_file->rrd_memory_mode = memory_mode;
2351
-
2352
- if(align_last_updated)
2353
- last_updated_time_align(st);
2354
-
2355
- // copy the useful values back to st_on_file
2356
- rrdset_memory_file_update(st);
2357
-
2358
- __atomic_add_fetch(&rrddim_db_memory_size, st_on_file->memsize, __ATOMIC_RELAXED);
2359
- return true;
2360
-}
database/storage_engine.c
-24
@@ -31,30 +31,6 @@ static STORAGE_ENGINE engines[] = {
31
.metric_retention_by_uuid = rrddim_metric_retention_by_uuid,
32
}
33
},
34
- {
35
- .id = RRD_MEMORY_MODE_MAP,
36
- .name = RRD_MEMORY_MODE_MAP_NAME,
37
- .seb = STORAGE_ENGINE_BACKEND_RRDDIM,
38
- .api = {
39
- .metric_get = rrddim_metric_get,
40
- .metric_get_or_create = rrddim_metric_get_or_create,
41
- .metric_dup = rrddim_metric_dup,
42
- .metric_release = rrddim_metric_release,
43
- .metric_retention_by_uuid = rrddim_metric_retention_by_uuid,
44
- }
45
- },
46
- {
47
- .id = RRD_MEMORY_MODE_SAVE,
48
- .name = RRD_MEMORY_MODE_SAVE_NAME,
49
- .seb = STORAGE_ENGINE_BACKEND_RRDDIM,
50
- .api = {
51
- .metric_get = rrddim_metric_get,
52
- .metric_get_or_create = rrddim_metric_get_or_create,
53
- .metric_dup = rrddim_metric_dup,
54
- .metric_release = rrddim_metric_release,
55
- .metric_retention_by_uuid = rrddim_metric_retention_by_uuid,
56
- }
57
- },
34
{
35
.id = RRD_MEMORY_MODE_ALLOC,
36
.name = RRD_MEMORY_MODE_ALLOC_NAME,
exporting/tests/exporting_doubles.c
deleted
-405
@@ -1,405 +0,0 @@
1
-// SPDX-License-Identifier: GPL-3.0-or-later
2
-
3
-#include "test_exporting_engine.h"
4
-
5
-struct engine *__real_read_exporting_config();
6
-struct engine *__wrap_read_exporting_config()
7
-{
8
- function_called();
9
- return mock_ptr_type(struct engine *);
10
-}
11
-
12
-struct engine *__mock_read_exporting_config()
13
-{
14
- struct engine *engine = calloc(1, sizeof(struct engine));
15
- engine->config.hostname = strdupz("test_engine_host");
16
- engine->config.update_every = 3;
17
-
18
-
19
- engine->instance_root = calloc(1, sizeof(struct instance));
20
- struct instance *instance = engine->instance_root;
21
- instance->engine = engine;
22
- instance->config.type = EXPORTING_CONNECTOR_TYPE_GRAPHITE;
23
- instance->config.name = strdupz("instance_name");
24
- instance->config.destination = strdupz("localhost");
25
- instance->config.username = strdupz("");
26
- instance->config.password = strdupz("");
27
- instance->config.prefix = strdupz("netdata");
28
- instance->config.hostname = strdupz("test-host");
29
- instance->config.update_every = 1;
30
- instance->config.buffer_on_failures = 10;
31
- instance->config.timeoutms = 10000;
32
- instance->config.charts_pattern = simple_pattern_create("*", NULL, SIMPLE_PATTERN_EXACT);
33
- instance->config.hosts_pattern = simple_pattern_create("*", NULL, SIMPLE_PATTERN_EXACT);
34
- instance->config.options = EXPORTING_SOURCE_DATA_AS_COLLECTED | EXPORTING_OPTION_SEND_NAMES;
35
-
36
- return engine;
37
-}
38
-
39
-int __real_init_connectors(struct engine *engine);
40
-int __wrap_init_connectors(struct engine *engine)
41
-{
42
- function_called();
43
- check_expected_ptr(engine);
44
- return mock_type(int);
45
-}
46
-
47
-int __real_mark_scheduled_instances(struct engine *engine);
48
-int __wrap_mark_scheduled_instances(struct engine *engine)
49
-{
50
- function_called();
51
- check_expected_ptr(engine);
52
- return mock_type(int);
53
-}
54
-
55
-NETDATA_DOUBLE __real_exporting_calculate_value_from_stored_data(
56
- struct instance *instance,
57
- RRDDIM *rd,
58
- time_t *last_timestamp);
59
-NETDATA_DOUBLE __wrap_exporting_calculate_value_from_stored_data(
60
- struct instance *instance,
61
- RRDDIM *rd,
62
- time_t *last_timestamp)
63
-{
64
- (void)instance;
65
- (void)rd;
66
-
67
- *last_timestamp = 15052;
68
-
69
- function_called();
70
- return mock_type(NETDATA_DOUBLE);
71
-}
72
-
73
-int __real_prepare_buffers(struct engine *engine);
74
-int __wrap_prepare_buffers(struct engine *engine)
75
-{
76
- function_called();
77
- check_expected_ptr(engine);
78
- return mock_type(int);
79
-}
80
-
81
-void __wrap_create_main_rusage_chart(RRDSET **st_rusage, RRDDIM **rd_user, RRDDIM **rd_system)
82
-{
83
- function_called();
84
- check_expected_ptr(st_rusage);
85
- check_expected_ptr(rd_user);
86
- check_expected_ptr(rd_system);
87
-}
88
-
89
-void __wrap_send_main_rusage(RRDSET *st_rusage, RRDDIM *rd_user, RRDDIM *rd_system)
90
-{
91
- function_called();
92
- check_expected_ptr(st_rusage);
93
- check_expected_ptr(rd_user);
94
- check_expected_ptr(rd_system);
95
-}
96
-
97
-int __wrap_send_internal_metrics(struct instance *instance)
98
-{
99
- function_called();
100
- check_expected_ptr(instance);
101
- return mock_type(int);
102
-}
103
-
104
-int __wrap_rrdhost_is_exportable(struct instance *instance, RRDHOST *host)
105
-{
106
- function_called();
107
- check_expected_ptr(instance);
108
- check_expected_ptr(host);
109
- return mock_type(int);
110
-}
111
-
112
-int __wrap_rrdset_is_exportable(struct instance *instance, RRDSET *st)
113
-{
114
- function_called();
115
- check_expected_ptr(instance);
116
- check_expected_ptr(st);
117
- return mock_type(int);
118
-}
119
-
120
-int __mock_start_batch_formatting(struct instance *instance)
121
-{
122
- function_called();
123
- check_expected_ptr(instance);
124
- return mock_type(int);
125
-}
126
-
127
-int __mock_start_host_formatting(struct instance *instance, RRDHOST *host)
128
-{
129
- function_called();
130
- check_expected_ptr(instance);
131
- check_expected_ptr(host);
132
- return mock_type(int);
133
-}
134
-
135
-int __mock_start_chart_formatting(struct instance *instance, RRDSET *st)
136
-{
137
- function_called();
138
- check_expected_ptr(instance);
139
- check_expected_ptr(st);
140
- return mock_type(int);
141
-}
142
-
143
-int __mock_metric_formatting(struct instance *instance, RRDDIM *rd)
144
-{
145
- function_called();
146
- check_expected_ptr(instance);
147
- check_expected_ptr(rd);
148
- return mock_type(int);
149
-}
150
-
151
-int __mock_end_chart_formatting(struct instance *instance, RRDSET *st)
152
-{
153
- function_called();
154
- check_expected_ptr(instance);
155
- check_expected_ptr(st);
156
- return mock_type(int);
157
-}
158
-
159
-int __mock_variables_formatting(struct instance *instance, RRDHOST *host)
160
-{
161
- function_called();
162
- check_expected_ptr(instance);
163
- check_expected_ptr(host);
164
- return mock_type(int);
165
-}
166
-
167
-int __mock_end_host_formatting(struct instance *instance, RRDHOST *host)
168
-{
169
- function_called();
170
- check_expected_ptr(instance);
171
- check_expected_ptr(host);
172
- return mock_type(int);
173
-}
174
-
175
-int __mock_end_batch_formatting(struct instance *instance)
176
-{
177
- function_called();
178
- check_expected_ptr(instance);
179
- return mock_type(int);
180
-}
181
-
182
-int __wrap_simple_connector_end_batch(struct instance *instance)
183
-{
184
- function_called();
185
- check_expected_ptr(instance);
186
- return mock_type(int);
187
-}
188
-
189
-#if ENABLE_PROMETHEUS_REMOTE_WRITE
190
-void *__wrap_init_write_request()
191
-{
192
- function_called();
193
- return mock_ptr_type(void *);
194
-}
195
-
196
-void __wrap_add_host_info(
197
- void *write_request_p,
198
- const char *name, const char *instance, const char *application, const char *version, const int64_t timestamp)
199
-{
200
- function_called();
201
- check_expected_ptr(write_request_p);
202
- check_expected_ptr(name);
203
- check_expected_ptr(instance);
204
- check_expected_ptr(application);
205
- check_expected_ptr(version);
206
- check_expected(timestamp);
207
-}
208
-
209
-void __wrap_add_label(void *write_request_p, char *key, char *value)
210
-{
211
- function_called();
212
- check_expected_ptr(write_request_p);
213
- check_expected_ptr(key);
214
- check_expected_ptr(value);
215
-}
216
-
217
-void __wrap_add_metric(
218
- void *write_request_p,
219
- const char *name, const char *chart, const char *family, const char *dimension,
220
- const char *instance, const double value, const int64_t timestamp)
221
-{
222
- function_called();
223
- check_expected_ptr(write_request_p);
224
- check_expected_ptr(name);
225
- check_expected_ptr(chart);
226
- check_expected_ptr(family);
227
- check_expected_ptr(dimension);
228
- check_expected_ptr(instance);
229
- check_expected(value);
230
- check_expected(timestamp);
231
-}
232
-#endif // ENABLE_PROMETHEUS_REMOTE_WRITE
233
-
234
-#if HAVE_KINESIS
235
-void __wrap_aws_sdk_init()
236
-{
237
- function_called();
238
-}
239
-
240
-void __wrap_kinesis_init(
241
- void *kinesis_specific_data_p, const char *region, const char *access_key_id, const char *secret_key,
242
- const long timeout)
243
-{
244
- function_called();
245
- check_expected_ptr(kinesis_specific_data_p);
246
- check_expected_ptr(region);
247
- check_expected_ptr(access_key_id);
248
- check_expected_ptr(secret_key);
249
- check_expected(timeout);
250
-}
251
-
252
-void __wrap_kinesis_put_record(
253
- void *kinesis_specific_data_p, const char *stream_name, const char *partition_key, const char *data,
254
- size_t data_len)
255
-{
256
- function_called();
257
- check_expected_ptr(kinesis_specific_data_p);
258
- check_expected_ptr(stream_name);
259
- check_expected_ptr(partition_key);
260
- check_expected_ptr(data);
261
- check_expected_ptr(data);
262
- check_expected(data_len);
263
-}
264
-
265
-int __wrap_kinesis_get_result(void *request_outcomes_p, char *error_message, size_t *sent_bytes, size_t *lost_bytes)
266
-{
267
- function_called();
268
- check_expected_ptr(request_outcomes_p);
269
- check_expected_ptr(error_message);
270
- check_expected_ptr(sent_bytes);
271
- check_expected_ptr(lost_bytes);
272
- return mock_type(int);
273
-}
274
-#endif // HAVE_KINESIS
275
-
276
-#if ENABLE_EXPORTING_PUBSUB
277
-int __wrap_pubsub_init(
278
- void *pubsub_specific_data_p, char *error_message, const char *destination, const char *credentials_file,
279
- const char *project_id, const char *topic_id)
280
-{
281
- function_called();
282
- check_expected_ptr(pubsub_specific_data_p);
283
- check_expected_ptr(error_message);
284
- check_expected_ptr(destination);
285
- check_expected_ptr(credentials_file);
286
- check_expected_ptr(project_id);
287
- check_expected_ptr(topic_id);
288
- return mock_type(int);
289
-}
290
-
291
-int __wrap_pubsub_add_message(void *pubsub_specific_data_p, char *data)
292
-{
293
- function_called();
294
- check_expected_ptr(pubsub_specific_data_p);
295
- check_expected_ptr(data);
296
- return mock_type(int);
297
-}
298
-
299
-int __wrap_pubsub_publish(
300
- void *pubsub_specific_data_p, char *error_message, size_t buffered_metrics, size_t buffered_bytes)
301
-{
302
- function_called();
303
- check_expected_ptr(pubsub_specific_data_p);
304
- check_expected_ptr(error_message);
305
- check_expected(buffered_metrics);
306
- check_expected(buffered_bytes);
307
- return mock_type(int);
308
-}
309
-
310
-int __wrap_pubsub_get_result(
311
- void *pubsub_specific_data_p, char *error_message,
312
- size_t *sent_metrics, size_t *sent_bytes, size_t *lost_metrics, size_t *lost_bytes)
313
-{
314
- function_called();
315
- check_expected_ptr(pubsub_specific_data_p);
316
- check_expected_ptr(error_message);
317
- check_expected_ptr(sent_metrics);
318
- check_expected_ptr(sent_bytes);
319
- check_expected_ptr(lost_metrics);
320
- check_expected_ptr(lost_bytes);
321
- return mock_type(int);
322
-}
323
-#endif // ENABLE_EXPORTING_PUBSUB
324
-
325
-#if HAVE_MONGOC
326
-void __wrap_mongoc_init()
327
-{
328
- function_called();
329
-}
330
-
331
-mongoc_uri_t * __wrap_mongoc_uri_new_with_error (const char *uri_string, bson_error_t *error)
332
-{
333
- function_called();
334
- check_expected_ptr(uri_string);
335
- check_expected_ptr(error);
336
- return mock_ptr_type(mongoc_uri_t *);
337
-}
338
-
339
-int32_t __wrap_mongoc_uri_get_option_as_int32(const mongoc_uri_t *uri, const char *option, int32_t fallback)
340
-{
341
- function_called();
342
- check_expected_ptr(uri);
343
- check_expected_ptr(option);
344
- check_expected(fallback);
345
- return mock_type(int32_t);
346
-}
347
-
348
-bool __wrap_mongoc_uri_set_option_as_int32 (const mongoc_uri_t *uri, const char *option, int32_t value)
349
-{
350
- function_called();
351
- check_expected_ptr(uri);
352
- check_expected_ptr(option);
353
- check_expected(value);
354
- return mock_type(bool);
355
-}
356
-
357
-mongoc_client_t * __wrap_mongoc_client_new_from_uri (const mongoc_uri_t *uri)
358
-{
359
- function_called();
360
- check_expected_ptr(uri);
361
- return mock_ptr_type(mongoc_client_t *);
362
-}
363
-
364
-bool __wrap_mongoc_client_set_appname (mongoc_client_t *client, const char *appname)
365
-{
366
- function_called();
367
- check_expected_ptr(client);
368
- check_expected_ptr(appname);
369
- return mock_type(bool);
370
-}
371
-
372
-mongoc_collection_t *
373
-__wrap_mongoc_client_get_collection(mongoc_client_t *client, const char *db, const char *collection)
374
-{
375
- function_called();
376
- check_expected_ptr(client);
377
- check_expected_ptr(db);
378
- check_expected_ptr(collection);
379
- return mock_ptr_type(mongoc_collection_t *);
380
-}
381
-
382
-void __wrap_mongoc_uri_destroy (mongoc_uri_t *uri)
383
-{
384
- function_called();
385
- check_expected_ptr(uri);
386
-}
387
-
388
-bool __wrap_mongoc_collection_insert_many(
389
- mongoc_collection_t *collection,
390
- const bson_t **documents,
391
- size_t n_documents,
392
- const bson_t *opts,
393
- bson_t *reply,
394
- bson_error_t *error)
395
-{
396
- function_called();
397
- check_expected_ptr(collection);
398
- check_expected_ptr(documents);
399
- check_expected(n_documents);
400
- check_expected_ptr(opts);
401
- check_expected_ptr(reply);
402
- check_expected_ptr(error);
403
- return mock_type(bool);
404
-}
405
-#endif // HAVE_MONGOC
exporting/tests/exporting_fixtures.c
deleted
-184
@@ -1,184 +0,0 @@
1
-// SPDX-License-Identifier: GPL-3.0-or-later
2
-
3
-#include "test_exporting_engine.h"
4
-
5
-int setup_configured_engine(void **state)
6
-{
7
- struct engine *engine = __mock_read_exporting_config();
8
- engine->instance_root->data_is_ready = 1;
9
-
10
- *state = engine;
11
-
12
- return 0;
13
-}
14
-
15
-int teardown_configured_engine(void **state)
16
-{
17
- struct engine *engine = *state;
18
-
19
- struct instance *instance = engine->instance_root;
20
- free((void *)instance->config.destination);
21
- free((void *)instance->config.username);
22
- free((void *)instance->config.password);
23
- free((void *)instance->config.name);
24
- free((void *)instance->config.prefix);
25
- free((void *)instance->config.hostname);
26
- simple_pattern_free(instance->config.charts_pattern);
27
- simple_pattern_free(instance->config.hosts_pattern);
28
- free(instance);
29
-
30
- free((void *)engine->config.hostname);
31
- free(engine);
32
-
33
- return 0;
34
-}
35
-
36
-static void rrddim_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrddim, void *st) {
37
- RRDDIM *rd = rrddim;
38
-
39
- rd->id = string_strdupz("dimension_id");
40
- rd->name = string_strdupz("dimension_name");
41
-
42
- rd->rrdset = (RRDSET *)st;
43
- rd->last_collected_value = 123000321;
44
- rd->last_collected_time.tv_sec = 15051;
45
- rd->collections_counter++;
46
- rd->next = NULL;
47
-
48
- rd->tiers[0] = calloc(1, sizeof(struct rrddim_tier));
49
- rd->tiers[0]->query_ops.oldest_time = __mock_rrddim_query_oldest_time;
50
- rd->tiers[0]->query_ops.latest_time = __mock_rrddim_query_latest_time;
51
- rd->tiers[0]->query_ops.init = __mock_rrddim_query_init;
52
- rd->tiers[0]->query_ops.is_finished = __mock_rrddim_query_is_finished;
53
- rd->tiers[0]->query_ops.next_metric = __mock_rrddim_query_next_metric;
54
- rd->tiers[0]->query_ops.finalize = __mock_rrddim_query_finalize;
55
-}
56
-
57
-static void rrdset_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdset, void *constructor_data __maybe_unused) {
58
- RRDHOST *host = localhost;
59
- RRDSET *st = rrdset;
60
-
61
- // const char *chart_full_id = dictionary_acquired_item_name(item);
62
-
63
- st->id = string_strdupz("chart_id");
64
- st->name = string_strdupz("chart_name");
65
-
66
- st->update_every = 1;
67
- st->rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
68
-
69
- st->rrdhost = host;
70
-
71
- st->rrddim_root_index = dictionary_create(DICT_OPTION_DONT_OVERWRITE_VALUE);
72
-
73
- dictionary_register_insert_callback(st->rrddim_root_index, rrddim_insert_callback, NULL);
74
-}
75
-
76
-int setup_rrdhost()
77
-{
78
- localhost = calloc(1, sizeof(RRDHOST));
79
-
80
- localhost->rrd_update_every = 1;
81
-
82
- localhost->tags = string_strdupz("TAG1=VALUE1 TAG2=VALUE2");
83
-
84
- localhost->rrdlabels = rrdlabels_create();
85
- rrdlabels_add(localhost->rrdlabels, "key1", "value1", RRDLABEL_SRC_CONFIG);
86
- rrdlabels_add(localhost->rrdlabels, "key2", "value2", RRDLABEL_SRC_CONFIG);
87
-
88
- localhost->rrdset_root_index = dictionary_create(DICT_OPTION_DONT_OVERWRITE_VALUE);
89
- dictionary_register_insert_callback(localhost->rrdset_root_index, rrdset_insert_callback, NULL);
90
- RRDSET *st = dictionary_set_advanced(localhost->rrdset_root_index, "chart_id", -1, NULL, sizeof(RRDSET), NULL);
91
-
92
- st->rrddim_root_index = dictionary_create(DICT_OPTION_DONT_OVERWRITE_VALUE);
93
- dictionary_register_insert_callback(st->rrddim_root_index, rrddim_insert_callback, NULL);
94
- st->dimensions = dictionary_set_advanced(st->rrddim_root_index, "dimension_id", -1, NULL, rrddim_size(), st);
95
-
96
- return 0;
97
-}
98
-
99
-int teardown_rrdhost()
100
-{
101
- RRDSET *st;
102
- rrdset_foreach_read(st, localhost);
103
- break;
104
- rrdset_foreach_done(st);
105
-
106
- RRDDIM *rd;
107
- rrddim_foreach_read(rd, st);
108
- break;
109
- rrddim_foreach_done(rd);
110
-
111
- string_freez(rd->id);
112
- string_freez(rd->name);
113
- free(rd->tiers[0]);
114
-
115
- string_freez(st->id);
116
- string_freez(st->name);
117
- dictionary_destroy(st->rrddim_root_index);
118
-
119
- rrdlabels_destroy(localhost->rrdlabels);
120
-
121
- string_freez(localhost->tags);
122
- dictionary_destroy(localhost->rrdset_root_index);
123
- free(localhost);
124
-
125
- return 0;
126
-}
127
-
128
-int setup_initialized_engine(void **state)
129
-{
130
- setup_configured_engine(state);
131
-
132
- struct engine *engine = *state;
133
- init_connectors_in_tests(engine);
134
-
135
- setup_rrdhost();
136
-
137
- return 0;
138
-}
139
-
140
-int teardown_initialized_engine(void **state)
141
-{
142
- struct engine *engine = *state;
143
-
144
- teardown_rrdhost();
145
- buffer_free(engine->instance_root->labels_buffer);
146
- buffer_free(engine->instance_root->buffer);
147
- teardown_configured_engine(state);
148
-
149
- return 0;
150
-}
151
-
152
-int setup_prometheus(void **state)
153
-{
154
- (void)state;
155
-
156
- prometheus_exporter_instance = calloc(1, sizeof(struct instance));
157
-
158
- setup_rrdhost();
159
-
160
- prometheus_exporter_instance->config.update_every = 10;
161
-
162
- prometheus_exporter_instance->config.options |=
163
- EXPORTING_OPTION_SEND_NAMES | EXPORTING_OPTION_SEND_CONFIGURED_LABELS | EXPORTING_OPTION_SEND_AUTOMATIC_LABELS;
164
-
165
- prometheus_exporter_instance->config.charts_pattern = simple_pattern_create("*", NULL, SIMPLE_PATTERN_EXACT);
166
- prometheus_exporter_instance->config.hosts_pattern = simple_pattern_create("*", NULL, SIMPLE_PATTERN_EXACT);
167
-
168
- prometheus_exporter_instance->config.initialized = 1;
169
-
170
- return 0;
171
-}
172
-
173
-int teardown_prometheus(void **state)
174
-{
175
- (void)state;
176
-
177
- teardown_rrdhost();
178
-
179
- simple_pattern_free(prometheus_exporter_instance->config.charts_pattern);
180
- simple_pattern_free(prometheus_exporter_instance->config.hosts_pattern);
181
- free(prometheus_exporter_instance);
182
-
183
- return 0;
184
-}
exporting/tests/netdata_doubles.c
deleted
-255
@@ -1,255 +0,0 @@
1
-// SPDX-License-Identifier: GPL-3.0-or-later
2
-
3
-#include "test_exporting_engine.h"
4
-
5
-// Use memory allocation functions guarded by CMocka in strdupz
6
-const char *__wrap_strdupz(const char *s)
7
-{
8
- char *duplicate = malloc(sizeof(char) * (strlen(s) + 1));
9
- strcpy(duplicate, s);
10
-
11
- return duplicate;
12
-}
13
-
14
-time_t __wrap_now_realtime_sec(void)
15
-{
16
- function_called();
17
- return mock_type(time_t);
18
-}
19
-
20
-void __wrap_uv_thread_set_name_np(uv_thread_t ut, const char* name)
21
-{
22
- (void)ut;
23
- (void)name;
24
-
25
- function_called();
26
-}
27
-
28
-void __wrap_info_int(const char *file, const char *function, const unsigned long line, const char *fmt, ...)
29
-{
30
- (void)file;
31
- (void)function;
32
- (void)line;
33
-
34
- function_called();
35
-
36
- va_list args;
37
-
38
- va_start(args, fmt);
39
- vsnprintf(log_line, MAX_LOG_LINE, fmt, args);
40
- va_end(args);
41
-}
42
-
43
-int __wrap_connect_to_one_of(
44
- const char *destination,
45
- int default_port,
46
- struct timeval *timeout,
47
- size_t *reconnects_counter,
48
- char *connected_to,
49
- size_t connected_to_size)
50
-{
51
- (void)timeout;
52
-
53
- function_called();
54
-
55
- check_expected(destination);
56
- check_expected_ptr(default_port);
57
- // TODO: check_expected_ptr(timeout);
58
- check_expected(reconnects_counter);
59
- check_expected(connected_to);
60
- check_expected(connected_to_size);
61
-
62
- return mock_type(int);
63
-}
64
-
65
-void __rrdhost_check_rdlock(RRDHOST *host, const char *file, const char *function, const unsigned long line)
66
-{
67
- (void)host;
68
- (void)file;
69
- (void)function;
70
- (void)line;
71
-}
72
-
73
-void __rrdset_check_rdlock(RRDSET *st, const char *file, const char *function, const unsigned long line)
74
-{
75
- (void)st;
76
- (void)file;
77
- (void)function;
78
- (void)line;
79
-}
80
-
81
-void __rrd_check_rdlock(const char *file, const char *function, const unsigned long line)
82
-{
83
- (void)file;
84
- (void)function;
85
- (void)line;
86
-}
87
-
88
-RRDSET *rrdset_create_custom(
89
- RRDHOST *host,
90
- const char *type,
91
- const char *id,
92
- const char *name,
93
- const char *family,
94
- const char *context,
95
- const char *title,
96
- const char *units,
97
- const char *plugin,
98
- const char *module,
99
- long priority,
100
- int update_every,
101
- RRDSET_TYPE chart_type,
102
- RRD_MEMORY_MODE memory_mode,
103
- long history_entries)
104
-{
105
- check_expected_ptr(host);
106
- check_expected_ptr(type);
107
- check_expected_ptr(id);
108
- check_expected_ptr(name);
109
- check_expected_ptr(family);
110
- check_expected_ptr(context);
111
- UNUSED(title);
112
- check_expected_ptr(units);
113
- check_expected_ptr(plugin);
114
- check_expected_ptr(module);
115
- check_expected(priority);
116
- check_expected(update_every);
117
- check_expected(chart_type);
118
- UNUSED(memory_mode);
119
- UNUSED(history_entries);
120
-
121
- function_called();
122
-
123
- return mock_ptr_type(RRDSET *);
124
-}
125
-
126
-void rrdset_next_usec(RRDSET *st, usec_t microseconds)
127
-{
128
- check_expected_ptr(st);
129
- UNUSED(microseconds);
130
-
131
- function_called();
132
-}
133
-
134
-void rrdset_done(RRDSET *st)
135
-{
136
- check_expected_ptr(st);
137
-
138
- function_called();
139
-}
140
-
141
-RRDDIM *rrddim_add_custom(
142
- RRDSET *st,
143
- const char *id,
144
- const char *name,
145
- collected_number multiplier,
146
- collected_number divisor,
147
- RRD_ALGORITHM algorithm,
148
- RRD_MEMORY_MODE memory_mode)
149
-{
150
- check_expected_ptr(st);
151
- UNUSED(id);
152
- check_expected_ptr(name);
153
- check_expected(multiplier);
154
- check_expected(divisor);
155
- check_expected(algorithm);
156
- UNUSED(memory_mode);
157
-
158
- function_called();
159
-
160
- return NULL;
161
-}
162
-
163
-collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value)
164
-{
165
- check_expected_ptr(st);
166
- UNUSED(rd);
167
- UNUSED(value);
168
-
169
- function_called();
170
-
171
- return 0;
172
-}
173
-
174
-const char *rrd_memory_mode_name(RRD_MEMORY_MODE id)
175
-{
176
- (void)id;
177
- return RRD_MEMORY_MODE_NONE_NAME;
178
-}
179
-
180
-void rrdset_update_heterogeneous_flag(RRDSET *st)
181
-{
182
- (void)st;
183
-}
184
-
185
-time_t __mock_rrddim_query_oldest_time(STORAGE_METRIC_HANDLE *smh)
186
-{
187
- (void)smh;
188
-
189
- function_called();
190
- return mock_type(time_t);
191
-}
192
-
193
-time_t __mock_rrddim_query_latest_time(STORAGE_METRIC_HANDLE *smh)
194
-{
195
- (void)smh;
196
-
197
- function_called();
198
- return mock_type(time_t);
199
-}
200
-
201
-void __mock_rrddim_query_init(STORAGE_METRIC_HANDLE *smh, struct rrddim_query_handle *handle, time_t start_time, time_t end_time)
202
-{
203
- (void)smh;
204
- (void)handle;
205
-
206
- function_called();
207
- check_expected(start_time);
208
- check_expected(end_time);
209
-}
210
-
211
-int __mock_rrddim_query_is_finished(struct rrddim_query_handle *handle)
212
-{
213
- (void)handle;
214
-
215
- function_called();
216
- return mock_type(int);
217
-}
218
-
219
-STORAGE_POINT __mock_rrddim_query_next_metric(struct rrddim_query_handle *handle)
220
-{
221
- (void)handle;
222
-
223
- function_called();
224
-
225
- STORAGE_POINT sp = {};
226
- return sp;
227
-}
228
-
229
-void __mock_rrddim_query_finalize(struct rrddim_query_handle *handle)
230
-{
231
- (void)handle;
232
-
233
- function_called();
234
-}
235
-
236
-void rrdcalc_update_rrdlabels(RRDSET *st)
237
-{
238
- (void)st;
239
-}
240
-
241
-void rrdpush_sender_send_this_host_variable_now(RRDHOST *host, const RRDVAR_ACQUIRED *rva)
242
-{
243
- (void)host;
244
- (void)rva;
245
-}
246
-
247
-void db_execute(const char *cmd)
248
-{
249
- (void)cmd;
250
-}
251
-
252
-DICTIONARY *rrdfamily_rrdvars_dict(const RRDFAMILY_ACQUIRED *rfa) {
253
- (void)rfa;
254
- return NULL;
255
-}
exporting/tests/system_doubles.c
deleted
-61
@@ -1,61 +0,0 @@
1
-// SPDX-License-Identifier: GPL-3.0-or-later
2
-
3
-#include "test_exporting_engine.h"
4
-
5
-void __wrap_uv_thread_create(uv_thread_t thread, void (*worker)(void *arg), void *arg)
6
-{
7
- function_called();
8
-
9
- check_expected_ptr(thread);
10
- check_expected_ptr(worker);
11
- check_expected_ptr(arg);
12
-}
13
-
14
-void __wrap_uv_mutex_lock(uv_mutex_t *mutex)
15
-{
16
- (void)mutex;
17
-}
18
-
19
-void __wrap_uv_mutex_unlock(uv_mutex_t *mutex)
20
-{
21
- (void)mutex;
22
-}
23
-
24
-void __wrap_uv_cond_signal(uv_cond_t *cond_var)
25
-{
26
- (void)cond_var;
27
-}
28
-
29
-void __wrap_uv_cond_wait(uv_cond_t *cond_var, uv_mutex_t *mutex)
30
-{
31
- (void)cond_var;
32
- (void)mutex;
33
-}
34
-
35
-ssize_t __wrap_recv(int sockfd, void *buf, size_t len, int flags)
36
-{
37
- function_called();
38
-
39
- check_expected(sockfd);
40
- check_expected_ptr(buf);
41
- check_expected(len);
42
- check_expected(flags);
43
-
44
- char *mock_string = "Test recv";
45
- strcpy(buf, mock_string);
46
-
47
- return strlen(mock_string);
48
-}
49
-
50
-ssize_t __wrap_send(int sockfd, const void *buf, size_t len, int flags)
51
-{
52
- function_called();
53
-
54
- check_expected(sockfd);
55
- check_expected_ptr(buf);
56
- check_expected_ptr(buf);
57
- check_expected(len);
58
- check_expected(flags);
59
-
60
- return strlen(buf);
61
-}
exporting/tests/test_exporting_engine.c
deleted
-2068
@@ -1,2068 +0,0 @@
1
-// SPDX-License-Identifier: GPL-3.0-or-later
2
-
3
-#include "test_exporting_engine.h"
4
-#include "libnetdata/required_dummies.h"
5
-
6
-RRDHOST *localhost;
7
-netdata_rwlock_t rrd_rwlock;
8
-
9
-// global variables needed by read_exporting_config()
10
-struct config netdata_config;
11
-char *netdata_configured_user_config_dir = ".";
12
-char *netdata_configured_stock_config_dir = ".";
13
-char *netdata_configured_hostname = "test_global_host";
14
-bool global_statistics_enabled = true;
15
-
16
-char log_line[MAX_LOG_LINE + 1];
17
-
18
-void init_connectors_in_tests(struct engine *engine)
19
-{
20
- expect_function_call(__wrap_now_realtime_sec);
21
- will_return(__wrap_now_realtime_sec, 2);
22
-
23
- expect_function_call(__wrap_uv_thread_create);
24
-
25
- expect_value(__wrap_uv_thread_create, thread, &engine->instance_root->thread);
26
- expect_value(__wrap_uv_thread_create, worker, simple_connector_worker);
27
- expect_value(__wrap_uv_thread_create, arg, engine->instance_root);
28
-
29
- expect_function_call(__wrap_uv_thread_set_name_np);
30
-
31
- assert_int_equal(__real_init_connectors(engine), 0);
32
-
33
- assert_int_equal(engine->now, 2);
34
- assert_int_equal(engine->instance_root->after, 2);
35
-}
36
-
37
-static void test_exporting_engine(void **state)
38
-{
39
- struct engine *engine = *state;
40
-
41
- expect_function_call(__wrap_read_exporting_config);
42
- will_return(__wrap_read_exporting_config, engine);
43
-
44
- expect_function_call(__wrap_init_connectors);
45
- expect_memory(__wrap_init_connectors, engine, engine, sizeof(struct engine));
46
- will_return(__wrap_init_connectors, 0);
47
-
48
- expect_function_call(__wrap_create_main_rusage_chart);
49
- expect_not_value(__wrap_create_main_rusage_chart, st_rusage, NULL);
50
- expect_not_value(__wrap_create_main_rusage_chart, rd_user, NULL);
51
- expect_not_value(__wrap_create_main_rusage_chart, rd_system, NULL);
52
-
53
- expect_function_call(__wrap_now_realtime_sec);
54
- will_return(__wrap_now_realtime_sec, 2);
55
-
56
- expect_function_call(__wrap_mark_scheduled_instances);
57
- expect_memory(__wrap_mark_scheduled_instances, engine, engine, sizeof(struct engine));
58
- will_return(__wrap_mark_scheduled_instances, 1);
59
-
60
- expect_function_call(__wrap_prepare_buffers);
61
- expect_memory(__wrap_prepare_buffers, engine, engine, sizeof(struct engine));
62
- will_return(__wrap_prepare_buffers, 0);
63
-
64
- expect_function_call(__wrap_send_main_rusage);
65
- expect_value(__wrap_send_main_rusage, st_rusage, NULL);
66
- expect_value(__wrap_send_main_rusage, rd_user, NULL);
67
- expect_value(__wrap_send_main_rusage, rd_system, NULL);
68
-
69
- void *ptr = malloc(sizeof(struct netdata_static_thread));
70
- assert_ptr_equal(exporting_main(ptr), NULL);
71
- assert_int_equal(engine->now, 2);
72
- free(ptr);
73
-}
74
-
75
-static void test_read_exporting_config(void **state)
76
-{
77
- struct engine *engine = __mock_read_exporting_config(); // TODO: use real read_exporting_config() function
78
- *state = engine;
79
-
80
- assert_ptr_not_equal(engine, NULL);
81
- assert_string_equal(engine->config.hostname, "test_engine_host");
82
- assert_int_equal(engine->config.update_every, 3);
83
- assert_int_equal(engine->instance_num, 0);
84
-
85
-
86
- struct instance *instance = engine->instance_root;
87
- assert_ptr_not_equal(instance, NULL);
88
- assert_ptr_equal(instance->next, NULL);
89
- assert_ptr_equal(instance->engine, engine);
90
- assert_int_equal(instance->config.type, EXPORTING_CONNECTOR_TYPE_GRAPHITE);
91
- assert_string_equal(instance->config.destination, "localhost");
92
- assert_string_equal(instance->config.prefix, "netdata");
93
- assert_int_equal(instance->config.update_every, 1);
94
- assert_int_equal(instance->config.buffer_on_failures, 10);
95
- assert_int_equal(instance->config.timeoutms, 10000);
96
- assert_true(simple_pattern_matches(instance->config.charts_pattern, "any_chart"));
97
- assert_true(simple_pattern_matches(instance->config.hosts_pattern, "anyt_host"));
98
- assert_int_equal(instance->config.options, EXPORTING_SOURCE_DATA_AS_COLLECTED | EXPORTING_OPTION_SEND_NAMES);
99
-
100
- teardown_configured_engine(state);
101
-}
102
-
103
-static void test_init_connectors(void **state)
104
-{
105
- struct engine *engine = *state;
106
-
107
- init_connectors_in_tests(engine);
108
-
109
- assert_int_equal(engine->instance_num, 1);
110
-
111
- struct instance *instance = engine->instance_root;
112
-
113
- assert_ptr_equal(instance->next, NULL);
114
- assert_int_equal(instance->index, 0);
115
-
116
- struct simple_connector_config *connector_specific_config = instance->config.connector_specific_config;
117
- assert_int_equal(connector_specific_config->default_port, 2003);
118
-
119
- assert_ptr_equal(instance->worker, simple_connector_worker);
120
- assert_ptr_equal(instance->start_batch_formatting, NULL);
121
- assert_ptr_equal(instance->start_host_formatting, format_host_labels_graphite_plaintext);
122
- assert_ptr_equal(instance->start_chart_formatting, NULL);
123
- assert_ptr_equal(instance->metric_formatting, format_dimension_collected_graphite_plaintext);
124
- assert_ptr_equal(instance->end_chart_formatting, NULL);
125
- assert_ptr_equal(instance->end_host_formatting, flush_host_labels);
126
-
127
- BUFFER *buffer = instance->buffer;
128
- assert_ptr_not_equal(buffer, NULL);
129
- buffer_sprintf(buffer, "%s", "graphite test");
130
- assert_string_equal(buffer_tostring(buffer), "graphite test");
131
-}
132
-
133
-static void test_init_graphite_instance(void **state)
134
-{
135
- struct engine *engine = *state;
136
- struct instance *instance = engine->instance_root;
137
-
138
- instance->config.options = EXPORTING_SOURCE_DATA_AS_COLLECTED | EXPORTING_OPTION_SEND_NAMES;
139
- assert_int_equal(init_graphite_instance(instance), 0);
140
- assert_int_equal(
141
- ((struct simple_connector_config *)(instance->config.connector_specific_config))->default_port, 2003);
142
- freez(instance->config.connector_specific_config);
143
- assert_ptr_equal(instance->metric_formatting, format_dimension_collected_graphite_plaintext);
144
- assert_ptr_not_equal(instance->buffer, NULL);
145
- buffer_free(instance->buffer);
146
-
147
- instance->config.options = EXPORTING_SOURCE_DATA_AVERAGE | EXPORTING_OPTION_SEND_NAMES;
148
- assert_int_equal(init_graphite_instance(instance), 0);
149
- assert_ptr_equal(instance->metric_formatting, format_dimension_stored_graphite_plaintext);
150
-}
151
-
152
-static void test_init_json_instance(void **state)
153
-{
154
- struct engine *engine = *state;
155
- struct instance *instance = engine->instance_root;
156
-
157
- instance->config.options = EXPORTING_SOURCE_DATA_AS_COLLECTED | EXPORTING_OPTION_SEND_NAMES;
158
- assert_int_equal(init_json_instance(instance), 0);
159
- assert_int_equal(
160
- ((struct simple_connector_config *)(instance->config.connector_specific_config))->default_port, 5448);
161
- freez(instance->config.connector_specific_config);
162
- assert_ptr_equal(instance->metric_formatting, format_dimension_collected_json_plaintext);
163
- assert_ptr_not_equal(instance->buffer, NULL);
164
- buffer_free(instance->buffer);
165
-
166
- instance->config.options = EXPORTING_SOURCE_DATA_AVERAGE | EXPORTING_OPTION_SEND_NAMES;
167
- assert_int_equal(init_json_instance(instance), 0);
168
- assert_ptr_equal(instance->metric_formatting, format_dimension_stored_json_plaintext);
169
-}
170
-
171
-static void test_init_opentsdb_telnet_instance(void **state)
172
-{
173
- struct engine *engine = *state;
174
- struct instance *instance = engine->instance_root;
175
-
176
- instance->config.options = EXPORTING_SOURCE_DATA_AS_COLLECTED | EXPORTING_OPTION_SEND_NAMES;
177
- assert_int_equal(init_opentsdb_telnet_instance(instance), 0);
178
- assert_int_equal(
179
- ((struct simple_connector_config *)(instance->config.connector_specific_config))->default_port, 4242);
180
- freez(instance->config.connector_specific_config);
181
- assert_ptr_equal(instance->metric_formatting, format_dimension_collected_opentsdb_telnet);
182
- assert_ptr_not_equal(instance->buffer, NULL);
183
- buffer_free(instance->buffer);
184
-
185
- instance->config.options = EXPORTING_SOURCE_DATA_AVERAGE | EXPORTING_OPTION_SEND_NAMES;
186
- assert_int_equal(init_opentsdb_telnet_instance(instance), 0);
187
- assert_ptr_equal(instance->metric_formatting, format_dimension_stored_opentsdb_telnet);
188
-}
189
-
190
-static void test_init_opentsdb_http_instance(void **state)
191
-{
192
- struct engine *engine = *state;
193
- struct instance *instance = engine->instance_root;
194
-
195
- instance->config.options = EXPORTING_SOURCE_DATA_AS_COLLECTED | EXPORTING_OPTION_SEND_NAMES;
196
- assert_int_equal(init_opentsdb_http_instance(instance), 0);
197
- assert_int_equal(
198
- ((struct simple_connector_config *)(instance->config.connector_specific_config))->default_port, 4242);
199
- freez(instance->config.connector_specific_config);
200
- assert_ptr_equal(instance->metric_formatting, format_dimension_collected_opentsdb_http);
201
- assert_ptr_not_equal(instance->buffer, NULL);
202
- buffer_free(instance->buffer);
203
-
204
- instance->config.options = EXPORTING_SOURCE_DATA_AVERAGE | EXPORTING_OPTION_SEND_NAMES;
205
- assert_int_equal(init_opentsdb_http_instance(instance), 0);
206
- assert_ptr_equal(instance->metric_formatting, format_dimension_stored_opentsdb_http);
207
-}
208
-
209
-static void test_mark_scheduled_instances(void **state)
210
-{
211
- struct engine *engine = *state;
212
-
213
- assert_int_equal(__real_mark_scheduled_instances(engine), 1);
214
-
215
- struct instance *instance = engine->instance_root;
216
- assert_int_equal(instance->scheduled, 1);
217
- assert_int_equal(instance->before, 2);
218
-}
219
-
220
-static void test_rrdhost_is_exportable(void **state)
221
-{
222
- struct engine *engine = *state;
223
- struct instance *instance = engine->instance_root;
224
-
225
- expect_function_call(__wrap_info_int);
226
-
227
- assert_ptr_equal(localhost->exporting_flags, NULL);
228
-
229
- assert_int_equal(__real_rrdhost_is_exportable(instance, localhost), 1);
230
-
231
- assert_string_equal(log_line, "enabled exporting of host 'localhost' for instance 'instance_name'");
232
-
233
- assert_ptr_not_equal(localhost->exporting_flags, NULL);
234
- assert_int_equal(localhost->exporting_flags[0], RRDHOST_FLAG_EXPORTING_SEND);
235
-}
236
-
237
-static void test_false_rrdhost_is_exportable(void **state)
238
-{
239
- struct engine *engine = *state;
240
- struct instance *instance = engine->instance_root;
241
-
242
- simple_pattern_free(instance->config.hosts_pattern);
243
- instance->config.hosts_pattern = simple_pattern_create("!*", NULL, SIMPLE_PATTERN_EXACT);
244
-
245
- expect_function_call(__wrap_info_int);
246
-
247
- assert_ptr_equal(localhost->exporting_flags, NULL);
248
-
249
- assert_int_equal(__real_rrdhost_is_exportable(instance, localhost), 0);
250
-
251
- assert_string_equal(log_line, "disabled exporting of host 'localhost' for instance 'instance_name'");
252
-
253
- assert_ptr_not_equal(localhost->exporting_flags, NULL);
254
- assert_int_equal(localhost->exporting_flags[0], RRDHOST_FLAG_EXPORTING_DONT_SEND);
255
-}
256
-
257
-static void test_rrdset_is_exportable(void **state)
258
-{
259
- struct engine *engine = *state;
260
- struct instance *instance = engine->instance_root;
261
- RRDSET *st;
262
- rrdset_foreach_read(st, localhost);
263
- break;
264
- rrdset_foreach_done(st);
265
-
266
- assert_ptr_equal(st->exporting_flags, NULL);
267
-
268
- assert_int_equal(__real_rrdset_is_exportable(instance, st), 1);
269
-
270
- assert_ptr_not_equal(st->exporting_flags, NULL);
271
- assert_int_equal(st->exporting_flags[0], RRDSET_FLAG_EXPORTING_SEND);
272
-}
273
-
274
-static void test_false_rrdset_is_exportable(void **state)
275
-{
276
- struct engine *engine = *state;
277
- struct instance *instance = engine->instance_root;
278
- RRDSET *st;
279
- rrdset_foreach_read(st, localhost);
280
- break;
281
- rrdset_foreach_done(st);
282
-
283
- simple_pattern_free(instance->config.charts_pattern);
284
- instance->config.charts_pattern = simple_pattern_create("!*", NULL, SIMPLE_PATTERN_EXACT);
285
-
286
- assert_ptr_equal(st->exporting_flags, NULL);
287
-
288
- assert_int_equal(__real_rrdset_is_exportable(instance, st), 0);
289
-
290
- assert_ptr_not_equal(st->exporting_flags, NULL);
291
- assert_int_equal(st->exporting_flags[0], RRDSET_FLAG_EXPORTING_IGNORE);
292
-}
293
-
294
-static void test_exporting_calculate_value_from_stored_data(void **state)
295
-{
296
- struct engine *engine = *state;
297
- struct instance *instance = engine->instance_root;
298
-
299
- RRDSET *st;
300
- rrdset_foreach_read(st, localhost);
301
- break;
302
- rrdset_foreach_done(st);
303
-
304
- RRDDIM *rd;
305
- rrddim_foreach_read(rd, st);
306
- break;
307
- rrddim_foreach_done(rd);
308
-
309
- time_t timestamp;
310
-
311
- instance->after = 3;
312
- instance->before = 10;
313
-
314
- expect_function_call(__mock_rrddim_query_oldest_time);
315
- will_return(__mock_rrddim_query_oldest_time, 1);
316
-
317
- expect_function_call(__mock_rrddim_query_latest_time);
318
- will_return(__mock_rrddim_query_latest_time, 2);
319
-
320
- expect_function_call(__mock_rrddim_query_init);
321
- expect_value(__mock_rrddim_query_init, start_time, 1);
322
- expect_value(__mock_rrddim_query_init, end_time, 2);
323
-
324
- expect_function_call(__mock_rrddim_query_is_finished);
325
- will_return(__mock_rrddim_query_is_finished, 0);
326
- expect_function_call(__mock_rrddim_query_next_metric);
327
-
328
- expect_function_call(__mock_rrddim_query_is_finished);
329
- will_return(__mock_rrddim_query_is_finished, 0);
330
- expect_function_call(__mock_rrddim_query_next_metric);
331
-
332
- expect_function_call(__mock_rrddim_query_is_finished);
333
- will_return(__mock_rrddim_query_is_finished, 1);
334
-
335
- expect_function_call(__mock_rrddim_query_finalize);
336
-
337
- assert_float_equal(__real_exporting_calculate_value_from_stored_data(instance, rd, ×tamp), 36, 0.1);
338
-}
339
-
340
-static void test_prepare_buffers(void **state)
341
-{
342
- struct engine *engine = *state;
343
- struct instance *instance = engine->instance_root;
344
-
345
- instance->start_batch_formatting = __mock_start_batch_formatting;
346
- instance->start_host_formatting = __mock_start_host_formatting;
347
- instance->start_chart_formatting = __mock_start_chart_formatting;
348
- instance->metric_formatting = __mock_metric_formatting;
349
- instance->end_chart_formatting = __mock_end_chart_formatting;
350
- instance->end_host_formatting = __mock_end_host_formatting;
351
- instance->end_batch_formatting = __mock_end_batch_formatting;
352
- __real_mark_scheduled_instances(engine);
353
-
354
- expect_function_call(__mock_start_batch_formatting);
355
- expect_value(__mock_start_batch_formatting, instance, instance);
356
- will_return(__mock_start_batch_formatting, 0);
357
-
358
- expect_function_call(__wrap_rrdhost_is_exportable);
359
- expect_value(__wrap_rrdhost_is_exportable, instance, instance);
360
- expect_value(__wrap_rrdhost_is_exportable, host, localhost);
361
- will_return(__wrap_rrdhost_is_exportable, 1);
362
-
363
- expect_function_call(__mock_start_host_formatting);
364
- expect_value(__mock_start_host_formatting, instance, instance);
365
- expect_value(__mock_start_host_formatting, host, localhost);
366
- will_return(__mock_start_host_formatting, 0);
367
-
368
- RRDSET *st;
369
- rrdset_foreach_read(st, localhost);
370
- break;
371
- rrdset_foreach_done(st);
372
-
373
- expect_function_call(__wrap_rrdset_is_exportable);
374
- expect_value(__wrap_rrdset_is_exportable, instance, instance);
375
- expect_value(__wrap_rrdset_is_exportable, st, st);
376
- will_return(__wrap_rrdset_is_exportable, 1);
377
-
378
- expect_function_call(__mock_start_chart_formatting);
379
- expect_value(__mock_start_chart_formatting, instance, instance);
380
- expect_value(__mock_start_chart_formatting, st, st);
381
- will_return(__mock_start_chart_formatting, 0);
382
-
383
- RRDDIM *rd;
384
- rrddim_foreach_read(rd, st);
385
- break;
386
- rrddim_foreach_done(rd);
387
- expect_function_call(__mock_metric_formatting);
388
- expect_value(__mock_metric_formatting, instance, instance);
389
- expect_value(__mock_metric_formatting, rd, rd);
390
- will_return(__mock_metric_formatting, 0);
391
-
392
- expect_function_call(__mock_end_chart_formatting);
393
- expect_value(__mock_end_chart_formatting, instance, instance);
394
- expect_value(__mock_end_chart_formatting, st, st);
395
- will_return(__mock_end_chart_formatting, 0);
396
-
397
- expect_function_call(__mock_end_host_formatting);
398
- expect_value(__mock_end_host_formatting, instance, instance);
399
- expect_value(__mock_end_host_formatting, host, localhost);
400
- will_return(__mock_end_host_formatting, 0);
401
-
402
- expect_function_call(__mock_end_batch_formatting);
403
- expect_value(__mock_end_batch_formatting, instance, instance);
404
- will_return(__mock_end_batch_formatting, 0);
405
-
406
- __real_prepare_buffers(engine);
407
-
408
- assert_int_equal(instance->stats.buffered_metrics, 1);
409
-
410
- // check with NULL functions
411
- instance->start_batch_formatting = NULL;
412
- instance->start_host_formatting = NULL;
413
- instance->start_chart_formatting = NULL;
414
- instance->metric_formatting = NULL;
415
- instance->end_chart_formatting = NULL;
416
- instance->end_host_formatting = NULL;
417
- instance->end_batch_formatting = NULL;
418
- __real_prepare_buffers(engine);
419
-
420
- assert_int_equal(instance->scheduled, 0);
421
- assert_int_equal(instance->after, 2);
422
-}
423
-
424
-static void test_exporting_name_copy(void **state)
425
-{
426
- (void)state;
427
-
428
- char *source_name = "test.name-with/special#characters_";
429
- char destination_name[RRD_ID_LENGTH_MAX + 1];
430
-
431
- assert_int_equal(exporting_name_copy(destination_name, source_name, RRD_ID_LENGTH_MAX), 34);
432
- assert_string_equal(destination_name, "test.name_with_special_characters_");
433
-}
434
-
435
-static void test_format_dimension_collected_graphite_plaintext(void **state)
436
-{
437
- struct engine *engine = *state;
438
-
439
- RRDSET *st;
440
- rrdset_foreach_read(st, localhost);
441
- break;
442
- rrdset_foreach_done(st);
443
-
444
- RRDDIM *rd;
445
- rrddim_foreach_read(rd, st);
446
- break;
447
- rrddim_foreach_done(rd);
448
- assert_int_equal(format_dimension_collected_graphite_plaintext(engine->instance_root, rd), 0);
449
- assert_string_equal(
450
- buffer_tostring(engine->instance_root->buffer),
451
- "netdata.test-host.chart_name.dimension_name;TAG1=VALUE1 TAG2=VALUE2 123000321 15051\n");
452
-}
453
-
454
-static void test_format_dimension_stored_graphite_plaintext(void **state)
455
-{
456
- struct engine *engine = *state;
457
-
458
- expect_function_call(__wrap_exporting_calculate_value_from_stored_data);
459
- will_return(__wrap_exporting_calculate_value_from_stored_data, pack_storage_number(27, SN_DEFAULT_FLAGS));
460
-
461
- RRDSET *st;
462
- rrdset_foreach_read(st, localhost);
463
- break;
464
- rrdset_foreach_done(st);
465
-
466
- RRDDIM *rd;
467
- rrddim_foreach_read(rd, st);
468
- break;
469
- rrddim_foreach_done(rd);
470
- assert_int_equal(format_dimension_stored_graphite_plaintext(engine->instance_root, rd), 0);
471
- assert_string_equal(
472
- buffer_tostring(engine->instance_root->buffer),
473
- "netdata.test-host.chart_name.dimension_name;TAG1=VALUE1 TAG2=VALUE2 690565856.0000000 15052\n");
474
-}
475
-
476
-static void test_format_dimension_collected_json_plaintext(void **state)
477
-{
478
- struct engine *engine = *state;
479
-
480
- RRDSET *st;
481
- rrdset_foreach_read(st, localhost);
482
- break;
483
- rrdset_foreach_done(st);
484
-
485
- RRDDIM *rd;
486
- rrddim_foreach_read(rd, st);
487
- break;
488
- rrddim_foreach_done(rd);
489
- assert_int_equal(format_dimension_collected_json_plaintext(engine->instance_root, rd), 0);
490
- assert_string_equal(
491
- buffer_tostring(engine->instance_root->buffer),
492
- "{\"prefix\":\"netdata\",\"hostname\":\"test-host\",\"host_tags\":\"TAG1=VALUE1 TAG2=VALUE2\","
493
- "\"chart_id\":\"chart_id\",\"chart_name\":\"chart_name\",\"chart_family\":\"\","
494
- "\"chart_context\":\"\",\"chart_type\":\"\",\"units\":\"\",\"id\":\"dimension_id\","
495
- "\"name\":\"dimension_name\",\"value\":123000321,\"timestamp\":15051}\n");
496
-}
497
-
498
-static void test_format_dimension_stored_json_plaintext(void **state)
499
-{
500
- struct engine *engine = *state;
501
-
502
- expect_function_call(__wrap_exporting_calculate_value_from_stored_data);
503
- will_return(__wrap_exporting_calculate_value_from_stored_data, pack_storage_number(27, SN_DEFAULT_FLAGS));
504
-
505
- RRDSET *st;
506
- rrdset_foreach_read(st, localhost);
507
- break;
508
- rrdset_foreach_done(st);
509
-
510
- RRDDIM *rd;
511
- rrddim_foreach_read(rd, st);
512
- break;
513
- rrddim_foreach_done(rd);
514
- assert_int_equal(format_dimension_stored_json_plaintext(engine->instance_root, rd), 0);
515
- assert_string_equal(
516
- buffer_tostring(engine->instance_root->buffer),
517
- "{\"prefix\":\"netdata\",\"hostname\":\"test-host\",\"host_tags\":\"TAG1=VALUE1 TAG2=VALUE2\","
518
- "\"chart_id\":\"chart_id\",\"chart_name\":\"chart_name\",\"chart_family\":\"\"," \
519
- "\"chart_context\": \"\",\"chart_type\":\"\",\"units\": \"\",\"id\":\"dimension_id\","
520
- "\"name\":\"dimension_name\",\"value\":690565856.0000000,\"timestamp\": 15052}\n");
521
-}
522
-
523
-static void test_format_dimension_collected_opentsdb_telnet(void **state)
524
-{
525
- struct engine *engine = *state;
526
-
527
- RRDSET *st;
528
- rrdset_foreach_read(st, localhost);
529
- break;
530
- rrdset_foreach_done(st);
531
-
532
- RRDDIM *rd;
533
- rrddim_foreach_read(rd, st);
534
- break;
535
- rrddim_foreach_done(rd);
536
- assert_int_equal(format_dimension_collected_opentsdb_telnet(engine->instance_root, rd), 0);
537
- assert_string_equal(
538
- buffer_tostring(engine->instance_root->buffer),
539
- "put netdata.chart_name.dimension_name 15051 123000321 host=test-host TAG1=VALUE1 TAG2=VALUE2\n");
540
-}
541
-
542
-static void test_format_dimension_stored_opentsdb_telnet(void **state)
543
-{
544
- struct engine *engine = *state;
545
-
546
- expect_function_call(__wrap_exporting_calculate_value_from_stored_data);
547
- will_return(__wrap_exporting_calculate_value_from_stored_data, pack_storage_number(27, SN_DEFAULT_FLAGS));
548
-
549
- RRDSET *st;
550
- rrdset_foreach_read(st, localhost);
551
- break;
552
- rrdset_foreach_done(st);
553
-
554
- RRDDIM *rd;
555
- rrddim_foreach_read(rd, st);
556
- break;
557
- rrddim_foreach_done(rd);
558
- assert_int_equal(format_dimension_stored_opentsdb_telnet(engine->instance_root, rd), 0);
559
- assert_string_equal(
560
- buffer_tostring(engine->instance_root->buffer),
561
- "put netdata.chart_name.dimension_name 15052 690565856.0000000 host=test-host TAG1=VALUE1 TAG2=VALUE2\n");
562
-}
563
-
564
-static void test_format_dimension_collected_opentsdb_http(void **state)
565
-{
566
- struct engine *engine = *state;
567
-
568
- RRDSET *st;
569
- rrdset_foreach_read(st, localhost);
570
- break;
571
- rrdset_foreach_done(st);
572
-
573
- RRDDIM *rd;
574
- rrddim_foreach_read(rd, st);
575
- break;
576
- rrddim_foreach_done(rd);
577
- assert_int_equal(format_dimension_collected_opentsdb_http(engine->instance_root, rd), 0);
578
- assert_string_equal(
579
- buffer_tostring(engine->instance_root->buffer),
580
- "{\"metric\":\"netdata.chart_name.dimension_name\","
581
- "\"timestamp\":15051,"
582
- "\"value\":123000321,"
583
- "\"tags\":{\"host\":\"test-host TAG1=VALUE1 TAG2=VALUE2\"}}");
584
-}
585
-
586
-static void test_format_dimension_stored_opentsdb_http(void **state)
587
-{
588
- struct engine *engine = *state;
589
-
590
- expect_function_call(__wrap_exporting_calculate_value_from_stored_data);
591
- will_return(__wrap_exporting_calculate_value_from_stored_data, pack_storage_number(27, SN_DEFAULT_FLAGS));
592
-
593
- RRDSET *st;
594
- rrdset_foreach_read(st, localhost);
595
- break;
596
- rrdset_foreach_done(st);
597
-
598
- RRDDIM *rd;
599
- rrddim_foreach_read(rd, st);
600
- break;
601
- rrddim_foreach_done(rd);
602
- assert_int_equal(format_dimension_stored_opentsdb_http(engine->instance_root, rd), 0);
603
- assert_string_equal(
604
- buffer_tostring(engine->instance_root->buffer),
605
- "{\"metric\":\"netdata.chart_name.dimension_name\","
606
- "\"timestamp\":15052,"
607
- "\"value\":690565856.0000000,"
608
- "\"tags\":{\"host\":\"test-host TAG1=VALUE1 TAG2=VALUE2\"}}");
609
-}
610
-
611
-static void test_exporting_discard_response(void **state)
612
-{
613
- struct engine *engine = *state;
614
-
615
- BUFFER *response = buffer_create(0, NULL);
616
- buffer_sprintf(response, "Test response");
617
-
618
- assert_int_equal(exporting_discard_response(response, engine->instance_root), 0);
619
- assert_int_equal(buffer_strlen(response), 0);
620
-
621
- buffer_free(response);
622
-}
623
-
624
-static void test_simple_connector_receive_response(void **state)
625
-{
626
- struct engine *engine = *state;
627
- struct instance *instance = engine->instance_root;
628
- struct stats *stats = &instance->stats;
629
-
630
- int sock = 1;
631
-
632
- expect_function_call(__wrap_recv);
633
- expect_value(__wrap_recv, sockfd, 1);
634
- expect_not_value(__wrap_recv, buf, 0);
635
- expect_value(__wrap_recv, len, 4096);
636
- expect_value(__wrap_recv, flags, MSG_DONTWAIT);
637
-
638
- simple_connector_receive_response(&sock, instance);
639
-
640
- assert_int_equal(stats->received_bytes, 9);
641
- assert_int_equal(stats->receptions, 1);
642
- assert_int_equal(sock, 1);
643
-}
644
-
645
-static void test_simple_connector_send_buffer(void **state)
646
-{
647
- struct engine *engine = *state;
648
- struct instance *instance = engine->instance_root;
649
- struct stats *stats = &instance->stats;
650
-
651
- int sock = 1;
652
- int failures = 3;
653
- size_t buffered_metrics = 1;
654
- BUFFER *header = buffer_create(0, NULL);
655
- BUFFER *buffer = buffer_create(0, NULL);
656
- buffer_strcat(header, "test header\n");
657
- buffer_strcat(buffer, "test buffer\n");
658
-
659
- expect_function_call(__wrap_send);
660
- expect_value(__wrap_send, sockfd, 1);
661
- expect_value(__wrap_send, buf, buffer_tostring(header));
662
- expect_string(__wrap_send, buf, "test header\n");
663
- expect_value(__wrap_send, len, 12);
664
- expect_value(__wrap_send, flags, MSG_NOSIGNAL);
665
-
666
- expect_function_call(__wrap_send);
667
- expect_value(__wrap_send, sockfd, 1);
668
- expect_value(__wrap_send, buf, buffer_tostring(buffer));
669
- expect_string(__wrap_send, buf, "test buffer\n");
670
- expect_value(__wrap_send, len, 12);
671
- expect_value(__wrap_send, flags, MSG_NOSIGNAL);
672
-
673
- simple_connector_send_buffer(&sock, &failures, instance, header, buffer, buffered_metrics);
674
-
675
- assert_int_equal(failures, 0);
676
- assert_int_equal(stats->transmission_successes, 1);
677
- assert_int_equal(stats->sent_bytes, 12);
678
- assert_int_equal(stats->sent_metrics, 1);
679
- assert_int_equal(stats->transmission_failures, 0);
680
-
681
- assert_int_equal(buffer_strlen(buffer), 0);
682
-
683
- assert_int_equal(sock, 1);
684
-}
685
-
686
-static void test_simple_connector_worker(void **state)
687
-{
688
- struct engine *engine = *state;
689
- struct instance *instance = engine->instance_root;
690
- struct stats *stats = &instance->stats;
691
-
692
- __real_mark_scheduled_instances(engine);
693
-
694
- struct simple_connector_data *simple_connector_data = callocz(1, sizeof(struct simple_connector_data));
695
- instance->connector_specific_data = simple_connector_data;
696
- simple_connector_data->last_buffer = callocz(1, sizeof(struct simple_connector_buffer));
697
- simple_connector_data->first_buffer = simple_connector_data->last_buffer;
698
- simple_connector_data->header = buffer_create(0, NULL);
699
- simple_connector_data->buffer = buffer_create(0, NULL);
700
- simple_connector_data->last_buffer->header = buffer_create(0, NULL);
701
- simple_connector_data->last_buffer->buffer = buffer_create(0, NULL);
702
- strcpy(simple_connector_data->connected_to, "localhost");
703
-
704
- buffer_sprintf(simple_connector_data->last_buffer->header, "test header");
705
- buffer_sprintf(simple_connector_data->last_buffer->buffer, "test buffer");
706
-
707
- expect_function_call(__wrap_now_realtime_sec);
708
- will_return(__wrap_now_realtime_sec, 2);
709
-
710
- expect_function_call(__wrap_now_realtime_sec);
711
- will_return(__wrap_now_realtime_sec, 2);
712
-
713
- expect_function_call(__wrap_now_realtime_sec);
714
- will_return(__wrap_now_realtime_sec, 2);
715
-
716
- expect_function_call(__wrap_send_internal_metrics);
717
- expect_value(__wrap_send_internal_metrics, instance, instance);
718
- will_return(__wrap_send_internal_metrics, 0);
719
-
720
- simple_connector_worker(instance);
721
-
722
- assert_int_equal(stats->buffered_metrics, 0);
723
- assert_int_equal(stats->buffered_bytes, 0);
724
- assert_int_equal(stats->received_bytes, 0);
725
- assert_int_equal(stats->sent_bytes, 0);
726
- assert_int_equal(stats->sent_metrics, 0);
727
- assert_int_equal(stats->lost_metrics, 0);
728
- assert_int_equal(stats->receptions, 0);
729
- assert_int_equal(stats->transmission_successes, 0);
730
- assert_int_equal(stats->transmission_failures, 0);
731
- assert_int_equal(stats->data_lost_events, 0);
732
- assert_int_equal(stats->lost_bytes, 0);
733
- assert_int_equal(stats->reconnects, 0);
734
-}
735
-
736
-static void test_sanitize_json_string(void **state)
737
-{
738
- (void)state;
739
-
740
- char *src = "check \t\\\" string";
741
- char dst[19 + 1];
742
-
743
- sanitize_json_string(dst, src, 19);
744
-
745
- assert_string_equal(dst, "check _\\\\\\\" string");
746
-}
747
-
748
-static void test_sanitize_graphite_label_value(void **state)
749
-{
750
- (void)state;
751
-
752
- char *src = "check ;~ string";
753
- char dst[15 + 1];
754
-
755
- sanitize_graphite_label_value(dst, src, 15);
756
-
757
- assert_string_equal(dst, "check____string");
758
-}
759
-
760
-static void test_sanitize_opentsdb_label_value(void **state)
761
-{
762
- (void)state;
763
-
764
- char *src = "check \t\\\" #&$? -_./ string";
765
- char dst[26 + 1];
766
-
767
- sanitize_opentsdb_label_value(dst, src, 26);
768
-
769
- assert_string_equal(dst, "check__________-_./_string");
770
-}
771
-
772
-static void test_format_host_labels_json_plaintext(void **state)
773
-{
774
- struct engine *engine = *state;
775
- struct instance *instance = engine->instance_root;
776
-
777
- instance->config.options |= EXPORTING_OPTION_SEND_CONFIGURED_LABELS;
778
- instance->config.options |= EXPORTING_OPTION_SEND_AUTOMATIC_LABELS;
779
-
780
- assert_int_equal(format_host_labels_json_plaintext(instance, localhost), 0);
781
- assert_string_equal(buffer_tostring(instance->labels_buffer), "\"labels\":{\"key1\":\"value1\",\"key2\":\"value2\"},");
782
-}
783
-
784
-static void test_format_host_labels_graphite_plaintext(void **state)
785
-{
786
- struct engine *engine = *state;
787
- struct instance *instance = engine->instance_root;
788
-
789
- instance->config.options |= EXPORTING_OPTION_SEND_CONFIGURED_LABELS;
790
- instance->config.options |= EXPORTING_OPTION_SEND_AUTOMATIC_LABELS;
791
-
792
- assert_int_equal(format_host_labels_graphite_plaintext(instance, localhost), 0);
793
- assert_string_equal(buffer_tostring(instance->labels_buffer), ";key1=value1;key2=value2");
794
-}
795
-
796
-static void test_format_host_labels_opentsdb_telnet(void **state)
797
-{
798
- struct engine *engine = *state;
799
- struct instance *instance = engine->instance_root;
800
-
801
- instance->config.options |= EXPORTING_OPTION_SEND_CONFIGURED_LABELS;
802
- instance->config.options |= EXPORTING_OPTION_SEND_AUTOMATIC_LABELS;
803
-
804
- assert_int_equal(format_host_labels_opentsdb_telnet(instance, localhost), 0);
805
- assert_string_equal(buffer_tostring(instance->labels_buffer), " key1=value1 key2=value2");
806
-}
807
-
808
-static void test_format_host_labels_opentsdb_http(void **state)
809
-{
810
- struct engine *engine = *state;
811
- struct instance *instance = engine->instance_root;
812
-
813
- instance->config.options |= EXPORTING_OPTION_SEND_CONFIGURED_LABELS;
814
- instance->config.options |= EXPORTING_OPTION_SEND_AUTOMATIC_LABELS;
815
-
816
- assert_int_equal(format_host_labels_opentsdb_http(instance, localhost), 0);
817
- assert_string_equal(buffer_tostring(instance->labels_buffer), ",\"key1\":\"value1\",\"key2\":\"value2\"");
818
-}
819
-
820
-static void test_flush_host_labels(void **state)
821
-{
822
- struct engine *engine = *state;
823
- struct instance *instance = engine->instance_root;
824
-
825
- instance->labels_buffer = buffer_create(12, NULL);
826
- buffer_strcat(instance->labels_buffer, "check string");
827
- assert_int_equal(buffer_strlen(instance->labels_buffer), 12);
828
-
829
- assert_int_equal(flush_host_labels(instance, localhost), 0);
830
- assert_int_equal(buffer_strlen(instance->labels_buffer), 0);
831
-}
832
-
833
-static void test_create_main_rusage_chart(void **state)
834
-{
835
- UNUSED(state);
836
-
837
- RRDSET *st_rusage = calloc(1, sizeof(RRDSET));
838
- RRDDIM *rd_user = NULL;
839
- RRDDIM *rd_system = NULL;
840
-
841
- expect_function_call(rrdset_create_custom);
842
- expect_value(rrdset_create_custom, host, localhost);
843
- expect_string(rrdset_create_custom, type, "netdata");
844
- expect_string(rrdset_create_custom, id, "exporting_main_thread_cpu");
845
- expect_value(rrdset_create_custom, name, NULL);
846
- expect_string(rrdset_create_custom, family, "exporting");
847
- expect_string(rrdset_create_custom, context, "netdata.exporting_cpu_usage");
848
- expect_string(rrdset_create_custom, units, "milliseconds/s");
849
- expect_string(rrdset_create_custom, plugin, "exporting");
850
- expect_value(rrdset_create_custom, module, NULL);
851
- expect_value(rrdset_create_custom, priority, 130600);
852
- expect_value(rrdset_create_custom, update_every, localhost->rrd_update_every);
853
- expect_value(rrdset_create_custom, chart_type, RRDSET_TYPE_STACKED);
854
- will_return(rrdset_create_custom, st_rusage);
855
-
856
- expect_function_calls(rrddim_add_custom, 2);
857
- expect_value_count(rrddim_add_custom, st, st_rusage, 2);
858
- expect_value_count(rrddim_add_custom, name, NULL, 2);
859
- expect_value_count(rrddim_add_custom, multiplier, 1, 2);
860
- expect_value_count(rrddim_add_custom, divisor, 1000, 2);
861
- expect_value_count(rrddim_add_custom, algorithm, RRD_ALGORITHM_INCREMENTAL, 2);
862
-
863
- __real_create_main_rusage_chart(&st_rusage, &rd_user, &rd_system);
864
-
865
- free(st_rusage);
866
-}
867
-
868
-static void test_send_main_rusage(void **state)
869
-{
870
- UNUSED(state);
871
-
872
- RRDSET *st_rusage = calloc(1, sizeof(RRDSET));
873
- st_rusage->counter_done = 1;
874
-
875
- expect_function_call(rrdset_next_usec);
876
- expect_value(rrdset_next_usec, st, st_rusage);
877
-
878
- expect_function_calls(rrddim_set_by_pointer, 2);
879
- expect_value_count(rrddim_set_by_pointer, st, st_rusage, 2);
880
-
881
- expect_function_call(rrdset_done);
882
- expect_value(rrdset_done, st, st_rusage);
883
-
884
- __real_send_main_rusage(st_rusage, NULL, NULL);
885
-
886
- free(st_rusage);
887
-}
888
-
889
-static void test_send_internal_metrics(void **state)
890
-{
891
- UNUSED(state);
892
-
893
- struct instance *instance = calloc(1, sizeof(struct instance));
894
- instance->config.name = (const char *)strdupz("test_instance");
895
- instance->config.update_every = 2;
896
-
897
- struct stats *stats = &instance->stats;
898
-
899
- stats->st_metrics = calloc(1, sizeof(RRDSET));
900
- stats->st_metrics->counter_done = 1;
901
- stats->st_bytes = calloc(1, sizeof(RRDSET));
902
- stats->st_bytes->counter_done = 1;
903
- stats->st_ops = calloc(1, sizeof(RRDSET));
904
- stats->st_ops->counter_done = 1;
905
- stats->st_rusage = calloc(1, sizeof(RRDSET));
906
- stats->st_rusage->counter_done = 1;
907
-
908
- // ------------------------------------------------------------------------
909
-
910
- expect_function_call(rrdset_create_custom);
911
- expect_value(rrdset_create_custom, host, localhost);
912
- expect_string(rrdset_create_custom, type, "netdata");
913
- expect_string(rrdset_create_custom, id, "exporting_test_instance_metrics");
914
- expect_value(rrdset_create_custom, name, NULL);
915
- expect_string(rrdset_create_custom, family, "exporting");
916
- expect_string(rrdset_create_custom, context, "exporting_buffer");
917
- expect_string(rrdset_create_custom, units, "metrics");
918
- expect_string(rrdset_create_custom, plugin, "exporting");
919
- expect_value(rrdset_create_custom, module, NULL);
920
- expect_value(rrdset_create_custom, priority, 130610);
921
- expect_value(rrdset_create_custom, update_every, 2);
922
- expect_value(rrdset_create_custom, chart_type, RRDSET_TYPE_LINE);
923
- will_return(rrdset_create_custom, stats->st_metrics);
924
-
925
- expect_function_calls(rrddim_add_custom, 3);
926
- expect_value_count(rrddim_add_custom, st, stats->st_metrics, 3);
927
- expect_value_count(rrddim_add_custom, name, NULL, 3);
928
- expect_value_count(rrddim_add_custom, multiplier, 1, 3);
929
- expect_value_count(rrddim_add_custom, divisor, 1, 3);
930
- expect_value_count(rrddim_add_custom, algorithm, RRD_ALGORITHM_ABSOLUTE, 3);
931
-
932
- // ------------------------------------------------------------------------
933
-
934
- expect_function_call(rrdset_create_custom);
935
- expect_value(rrdset_create_custom, host, localhost);
936
- expect_string(rrdset_create_custom, type, "netdata");
937
- expect_string(rrdset_create_custom, id, "exporting_test_instance_bytes");
938
- expect_value(rrdset_create_custom, name, NULL);
939
- expect_string(rrdset_create_custom, family, "exporting");
940
- expect_string(rrdset_create_custom, context, "exporting_data_size");
941
- expect_string(rrdset_create_custom, units, "KiB");
942
- expect_string(rrdset_create_custom, plugin, "exporting");
943
- expect_value(rrdset_create_custom, module, NULL);
944
- expect_value(rrdset_create_custom, priority, 130620);
945
- expect_value(rrdset_create_custom, update_every, 2);
946
- expect_value(rrdset_create_custom, chart_type, RRDSET_TYPE_AREA);
947
- will_return(rrdset_create_custom, stats->st_bytes);
948
-
949
- expect_function_calls(rrddim_add_custom, 4);
950
- expect_value_count(rrddim_add_custom, st, stats->st_bytes, 4);
951
- expect_value_count(rrddim_add_custom, name, NULL, 4);
952
- expect_value_count(rrddim_add_custom, multiplier, 1, 4);
953
- expect_value_count(rrddim_add_custom, divisor, 1024, 4);
954
- expect_value_count(rrddim_add_custom, algorithm, RRD_ALGORITHM_ABSOLUTE, 4);
955
-
956
- // ------------------------------------------------------------------------
957
-
958
- expect_function_call(rrdset_create_custom);
959
- expect_value(rrdset_create_custom, host, localhost);
960
- expect_string(rrdset_create_custom, type, "netdata");
961
- expect_string(rrdset_create_custom, id, "exporting_test_instance_ops");
962
- expect_value(rrdset_create_custom, name, NULL);
963
- expect_string(rrdset_create_custom, family, "exporting");
964
- expect_string(rrdset_create_custom, context, "exporting_operations");
965
- expect_string(rrdset_create_custom, units, "operations");
966
- expect_string(rrdset_create_custom, plugin, "exporting");
967
- expect_value(rrdset_create_custom, module, NULL);
968
- expect_value(rrdset_create_custom, priority, 130630);
969
- expect_value(rrdset_create_custom, update_every, 2);
970
- expect_value(rrdset_create_custom, chart_type, RRDSET_TYPE_LINE);
971
- will_return(rrdset_create_custom, stats->st_ops);
972
-
973
- expect_function_calls(rrddim_add_custom, 5);
974
- expect_value_count(rrddim_add_custom, st, stats->st_ops, 5);
975
- expect_value_count(rrddim_add_custom, name, NULL, 5);
976
- expect_value_count(rrddim_add_custom, multiplier, 1, 5);
977
- expect_value_count(rrddim_add_custom, divisor, 1, 5);
978
- expect_value_count(rrddim_add_custom, algorithm, RRD_ALGORITHM_ABSOLUTE, 5);
979
-
980
- // ------------------------------------------------------------------------
981
-
982
- expect_function_call(rrdset_create_custom);
983
- expect_value(rrdset_create_custom, host, localhost);
984
- expect_string(rrdset_create_custom, type, "netdata");
985
- expect_string(rrdset_create_custom, id, "exporting_test_instance_thread_cpu");
986
- expect_value(rrdset_create_custom, name, NULL);
987
- expect_string(rrdset_create_custom, family, "exporting");
988
- expect_string(rrdset_create_custom, context, "exporting_instance");
989
- expect_string(rrdset_create_custom, units, "milliseconds/s");
990
- expect_string(rrdset_create_custom, plugin, "exporting");
991
- expect_value(rrdset_create_custom, module, NULL);
992
- expect_value(rrdset_create_custom, priority, 130640);
993
- expect_value(rrdset_create_custom, update_every, 2);
994
- expect_value(rrdset_create_custom, chart_type, RRDSET_TYPE_STACKED);
995
- will_return(rrdset_create_custom, stats->st_rusage);
996
-
997
- expect_function_calls(rrddim_add_custom, 2);
998
- expect_value_count(rrddim_add_custom, st, stats->st_rusage, 2);
999
- expect_value_count(rrddim_add_custom, name, NULL, 2);
1000
- expect_value_count(rrddim_add_custom, multiplier, 1, 2);
1001
- expect_value_count(rrddim_add_custom, divisor, 1000, 2);
1002
- expect_value_count(rrddim_add_custom, algorithm, RRD_ALGORITHM_INCREMENTAL, 2);
1003
-
1004
- // ------------------------------------------------------------------------
1005
-
1006
- expect_function_call(rrdset_next_usec);
1007
- expect_value(rrdset_next_usec, st, stats->st_metrics);
1008
-
1009
- expect_function_calls(rrddim_set_by_pointer, 3);
1010
- expect_value_count(rrddim_set_by_pointer, st, stats->st_metrics, 3);
1011
-
1012
- expect_function_call(rrdset_done);
1013
- expect_value(rrdset_done, st, stats->st_metrics);
1014
-
1015
- // ------------------------------------------------------------------------
1016
-
1017
- expect_function_call(rrdset_next_usec);
1018
- expect_value(rrdset_next_usec, st, stats->st_bytes);
1019
-
1020
- expect_function_calls(rrddim_set_by_pointer, 4);
1021
- expect_value_count(rrddim_set_by_pointer, st, stats->st_bytes, 4);
1022
-
1023
- expect_function_call(rrdset_done);
1024
- expect_value(rrdset_done, st, stats->st_bytes);
1025
-
1026
- // ------------------------------------------------------------------------
1027
-
1028
- expect_function_call(rrdset_next_usec);
1029
- expect_value(rrdset_next_usec, st, stats->st_ops);
1030
-
1031
- expect_function_calls(rrddim_set_by_pointer, 5);
1032
- expect_value_count(rrddim_set_by_pointer, st, stats->st_ops, 5);
1033
-
1034
- expect_function_call(rrdset_done);
1035
- expect_value(rrdset_done, st, stats->st_ops);
1036
-
1037
- // ------------------------------------------------------------------------
1038
-
1039
- expect_function_call(rrdset_next_usec);
1040
- expect_value(rrdset_next_usec, st, stats->st_rusage);
1041
-
1042
- expect_function_calls(rrddim_set_by_pointer, 2);
1043
- expect_value_count(rrddim_set_by_pointer, st, stats->st_rusage, 2);
1044
-
1045
- expect_function_call(rrdset_done);
1046
- expect_value(rrdset_done, st, stats->st_rusage);
1047
-
1048
- // ------------------------------------------------------------------------
1049
-
1050
- __real_send_internal_metrics(instance);
1051
-
1052
- free(stats->st_metrics);
1053
- free(stats->st_bytes);
1054
- free(stats->st_ops);
1055
- free(stats->st_rusage);
1056
- free((void *)instance->config.name);
1057
- free(instance);
1058
-}
1059
-
1060
-static void test_can_send_rrdset(void **state)
1061
-{
1062
- (void)*state;
1063
-
1064
- RRDSET *st;
1065
- rrdset_foreach_read(st, localhost);
1066
- break;
1067
- rrdset_foreach_done(st);
1068
-
1069
- assert_int_equal(can_send_rrdset(prometheus_exporter_instance, st, NULL), 1);
1070
-
1071
- rrdset_flag_set(st, RRDSET_FLAG_EXPORTING_IGNORE);
1072
- assert_int_equal(can_send_rrdset(prometheus_exporter_instance, st, NULL), 0);
1073
- rrdset_flag_clear(st, RRDSET_FLAG_EXPORTING_IGNORE);
1074
-
1075
- // TODO: test with a denying simple pattern
1076
-
1077
- rrdset_flag_set(st, RRDSET_FLAG_OBSOLETE);
1078
- assert_int_equal(can_send_rrdset(prometheus_exporter_instance, st, NULL), 0);
1079
- rrdset_flag_clear(st, RRDSET_FLAG_OBSOLETE);
1080
-
1081
- st->rrd_memory_mode = RRD_MEMORY_MODE_NONE;
1082
- prometheus_exporter_instance->config.options |= EXPORTING_SOURCE_DATA_AVERAGE;
1083
- assert_int_equal(can_send_rrdset(prometheus_exporter_instance, st, NULL), 0);
1084
-}
1085
-
1086
-static void test_prometheus_name_copy(void **state)
1087
-{
1088
- (void)*state;
1089
-
1090
- char destination_name[PROMETHEUS_ELEMENT_MAX + 1];
1091
- assert_int_equal(prometheus_name_copy(destination_name, "test-name", PROMETHEUS_ELEMENT_MAX), 9);
1092
-
1093
- assert_string_equal(destination_name, "test_name");
1094
-}
1095
-
1096
-static void test_prometheus_label_copy(void **state)
1097
-{
1098
- (void)*state;
1099
-
1100
- char destination_name[PROMETHEUS_ELEMENT_MAX + 1];
1101
- assert_int_equal(prometheus_label_copy(destination_name, "test\"\\\nlabel", PROMETHEUS_ELEMENT_MAX), 15);
1102
-
1103
- assert_string_equal(destination_name, "test\\\"\\\\\\\nlabel");
1104
-}
1105
-
1106
-static void test_prometheus_units_copy(void **state)
1107
-{
1108
- (void)*state;
1109
-
1110
- char destination_name[PROMETHEUS_ELEMENT_MAX + 1];
1111
- assert_string_equal(prometheus_units_copy(destination_name, "test-units", PROMETHEUS_ELEMENT_MAX, 0), "_test_units");
1112
- assert_string_equal(destination_name, "_test_units");
1113
-
1114
- assert_string_equal(prometheus_units_copy(destination_name, "%", PROMETHEUS_ELEMENT_MAX, 0), "_percent");
1115
- assert_string_equal(prometheus_units_copy(destination_name, "test-units/s", PROMETHEUS_ELEMENT_MAX, 0), "_test_units_persec");
1116
-
1117
- assert_string_equal(prometheus_units_copy(destination_name, "KiB", PROMETHEUS_ELEMENT_MAX, 1), "_KB");
1118
-}
1119
-
1120
-static void test_format_host_labels_prometheus(void **state)
1121
-{
1122
- struct engine *engine = *state;
1123
- struct instance *instance = engine->instance_root;
1124
-
1125
- instance->config.options |= EXPORTING_OPTION_SEND_CONFIGURED_LABELS;
1126
- instance->config.options |= EXPORTING_OPTION_SEND_AUTOMATIC_LABELS;
1127
-
1128
- format_host_labels_prometheus(instance, localhost);
1129
- assert_string_equal(buffer_tostring(instance->labels_buffer), "key1=\"value1\",key2=\"value2\"");
1130
-}
1131
-
1132
-static void rrd_stats_api_v1_charts_allmetrics_prometheus(void **state)
1133
-{
1134
- (void)state;
1135
-
1136
- BUFFER *buffer = buffer_create(0, NULL);
1137
-
1138
- RRDSET *st;
1139
- rrdset_foreach_read(st, localhost);
1140
- break;
1141
- rrdset_foreach_done(st);
1142
-
1143
- localhost->hostname = string_strdupz("test_hostname");
1144
- st->family = string_strdupz("test_family");
1145
- st->context = string_strdupz("test_context");
1146
-
1147
- expect_function_call(__wrap_now_realtime_sec);
1148
- will_return(__wrap_now_realtime_sec, 2);
1149
-
1150
- expect_function_call(__wrap_exporting_calculate_value_from_stored_data);
1151
- will_return(__wrap_exporting_calculate_value_from_stored_data, pack_storage_number(27, SN_DEFAULT_FLAGS));
1152
-
1153
- rrd_stats_api_v1_charts_allmetrics_prometheus_single_host(localhost, NULL, buffer, "test_server", "test_prefix", 0, 0);
1154
-
1155
- assert_string_equal(
1156
- buffer_tostring(buffer),
1157
- "netdata_info{instance=\"test_hostname\",application=\"\",version=\"\",key1=\"value1\",key2=\"value2\"} 1\n"
1158
- "test_prefix_test_context{chart=\"chart_id\",family=\"test_family\",dimension=\"dimension_id\"} 690565856.0000000\n");
1159
-
1160
- buffer_flush(buffer);
1161
-
1162
- expect_function_call(__wrap_now_realtime_sec);
1163
- will_return(__wrap_now_realtime_sec, 2);
1164
-
1165
- expect_function_call(__wrap_exporting_calculate_value_from_stored_data);
1166
- will_return(__wrap_exporting_calculate_value_from_stored_data, pack_storage_number(27, SN_DEFAULT_FLAGS));
1167
-
1168
- rrd_stats_api_v1_charts_allmetrics_prometheus_single_host(
1169
- localhost, NULL, buffer, "test_server", "test_prefix", 0, PROMETHEUS_OUTPUT_NAMES | PROMETHEUS_OUTPUT_TYPES);
1170
-
1171
- assert_string_equal(
1172
- buffer_tostring(buffer),
1173
- "netdata_info{instance=\"test_hostname\",application=\"\",version=\"\",key1=\"value1\",key2=\"value2\"} 1\n"
1174
- "# TYPE test_prefix_test_context gauge\n"
1175
- "test_prefix_test_context{chart=\"chart_name\",family=\"test_family\",dimension=\"dimension_name\"} 690565856.0000000\n");
1176
-
1177
- buffer_flush(buffer);
1178
-
1179
- expect_function_call(__wrap_now_realtime_sec);
1180
- will_return(__wrap_now_realtime_sec, 2);
1181
-
1182
- expect_function_call(__wrap_exporting_calculate_value_from_stored_data);
1183
- will_return(__wrap_exporting_calculate_value_from_stored_data, pack_storage_number(27, SN_DEFAULT_FLAGS));
1184
-
1185
- rrd_stats_api_v1_charts_allmetrics_prometheus_all_hosts(localhost, NULL, buffer, "test_server", "test_prefix", 0, 0);
1186
-
1187
- assert_string_equal(
1188
- buffer_tostring(buffer),
1189
- "netdata_info{instance=\"test_hostname\",application=\"\",version=\"\",key1=\"value1\",key2=\"value2\"} 1\n"
1190
- "test_prefix_test_context{chart=\"chart_id\",family=\"test_family\",dimension=\"dimension_id\",instance=\"test_hostname\"} 690565856.0000000\n");
1191
-
1192
- free(st->context);
1193
- free(st->family);
1194
- free(localhost->hostname);
1195
- buffer_free(buffer);
1196
-}
1197
-
1198
-#if ENABLE_PROMETHEUS_REMOTE_WRITE
1199
-static void test_init_prometheus_remote_write_instance(void **state)
1200
-{
1201
- struct engine *engine = *state;
1202
- struct instance *instance = engine->instance_root;
1203
-
1204
- expect_function_call(__wrap_init_write_request);
1205
- will_return(__wrap_init_write_request, 0xff);
1206
-
1207
- assert_int_equal(init_prometheus_remote_write_instance(instance), 0);
1208
-
1209
- assert_ptr_equal(instance->worker, simple_connector_worker);
1210
- assert_ptr_equal(instance->start_batch_formatting, NULL);
1211
- assert_ptr_equal(instance->start_host_formatting, format_host_prometheus_remote_write);
1212
- assert_ptr_equal(instance->start_chart_formatting, format_chart_prometheus_remote_write);
1213
- assert_ptr_equal(instance->metric_formatting, format_dimension_prometheus_remote_write);
1214
- assert_ptr_equal(instance->end_chart_formatting, NULL);
1215
- assert_ptr_equal(instance->end_host_formatting, NULL);
1216
- assert_ptr_equal(instance->end_batch_formatting, format_batch_prometheus_remote_write);
1217
- assert_ptr_equal(instance->prepare_header, prometheus_remote_write_prepare_header);
1218
- assert_ptr_equal(instance->check_response, process_prometheus_remote_write_response);
1219
-
1220
- assert_ptr_not_equal(instance->buffer, NULL);
1221
- buffer_free(instance->buffer);
1222
-
1223
- struct prometheus_remote_write_specific_data *connector_specific_data =
1224
- (struct prometheus_remote_write_specific_data *)instance->connector_specific_data;
1225
-
1226
- assert_ptr_not_equal(instance->connector_specific_data, NULL);
1227
- assert_ptr_not_equal(connector_specific_data->write_request, NULL);
1228
- freez(instance->connector_specific_data);
1229
-}
1230
-
1231
-static void test_prometheus_remote_write_prepare_header(void **state)
1232
-{
1233
- struct engine *engine = *state;
1234
- struct instance *instance = engine->instance_root;
1235
-
1236
- struct prometheus_remote_write_specific_config *connector_specific_config =
1237
- callocz(1, sizeof(struct prometheus_remote_write_specific_config));
1238
- instance->config.connector_specific_config = connector_specific_config;
1239
- connector_specific_config->remote_write_path = strdupz("/receive");
1240
-
1241
- struct simple_connector_data *simple_connector_data = callocz(1, sizeof(struct simple_connector_data));
1242
- instance->connector_specific_data = simple_connector_data;
1243
- simple_connector_data->last_buffer = callocz(1, sizeof(struct simple_connector_buffer));
1244
- simple_connector_data->last_buffer->header = buffer_create(0, NULL);
1245
- simple_connector_data->last_buffer->buffer = buffer_create(0, NULL);
1246
- strcpy(simple_connector_data->connected_to, "localhost");
1247
-
1248
- buffer_sprintf(simple_connector_data->last_buffer->buffer, "test buffer");
1249
-
1250
- prometheus_remote_write_prepare_header(instance);
1251
-
1252
- assert_string_equal(
1253
- buffer_tostring(simple_connector_data->last_buffer->header),
1254
- "POST /receive HTTP/1.1\r\n"
1255
- "Host: localhost\r\n"
1256
- "Accept: */*\r\n"
1257
- "Content-Encoding: snappy\r\n"
1258
- "Content-Type: application/x-protobuf\r\n"
1259
- "X-Prometheus-Remote-Write-Version: 0.1.0\r\n"
1260
- "Content-Length: 11\r\n"
1261
- "\r\n");
1262
-
1263
- free(connector_specific_config->remote_write_path);
1264
-
1265
- buffer_free(simple_connector_data->last_buffer->header);
1266
- buffer_free(simple_connector_data->last_buffer->buffer);
1267
-}
1268
-
1269
-static void test_process_prometheus_remote_write_response(void **state)
1270
-{
1271
- (void)state;
1272
- BUFFER *buffer = buffer_create(0, NULL);
1273
-
1274
- buffer_sprintf(buffer, "HTTP/1.1 200 OK\r\n");
1275
- assert_int_equal(process_prometheus_remote_write_response(buffer, NULL), 0);
1276
-
1277
- buffer_free(buffer);
1278
-}
1279
-
1280
-static void test_format_host_prometheus_remote_write(void **state)
1281
-{
1282
- struct engine *engine = *state;
1283
- struct instance *instance = engine->instance_root;
1284
-
1285
- instance->config.options |= EXPORTING_OPTION_SEND_CONFIGURED_LABELS;
1286
- instance->config.options |= EXPORTING_OPTION_SEND_AUTOMATIC_LABELS;
1287
-
1288
- struct simple_connector_data *simple_connector_data = mallocz(sizeof(struct simple_connector_data *));
1289
- instance->connector_specific_data = simple_connector_data;
1290
- struct prometheus_remote_write_specific_data *connector_specific_data =
1291
- mallocz(sizeof(struct prometheus_remote_write_specific_data *));
1292
- simple_connector_data->connector_specific_data = (void *)connector_specific_data;
1293
- connector_specific_data->write_request = (void *)0xff;
1294
-
1295
- localhost->program_name = string_strdupz("test_program");
1296
- localhost->program_version = string_strdupz("test_version");
1297
-
1298
- expect_function_call(__wrap_add_host_info);
1299
- expect_value(__wrap_add_host_info, write_request_p, 0xff);
1300
- expect_string(__wrap_add_host_info, name, "netdata_info");
1301
- expect_string(__wrap_add_host_info, instance, "test-host");
1302
- expect_string(__wrap_add_host_info, application, "test_program");
1303
- expect_string(__wrap_add_host_info, version, "test_version");
1304
- expect_in_range(
1305
- __wrap_add_host_info, timestamp, now_realtime_usec() / USEC_PER_MS - 1000, now_realtime_usec() / USEC_PER_MS);
1306
-
1307
- expect_function_call(__wrap_add_label);
1308
- expect_value(__wrap_add_label, write_request_p, 0xff);
1309
- expect_string(__wrap_add_label, key, "key1");
1310
- expect_string(__wrap_add_label, value, "value1");
1311
-
1312
- expect_function_call(__wrap_add_label);
1313
- expect_value(__wrap_add_label, write_request_p, 0xff);
1314
- expect_string(__wrap_add_label, key, "key2");
1315
- expect_string(__wrap_add_label, value, "value2");
1316
-
1317
- assert_int_equal(format_host_prometheus_remote_write(instance, localhost), 0);
1318
-
1319
- freez(connector_specific_data);
1320
- freez(simple_connector_data);
1321
- free(localhost->program_name);
1322
- free(localhost->program_version);
1323
-}
1324
-
1325
-static void test_format_dimension_prometheus_remote_write(void **state)
1326
-{
1327
- struct engine *engine = *state;
1328
- struct instance *instance = engine->instance_root;
1329
-
1330
- struct simple_connector_data *simple_connector_data = mallocz(sizeof(struct simple_connector_data *));
1331
- instance->connector_specific_data = simple_connector_data;
1332
- struct prometheus_remote_write_specific_data *connector_specific_data =
1333
- mallocz(sizeof(struct prometheus_remote_write_specific_data *));
1334
- simple_connector_data->connector_specific_data = (void *)connector_specific_data;
1335
- connector_specific_data->write_request = (void *)0xff;
1336
-
1337
- RRDSET *st;
1338
- rrdset_foreach_read(st, localhost);
1339
- break;
1340
- rrdset_foreach_done(st);
1341
-
1342
- RRDDIM *rd;
1343
- rrddim_foreach_read(rd, st);
1344
- break;
1345
- rrddim_foreach_done(rd);
1346
-
1347
- expect_function_call(__wrap_exporting_calculate_value_from_stored_data);
1348
- will_return(__wrap_exporting_calculate_value_from_stored_data, pack_storage_number(27, SN_DEFAULT_FLAGS));
1349
-
1350
- expect_function_call(__wrap_add_metric);
1351
- expect_value(__wrap_add_metric, write_request_p, 0xff);
1352
- expect_string(__wrap_add_metric, name, "netdata_");
1353
- expect_string(__wrap_add_metric, chart, "");
1354
- expect_string(__wrap_add_metric, family, "");
1355
- expect_string(__wrap_add_metric, dimension, "dimension_name");
1356
- expect_string(__wrap_add_metric, instance, "test-host");
1357
- expect_value(__wrap_add_metric, value, 0x292932e0);
1358
- expect_value(__wrap_add_metric, timestamp, 15052 * MSEC_PER_SEC);
1359
-
1360
- assert_int_equal(format_dimension_prometheus_remote_write(instance, rd), 0);
1361
-}
1362
-
1363
-static void test_format_batch_prometheus_remote_write(void **state)
1364
-{
1365
- struct engine *engine = *state;
1366
- struct instance *instance = engine->instance_root;
1367
-
1368
- struct simple_connector_data *simple_connector_data = mallocz(sizeof(struct simple_connector_data *));
1369
- instance->connector_specific_data = simple_connector_data;
1370
- struct prometheus_remote_write_specific_data *connector_specific_data =
1371
- mallocz(sizeof(struct prometheus_remote_write_specific_data *));
1372
- simple_connector_data->connector_specific_data = (void *)connector_specific_data;
1373
- connector_specific_data->write_request = __real_init_write_request();
1374
-
1375
- expect_function_call(__wrap_simple_connector_end_batch);
1376
- expect_value(__wrap_simple_connector_end_batch, instance, instance);
1377
- will_return(__wrap_simple_connector_end_batch, 0);
1378
- __real_add_host_info(
1379
- connector_specific_data->write_request,
1380
- "test_name", "test_instance", "test_application", "test_version", 15051);
1381
-
1382
- __real_add_label(connector_specific_data->write_request, "test_key", "test_value");
1383
-
1384
- __real_add_metric(
1385
- connector_specific_data->write_request,
1386
- "test_name", "test chart", "test_family", "test_dimension", "test_instance",
1387
- 123000321, 15052);
1388
-
1389
- assert_int_equal(format_batch_prometheus_remote_write(instance), 0);
1390
-
1391
- BUFFER *buffer = instance->buffer;
1392
- char *write_request_string = calloc(1, 1000);
1393
- convert_write_request_to_string(buffer_tostring(buffer), buffer_strlen(buffer), write_request_string, 999);
1394
- assert_int_equal(strlen(write_request_string), 753);
1395
- assert_string_equal(
1396
- write_request_string,
1397
- "timeseries {\n"
1398
- " labels {\n"
1399
- " name: \"__name__\"\n"
1400
- " value: \"test_name\"\n"
1401
- " }\n"
1402
- " labels {\n"
1403
- " name: \"instance\"\n"
1404
- " value: \"test_instance\"\n"
1405
- " }\n"
1406
- " labels {\n"
1407
- " name: \"application\"\n"
1408
- " value: \"test_application\"\n"
1409
- " }\n"
1410
- " labels {\n"
1411
- " name: \"version\"\n"
1412
- " value: \"test_version\"\n"
1413
- " }\n"
1414
- " labels {\n"
1415
- " name: \"test_key\"\n"
1416
- " value: \"test_value\"\n"
1417
- " }\n"
1418
- " samples {\n"
1419
- " value: 1\n"
1420
- " timestamp: 15051\n"
1421
- " }\n"
1422
- "}\n"
1423
- "timeseries {\n"
1424
- " labels {\n"
1425
- " name: \"__name__\"\n"
1426
- " value: \"test_name\"\n"
1427
- " }\n"
1428
- " labels {\n"
1429
- " name: \"chart\"\n"
1430
- " value: \"test chart\"\n"
1431
- " }\n"
1432
- " labels {\n"
1433
- " name: \"family\"\n"
1434
- " value: \"test_family\"\n"
1435
- " }\n"
1436
- " labels {\n"
1437
- " name: \"dimension\"\n"
1438
- " value: \"test_dimension\"\n"
1439
- " }\n"
1440
- " labels {\n"
1441
- " name: \"instance\"\n"
1442
- " value: \"test_instance\"\n"
1443
- " }\n"
1444
- " samples {\n"
1445
- " value: 123000321\n"
1446
- " timestamp: 15052\n"
1447
- " }\n"
1448
- "}\n");
1449
- free(write_request_string);
1450
-
1451
- protocol_buffers_shutdown();
1452
-}
1453
-#endif // ENABLE_PROMETHEUS_REMOTE_WRITE
1454
-
1455
-#if HAVE_KINESIS
1456
-static void test_init_aws_kinesis_instance(void **state)
1457
-{
1458
- struct engine *engine = *state;
1459
- struct instance *instance = engine->instance_root;
1460
-
1461
- instance->config.options = EXPORTING_SOURCE_DATA_AS_COLLECTED | EXPORTING_OPTION_SEND_NAMES;
1462
-
1463
- struct aws_kinesis_specific_config *connector_specific_config =
1464
- callocz(1, sizeof(struct aws_kinesis_specific_config));
1465
- instance->config.connector_specific_config = connector_specific_config;
1466
- connector_specific_config->stream_name = strdupz("test_stream");
1467
- connector_specific_config->auth_key_id = strdupz("test_auth_key_id");
1468
- connector_specific_config->secure_key = strdupz("test_secure_key");
1469
-
1470
- expect_function_call(__wrap_aws_sdk_init);
1471
- expect_function_call(__wrap_kinesis_init);
1472
- expect_not_value(__wrap_kinesis_init, kinesis_specific_data_p, NULL);
1473
- expect_string(__wrap_kinesis_init, region, "localhost");
1474
- expect_string(__wrap_kinesis_init, access_key_id, "test_auth_key_id");
1475
- expect_string(__wrap_kinesis_init, secret_key, "test_secure_key");
1476
- expect_value(__wrap_kinesis_init, timeout, 10000);
1477
-
1478
- assert_int_equal(init_aws_kinesis_instance(instance), 0);
1479
-
1480
- assert_ptr_equal(instance->worker, aws_kinesis_connector_worker);
1481
- assert_ptr_equal(instance->start_batch_formatting, NULL);
1482
- assert_ptr_equal(instance->start_host_formatting, format_host_labels_json_plaintext);
1483
- assert_ptr_equal(instance->start_chart_formatting, NULL);
1484
- assert_ptr_equal(instance->metric_formatting, format_dimension_collected_json_plaintext);
1485
- assert_ptr_equal(instance->end_chart_formatting, NULL);
1486
- assert_ptr_equal(instance->end_host_formatting, flush_host_labels);
1487
- assert_ptr_equal(instance->end_batch_formatting, NULL);
1488
- assert_ptr_not_equal(instance->buffer, NULL);
1489
- buffer_free(instance->buffer);
1490
- assert_ptr_not_equal(instance->connector_specific_data, NULL);
1491
- freez(instance->connector_specific_data);
1492
-
1493
- instance->config.options = EXPORTING_SOURCE_DATA_AVERAGE | EXPORTING_OPTION_SEND_NAMES;
1494
-
1495
- expect_function_call(__wrap_kinesis_init);
1496
- expect_not_value(__wrap_kinesis_init, kinesis_specific_data_p, NULL);
1497
- expect_string(__wrap_kinesis_init, region, "localhost");
1498
- expect_string(__wrap_kinesis_init, access_key_id, "test_auth_key_id");
1499
- expect_string(__wrap_kinesis_init, secret_key, "test_secure_key");
1500
- expect_value(__wrap_kinesis_init, timeout, 10000);
1501
-
1502
- assert_int_equal(init_aws_kinesis_instance(instance), 0);
1503
- assert_ptr_equal(instance->metric_formatting, format_dimension_stored_json_plaintext);
1504
-
1505
- free(connector_specific_config->stream_name);
1506
- free(connector_specific_config->auth_key_id);
1507
- free(connector_specific_config->secure_key);
1508
-}
1509
-
1510
-static void test_aws_kinesis_connector_worker(void **state)
1511
-{
1512
- struct engine *engine = *state;
1513
- struct instance *instance = engine->instance_root;
1514
- struct stats *stats = &instance->stats;
1515
- BUFFER *buffer = instance->buffer;
1516
-
1517
- __real_mark_scheduled_instances(engine);
1518
-
1519
- expect_function_call(__wrap_rrdhost_is_exportable);
1520
- expect_value(__wrap_rrdhost_is_exportable, instance, instance);
1521
- expect_value(__wrap_rrdhost_is_exportable, host, localhost);
1522
- will_return(__wrap_rrdhost_is_exportable, 1);
1523
-
1524
- RRDSET *st;
1525
- rrdset_foreach_read(st, localhost);
1526
- break;
1527
- rrdset_foreach_done(st);
1528
-
1529
- expect_function_call(__wrap_rrdset_is_exportable);
1530
- expect_value(__wrap_rrdset_is_exportable, instance, instance);
1531
- expect_value(__wrap_rrdset_is_exportable, st, st);
1532
- will_return(__wrap_rrdset_is_exportable, 1);
1533
-
1534
- expect_function_call(__wrap_simple_connector_end_batch);
1535
- expect_value(__wrap_simple_connector_end_batch, instance, instance);
1536
- will_return(__wrap_simple_connector_end_batch, 0);
1537
- __real_prepare_buffers(engine);
1538
-
1539
- struct aws_kinesis_specific_config *connector_specific_config =
1540
- callocz(1, sizeof(struct aws_kinesis_specific_config));
1541
- instance->config.connector_specific_config = connector_specific_config;
1542
- connector_specific_config->stream_name = strdupz("test_stream");
1543
- connector_specific_config->auth_key_id = strdupz("test_auth_key_id");
1544
- connector_specific_config->secure_key = strdupz("test_secure_key");
1545
-
1546
- struct aws_kinesis_specific_data *connector_specific_data = callocz(1, sizeof(struct aws_kinesis_specific_data));
1547
- instance->connector_specific_data = (void *)connector_specific_data;
1548
-
1549
- expect_function_call(__wrap_kinesis_put_record);
1550
- expect_not_value(__wrap_kinesis_put_record, kinesis_specific_data_p, NULL);
1551
- expect_string(__wrap_kinesis_put_record, stream_name, "test_stream");
1552
- expect_string(__wrap_kinesis_put_record, partition_key, "netdata_0");
1553
- expect_value(__wrap_kinesis_put_record, data, buffer_tostring(buffer));
1554
- // The buffer is prepared by Graphite exporting connector
1555
- expect_string(
1556
- __wrap_kinesis_put_record, data,
1557
- "netdata.test-host.chart_name.dimension_name;TAG1=VALUE1 TAG2=VALUE2 123000321 15051\n");
1558
- expect_value(__wrap_kinesis_put_record, data_len, 84);
1559
-
1560
- expect_function_call(__wrap_kinesis_get_result);
1561
- expect_value(__wrap_kinesis_get_result, request_outcomes_p, NULL);
1562
- expect_not_value(__wrap_kinesis_get_result, error_message, NULL);
1563
- expect_not_value(__wrap_kinesis_get_result, sent_bytes, NULL);
1564
- expect_not_value(__wrap_kinesis_get_result, lost_bytes, NULL);
1565
- will_return(__wrap_kinesis_get_result, 0);
1566
-
1567
- expect_function_call(__wrap_send_internal_metrics);
1568
- expect_value(__wrap_send_internal_metrics, instance, instance);
1569
- will_return(__wrap_send_internal_metrics, 0);
1570
-
1571
- aws_kinesis_connector_worker(instance);
1572
-
1573
- assert_int_equal(stats->buffered_metrics, 0);
1574
- assert_int_equal(stats->buffered_bytes, 84);
1575
- assert_int_equal(stats->received_bytes, 0);
1576
- assert_int_equal(stats->sent_bytes, 84);
1577
- assert_int_equal(stats->sent_metrics, 1);
1578
- assert_int_equal(stats->lost_metrics, 0);
1579
- assert_int_equal(stats->receptions, 1);
1580
- assert_int_equal(stats->transmission_successes, 1);
1581
- assert_int_equal(stats->transmission_failures, 0);
1582
- assert_int_equal(stats->data_lost_events, 0);
1583
- assert_int_equal(stats->lost_bytes, 0);
1584
- assert_int_equal(stats->reconnects, 0);
1585
-
1586
- free(connector_specific_config->stream_name);
1587
- free(connector_specific_config->auth_key_id);
1588
- free(connector_specific_config->secure_key);
1589
-}
1590
-#endif // HAVE_KINESIS
1591
-
1592
-#if ENABLE_EXPORTING_PUBSUB
1593
-static void test_init_pubsub_instance(void **state)
1594
-{
1595
- struct engine *engine = *state;
1596
- struct instance *instance = engine->instance_root;
1597
-
1598
- instance->config.options = EXPORTING_SOURCE_DATA_AS_COLLECTED | EXPORTING_OPTION_SEND_NAMES;
1599
-
1600
- struct pubsub_specific_config *connector_specific_config =
1601
- callocz(1, sizeof(struct pubsub_specific_config));
1602
- instance->config.connector_specific_config = connector_specific_config;
1603
- connector_specific_config->credentials_file = strdupz("/test/credentials/file");
1604
- connector_specific_config->project_id = strdupz("test_project_id");
1605
- connector_specific_config->topic_id = strdupz("test_topic_id");
1606
-
1607
- expect_function_call(__wrap_pubsub_init);
1608
- expect_not_value(__wrap_pubsub_init, pubsub_specific_data_p, NULL);
1609
- expect_string(__wrap_pubsub_init, destination, "localhost");
1610
- expect_string(__wrap_pubsub_init, error_message, "");
1611
- expect_string(__wrap_pubsub_init, credentials_file, "/test/credentials/file");
1612
- expect_string(__wrap_pubsub_init, project_id, "test_project_id");
1613
- expect_string(__wrap_pubsub_init, topic_id, "test_topic_id");
1614
- will_return(__wrap_pubsub_init, 0);
1615
-
1616
- assert_int_equal(init_pubsub_instance(instance), 0);
1617
-
1618
- assert_ptr_equal(instance->worker, pubsub_connector_worker);
1619
- assert_ptr_equal(instance->start_batch_formatting, NULL);
1620
- assert_ptr_equal(instance->start_host_formatting, format_host_labels_json_plaintext);
1621
- assert_ptr_equal(instance->start_chart_formatting, NULL);
1622
- assert_ptr_equal(instance->metric_formatting, format_dimension_collected_json_plaintext);
1623
- assert_ptr_equal(instance->end_chart_formatting, NULL);
1624
- assert_ptr_equal(instance->end_host_formatting, flush_host_labels);
1625
- assert_ptr_equal(instance->end_batch_formatting, NULL);
1626
- assert_ptr_not_equal(instance->buffer, NULL);
1627
- buffer_free(instance->buffer);
1628
- assert_ptr_not_equal(instance->connector_specific_data, NULL);
1629
- freez(instance->connector_specific_data);
1630
-
1631
- instance->config.options = EXPORTING_SOURCE_DATA_AVERAGE | EXPORTING_OPTION_SEND_NAMES;
1632
-
1633
- expect_function_call(__wrap_pubsub_init);
1634
- expect_not_value(__wrap_pubsub_init, pubsub_specific_data_p, NULL);
1635
- expect_string(__wrap_pubsub_init, destination, "localhost");
1636
- expect_string(__wrap_pubsub_init, error_message, "");
1637
- expect_string(__wrap_pubsub_init, credentials_file, "/test/credentials/file");
1638
- expect_string(__wrap_pubsub_init, project_id, "test_project_id");
1639
- expect_string(__wrap_pubsub_init, topic_id, "test_topic_id");
1640
- will_return(__wrap_pubsub_init, 0);
1641
-
1642
- assert_int_equal(init_pubsub_instance(instance), 0);
1643
- assert_ptr_equal(instance->metric_formatting, format_dimension_stored_json_plaintext);
1644
-
1645
- free(connector_specific_config->credentials_file);
1646
- free(connector_specific_config->project_id);
1647
- free(connector_specific_config->topic_id);
1648
-}
1649
-
1650
-static void test_pubsub_connector_worker(void **state)
1651
-{
1652
- struct engine *engine = *state;
1653
- struct instance *instance = engine->instance_root;
1654
- struct stats *stats = &instance->stats;
1655
-
1656
- __real_mark_scheduled_instances(engine);
1657
-
1658
- expect_function_call(__wrap_rrdhost_is_exportable);
1659
- expect_value(__wrap_rrdhost_is_exportable, instance, instance);
1660
- expect_value(__wrap_rrdhost_is_exportable, host, localhost);
1661
- will_return(__wrap_rrdhost_is_exportable, 1);
1662
-
1663
- RRDSET *st;
1664
- rrdset_foreach_read(st, localhost);
1665
- break;
1666
- rrdset_foreach_done(st);
1667
-
1668
- expect_function_call(__wrap_rrdset_is_exportable);
1669
- expect_value(__wrap_rrdset_is_exportable, instance, instance);
1670
- expect_value(__wrap_rrdset_is_exportable, st, st);
1671
- will_return(__wrap_rrdset_is_exportable, 1);
1672
-
1673
- expect_function_call(__wrap_simple_connector_end_batch);
1674
- expect_value(__wrap_simple_connector_end_batch, instance, instance);
1675
- will_return(__wrap_simple_connector_end_batch, 0);
1676
- __real_prepare_buffers(engine);
1677
-
1678
- struct pubsub_specific_config *connector_specific_config =
1679
- callocz(1, sizeof(struct pubsub_specific_config));
1680
- instance->config.connector_specific_config = connector_specific_config;
1681
- connector_specific_config->credentials_file = strdupz("/test/credentials/file");
1682
- connector_specific_config->project_id = strdupz("test_project_id");
1683
- connector_specific_config->topic_id = strdupz("test_topic_id");
1684
-
1685
- struct pubsub_specific_data *connector_specific_data = callocz(1, sizeof(struct pubsub_specific_data));
1686
- instance->connector_specific_data = (void *)connector_specific_data;
1687
-
1688
- expect_function_call(__wrap_pubsub_add_message);
1689
- expect_not_value(__wrap_pubsub_add_message, pubsub_specific_data_p, NULL);
1690
- // The buffer is prepared by Graphite exporting connector
1691
- expect_string(
1692
- __wrap_pubsub_add_message, data,
1693
- "netdata.test-host.chart_name.dimension_name;TAG1=VALUE1 TAG2=VALUE2 123000321 15051\n");
1694
- will_return(__wrap_pubsub_add_message, 0);
1695
-
1696
- expect_function_call(__wrap_pubsub_publish);
1697
- expect_not_value(__wrap_pubsub_publish, pubsub_specific_data_p, NULL);
1698
- expect_string(__wrap_pubsub_publish, error_message, "");
1699
- expect_value(__wrap_pubsub_publish, buffered_metrics, 1);
1700
- expect_value(__wrap_pubsub_publish, buffered_bytes, 84);
1701
- will_return(__wrap_pubsub_publish, 0);
1702
-
1703
- expect_function_call(__wrap_pubsub_get_result);
1704
- expect_not_value(__wrap_pubsub_get_result, pubsub_specific_data_p, NULL);
1705
- expect_not_value(__wrap_pubsub_get_result, error_message, NULL);
1706
- expect_not_value(__wrap_pubsub_get_result, sent_metrics, NULL);
1707
- expect_not_value(__wrap_pubsub_get_result, sent_bytes, NULL);
1708
- expect_not_value(__wrap_pubsub_get_result, lost_metrics, NULL);
1709
- expect_not_value(__wrap_pubsub_get_result, lost_bytes, NULL);
1710
- will_return(__wrap_pubsub_get_result, 0);
1711
-
1712
- expect_function_call(__wrap_send_internal_metrics);
1713
- expect_value(__wrap_send_internal_metrics, instance, instance);
1714
- will_return(__wrap_send_internal_metrics, 0);
1715
-
1716
- pubsub_connector_worker(instance);
1717
-
1718
- assert_int_equal(stats->buffered_metrics, 0);
1719
- assert_int_equal(stats->buffered_bytes, 84);
1720
- assert_int_equal(stats->received_bytes, 0);
1721
- assert_int_equal(stats->sent_bytes, 84);
1722
- assert_int_equal(stats->sent_metrics, 0);
1723
- assert_int_equal(stats->lost_metrics, 0);
1724
- assert_int_equal(stats->receptions, 1);
1725
- assert_int_equal(stats->transmission_successes, 1);
1726
- assert_int_equal(stats->transmission_failures, 0);
1727
- assert_int_equal(stats->data_lost_events, 0);
1728
- assert_int_equal(stats->lost_bytes, 0);
1729
- assert_int_equal(stats->reconnects, 0);
1730
-
1731
- free(connector_specific_config->credentials_file);
1732
- free(connector_specific_config->project_id);
1733
- free(connector_specific_config->topic_id);
1734
-}
1735
-#endif // ENABLE_EXPORTING_PUBSUB
1736
-
1737
-#if HAVE_MONGOC
1738
-static void test_init_mongodb_instance(void **state)
1739
-{
1740
- struct engine *engine = *state;
1741
- struct instance *instance = engine->instance_root;
1742
-
1743
- instance->config.options = EXPORTING_SOURCE_DATA_AS_COLLECTED | EXPORTING_OPTION_SEND_NAMES;
1744
-
1745
- struct mongodb_specific_config *connector_specific_config = callocz(1, sizeof(struct mongodb_specific_config));
1746
- instance->config.connector_specific_config = connector_specific_config;
1747
- connector_specific_config->database = strdupz("test_database");
1748
- connector_specific_config->collection = strdupz("test_collection");
1749
- instance->config.buffer_on_failures = 10;
1750
-
1751
- expect_function_call(__wrap_mongoc_init);
1752
- expect_function_call(__wrap_mongoc_uri_new_with_error);
1753
- expect_string(__wrap_mongoc_uri_new_with_error, uri_string, "localhost");
1754
- expect_not_value(__wrap_mongoc_uri_new_with_error, error, NULL);
1755
- will_return(__wrap_mongoc_uri_new_with_error, 0xf1);
1756
-
1757
- expect_function_call(__wrap_mongoc_uri_get_option_as_int32);
1758
- expect_value(__wrap_mongoc_uri_get_option_as_int32, uri, 0xf1);
1759
- expect_string(__wrap_mongoc_uri_get_option_as_int32, option, MONGOC_URI_SOCKETTIMEOUTMS);
1760
- expect_value(__wrap_mongoc_uri_get_option_as_int32, fallback, 1000);
1761
- will_return(__wrap_mongoc_uri_get_option_as_int32, 1000);
1762
-
1763
- expect_function_call(__wrap_mongoc_uri_set_option_as_int32);
1764
- expect_value(__wrap_mongoc_uri_set_option_as_int32, uri, 0xf1);
1765
- expect_string(__wrap_mongoc_uri_set_option_as_int32, option, MONGOC_URI_SOCKETTIMEOUTMS);
1766
- expect_value(__wrap_mongoc_uri_set_option_as_int32, value, 1000);
1767
- will_return(__wrap_mongoc_uri_set_option_as_int32, true);
1768
-
1769
- expect_function_call(__wrap_mongoc_client_new_from_uri);
1770
- expect_value(__wrap_mongoc_client_new_from_uri, uri, 0xf1);
1771
- will_return(__wrap_mongoc_client_new_from_uri, 0xf2);
1772
-
1773
- expect_function_call(__wrap_mongoc_client_set_appname);
1774
- expect_value(__wrap_mongoc_client_set_appname, client, 0xf2);
1775
- expect_string(__wrap_mongoc_client_set_appname, appname, "netdata");
1776
- will_return(__wrap_mongoc_client_set_appname, true);
1777
-
1778
- expect_function_call(__wrap_mongoc_client_get_collection);
1779
- expect_value(__wrap_mongoc_client_get_collection, client, 0xf2);
1780
- expect_string(__wrap_mongoc_client_get_collection, db, "test_database");
1781
- expect_string(__wrap_mongoc_client_get_collection, collection, "test_collection");
1782
- will_return(__wrap_mongoc_client_get_collection, 0xf3);
1783
-
1784
- expect_function_call(__wrap_mongoc_uri_destroy);
1785
- expect_value(__wrap_mongoc_uri_destroy, uri, 0xf1);
1786
-
1787
- assert_int_equal(init_mongodb_instance(instance), 0);
1788
-
1789
- assert_ptr_equal(instance->worker, mongodb_connector_worker);
1790
- assert_ptr_equal(instance->start_batch_formatting, NULL);
1791
- assert_ptr_equal(instance->start_host_formatting, format_host_labels_json_plaintext);
1792
- assert_ptr_equal(instance->start_chart_formatting, NULL);
1793
- assert_ptr_equal(instance->metric_formatting, format_dimension_collected_json_plaintext);
1794
- assert_ptr_equal(instance->end_chart_formatting, NULL);
1795
- assert_ptr_equal(instance->end_host_formatting, flush_host_labels);
1796
- assert_ptr_equal(instance->end_batch_formatting, format_batch_mongodb);
1797
- assert_ptr_equal(instance->prepare_header, NULL);
1798
- assert_ptr_equal(instance->check_response, NULL);
1799
-
1800
- assert_ptr_not_equal(instance->buffer, NULL);
1801
- buffer_free(instance->buffer);
1802
-
1803
- assert_ptr_not_equal(instance->connector_specific_data, NULL);
1804
-
1805
- struct mongodb_specific_data *connector_specific_data =
1806
- (struct mongodb_specific_data *)instance->connector_specific_data;
1807
- size_t number_of_buffers = 1;
1808
- struct bson_buffer *current_buffer = connector_specific_data->first_buffer;
1809
- while (current_buffer->next != connector_specific_data->first_buffer) {
1810
- current_buffer = current_buffer->next;
1811
- number_of_buffers++;
1812
- if (number_of_buffers == (size_t)(instance->config.buffer_on_failures + 1)) {
1813
- number_of_buffers = 0;
1814
- break;
1815
- }
1816
- }
1817
- assert_int_equal(number_of_buffers, 9);
1818
-
1819
- free(connector_specific_config->database);
1820
- free(connector_specific_config->collection);
1821
-}
1822
-
1823
-static void test_format_batch_mongodb(void **state)
1824
-{
1825
- struct engine *engine = *state;
1826
- struct instance *instance = engine->instance_root;
1827
- struct stats *stats = &instance->stats;
1828
-
1829
- struct mongodb_specific_data *connector_specific_data = mallocz(sizeof(struct mongodb_specific_data));
1830
- instance->connector_specific_data = (void *)connector_specific_data;
1831
-
1832
- struct bson_buffer *current_buffer = callocz(1, sizeof(struct bson_buffer));
1833
- connector_specific_data->first_buffer = current_buffer;
1834
- connector_specific_data->first_buffer->next = current_buffer;
1835
- connector_specific_data->last_buffer = current_buffer;
1836
-
1837
- BUFFER *buffer = buffer_create(0, NULL);
1838
- buffer_sprintf(buffer, "{ \"metric\": \"test_metric\" }\n");
1839
- instance->buffer = buffer;
1840
- stats->buffered_metrics = 1;
1841
-
1842
- assert_int_equal(format_batch_mongodb(instance), 0);
1843
-
1844
- assert_int_equal(connector_specific_data->last_buffer->documents_inserted, 1);
1845
- assert_int_equal(buffer_strlen(buffer), 0);
1846
-
1847
- size_t len;
1848
- char *str = bson_as_canonical_extended_json(connector_specific_data->last_buffer->insert[0], &len);
1849
- assert_string_equal(str, "{ \"metric\" : \"test_metric\" }");
1850
-
1851
- freez(str);
1852
- buffer_free(buffer);
1853
-}
1854
-
1855
-static void test_mongodb_connector_worker(void **state)
1856
-{
1857
- struct engine *engine = *state;
1858
- struct instance *instance = engine->instance_root;
1859
-
1860
- struct mongodb_specific_config *connector_specific_config = callocz(1, sizeof(struct mongodb_specific_config));
1861
- instance->config.connector_specific_config = connector_specific_config;
1862
- connector_specific_config->database = strdupz("test_database");
1863
-
1864
- struct mongodb_specific_data *connector_specific_data = callocz(1, sizeof(struct mongodb_specific_data));
1865
- instance->connector_specific_data = (void *)connector_specific_data;
1866
- connector_specific_config->collection = strdupz("test_collection");
1867
-
1868
- struct bson_buffer *buffer = callocz(1, sizeof(struct bson_buffer));
1869
- buffer->documents_inserted = 1;
1870
- connector_specific_data->first_buffer = buffer;
1871
- connector_specific_data->first_buffer->next = buffer;
1872
-
1873
- connector_specific_data->first_buffer->insert = callocz(1, sizeof(bson_t *));
1874
- bson_error_t bson_error;
1875
- connector_specific_data->first_buffer->insert[0] =
1876
- bson_new_from_json((const uint8_t *)"{ \"test_key\" : \"test_value\" }", -1, &bson_error);
1877
-
1878
- connector_specific_data->client = mongoc_client_new("mongodb://localhost");
1879
- connector_specific_data->collection =
1880
- __real_mongoc_client_get_collection(connector_specific_data->client, "test_database", "test_collection");
1881
-
1882
- expect_function_call(__wrap_mongoc_collection_insert_many);
1883
- expect_value(__wrap_mongoc_collection_insert_many, collection, connector_specific_data->collection);
1884
- expect_value(__wrap_mongoc_collection_insert_many, documents, connector_specific_data->first_buffer->insert);
1885
- expect_value(__wrap_mongoc_collection_insert_many, n_documents, 1);
1886
- expect_value(__wrap_mongoc_collection_insert_many, opts, NULL);
1887
- expect_value(__wrap_mongoc_collection_insert_many, reply, NULL);
1888
- expect_not_value(__wrap_mongoc_collection_insert_many, error, NULL);
1889
- will_return(__wrap_mongoc_collection_insert_many, true);
1890
-
1891
- expect_function_call(__wrap_send_internal_metrics);
1892
- expect_value(__wrap_send_internal_metrics, instance, instance);
1893
- will_return(__wrap_send_internal_metrics, 0);
1894
-
1895
- mongodb_connector_worker(instance);
1896
-
1897
- assert_ptr_equal(connector_specific_data->first_buffer->insert, NULL);
1898
- assert_int_equal(connector_specific_data->first_buffer->documents_inserted, 0);
1899
- assert_ptr_equal(connector_specific_data->first_buffer, connector_specific_data->first_buffer->next);
1900
-
1901
- struct stats *stats = &instance->stats;
1902
- assert_int_equal(stats->buffered_metrics, 0);
1903
- assert_int_equal(stats->buffered_bytes, 0);
1904
- assert_int_equal(stats->received_bytes, 0);
1905
- assert_int_equal(stats->sent_bytes, 30);
1906
- assert_int_equal(stats->sent_metrics, 1);
1907
- assert_int_equal(stats->lost_metrics, 0);
1908
- assert_int_equal(stats->receptions, 1);
1909
- assert_int_equal(stats->transmission_successes, 1);
1910
- assert_int_equal(stats->transmission_failures, 0);
1911
- assert_int_equal(stats->data_lost_events, 0);
1912
- assert_int_equal(stats->lost_bytes, 0);
1913
- assert_int_equal(stats->reconnects, 0);
1914
-
1915
- free(connector_specific_config->database);
1916
- free(connector_specific_config->collection);
1917
-}
1918
-#endif // HAVE_MONGOC
1919
-
1920
-int main(void)
1921
-{
1922
- const struct CMUnitTest tests[] = {
1923
- cmocka_unit_test_setup_teardown(test_exporting_engine, setup_initialized_engine, teardown_initialized_engine),
1924
- cmocka_unit_test(test_read_exporting_config),
1925
- cmocka_unit_test_setup_teardown(test_init_connectors, setup_configured_engine, teardown_configured_engine),
1926
- cmocka_unit_test_setup_teardown(
1927
- test_init_graphite_instance, setup_configured_engine, teardown_configured_engine),
1928
- cmocka_unit_test_setup_teardown(
1929
- test_init_json_instance, setup_configured_engine, teardown_configured_engine),
1930
- cmocka_unit_test_setup_teardown(
1931
- test_init_opentsdb_telnet_instance, setup_configured_engine, teardown_configured_engine),
1932
- cmocka_unit_test_setup_teardown(
1933
- test_init_opentsdb_http_instance, setup_configured_engine, teardown_configured_engine),
1934
- cmocka_unit_test_setup_teardown(
1935
- test_mark_scheduled_instances, setup_initialized_engine, teardown_initialized_engine),
1936
- cmocka_unit_test_setup_teardown(
1937
- test_rrdhost_is_exportable, setup_initialized_engine, teardown_initialized_engine),
1938
- cmocka_unit_test_setup_teardown(
1939
- test_false_rrdhost_is_exportable, setup_initialized_engine, teardown_initialized_engine),
1940
- cmocka_unit_test_setup_teardown(
1941
- test_rrdset_is_exportable, setup_initialized_engine, teardown_initialized_engine),
1942
- cmocka_unit_test_setup_teardown(
1943
- test_false_rrdset_is_exportable, setup_initialized_engine, teardown_initialized_engine),
1944
- cmocka_unit_test_setup_teardown(
1945
- test_exporting_calculate_value_from_stored_data, setup_initialized_engine, teardown_initialized_engine),
1946
- cmocka_unit_test_setup_teardown(test_prepare_buffers, setup_initialized_engine, teardown_initialized_engine),
1947
- cmocka_unit_test(test_exporting_name_copy),
1948
- cmocka_unit_test_setup_teardown(
1949
- test_format_dimension_collected_graphite_plaintext, setup_initialized_engine, teardown_initialized_engine),
1950
- cmocka_unit_test_setup_teardown(
1951
- test_format_dimension_stored_graphite_plaintext, setup_initialized_engine, teardown_initialized_engine),
1952
- cmocka_unit_test_setup_teardown(
1953
- test_format_dimension_collected_json_plaintext, setup_initialized_engine, teardown_initialized_engine),
1954
- cmocka_unit_test_setup_teardown(
1955
- test_format_dimension_stored_json_plaintext, setup_initialized_engine, teardown_initialized_engine),
1956
- cmocka_unit_test_setup_teardown(
1957
- test_format_dimension_collected_opentsdb_telnet, setup_initialized_engine, teardown_initialized_engine),
1958
- cmocka_unit_test_setup_teardown(
1959
- test_format_dimension_stored_opentsdb_telnet, setup_initialized_engine, teardown_initialized_engine),
1960
- cmocka_unit_test_setup_teardown(
1961
- test_format_dimension_collected_opentsdb_http, setup_initialized_engine, teardown_initialized_engine),
1962
- cmocka_unit_test_setup_teardown(
1963
- test_format_dimension_stored_opentsdb_http, setup_initialized_engine, teardown_initialized_engine),
1964
- cmocka_unit_test_setup_teardown(
1965
- test_exporting_discard_response, setup_initialized_engine, teardown_initialized_engine),
1966
- cmocka_unit_test_setup_teardown(
1967
- test_simple_connector_receive_response, setup_initialized_engine, teardown_initialized_engine),
1968
- cmocka_unit_test_setup_teardown(
1969
- test_simple_connector_send_buffer, setup_initialized_engine, teardown_initialized_engine),
1970
- cmocka_unit_test_setup_teardown(
1971
- test_simple_connector_worker, setup_initialized_engine, teardown_initialized_engine),
1972
- };
1973
-
1974
- const struct CMUnitTest label_tests[] = {
1975
- cmocka_unit_test(test_sanitize_json_string),
1976
- cmocka_unit_test(test_sanitize_graphite_label_value),
1977
- cmocka_unit_test(test_sanitize_opentsdb_label_value),
1978
- cmocka_unit_test_setup_teardown(
1979
- test_format_host_labels_json_plaintext, setup_initialized_engine, teardown_initialized_engine),
1980
- cmocka_unit_test_setup_teardown(
1981
- test_format_host_labels_graphite_plaintext, setup_initialized_engine, teardown_initialized_engine),
1982
- cmocka_unit_test_setup_teardown(
1983
- test_format_host_labels_opentsdb_telnet, setup_initialized_engine, teardown_initialized_engine),
1984
- cmocka_unit_test_setup_teardown(
1985
- test_format_host_labels_opentsdb_http, setup_initialized_engine, teardown_initialized_engine),
1986
- cmocka_unit_test_setup_teardown(test_flush_host_labels, setup_initialized_engine, teardown_initialized_engine),
1987
- };
1988
-
1989
- int test_res = cmocka_run_group_tests_name("exporting_engine", tests, NULL, NULL) +
1990
- cmocka_run_group_tests_name("labels_in_exporting_engine", label_tests, NULL, NULL);
1991
-
1992
- const struct CMUnitTest internal_metrics_tests[] = {
1993
- cmocka_unit_test_setup_teardown(test_create_main_rusage_chart, setup_rrdhost, teardown_rrdhost),
1994
- cmocka_unit_test(test_send_main_rusage),
1995
- cmocka_unit_test(test_send_internal_metrics),
1996
- };
1997
-
1998
- test_res += cmocka_run_group_tests_name("internal_metrics", internal_metrics_tests, NULL, NULL);
1999
-
2000
- const struct CMUnitTest prometheus_web_api_tests[] = {
2001
- cmocka_unit_test_setup_teardown(test_can_send_rrdset, setup_prometheus, teardown_prometheus),
2002
- cmocka_unit_test_setup_teardown(test_prometheus_name_copy, setup_prometheus, teardown_prometheus),
2003
- cmocka_unit_test_setup_teardown(test_prometheus_label_copy, setup_prometheus, teardown_prometheus),
2004
- cmocka_unit_test_setup_teardown(test_prometheus_units_copy, setup_prometheus, teardown_prometheus),
2005
- cmocka_unit_test_setup_teardown(
2006
- test_format_host_labels_prometheus, setup_initialized_engine, teardown_initialized_engine),
2007
- cmocka_unit_test_setup_teardown(
2008
- rrd_stats_api_v1_charts_allmetrics_prometheus, setup_prometheus, teardown_prometheus),
2009
- };
2010
-
2011
- test_res += cmocka_run_group_tests_name("prometheus_web_api", prometheus_web_api_tests, NULL, NULL);
2012
-
2013
-#if ENABLE_PROMETHEUS_REMOTE_WRITE
2014
- const struct CMUnitTest prometheus_remote_write_tests[] = {
2015
- cmocka_unit_test_setup_teardown(
2016
- test_init_prometheus_remote_write_instance, setup_configured_engine, teardown_configured_engine),
2017
- cmocka_unit_test_setup_teardown(
2018
- test_prometheus_remote_write_prepare_header, setup_initialized_engine, teardown_initialized_engine),
2019
- cmocka_unit_test(test_process_prometheus_remote_write_response),
2020
- cmocka_unit_test_setup_teardown(
2021
- test_format_host_prometheus_remote_write, setup_initialized_engine, teardown_initialized_engine),
2022
- cmocka_unit_test_setup_teardown(
2023
- test_format_dimension_prometheus_remote_write, setup_initialized_engine, teardown_initialized_engine),
2024
- cmocka_unit_test_setup_teardown(
2025
- test_format_batch_prometheus_remote_write, setup_initialized_engine, teardown_initialized_engine),
2026
- };
2027
-
2028
- test_res += cmocka_run_group_tests_name(
2029
- "prometheus_remote_write_exporting_connector", prometheus_remote_write_tests, NULL, NULL);
2030
-#endif
2031
-
2032
-#if HAVE_KINESIS
2033
- const struct CMUnitTest kinesis_tests[] = {
2034
- cmocka_unit_test_setup_teardown(
2035
- test_init_aws_kinesis_instance, setup_configured_engine, teardown_configured_engine),
2036
- cmocka_unit_test_setup_teardown(
2037
- test_aws_kinesis_connector_worker, setup_initialized_engine, teardown_initialized_engine),
2038
- };
2039
-
2040
- test_res += cmocka_run_group_tests_name("kinesis_exporting_connector", kinesis_tests, NULL, NULL);
2041
-#endif
2042
-
2043
-#if ENABLE_EXPORTING_PUBSUB
2044
- const struct CMUnitTest pubsub_tests[] = {
2045
- cmocka_unit_test_setup_teardown(
2046
- test_init_pubsub_instance, setup_configured_engine, teardown_configured_engine),
2047
- cmocka_unit_test_setup_teardown(
2048
- test_pubsub_connector_worker, setup_initialized_engine, teardown_initialized_engine),
2049
- };
2050
-
2051
- test_res += cmocka_run_group_tests_name("pubsub_exporting_connector", pubsub_tests, NULL, NULL);
2052
-#endif
2053
-
2054
-#if HAVE_MONGOC
2055
- const struct CMUnitTest mongodb_tests[] = {
2056
- cmocka_unit_test_setup_teardown(
2057
- test_init_mongodb_instance, setup_configured_engine, teardown_configured_engine),
2058
- cmocka_unit_test_setup_teardown(
2059
- test_format_batch_mongodb, setup_configured_engine, teardown_configured_engine),
2060
- cmocka_unit_test_setup_teardown(
2061
- test_mongodb_connector_worker, setup_configured_engine, teardown_configured_engine),
2062
- };
2063
-
2064
- test_res += cmocka_run_group_tests_name("mongodb_exporting_connector", mongodb_tests, NULL, NULL);
2065
-#endif
2066
-
2067
- return test_res;
2068
-}
exporting/tests/test_exporting_engine.h
deleted
-209
@@ -1,209 +0,0 @@
1
-// SPDX-License-Identifier: GPL-3.0-or-later
2
-
3
-#ifndef TEST_EXPORTING_ENGINE_H
4
-#define TEST_EXPORTING_ENGINE_H 1
5
-
6
-#include "libnetdata/libnetdata.h"
7
-#include "database/rrdvar.h"
8
-
9
-#include "exporting/exporting_engine.h"
10
-#include "exporting/graphite/graphite.h"
11
-#include "exporting/json/json.h"
12
-#include "exporting/opentsdb/opentsdb.h"
13
-
14
-#if ENABLE_PROMETHEUS_REMOTE_WRITE
15
-#include "exporting/prometheus/remote_write/remote_write.h"
16
-#endif
17
-
18
-#if HAVE_KINESIS
19
-#include "exporting/aws_kinesis/aws_kinesis.h"
20
-#endif
21
-
22
-#if ENABLE_EXPORTING_PUBSUB
23
-#include "exporting/pubsub/pubsub.h"
24
-#endif
25
-
26
-#if HAVE_MONGOC
27
-#include "exporting/mongodb/mongodb.h"
28
-#endif
29
-
30
-#include <stdarg.h>
31
-#include <stddef.h>
32
-#include <setjmp.h>
33
-#include <stdint.h>
34
-
35
-#ifndef UNIT_TESTING
36
-#include <cmocka.h>
37
-#else
38
-#undef UNIT_TESTING
39
-#include <cmocka.h>
40
-#define UNIT_TESTING
41
-#endif
42
-
43
-#define MAX_LOG_LINE 1024
44
-extern char log_line[];
45
-
46
-// -----------------------------------------------------------------------
47
-// doubles for Netdata functions
48
-
49
-const char *__wrap_strdupz(const char *s);
50
-void __wrap_info_int(const char *file, const char *function, const unsigned long line, const char *fmt, ...);
51
-int __wrap_connect_to_one_of(
52
- const char *destination,
53
- int default_port,
54
- struct timeval *timeout,
55
- size_t *reconnects_counter,
56
- char *connected_to,
57
- size_t connected_to_size);
58
-time_t __mock_rrddim_query_oldest_time(STORAGE_METRIC_HANDLE *smh);
59
-time_t __mock_rrddim_query_latest_time(STORAGE_METRIC_HANDLE *smh);
60
-void __mock_rrddim_query_init(STORAGE_METRIC_HANDLE *smh, struct rrddim_query_handle *handle, time_t start_time, time_t end_time);
61
-int __mock_rrddim_query_is_finished(struct rrddim_query_handle *handle);
62
-STORAGE_POINT __mock_rrddim_query_next_metric(struct rrddim_query_handle *handle);
63
-void __mock_rrddim_query_finalize(struct rrddim_query_handle *handle);
64
-
65
-// -----------------------------------------------------------------------
66
-// wraps for system functions
67
-
68
-void __wrap_uv_thread_create(uv_thread_t thread, void (*worker)(void *arg), void *arg);
69
-void __wrap_uv_mutex_lock(uv_mutex_t *mutex);
70
-void __wrap_uv_mutex_unlock(uv_mutex_t *mutex);
71
-void __wrap_uv_cond_signal(uv_cond_t *cond_var);
72
-void __wrap_uv_cond_wait(uv_cond_t *cond_var, uv_mutex_t *mutex);
73
-ssize_t __wrap_recv(int sockfd, void *buf, size_t len, int flags);
74
-ssize_t __wrap_send(int sockfd, const void *buf, size_t len, int flags);
75
-
76
-// -----------------------------------------------------------------------
77
-// doubles and originals for exporting engine functions
78
-
79
-struct engine *__real_read_exporting_config();
80
-struct engine *__wrap_read_exporting_config();
81
-struct engine *__mock_read_exporting_config();
82
-
83
-int __real_init_connectors(struct engine *engine);
84
-int __wrap_init_connectors(struct engine *engine);
85
-
86
-int __real_mark_scheduled_instances(struct engine *engine);
87
-int __wrap_mark_scheduled_instances(struct engine *engine);
88
-
89
-NETDATA_DOUBLE __real_exporting_calculate_value_from_stored_data(
90
- struct instance *instance,
91
- RRDDIM *rd,
92
- time_t *last_timestamp);
93
-NETDATA_DOUBLE __wrap_exporting_calculate_value_from_stored_data(
94
- struct instance *instance,
95
- RRDDIM *rd,
96
- time_t *last_timestamp);
97
-
98
-int __real_prepare_buffers(struct engine *engine);
99
-int __wrap_prepare_buffers(struct engine *engine);
100
-
101
-void __real_create_main_rusage_chart(RRDSET **st_rusage, RRDDIM **rd_user, RRDDIM **rd_system);
102
-void __wrap_create_main_rusage_chart(RRDSET **st_rusage, RRDDIM **rd_user, RRDDIM **rd_system);
103
-
104
-void __real_send_main_rusage(RRDSET *st_rusage, RRDDIM *rd_user, RRDDIM *rd_system);
105
-void __wrap_send_main_rusage(RRDSET *st_rusage, RRDDIM *rd_user, RRDDIM *rd_system);
106
-
107
-int __real_send_internal_metrics(struct instance *instance);
108
-int __wrap_send_internal_metrics(struct instance *instance);
109
-
110
-int __real_rrdhost_is_exportable(struct instance *instance, RRDHOST *host);
111
-int __wrap_rrdhost_is_exportable(struct instance *instance, RRDHOST *host);
112
-
113
-int __real_rrdset_is_exportable(struct instance *instance, RRDSET *st);
114
-int __wrap_rrdset_is_exportable(struct instance *instance, RRDSET *st);
115
-
116
-int __mock_start_batch_formatting(struct instance *instance);
117
-int __mock_start_host_formatting(struct instance *instance, RRDHOST *host);
118
-int __mock_start_chart_formatting(struct instance *instance, RRDSET *st);
119
-int __mock_metric_formatting(struct instance *instance, RRDDIM *rd);
120
-int __mock_end_chart_formatting(struct instance *instance, RRDSET *st);
121
-int __mock_variables_formatting(struct instance *instance, RRDHOST *host);
122
-int __mock_end_host_formatting(struct instance *instance, RRDHOST *host);
123
-int __mock_end_batch_formatting(struct instance *instance);
124
-
125
-int __wrap_simple_connector_end_batch(struct instance *instance);
126
-
127
-#if ENABLE_PROMETHEUS_REMOTE_WRITE
128
-void *__real_init_write_request();
129
-void *__wrap_init_write_request();
130
-
131
-void __real_add_host_info(
132
- void *write_request_p,
133
- const char *name, const char *instance, const char *application, const char *version, const int64_t timestamp);
134
-void __wrap_add_host_info(
135
- void *write_request_p,
136
- const char *name, const char *instance, const char *application, const char *version, const int64_t timestamp);
137
-
138
-void __real_add_label(void *write_request_p, char *key, char *value);
139
-void __wrap_add_label(void *write_request_p, char *key, char *value);
140
-
141
-void __real_add_metric(
142
- void *write_request_p,
143
- const char *name, const char *chart, const char *family, const char *dimension,
144
- const char *instance, const double value, const int64_t timestamp);
145
-void __wrap_add_metric(
146
- void *write_request_p,
147
- const char *name, const char *chart, const char *family, const char *dimension,
148
- const char *instance, const double value, const int64_t timestamp);
149
-#endif /* ENABLE_PROMETHEUS_REMOTE_WRITE */
150
-
151
-#if HAVE_KINESIS
152
-void __wrap_aws_sdk_init();
153
-void __wrap_kinesis_init(
154
- void *kinesis_specific_data_p, const char *region, const char *access_key_id, const char *secret_key,
155
- const long timeout);
156
-void __wrap_kinesis_put_record(
157
- void *kinesis_specific_data_p, const char *stream_name, const char *partition_key, const char *data,
158
- size_t data_len);
159
-int __wrap_kinesis_get_result(void *request_outcomes_p, char *error_message, size_t *sent_bytes, size_t *lost_bytes);
160
-#endif /* HAVE_KINESIS */
161
-
162
-#if ENABLE_EXPORTING_PUBSUB
163
-int __wrap_pubsub_init(
164
- void *pubsub_specific_data_p, char *error_message, const char *destination, const char *credentials_file,
165
- const char *project_id, const char *topic_id);
166
-int __wrap_pubsub_add_message(void *pubsub_specific_data_p, char *data);
167
-int __wrap_pubsub_publish(
168
- void *pubsub_specific_data_p, char *error_message, size_t buffered_metrics, size_t buffered_bytes);
169
-int __wrap_pubsub_get_result(
170
- void *pubsub_specific_data_p, char *error_message,
171
- size_t *sent_metrics, size_t *sent_bytes, size_t *lost_metrics, size_t *lost_bytes);
172
-#endif /* ENABLE_EXPORTING_PUBSUB */
173
-
174
-#if HAVE_MONGOC
175
-void __wrap_mongoc_init();
176
-mongoc_uri_t *__wrap_mongoc_uri_new_with_error(const char *uri_string, bson_error_t *error);
177
-int32_t __wrap_mongoc_uri_get_option_as_int32(const mongoc_uri_t *uri, const char *option, int32_t fallback);
178
-bool __wrap_mongoc_uri_set_option_as_int32(const mongoc_uri_t *uri, const char *option, int32_t value);
179
-mongoc_client_t *__wrap_mongoc_client_new_from_uri(const mongoc_uri_t *uri);
180
-bool __wrap_mongoc_client_set_appname(mongoc_client_t *client, const char *appname);
181
-mongoc_collection_t *
182
-__wrap_mongoc_client_get_collection(mongoc_client_t *client, const char *db, const char *collection);
183
-mongoc_collection_t *
184
-__real_mongoc_client_get_collection(mongoc_client_t *client, const char *db, const char *collection);
185
-void __wrap_mongoc_uri_destroy(mongoc_uri_t *uri);
186
-bool __wrap_mongoc_collection_insert_many(
187
- mongoc_collection_t *collection,
188
- const bson_t **documents,
189
- size_t n_documents,
190
- const bson_t *opts,
191
- bson_t *reply,
192
- bson_error_t *error);
193
-#endif /* HAVE_MONGOC */
194
-
195
-// -----------------------------------------------------------------------
196
-// fixtures
197
-
198
-int setup_configured_engine(void **state);
199
-int teardown_configured_engine(void **state);
200
-int setup_rrdhost();
201
-int teardown_rrdhost();
202
-int setup_initialized_engine(void **state);
203
-int teardown_initialized_engine(void **state);
204
-int setup_prometheus(void **state);
205
-int teardown_prometheus(void **state);
206
-
207
-void init_connectors_in_tests(struct engine *engine);
208
-
209
-#endif /* TEST_EXPORTING_ENGINE_H */
libnetdata/libnetdata.c
+5
-90
@@ -1260,37 +1260,6 @@ int netdata_munmap(void *ptr, size_t size) {
1260
return munmap(ptr, size);
1261
}
1262
1263
-int memory_file_save(const char *filename, void *mem, size_t size) {
1264
- char tmpfilename[FILENAME_MAX + 1];
1265
-
1266
- snprintfz(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long) getpid());
1267
-
1268
- int fd = open(tmpfilename, O_RDWR | O_CREAT | O_NOATIME, 0664);
1269
- if (fd < 0) {
1270
- netdata_log_error("Cannot create/open file '%s'.", filename);
1271
- return -1;
1272
- }
1273
-
1274
- if (write(fd, mem, size) != (ssize_t) size) {
1275
- netdata_log_error("Cannot write to file '%s' %ld bytes.", filename, (long) size);
1276
- close(fd);
1277
- return -1;
1278
- }
1279
-
1280
- close(fd);
1281
-
1282
- if (rename(tmpfilename, filename)) {
1283
- netdata_log_error("Cannot rename '%s' to '%s'", tmpfilename, filename);
1284
- return -1;
1285
- }
1286
-
1287
- return 0;
1288
-}
1289
-
1290
-int fd_is_valid(int fd) {
1291
- return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
1292
-}
1293
-
1263
char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len) {
1264
char *s = fgets(buf, (int)buf_size, fp);
1265
if (!s) return NULL;
@@ -1334,65 +1303,6 @@ int snprintfz(char *dst, size_t n, const char *fmt, ...) {
1303
return ret;
1304
}
1305
1337
-/*
1338
-// poor man cycle counting
1339
-static unsigned long tsc;
1340
-void begin_tsc(void) {
1341
- unsigned long a, d;
1342
- asm volatile ("cpuid\nrdtsc" : "=a" (a), "=d" (d) : "0" (0) : "ebx", "ecx");
1343
- tsc = ((unsigned long)d << 32) | (unsigned long)a;
1344
-}
1345
-unsigned long end_tsc(void) {
1346
- unsigned long a, d;
1347
- asm volatile ("rdtscp" : "=a" (a), "=d" (d) : : "ecx");
1348
- return (((unsigned long)d << 32) | (unsigned long)a) - tsc;
1349
-}
1350
-*/
1351
-
1352
-int recursively_delete_dir(const char *path, const char *reason) {
1353
- DIR *dir = opendir(path);
1354
- if(!dir) {
1355
- netdata_log_error("Cannot read %s directory to be deleted '%s'", reason?reason:"", path);
1356
- return -1;
1357
- }
1358
-
1359
- int ret = 0;
1360
- struct dirent *de = NULL;
1361
- while((de = readdir(dir))) {
1362
- if(de->d_type == DT_DIR
1363
- && (
1364
- (de->d_name[0] == '.' && de->d_name[1] == '\0')
1365
- || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
1366
- ))
1367
- continue;
1368
-
1369
- char fullpath[FILENAME_MAX + 1];
1370
- snprintfz(fullpath, FILENAME_MAX, "%s/%s", path, de->d_name);
1371
-
1372
- if(de->d_type == DT_DIR) {
1373
- int r = recursively_delete_dir(fullpath, reason);
1374
- if(r > 0) ret += r;
1375
- continue;
1376
- }
1377
-
1378
- netdata_log_info("Deleting %s file '%s'", reason?reason:"", fullpath);
1379
- if(unlikely(unlink(fullpath) == -1))
1380
- netdata_log_error("Cannot delete %s file '%s'", reason?reason:"", fullpath);
1381
- else
1382
- ret++;
1383
- }
1384
-
1385
- netdata_log_info("Deleting empty directory '%s'", path);
1386
- if(unlikely(rmdir(path) == -1))
1387
- netdata_log_error("Cannot delete empty directory '%s'", path);
1388
- else
1389
- ret++;
1390
-
1391
- closedir(dir);
1392
-
1393
- return ret;
1394
-}
1395
-
1306
static int is_virtual_filesystem(const char *path, char **reason) {
1307
1308
#if defined(__APPLE__) || defined(__FreeBSD__)
@@ -1780,6 +1690,11 @@ bool run_command_and_copy_output_to_stdout(const char *command, int max_line_len
1690
return true;
1691
}
1692
1693
+
1694
+static int fd_is_valid(int fd) {
1695
+ return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
1696
+}
1697
+
1698
void for_each_open_fd(OPEN_FD_ACTION action, OPEN_FD_EXCLUDE excluded_fds){
1699
int fd;
1700
libnetdata/libnetdata.h
-4
@@ -553,8 +553,6 @@ void *netdata_mmap(const char *filename, size_t size, int flags, int ksm, bool r
553
int netdata_munmap(void *ptr, size_t size);
554
int memory_file_save(const char *filename, void *mem, size_t size);
555
556
-int fd_is_valid(int fd);
557
-
556
extern struct rlimit rlimit_nofile;
557
558
extern int enable_ksm;
@@ -563,8 +561,6 @@ char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len);
561
562
int verify_netdata_host_prefix();
563
566
-int recursively_delete_dir(const char *path, const char *reason);
567
-
564
extern volatile sig_atomic_t netdata_exit;
565
566
extern const char *program_version;
streaming/README.md
+6
-9
@@ -52,8 +52,8 @@ node**. This file is automatically generated by Netdata the first time it is sta
52
|-----------------------------------------------|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
53
| `enabled` | `no` | Whether this API KEY enabled or disabled. |
54
| [`allow from`](#allow-from) | `*` | A space-separated list of [Netdata simple patterns](https://github.com/netdata/netdata/blob/master/libnetdata/simple_pattern/README.md) matching the IPs of nodes that will stream metrics using this API key. [Read more →](#allow-from) |
55
-| `default history` | `3600` | The default amount of child metrics history to retain when using the `save`, `map`, or `ram` memory modes. |
56
-| [`default memory mode`](#default-memory-mode) | `ram` | The [database](https://github.com/netdata/netdata/blob/master/database/README.md) to use for all nodes using this `API_KEY`. Valid settings are `dbengine`, `map`, `save`, `ram`, or `none`. [Read more →](#default-memory-mode) |
55
+| `default history` | `3600` | The default amount of child metrics history to retain when using the `ram` memory mode. |
56
+| [`default memory mode`](#default-memory-mode) | `ram` | The [database](https://github.com/netdata/netdata/blob/master/database/README.md) to use for all nodes using this `API_KEY`. Valid settings are `dbengine`, `ram`, or `none`. [Read more →](#default-memory-mode) |
57
| `health enabled by default` | `auto` | Whether alerts and notifications should be enabled for nodes using this `API_KEY`. `auto` enables alerts when the child is connected. `yes` enables alerts always, and `no` disables alerts. |
58
| `default postpone alarms on connect seconds` | `60` | Postpone alerts and notifications for a period of time after the child connects. |
59
| `default health log history` | `432000` | History of health log events (in seconds) kept in the database. |
@@ -128,16 +128,13 @@ To allow all IPs starting with `10.*`, except `10.1.2.3`:
128
129
#### `default memory mode`
130
131
-The [database](https://github.com/netdata/netdata/blob/master/database/README.md) to use for all nodes using this `API_KEY`. Valid settings are `dbengine`, `ram`,
132
-`save`, `map`, or `none`.
131
+The [database](https://github.com/netdata/netdata/blob/master/database/README.md) to use for all nodes using this `API_KEY`.
132
+Valid settings are `dbengine`, `ram`, , or `none`.
133
134
- `dbengine`: The default, recommended time-series database (TSDB) for Netdata. Stores recent metrics in memory, then
135
efficiently spills them to disk for long-term storage.
136
- `ram`: Stores metrics _only_ in memory, which means metrics are lost when Netdata stops or restarts. Ideal for
137
streaming configurations that use ephemeral nodes.
138
-- `save`: Stores metrics in memory, but saves metrics to disk when Netdata stops or restarts, and loads historical
139
- metrics on start.
140
-- `map`: Stores metrics in memory-mapped files, like swap, with constant disk write.
138
- `none`: No database.
139
140
When using `default memory mode = dbengine`, the parent node creates a separate instance of the TSDB to store metrics
@@ -149,7 +146,7 @@ cache size` and `dbengine multihost disk space` settings in the `[global]` secti
146
| Setting | Default | Description |
147
|--------------------------------------------|-------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
148
| `[global]` section | | |
152
-| `memory mode` | `dbengine` | Determines the [database type](https://github.com/netdata/netdata/blob/master/database/README.md) to be used on that node. Other options settings include `none`, `ram`, `save`, and `map`. `none` disables the database at this host. This also disables alerts and notifications, as those can't run without a database. |
149
+| `memory mode` | `dbengine` | Determines the [database type](https://github.com/netdata/netdata/blob/master/database/README.md) to be used on that node. Other options settings include `none`, and `ram`. `none` disables the database at this host. This also disables alerts and notifications, as those can't run without a database. |
150
| `[web]` section | | |
151
| `mode` | `static-threaded` | Determines the [web server](https://github.com/netdata/netdata/blob/master/web/server/README.md) type. The other option is `none`, which disables the dashboard, API, and registry. |
152
| `accept a streaming request every seconds` | `0` | Set a limit on how often a parent node accepts streaming requests from child nodes. `0` equals no limit. If this is set, you may see `... too busy to accept new streaming request. Will be allowed in X secs` in Netdata's `error.log`. |
@@ -182,7 +179,7 @@ default `dbengine` as specified by the `API_KEY`, and alerts are disabled.
179
180
[MACHINE_GUID]
181
enabled = yes
185
- memory mode = save
182
+ memory mode = ram
183
health enabled = no
184
```
185