// SPDX-License-Identifier: GPL-3.0-or-later
#define BACKENDS_INTERNALS
#include "backends.h"
// ----------------------------------------------------------------------------
// How backends work in netdata:
//
// 1. There is an independent thread that runs at the required interval
// (for example, once every 10 seconds)
//
// 2. Every time it wakes, it calls the backend formatting functions to build
// a buffer of data. This is a very fast, memory only operation.
//
// 3. If the buffer already includes data, the new data are appended.
// If the buffer becomes too big, because the data cannot be sent, a
// log is written and the buffer is discarded.
//
// 4. Then it tries to send all the data. It blocks until all the data are sent
// or the socket returns an error.
// If the time required for this is above the interval, it starts skipping
// intervals, but the calculated values include the entire database, without
// gaps (it remembers the timestamps and continues from where it stopped).
//
// 5. repeats the above forever.
//
const char *global_backend_prefix = "netdata";
int global_backend_update_every = 10;
BACKEND_OPTIONS global_backend_options = BACKEND_SOURCE_DATA_AVERAGE | BACKEND_OPTION_SEND_NAMES;
const char *global_backend_source = NULL;
// ----------------------------------------------------------------------------
// helper functions for backends
size_t backend_name_copy(char *d, const char *s, size_t usable) {
size_t n;
for(n = 0; *s && n < usable ; d++, s++, n++) {
char c = *s;
if(c != '.' && !isalnum(c)) *d = '_';
else *d = c;
}
*d = '\0';
return n;
}
// calculate the SUM or AVERAGE of a dimension, for any timeframe
// may return NAN if the database does not have any value in the give timeframe
calculated_number backend_calculate_value_from_stored_data(
RRDSET *st // the chart
, RRDDIM *rd // the dimension
, time_t after // the start timestamp
, time_t before // the end timestamp
, BACKEND_OPTIONS backend_options // BACKEND_SOURCE_* bitmap
, time_t *first_timestamp // the first point of the database used in this response
, time_t *last_timestamp // the timestamp that should be reported to backend
) {
RRDHOST *host = st->rrdhost;
(void)host;
// find the edges of the rrd database for this chart
time_t first_t = rd->state->query_ops.oldest_time(rd);
time_t last_t = rd->state->query_ops.latest_time(rd);
time_t update_every = st->update_every;
struct rrddim_query_handle handle;
storage_number n;
// step back a little, to make sure we have complete data collection
// for all metrics
after -= updat