@cryptotaxi247 / netdata-1 / commits / 7e9a2cbb0

Stream chart labels (#11675)

* stream chart labels * update stream protocol to 4 * only send CLABEL_COMMIT when there are labels * mark host as UNUSED * log error for stray CLABEL_COMMIT * remove commented define

Emmanuel Vasilakis committed Oct 25, 2021 at 11:32 UTC 7e9a2cbb0ba29a8144d30d20be43178faa0a7668
7 files changed +86 -3
collectors/plugins.d/pluginsd_parser.c
+50
@@ -152,6 +152,24 @@ PARSER_RC pluginsd_label_action(void *user, char *key, char *value, LABEL_SOURCE
152 return PARSER_RC_OK;
153 }
154
155 +PARSER_RC pluginsd_clabel_action(void *user, char *key, char *value, LABEL_SOURCE source)
156 +{
157 + ((PARSER_USER_OBJECT *) user)->chart_labels = add_label_to_list(((PARSER_USER_OBJECT *) user)->chart_labels, key, value, source);
158 +
159 + return PARSER_RC_OK;
160 +}
161 +
162 +PARSER_RC pluginsd_clabel_commit_action(void *user, RRDHOST *host, struct label *new_labels)
163 +{
164 + RRDSET *st = ((PARSER_USER_OBJECT *)user)->st;
165 + if (unlikely(!st)) {
166 + error("requested CLABEL_COMMIT on host '%s', without a BEGIN, ignoring it.", host->hostname);
167 + return PARSER_RC_OK;
168 + }
169 +
170 + rrdset_update_labels(st, new_labels);
171 + return PARSER_RC_OK;
172 +}
173
174 PARSER_RC pluginsd_overwrite_action(void *user, RRDHOST *host, struct label *new_labels)
175 {
@@ -560,6 +578,38 @@ PARSER_RC pluginsd_label(char **words, void *user, PLUGINSD_ACTION *plugins_act
578 return PARSER_RC_OK;
579 }
580
581 +PARSER_RC pluginsd_clabel(char **words, void *user, PLUGINSD_ACTION *plugins_action)
582 +{
583 + if (!words[1] || !words[2] || !words[3]) {
584 + error("Ignoring malformed or empty CHART LABEL command.");
585 + return PARSER_RC_OK;
586 + }
587 +
588 + if (plugins_action->clabel_action) {
589 + PARSER_RC rc = plugins_action->clabel_action(user, words[1], words[2], strtol(words[3], NULL, 10));
590 + return rc;
591 + }
592 +
593 + return PARSER_RC_OK;
594 +}
595 +
596 +PARSER_RC pluginsd_clabel_commit(char **words, void *user, PLUGINSD_ACTION *plugins_action)
597 +{
598 + UNUSED(words);
599 +
600 + RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
601 + debug(D_PLUGINSD, "requested to commit chart labels");
602 +
603 + struct label *chart_labels = ((PARSER_USER_OBJECT *)user)->chart_labels;
604 + ((PARSER_USER_OBJECT *)user)->chart_labels = NULL;
605 +
606 + if (plugins_action->clabel_commit_action) {
607 + return plugins_action->clabel_commit_action(user, host, chart_labels);
608 + }
609 +
610 + return PARSER_RC_OK;
611 +}
612 +
613 PARSER_RC pluginsd_overwrite(char **words, void *user, PLUGINSD_ACTION *plugins_action)
614 {
615 UNUSED(words);
collectors/plugins.d/pluginsd_parser.h
+3
@@ -14,6 +14,7 @@ typedef struct parser_user_object {
14 struct plugind *cd;
15 int trust_durations;
16 struct label *new_labels;
17 + struct label *chart_labels;
18 size_t count;
19 int enabled;
20 uint8_t st_exists;
@@ -35,6 +36,8 @@ extern PARSER_RC pluginsd_dimension_action(void *user, RRDSET *st, char *id, cha
36 long multiplier, long divisor, char *options, RRD_ALGORITHM algorithm_type);
37 extern PARSER_RC pluginsd_label_action(void *user, char *key, char *value, LABEL_SOURCE source);
38 extern PARSER_RC pluginsd_overwrite_action(void *user, RRDHOST *host, struct label *new_labels);
39 +extern PARSER_RC pluginsd_clabel_commit_action(void *user, RRDHOST *host, struct label *new_labels);
40 +extern PARSER_RC pluginsd_clabel_action(void *user, char *key, char *value, LABEL_SOURCE source);
41
42
43 #endif //NETDATA_PLUGINSD_PARSER_H
parser/parser.c
+2
@@ -61,6 +61,8 @@ PARSER *parser_init(RRDHOST *host, void *user, void *input, PARSER_INPUT_TYPE fl
61 rc += parser_add_keyword(parser, PLUGINSD_KEYWORD_LABEL, pluginsd_label);
62 rc += parser_add_keyword(parser, PLUGINSD_KEYWORD_OVERWRITE, pluginsd_overwrite);
63 rc += parser_add_keyword(parser, PLUGINSD_KEYWORD_END, pluginsd_end);
64 + rc += parser_add_keyword(parser, "CLABEL_COMMIT", pluginsd_clabel_commit);
65 + rc += parser_add_keyword(parser, "CLABEL", pluginsd_clabel);
66 rc += parser_add_keyword(parser, PLUGINSD_KEYWORD_BEGIN, pluginsd_begin);
67 rc += parser_add_keyword(parser, "SET", pluginsd_set);
68 }
parser/parser.h
+4
@@ -31,6 +31,8 @@ typedef struct pluginsd_action {
31 PARSER_RC (*variable_action)(void *user, RRDHOST *host, RRDSET *st, char *name, int global, calculated_number value);
32 PARSER_RC (*label_action)(void *user, char *key, char *value, LABEL_SOURCE source);
33 PARSER_RC (*overwrite_action)(void *user, RRDHOST *host, struct label *new_labels);
34 + PARSER_RC (*clabel_action)(void *user, char *key, char *value, LABEL_SOURCE source);
35 + PARSER_RC (*clabel_commit_action)(void *user, RRDHOST *host, struct label *new_labels);
36
37 PARSER_RC (*guid_action)(void *user, uuid_t *uuid);
38 PARSER_RC (*context_action)(void *user, uuid_t *uuid);
@@ -110,5 +112,7 @@ extern PARSER_RC pluginsd_overwrite(char **words, void *user, PLUGINSD_ACTION *
112 extern PARSER_RC pluginsd_guid(char **words, void *user, PLUGINSD_ACTION *plugins_action);
113 extern PARSER_RC pluginsd_context(char **words, void *user, PLUGINSD_ACTION *plugins_action);
114 extern PARSER_RC pluginsd_tombstone(char **words, void *user, PLUGINSD_ACTION *plugins_action);
115 +extern PARSER_RC pluginsd_clabel_commit(char **words, void *user, PLUGINSD_ACTION *plugins_action);
116 +extern PARSER_RC pluginsd_clabel(char **words, void *user, PLUGINSD_ACTION *plugins_action);
117
118 #endif
streaming/receiver.c
+2
@@ -221,6 +221,8 @@ size_t streaming_parser(struct receiver_state *rpt, struct plugind *cd, FILE *fp
221 parser->plugins_action->overwrite_action = &pluginsd_overwrite_action;
222 parser->plugins_action->chart_action = &pluginsd_chart_action;
223 parser->plugins_action->set_action = &pluginsd_set_action;
224 + parser->plugins_action->clabel_commit_action = &pluginsd_clabel_commit_action;
225 + parser->plugins_action->clabel_action = &pluginsd_clabel_action;
226
227 user->parser = parser;
228
streaming/rrdpush.c
+22
@@ -183,6 +183,24 @@ static inline int need_to_send_chart_definition(RRDSET *st) {
183 return 0;
184 }
185
186 +// chart labels
187 +void rrdpush_send_clabels(RRDHOST *host, RRDSET *st) {
188 + struct label_index *labels_c = &st->state->labels;
189 + if (labels_c) {
190 + netdata_rwlock_rdlock(&host->labels.labels_rwlock);
191 + struct label *lbl = labels_c->head;
192 + while(lbl) {
193 + buffer_sprintf(host->sender->build,
194 + "CLABEL \"%s\" \"%s\" %d\n", lbl->key, lbl->value, (int)lbl->label_source);
195 +
196 + lbl = lbl->next;
197 + }
198 + if (labels_c->head)
199 + buffer_sprintf(host->sender->build,"CLABEL_COMMIT\n");
200 + netdata_rwlock_unlock(&host->labels.labels_rwlock);
201 + }
202 +}
203 +
204 // Send the current chart definition.
205 // Assumes that collector thread has already called sender_start for mutex / buffer state.
206 static inline void rrdpush_send_chart_definition_nolock(RRDSET *st) {
@@ -224,6 +242,10 @@ static inline void rrdpush_send_chart_definition_nolock(RRDSET *st) {
242 , (st->module_name)?st->module_name:""
243 );
244
245 + // send the chart labels
246 + if (host->sender->version >= STREAM_VERSION_CLABELS)
247 + rrdpush_send_clabels(host, st);
248 +
249 // send the dimensions
250 RRDDIM *rd;
251 rrddim_foreach_read(rd, st) {
streaming/rrdpush.h
+3 -3
@@ -10,10 +10,10 @@
10
11 #define CONNECTED_TO_SIZE 100
12
13 -// #define STREAMING_PROTOCOL_CURRENT_VERSION (uint32_t)4 Gap-filling
14 -#define STREAMING_PROTOCOL_CURRENT_VERSION (uint32_t)3
15 -#define VERSION_GAP_FILLING 4
13 +#define STREAMING_PROTOCOL_CURRENT_VERSION (uint32_t)4
14 #define STREAM_VERSION_CLAIM 3
15 +#define STREAM_VERSION_CLABELS 4
16 +#define VERSION_GAP_FILLING 5
17
18 #define STREAMING_PROTOCOL_VERSION "1.1"
19 #define START_STREAMING_PROMPT "Hit me baby, push them over..."