rrdcontext.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_RRDCONTEXT_H
  3. #define NETDATA_RRDCONTEXT_H 1
  4. // ----------------------------------------------------------------------------
  5. // RRDMETRIC
  6. typedef struct rrdmetric_acquired RRDMETRIC_ACQUIRED;
  7. // ----------------------------------------------------------------------------
  8. // RRDINSTANCE
  9. typedef struct rrdinstance_acquired RRDINSTANCE_ACQUIRED;
  10. // ----------------------------------------------------------------------------
  11. // RRDCONTEXT
  12. typedef struct rrdcontexts_dictionary RRDCONTEXTS;
  13. typedef struct rrdcontext_acquired RRDCONTEXT_ACQUIRED;
  14. // ----------------------------------------------------------------------------
  15. #include "rrd.h"
  16. const char *rrdmetric_acquired_id(RRDMETRIC_ACQUIRED *rma);
  17. const char *rrdmetric_acquired_name(RRDMETRIC_ACQUIRED *rma);
  18. NETDATA_DOUBLE rrdmetric_acquired_last_stored_value(RRDMETRIC_ACQUIRED *rma);
  19. const char *rrdinstance_acquired_id(RRDINSTANCE_ACQUIRED *ria);
  20. const char *rrdinstance_acquired_name(RRDINSTANCE_ACQUIRED *ria);
  21. DICTIONARY *rrdinstance_acquired_labels(RRDINSTANCE_ACQUIRED *ria);
  22. DICTIONARY *rrdinstance_acquired_functions(RRDINSTANCE_ACQUIRED *ria);
  23. // ----------------------------------------------------------------------------
  24. // public API for rrdhost
  25. void rrdhost_load_rrdcontext_data(RRDHOST *host);
  26. void rrdhost_create_rrdcontexts(RRDHOST *host);
  27. void rrdhost_destroy_rrdcontexts(RRDHOST *host);
  28. void rrdcontext_host_child_connected(RRDHOST *host);
  29. void rrdcontext_host_child_disconnected(RRDHOST *host);
  30. int rrdcontext_foreach_instance_with_rrdset_in_context(RRDHOST *host, const char *context, int (*callback)(RRDSET *st, void *data), void *data);
  31. typedef enum {
  32. RRDCONTEXT_OPTION_NONE = 0,
  33. RRDCONTEXT_OPTION_SHOW_METRICS = (1 << 0),
  34. RRDCONTEXT_OPTION_SHOW_INSTANCES = (1 << 1),
  35. RRDCONTEXT_OPTION_SHOW_LABELS = (1 << 2),
  36. RRDCONTEXT_OPTION_SHOW_QUEUED = (1 << 3),
  37. RRDCONTEXT_OPTION_SHOW_FLAGS = (1 << 4),
  38. RRDCONTEXT_OPTION_SHOW_DELETED = (1 << 5),
  39. RRDCONTEXT_OPTION_DEEPSCAN = (1 << 6),
  40. RRDCONTEXT_OPTION_SHOW_UUIDS = (1 << 7),
  41. RRDCONTEXT_OPTION_SHOW_HIDDEN = (1 << 8),
  42. RRDCONTEXT_OPTION_SKIP_ID = (1 << 31), // internal use
  43. } RRDCONTEXT_TO_JSON_OPTIONS;
  44. #define RRDCONTEXT_OPTIONS_ALL (RRDCONTEXT_OPTION_SHOW_METRICS|RRDCONTEXT_OPTION_SHOW_INSTANCES|RRDCONTEXT_OPTION_SHOW_LABELS|RRDCONTEXT_OPTION_SHOW_QUEUED|RRDCONTEXT_OPTION_SHOW_FLAGS|RRDCONTEXT_OPTION_SHOW_DELETED|RRDCONTEXT_OPTION_SHOW_UUIDS|RRDCONTEXT_OPTION_SHOW_HIDDEN)
  45. int rrdcontext_to_json(RRDHOST *host, BUFFER *wb, time_t after, time_t before, RRDCONTEXT_TO_JSON_OPTIONS options, const char *context, SIMPLE_PATTERN *chart_label_key, SIMPLE_PATTERN *chart_labels_filter, SIMPLE_PATTERN *chart_dimensions);
  46. int rrdcontexts_to_json(RRDHOST *host, BUFFER *wb, time_t after, time_t before, RRDCONTEXT_TO_JSON_OPTIONS options, SIMPLE_PATTERN *chart_label_key, SIMPLE_PATTERN *chart_labels_filter, SIMPLE_PATTERN *chart_dimensions);
  47. // ----------------------------------------------------------------------------
  48. // public API for rrdcontexts
  49. const char *rrdcontext_acquired_id(RRDCONTEXT_ACQUIRED *rca);
  50. // ----------------------------------------------------------------------------
  51. // public API for rrddims
  52. void rrdcontext_updated_rrddim(RRDDIM *rd);
  53. void rrdcontext_removed_rrddim(RRDDIM *rd);
  54. void rrdcontext_updated_rrddim_algorithm(RRDDIM *rd);
  55. void rrdcontext_updated_rrddim_multiplier(RRDDIM *rd);
  56. void rrdcontext_updated_rrddim_divisor(RRDDIM *rd);
  57. void rrdcontext_updated_rrddim_flags(RRDDIM *rd);
  58. void rrdcontext_collected_rrddim(RRDDIM *rd);
  59. int rrdcontext_find_dimension_uuid(RRDSET *st, const char *id, uuid_t *store_uuid);
  60. // ----------------------------------------------------------------------------
  61. // public API for rrdsets
  62. void rrdcontext_updated_rrdset(RRDSET *st);
  63. void rrdcontext_removed_rrdset(RRDSET *st);
  64. void rrdcontext_updated_rrdset_name(RRDSET *st);
  65. void rrdcontext_updated_rrdset_flags(RRDSET *st);
  66. void rrdcontext_updated_retention_rrdset(RRDSET *st);
  67. void rrdcontext_collected_rrdset(RRDSET *st);
  68. int rrdcontext_find_chart_uuid(RRDSET *st, uuid_t *store_uuid);
  69. // ----------------------------------------------------------------------------
  70. // public API for ACLK
  71. void rrdcontext_hub_checkpoint_command(void *cmd);
  72. void rrdcontext_hub_stop_streaming_command(void *cmd);
  73. // ----------------------------------------------------------------------------
  74. // public API for threads
  75. void rrdcontext_db_rotation(void);
  76. void *rrdcontext_main(void *);
  77. // ----------------------------------------------------------------------------
  78. // public API for weights
  79. struct metric_entry {
  80. RRDCONTEXT_ACQUIRED *rca;
  81. RRDINSTANCE_ACQUIRED *ria;
  82. RRDMETRIC_ACQUIRED *rma;
  83. };
  84. DICTIONARY *rrdcontext_all_metrics_to_dict(RRDHOST *host, SIMPLE_PATTERN *contexts);
  85. // ----------------------------------------------------------------------------
  86. // public API for queries
  87. typedef struct query_plan_entry {
  88. size_t tier;
  89. time_t after;
  90. time_t before;
  91. time_t expanded_after;
  92. time_t expanded_before;
  93. struct storage_engine_query_handle handle;
  94. STORAGE_POINT (*next_metric)(struct storage_engine_query_handle *handle);
  95. int (*is_finished)(struct storage_engine_query_handle *handle);
  96. void (*finalize)(struct storage_engine_query_handle *handle);
  97. bool initialized;
  98. bool finalized;
  99. } QUERY_PLAN_ENTRY;
  100. #define QUERY_PLANS_MAX (RRD_STORAGE_TIERS * 2)
  101. typedef struct query_metric {
  102. struct query_metric_tier {
  103. struct storage_engine *eng;
  104. STORAGE_METRIC_HANDLE *db_metric_handle;
  105. time_t db_first_time_s; // the oldest timestamp available for this tier
  106. time_t db_last_time_s; // the latest timestamp available for this tier
  107. time_t db_update_every_s; // latest update every for this tier
  108. long weight;
  109. } tiers[RRD_STORAGE_TIERS];
  110. struct {
  111. size_t used;
  112. QUERY_PLAN_ENTRY array[QUERY_PLANS_MAX];
  113. } plan;
  114. struct {
  115. RRDHOST *host;
  116. RRDCONTEXT_ACQUIRED *rca;
  117. RRDINSTANCE_ACQUIRED *ria;
  118. RRDMETRIC_ACQUIRED *rma;
  119. } link;
  120. struct {
  121. STRING *id;
  122. STRING *name;
  123. RRDR_DIMENSION_FLAGS options;
  124. } dimension;
  125. struct {
  126. STRING *id;
  127. STRING *name;
  128. } chart;
  129. } QUERY_METRIC;
  130. #define MAX_QUERY_TARGET_ID_LENGTH 255
  131. typedef struct query_target_request {
  132. RRDHOST *host; // the host to be queried (can be NULL, hosts will be used)
  133. RRDCONTEXT_ACQUIRED *rca; // the context to be queried (can be NULL)
  134. RRDINSTANCE_ACQUIRED *ria; // the instance to be queried (can be NULL)
  135. RRDMETRIC_ACQUIRED *rma; // the metric to be queried (can be NULL)
  136. RRDSET *st; // the chart to be queried (NULL, for context queries)
  137. const char *hosts; // hosts simple pattern
  138. const char *contexts; // contexts simple pattern (context queries)
  139. const char *charts; // charts simple pattern (for context queries)
  140. const char *dimensions; // dimensions simple pattern
  141. const char *chart_label_key; // select only the chart having this label key
  142. const char *charts_labels_filter; // select only the charts having this combo of label key:value
  143. time_t after; // the requested timeframe
  144. time_t before; // the requested timeframe
  145. size_t points; // the requested number of points
  146. time_t timeout; // the timeout of the query in seconds
  147. uint32_t format; // DATASOURCE_FORMAT
  148. RRDR_OPTIONS options;
  149. RRDR_GROUPING group_method;
  150. const char *group_options;
  151. time_t resampling_time;
  152. size_t tier;
  153. QUERY_SOURCE query_source;
  154. STORAGE_PRIORITY priority;
  155. } QUERY_TARGET_REQUEST;
  156. typedef struct query_target {
  157. char id[MAX_QUERY_TARGET_ID_LENGTH + 1]; // query identifier (for logging)
  158. QUERY_TARGET_REQUEST request;
  159. bool used; // when true, this query is currently being used
  160. size_t queries; // how many query we have done so far
  161. struct {
  162. bool relative; // true when the request made with relative timestamps, true if it was absolute
  163. bool aligned;
  164. time_t after; // the absolute timestamp this query is about
  165. time_t before; // the absolute timestamp this query is about
  166. time_t query_granularity;
  167. size_t points; // the number of points the query will return (maybe different from the request)
  168. size_t group;
  169. RRDR_GROUPING group_method;
  170. const char *group_options;
  171. size_t resampling_group;
  172. NETDATA_DOUBLE resampling_divisor;
  173. RRDR_OPTIONS options;
  174. size_t tier;
  175. } window;
  176. struct {
  177. time_t first_time_s; // the combined first_time_t of all metrics in the query, across all tiers
  178. time_t last_time_s; // the combined last_time_T of all metrics in the query, across all tiers
  179. time_t minimum_latest_update_every_s; // the min update every of the metrics in the query
  180. } db;
  181. struct {
  182. QUERY_METRIC *array; // the metrics to be queried (all of them should be queried, no exceptions)
  183. uint32_t used; // how many items of the array are used
  184. uint32_t size; // the size of the array
  185. SIMPLE_PATTERN *pattern;
  186. } query;
  187. struct {
  188. RRDMETRIC_ACQUIRED **array;
  189. uint32_t used; // how many items of the array are used
  190. uint32_t size; // the size of the array
  191. } metrics;
  192. struct {
  193. RRDINSTANCE_ACQUIRED **array;
  194. uint32_t used; // how many items of the array are used
  195. uint32_t size; // the size of the array
  196. SIMPLE_PATTERN *pattern;
  197. SIMPLE_PATTERN *chart_label_key_pattern;
  198. SIMPLE_PATTERN *charts_labels_filter_pattern;
  199. } instances;
  200. struct {
  201. RRDCONTEXT_ACQUIRED **array;
  202. uint32_t used; // how many items of the array are used
  203. uint32_t size; // the size of the array
  204. SIMPLE_PATTERN *pattern;
  205. } contexts;
  206. struct {
  207. RRDHOST **array;
  208. uint32_t used; // how many items of the array are used
  209. uint32_t size; // the size of the array
  210. SIMPLE_PATTERN *pattern;
  211. } hosts;
  212. } QUERY_TARGET;
  213. void query_target_free(void);
  214. void query_target_release(QUERY_TARGET *qt);
  215. QUERY_TARGET *query_target_create(QUERY_TARGET_REQUEST *qtr);
  216. #endif // NETDATA_RRDCONTEXT_H