Use the new error mechanism in case host not found (#12277)
Timotej S committed
Mar 1, 2022 at 17:15 UTC
f63fc2a5cd928048d47e5e62a0d757ab4be5f8e5
3 files changed
+33
-19
aclk/aclk_query.c
+18
-10
@@ -98,26 +98,34 @@ static int http_api_v2(struct aclk_query_thread *query_thr, aclk_query_t query)
98
w->cookie2[0] = 0; // Simulate web_client_create_on_fd()
99
w->acl = 0x1f;
100
101
+ buffer_strcat(log_buffer, query->data.http_api_v2.query);
102
+ size_t size = 0;
103
+ size_t sent = 0;
104
+ w->tv_in = query->created_tv;
105
+ now_realtime_timeval(&w->tv_ready);
106
+
107
if (!strncmp(query->data.http_api_v2.query, NODE_ID_QUERY, strlen(NODE_ID_QUERY))) {
108
char *node_uuid = query->data.http_api_v2.query + strlen(NODE_ID_QUERY);
109
char nodeid[UUID_STR_LEN];
110
if (strlen(node_uuid) < (UUID_STR_LEN - 1)) {
105
- error("URL requests node_id but there is not enough chars following");
111
+ error("URL requests node_id but there is not enough chars following. Returning 404 to Cloud.");
112
retval = 1;
113
+ w->response.code = 404;
114
+ aclk_http_msg_v2_err(query_thr->client, query->callback_topic, query->msg_id, w->response.code, NULL, 0);
115
goto cleanup;
116
}
117
strncpyz(nodeid, node_uuid, UUID_STR_LEN - 1);
118
119
query_host = node_id_2_rrdhost(nodeid);
120
if (!query_host) {
113
- error("Host with node_id \"%s\" not found! Query Ignored!", node_uuid);
121
+ error("Host with node_id \"%s\" not found! Returning 404 to Cloud!", node_uuid);
122
retval = 1;
123
+ w->response.code = 404;
124
+ aclk_http_msg_v2_err(query_thr->client, query->callback_topic, query->msg_id, w->response.code, NULL, 0);
125
goto cleanup;
126
}
127
}
128
119
- buffer_strcat(log_buffer, query->data.http_api_v2.query);
120
-
129
char *mysep = strchr(query->data.http_api_v2.query, '?');
130
if (mysep) {
131
url_decode_r(w->decoded_query_string, mysep, NETDATA_WEB_REQUEST_URL_SIZE + 1);
@@ -135,11 +143,9 @@ static int http_api_v2(struct aclk_query_thread *query_thr, aclk_query_t query)
143
}
144
145
// execute the query
138
- w->tv_in = query->created_tv;
139
- now_realtime_timeval(&w->tv_ready);
146
t = aclk_web_api_v1_request(query_host, w, mysep ? mysep + 1 : "noop");
141
- size_t size = (w->mode == WEB_CLIENT_MODE_FILECOPY) ? w->response.rlen : w->response.data->len;
142
- size_t sent = size;
147
+ size = (w->mode == WEB_CLIENT_MODE_FILECOPY) ? w->response.rlen : w->response.data->len;
148
+ sent = size;
149
150
#ifdef NETDATA_WITH_ZLIB
151
// check if gzip encoding can and should be used
@@ -173,6 +179,8 @@ static int http_api_v2(struct aclk_query_thread *query_thr, aclk_query_t query)
179
else
180
error("Unknown error during zlib compression.");
181
retval = 1;
182
+ w->response.code = 500;
183
+ aclk_http_msg_v2_err(query_thr->client, query->callback_topic, query->msg_id, w->response.code, NULL, 0);
184
goto cleanup;
185
}
186
int bytes_to_cpy = NETDATA_WEB_RESPONSE_ZLIB_CHUNK_SIZE - w->response.zstream.avail_out;
@@ -213,8 +221,9 @@ static int http_api_v2(struct aclk_query_thread *query_thr, aclk_query_t query)
221
// send msg.
222
aclk_http_msg_v2(query_thr->client, query->callback_topic, query->msg_id, t, query->created, w->response.code, local_buffer->buffer, local_buffer->len);
223
216
- // log.
224
struct timeval tv;
225
+
226
+cleanup:
227
now_realtime_timeval(&tv);
228
log_access("%llu: %d '[ACLK]:%d' '%s' (sent/all = %zu/%zu bytes %0.0f%%, prep/sent/total = %0.2f/%0.2f/%0.2f ms) %d '%s'",
229
w->id
@@ -231,7 +240,6 @@ static int http_api_v2(struct aclk_query_thread *query_thr, aclk_query_t query)
240
, strip_control_characters((char *)buffer_tostring(log_buffer))
241
);
242
234
-cleanup:
243
#ifdef NETDATA_WITH_ZLIB
244
if(w->response.zinitialized)
245
deflateEnd(&w->response.zstream);
aclk/aclk_tx_msgs.c
+14
-9
@@ -325,6 +325,18 @@ void aclk_send_alarm_metadata(mqtt_wss_client client, int metadata_submitted)
325
buffer_free(local_buffer);
326
}
327
328
+void aclk_http_msg_v2_err(mqtt_wss_client client, const char *topic, const char *msg_id, int http_code, const char *payload, size_t payload_len)
329
+{
330
+ json_object *tmp, *msg;
331
+ msg = create_hdr("http", msg_id, 0, 0, 2);
332
+ tmp = json_object_new_int(http_code);
333
+ json_object_object_add(msg, "http-code", tmp);
334
+ if (aclk_send_message_with_bin_payload(client, msg, topic, payload, payload_len)) {
335
+ error("Failed to send cancelation message for http reply");
336
+ }
337
+ json_object_put(msg);
338
+}
339
+
340
void aclk_http_msg_v2(mqtt_wss_client client, const char *topic, const char *msg_id, usec_t t_exec, usec_t created, int http_code, const char *payload, size_t payload_len)
341
{
342
json_object *tmp, *msg;
@@ -343,15 +355,8 @@ void aclk_http_msg_v2(mqtt_wss_client client, const char *topic, const char *msg
355
int rc = aclk_send_message_with_bin_payload(client, msg, topic, payload, payload_len);
356
json_object_put(msg);
357
346
- if (rc) {
347
- msg = create_hdr("http", msg_id, 0, 0, 2);
348
- tmp = json_object_new_int(rc);
349
- json_object_object_add(msg, "http-code", tmp);
350
- if (aclk_send_message_with_bin_payload(client, msg, topic, payload, payload_len)) {
351
- error("Failed to send cancelation message for http reply");
352
- }
353
- json_object_put(msg);
354
- }
358
+ if (rc)
359
+ aclk_http_msg_v2_err(client, topic, msg_id, rc, payload, payload_len);
360
}
361
362
void aclk_chart_msg(mqtt_wss_client client, RRDHOST *host, const char *chart)
aclk/aclk_tx_msgs.h
+1
@@ -14,6 +14,7 @@ uint16_t aclk_send_bin_message_subtopic_pid(mqtt_wss_client client, char *msg, s
14
void aclk_send_info_metadata(mqtt_wss_client client, int metadata_submitted, RRDHOST *host);
15
void aclk_send_alarm_metadata(mqtt_wss_client client, int metadata_submitted);
16
17
+void aclk_http_msg_v2_err(mqtt_wss_client client, const char *topic, const char *msg_id, int http_code, const char *payload, size_t payload_len);
18
void aclk_http_msg_v2(mqtt_wss_client client, const char *topic, const char *msg_id, usec_t t_exec, usec_t created, int http_code, const char *payload, size_t payload_len);
19
20
void aclk_chart_msg(mqtt_wss_client client, RRDHOST *host, const char *chart);