Skip to content

Commit

Permalink
added /api/v3/stream_path (netdata#18943)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsaou authored Nov 9, 2024
1 parent 73d5b5b commit b3ef98c
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 21 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,7 @@ set(WEB_PLUGIN_FILES
src/web/server/static/static-threaded.h
src/web/server/web_client_cache.c
src/web/server/web_client_cache.h
src/web/api/v3/api_v3_stream_path.c
)

set(CLAIM_PLUGIN_FILES
Expand Down
14 changes: 11 additions & 3 deletions src/database/contexts/api_v2_contexts.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,11 @@ static void rrdcontext_to_json_v2_rrdhost(BUFFER *wb, RRDHOST *host, struct rrdc
buffer_json_node_add_v2(wb, host, node_id, 0,
(ctl->mode & CONTEXTS_V2_AGENTS) && !(ctl->mode & CONTEXTS_V2_NODE_INSTANCES));

if(ctl->mode & (CONTEXTS_V2_NODES_INFO | CONTEXTS_V2_NODE_INSTANCES)) {
if(ctl->mode & (CONTEXTS_V2_NODES_INFO | CONTEXTS_V2_NODES_STREAM_PATH | CONTEXTS_V2_NODE_INSTANCES)) {
RRDHOST_STATUS s;
rrdhost_status(host, ctl->now, &s);

if (ctl->mode & (CONTEXTS_V2_NODES_INFO)) {
if (ctl->mode & (CONTEXTS_V2_NODES_INFO | CONTEXTS_V2_NODES_STREAM_PATH)) {
buffer_json_member_add_string(wb, "v", rrdhost_program_version(host));

host_labels2json(host, wb, "labels");
Expand Down Expand Up @@ -423,9 +423,14 @@ static void rrdcontext_to_json_v2_rrdhost(BUFFER *wb, RRDHOST *host, struct rrdc
// reachable - connected with live data
// pruned - not connected for some time and has been removed
buffer_json_member_add_string(wb, "state", rrdhost_is_online(host) ? "reachable" : "stale");
}

if (ctl->mode & (CONTEXTS_V2_NODES_INFO)) {
rrdhost_health_to_json_v2(wb, "health", &s);
agent_capabilities_to_json(wb, host, "capabilities");
}

if (ctl->mode & (CONTEXTS_V2_NODES_STREAM_PATH)) {
rrdhost_stream_path_to_json(wb, host, STREAM_PATH_JSON_MEMBER, false);
}

Expand Down Expand Up @@ -608,6 +613,9 @@ static void buffer_json_contexts_v2_mode_to_array(BUFFER *wb, const char *key, C
if(mode & CONTEXTS_V2_NODES_INFO)
buffer_json_add_array_item_string(wb, "nodes-info");

if(mode & CONTEXTS_V2_NODES_STREAM_PATH)
buffer_json_add_array_item_string(wb, "nodes-stream-path");

if(mode & CONTEXTS_V2_NODE_INSTANCES)
buffer_json_add_array_item_string(wb, "nodes-instances");

Expand Down Expand Up @@ -750,7 +758,7 @@ int rrdcontext_to_json_v2(BUFFER *wb, struct api_v2_contexts_request *req, CONTE
if(mode & (CONTEXTS_V2_AGENTS_INFO))
mode |= CONTEXTS_V2_AGENTS;

if(mode & (CONTEXTS_V2_FUNCTIONS | CONTEXTS_V2_CONTEXTS | CONTEXTS_V2_SEARCH | CONTEXTS_V2_NODES_INFO | CONTEXTS_V2_NODE_INSTANCES))
if(mode & (CONTEXTS_V2_FUNCTIONS | CONTEXTS_V2_CONTEXTS | CONTEXTS_V2_SEARCH | CONTEXTS_V2_NODES_INFO | CONTEXTS_V2_NODES_STREAM_PATH | CONTEXTS_V2_NODE_INSTANCES))
mode |= CONTEXTS_V2_NODES;

if(mode & CONTEXTS_V2_ALERTS) {
Expand Down
15 changes: 8 additions & 7 deletions src/database/contexts/rrdcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -648,13 +648,14 @@ typedef enum __attribute__ ((__packed__)) {
CONTEXTS_V2_NODES = (1 << 2),
CONTEXTS_V2_NODES_INFO = (1 << 3),
CONTEXTS_V2_NODE_INSTANCES = (1 << 4),
CONTEXTS_V2_CONTEXTS = (1 << 5),
CONTEXTS_V2_AGENTS = (1 << 6),
CONTEXTS_V2_AGENTS_INFO = (1 << 7),
CONTEXTS_V2_VERSIONS = (1 << 8),
CONTEXTS_V2_FUNCTIONS = (1 << 9),
CONTEXTS_V2_ALERTS = (1 << 10),
CONTEXTS_V2_ALERT_TRANSITIONS = (1 << 11),
CONTEXTS_V2_NODES_STREAM_PATH = (1 << 5),
CONTEXTS_V2_CONTEXTS = (1 << 6),
CONTEXTS_V2_AGENTS = (1 << 7),
CONTEXTS_V2_AGENTS_INFO = (1 << 8),
CONTEXTS_V2_VERSIONS = (1 << 9),
CONTEXTS_V2_FUNCTIONS = (1 << 10),
CONTEXTS_V2_ALERTS = (1 << 11),
CONTEXTS_V2_ALERT_TRANSITIONS = (1 << 12),
} CONTEXTS_V2_MODE;

int rrdcontext_to_json_v2(BUFFER *wb, struct api_v2_contexts_request *req, CONTEXTS_V2_MODE mode);
Expand Down
4 changes: 2 additions & 2 deletions src/database/rrd.h
Original file line number Diff line number Diff line change
Expand Up @@ -1104,8 +1104,8 @@ typedef struct health {
time_t health_delay_up_to; // a timestamp to delay alarms processing up to
STRING *health_default_exec; // the full path of the alarms notifications program
STRING *health_default_recipient; // the default recipient for all alarms
unsigned int health_enabled; // 1 when this host has health enabled
bool use_summary_for_notifications; // whether or not to use the summary field as a subject for notifications
bool health_enabled; // 1 when this host has health enabled
bool use_summary_for_notifications; // whether to use the summary field as a subject for notifications
} HEALTH;

// ----------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/database/sqlite/sqlite_metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ static int store_host_metadata(RRDHOST *host)
SQLITE_BIND_FAIL(bind_fail, bind_text_null(res, ++param, rrdhost_program_name(host), 1));
SQLITE_BIND_FAIL(bind_fail, bind_text_null(res, ++param, rrdhost_program_version(host), 1));
SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_int64(res, ++param, host->rrd_history_entries));
SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_int(res, ++param, (int ) host->health.health_enabled));
SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_int(res, ++param, (int)host->health.health_enabled));
SQLITE_BIND_FAIL(bind_fail, sqlite3_bind_int64(res, ++param, (sqlite3_int64) host->last_connected));

int store_rc = sqlite3_step_monitored(res);
Expand Down
25 changes: 19 additions & 6 deletions src/streaming/stream-path.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
#include "plugins.d/pluginsd_internals.h"

ENUM_STR_MAP_DEFINE(STREAM_PATH_FLAGS) = {
{ .id = STREAM_PATH_FLAG_ACLK, .name = "aclk" },
{ .id = STREAM_PATH_FLAG_ACLK, .name = "aclk" },
{ .id = STREAM_PATH_FLAG_HEALTH, .name = "health" },
{ .id = STREAM_PATH_FLAG_ML, .name = "ml" },
{ .id = STREAM_PATH_FLAG_EPHEMERAL, .name = "ephemeral" },
{ .id = STREAM_PATH_FLAG_VIRTUAL, .name = "virtual" },

// terminator
{ . id = 0, .name = NULL }
Expand Down Expand Up @@ -79,19 +83,28 @@ static STREAM_PATH rrdhost_stream_path_self(RRDHOST *host) {
if(!UUIDiszero(p.claim_id))
p.flags |= STREAM_PATH_FLAG_ACLK;

bool has_receiver = false;
if(rrdhost_option_check(host, RRDHOST_OPTION_EPHEMERAL_HOST))
p.flags |= STREAM_PATH_FLAG_EPHEMERAL;

if(rrdhost_option_check(host, RRDHOST_OPTION_VIRTUAL_HOST))
p.flags |= STREAM_PATH_FLAG_VIRTUAL;

if(host->health.health_enabled)
p.flags |= STREAM_PATH_FLAG_HEALTH;

if(ml_enabled(host))
p.flags |= STREAM_PATH_FLAG_ML;

spinlock_lock(&host->receiver_lock);
if(host->receiver) {
has_receiver = true;
p.hops = (int16_t)host->receiver->hops;
p.since = host->receiver->connected_since_s;
}
spinlock_unlock(&host->receiver_lock);

if(!has_receiver) {
else {
p.hops = (is_localhost) ? 0 : -1; // -1 for stale nodes
p.since = netdata_start_time;
}
spinlock_unlock(&host->receiver_lock);

// the following may get the receiver lock again!
p.capabilities = stream_our_capabilities(host, true);
Expand Down
8 changes: 6 additions & 2 deletions src/streaming/stream-path.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
#define STREAM_PATH_JSON_MEMBER "streaming_path"

typedef enum __attribute__((packed)) {
STREAM_PATH_FLAG_NONE = 0,
STREAM_PATH_FLAG_ACLK = (1 << 0),
STREAM_PATH_FLAG_NONE = 0,
STREAM_PATH_FLAG_ACLK = (1 << 0),
STREAM_PATH_FLAG_HEALTH = (1 << 1),
STREAM_PATH_FLAG_ML = (1 << 2),
STREAM_PATH_FLAG_EPHEMERAL = (1 << 3),
STREAM_PATH_FLAG_VIRTUAL = (1 << 4),
} STREAM_PATH_FLAGS;

typedef struct stream_path {
Expand Down
4 changes: 4 additions & 0 deletions src/web/api/v3/api_v3_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

#include "../web_api_v3.h"

int api_v2_contexts_internal(RRDHOST *host, struct web_client *w, char *url, CONTEXTS_V2_MODE mode);
#define api_v3_contexts_internal(host, w, url, mode) api_v2_contexts_internal(host, w, url, mode)

int api_v3_settings(RRDHOST *host, struct web_client *w, char *url);
int api_v3_me(RRDHOST *host, struct web_client *w, char *url);
int api_v3_stream_path(RRDHOST *host __maybe_unused, struct web_client *w, char *url);

#endif //NETDATA_API_V3_CALLS_H
7 changes: 7 additions & 0 deletions src/web/api/v3/api_v3_stream_path.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include "api_v3_calls.h"

int api_v3_stream_path(RRDHOST *host __maybe_unused, struct web_client *w, char *url) {
return api_v3_contexts_internal(host, w, url, CONTEXTS_V2_NODES | CONTEXTS_V2_NODES_STREAM_PATH);
}
8 changes: 8 additions & 0 deletions src/web/api/web_api_v3.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ static struct web_api_command api_commands_v3[] = {
.callback = api_v2_node_instances,
.allow_subpaths = 0
},
{
.api = "stream_path",
.hash = 0,
.acl = HTTP_ACL_NODES,
.access = HTTP_ACCESS_ANONYMOUS_DATA,
.callback = api_v3_stream_path,
.allow_subpaths = 0
},
{
.api = "versions",
.hash = 0,
Expand Down

0 comments on commit b3ef98c

Please sign in to comment.