diff options
author | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2019-07-09 00:03:31 +0200 |
---|---|---|
committer | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2019-07-09 00:03:31 +0200 |
commit | ab0d33154340f25adb7f8a027eb7ffa928ffd0b3 (patch) | |
tree | 0ffe38ae448cfbc16da266a0254716ef59aeac9e | |
parent | 946b42978fb407b323a108ce51f8b3b7cb3630e7 (diff) |
Rewrite tracing_events in C (instead of lua)
Change-Id: I1e88f18a7688cc5e1708256306fb444eb86eb867
-rw-r--r-- | README.md | 5 | ||||
-rw-r--r-- | conf.d/project/etc/xds-supervisor-config.json | 4 | ||||
-rw-r--r-- | conf.d/project/lua.d/xds-supervisor.lua | 5 | ||||
-rw-r--r-- | src/plugins/CMakeLists.txt | 29 | ||||
-rw-r--r-- | src/plugins/supervisor-api.c | 211 | ||||
-rw-r--r-- | src/plugins/supervisor-api.h | 14 |
6 files changed, 154 insertions, 114 deletions
@@ -68,7 +68,7 @@ afb-daemon -t '' -p 4444 -M --roothttp test --no-ldpaths --ws-client unix:ave -- afb-client-demo --raw 'localhost:2222/api?token=1977&uuid=magic' xds list | jq .response[].pid # Request monitoring of socket "unix:ave" -afb-client-demo 'localhost:2222/api?token=HELLO&uuid=c' xds trace '{"ws":"unix:ave"}' +afb-client-demo 'localhost:2222/api?token=HELLO&uuid=c' xds trace/start '{"ws":"unix:ave"}' # Call ping verb of hello api (should be spy by supervision/monitoring) afb-client-demo 'localhost:5555/api?token=toto&uuid=magic' hello ping @@ -81,6 +81,9 @@ afb-client-demo 'localhost:5555/api?token=toto&uuid=magic' hello eventpush '{"ta # Dump data from TSDB influx -database 'agl-garner' -execute 'show series' influx -database 'agl-garner' -execute 'select * from "xds/supervisor/trace"' + +# Stop monitoring +afb-client-demo 'localhost:2222/api?token=HELLO&uuid=c' xds trace/stop ``` **Example 2**: Demo based on AGL mockup services diff --git a/conf.d/project/etc/xds-supervisor-config.json b/conf.d/project/etc/xds-supervisor-config.json index 654b4b2..a763818 100644 --- a/conf.d/project/etc/xds-supervisor-config.json +++ b/conf.d/project/etc/xds-supervisor-config.json @@ -37,6 +37,10 @@ "events": [{ "uid": "supervisor/xds-trace", + "action": "plugin://supervisor#tracing_events" + }, + { + "uid": "_DISABLE_supervisor/xds-trace", "action": "lua://supervisor#_trace_events_" } ] diff --git a/conf.d/project/lua.d/xds-supervisor.lua b/conf.d/project/lua.d/xds-supervisor.lua index 1058b59..90bc6c0 100644 --- a/conf.d/project/lua.d/xds-supervisor.lua +++ b/conf.d/project/lua.d/xds-supervisor.lua @@ -17,6 +17,10 @@ NOTE: strict mode: every global variables should be prefixed by '_' --]] + +--[[ + NO longer used, replace by C code : tracing_events + -- return serialised version of printable table function ToJson(o) if type(o) == "table" then @@ -120,3 +124,4 @@ function _trace_events_(source, args, event) return 0 end +--]] diff --git a/src/plugins/CMakeLists.txt b/src/plugins/CMakeLists.txt index e6593be..a456dc0 100644 --- a/src/plugins/CMakeLists.txt +++ b/src/plugins/CMakeLists.txt @@ -18,19 +18,20 @@ PROJECT_TARGET_ADD(supervisor) - # Define targets - ADD_LIBRARY(${TARGET_NAME} MODULE - ${TARGET_NAME}-api.c - ${TARGET_NAME}.c - ../utils/list.c) + # Define targets + ADD_LIBRARY(${TARGET_NAME} MODULE + ${TARGET_NAME}-api.c + ${TARGET_NAME}.c + ../utils/list.c) - # Alsa Plugin properties - SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES - LABELS "PLUGIN" - PREFIX "" - SUFFIX ".ctlso" - OUTPUT_NAME ${TARGET_NAME} - ) + # Alsa Plugin properties + SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES + LABELS "PLUGIN" + PREFIX "" + SUFFIX ".ctlso" + OUTPUT_NAME ${TARGET_NAME} + ) - target_include_directories(${TARGET_NAME} - PRIVATE "${CMAKE_SOURCE_DIR}/app-controller/ctl-lib") + TARGET_INCLUDE_DIRECTORIES(${TARGET_NAME} + PRIVATE "${CMAKE_SOURCE_DIR}/app-controller/ctl-lib" + ) diff --git a/src/plugins/supervisor-api.c b/src/plugins/supervisor-api.c index cb2b476..a649c1e 100644 --- a/src/plugins/supervisor-api.c +++ b/src/plugins/supervisor-api.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 "IoT.bzh" + * Copyright (C) 2018-2019 "IoT.bzh" * Author "Sebastien Douheret" <sebastien@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -31,14 +31,14 @@ static int TracePidsNum = 0; static int* TracePidsArr = NULL; -#define TRACE_DAEMON(src, nb, dd, lvl, res) \ - { \ - nb++; \ - json_object_array_add(res, json_object_new_int(dd->pid)); \ - if ((rc = trace_daemon(src->api, dd, lvl)) < 0) { \ +#define TRACE_DAEMON(src, nb, dd, lvl, res) \ + { \ + nb++; \ + json_object_array_add(res, json_object_new_int(dd->pid)); \ + if ((rc = trace_daemon(src->api, dd, lvl)) < 0) { \ afb_req_fail_f(src->request, "failed", "Trace pid %d error %d", dd->pid, rc); \ - goto error; \ - } \ + goto error; \ + } \ } CTLP_CAPI_REGISTER("supervisor"); @@ -251,8 +251,7 @@ CTLP_CAPI(trace_stop, source, argsJ, queryJ) return 0; } -#if 0 // Not used, implemented in lua for now (see xds-supervisor.lua / _trace_events_) -uint64_t get_ts() +static inline uint64_t get_ts() { struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); @@ -262,112 +261,154 @@ uint64_t get_ts() static void cb_harvester_write(void* closure, int status, struct json_object* result, struct afb_dynapi* dynapi) { - AFB_API_DEBUG(dynapi, "SEB cb_harvester_write"); + json_object* err = NULL; + if (result) { + wrap_json_unpack(result, "{s:{s:o}}", "request", "info", &err); + } + + AFB_API_DEBUG(dynapi, "cb_harvester_write status: %d, %s", status, json_object_to_json_string(err)); } -static int harvester_post_data(CtlSourceT* source, METRIC_T* metric) +CTLP_CAPI(tracing_events, source, argsJ, eventJ) { int rc; + json_object* request = NULL; + json_object* event = NULL; + json_object* data = NULL; + json_object* timestamp = NULL; json_object *j_res, *j_query, *j_metric; + json_object *j_metadata, *j_values; + int id = 0; + const char* type = NULL; + const char* tag = NULL; + const char* cdata = NULL; + + AFB_API_DEBUG(source->api, ">>> RECV Event uid %s : %s", source->uid, + json_object_to_json_string(eventJ)); - if (!metric->timestamp) { - metric->timestamp = get_ts(); + if (strcmp(source->uid, "supervisor/xds-trace") != 0) { + AFB_API_NOTICE(source->api, "WARNING: un-handle uid '%s'", source->uid); + return 0; } - // To be REWORK - if (metric->dataType == SPVR_DATA_STRING) { - rc = wrap_json_pack(&j_metric, "{s:s s:{s:s s:s} s:{s:s} s:I }", - "name", metric->name, - "metadata", "source", "my_source", "identity", source->uid, - "values", "value", metric->data, - "timestamp", metric->timestamp); - } else if (metric->dataType == SPVR_DATA_INT) { - rc = wrap_json_pack(&j_metric, "{s:s s:{s:s s:s} s:{s:i} s:I }", - "name", metric->name, - "metadata", "source", "my_source", "identity", source->uid, - "values", "value", metric->data, - "timestamp", metric->timestamp); - } else { - afb_req_fail(source->request, "Failed", "Unsupported dataType"); + if ((rc = wrap_json_unpack(eventJ, "{so ss si ss s?o s?o s?o}", + "time", ×tamp, + "tag", &tag, + "id", &id, + "type", &type, + "request", &request, + "event", &event, + "data", &data)) + < 0) { + AFB_API_ERROR(source->api, "Cannot decode event object : %s", wrap_json_get_error_string(rc)); return ERROR; } - if (rc < 0) { - afb_req_fail(source->request, "Failed", "Error packing metric, rc=%d", rc); - return rc; + if (!timestamp) { + timestamp = json_object_new_int64(get_ts()); } - rc = wrap_json_pack(&j_query, "{s:s s:i s:o }", "host", - "localhost", "port", 8086, "metric", j_metric); + rc = wrap_json_pack(&j_metadata, "{s:s s:s}", + "identity", "xds_supervisor", + "tag", tag); if (rc < 0) { - afb_req_fail(source->request, "Failed", "Error packing query, rc=%d", rc); - return rc; + AFB_API_ERROR(source->api, "Error packing j_metadata, rc=%d", rc); + goto _EXIT_TRACE_EVT; } - - AFB_API_DEBUG(source->api, "%s write: %s", SRV_HARVESTER_NAME, - json_object_to_json_string(j_query)); - - /* SEB - rc = afb_api_call_sync_legacy(source->api, SRV_HARVESTER_NAME, "write", j_query, &j_res); + rc = wrap_json_pack(&j_values, "{si}", "id", id); if (rc < 0) { - afb_req_fail(source->request, "Failed", "Error %s write : rc=%d, j_res=%s", SRV_HARVESTER_NAME, rc, - json_object_to_json_string(j_res)); - return rc; + AFB_API_ERROR(source->api, "Error packing j_values, rc=%d", rc); + goto _EXIT_TRACE_EVT; } -*/ - afb_api_call_legacy(source->api, SRV_HARVESTER_NAME, "write", j_query, cb_harvester_write, &j_res); - return 0; -} + if (!strncmp(type, "request", 6) && request) { + // Process "request" type -CTLP_CAPI(tracing_events, source, argsJ, eventJ) -{ - int rc; - METRIC_T metric = { 0 }; - const char* type = NULL; - struct json_object* request = NULL; + json_object* j_action = json_object_object_get(request, "action"); + + // Filter out some traces +#if 0 // TO Test + const char* action = json_object_get_string(j_action); + if (action && (!strncmp(action, "begin", 5) || !strncmp(action, "end", 3) || !strncmp(action, "json", 4))) { + AFB_API_DEBUG(source->api, "trace_events IGNORED event=%s", json_object_to_json_string(request)); + rc = 0; + free(action); + goto _EXIT_TRACE_EVT; + } + if (action) + free(action); +#endif - //struct signalCBT* ctx = (struct signalCBT*)source->context; + json_object_object_add(j_metadata, "type", json_object_new_string("request")); + json_object_object_add(j_metadata, "api", json_object_object_get(request, "api")); + json_object_object_add(j_metadata, "verb", json_object_object_get(request, "verb")); + json_object_object_add(j_metadata, "action", j_action); + json_object_object_add(j_metadata, "session", json_object_object_get(request, "session")); + json_object_object_add(j_metadata, "req_index", json_object_new_string(json_object_to_json_string(json_object_object_get(request, "index")))); - AFB_API_DEBUG(source->api, ">>> RECV Event uid %s : %s", source->uid, - json_object_to_json_string(eventJ)); + AFB_API_DEBUG(source->api, "Type request, j_metadata=%s", json_object_to_json_string(j_metadata)); - if (strcmp(source->uid, "xds/supervisor/trace") != 0) { - AFB_API_NOTICE(source->api, "WARNING: un-handle uid '%s'", source->uid); - return 0; - } + } else if (!strncmp(type, "event", 5) && event) { + // Process "event" type - if ((rc = wrap_json_unpack(eventJ, "{s:?s}", "type", &type)) < 0) { - afb_req_fail(source->request, "Failed", "Cannot decode event type"); - return ERROR; + json_object_object_add(j_metadata, "type", json_object_new_string("event")); + json_object_object_add(j_metadata, "id", json_object_object_get(event, "id")); + json_object_object_add(j_metadata, "name", json_object_object_get(event, "name")); + json_object_object_add(j_metadata, "action", json_object_object_get(event, "action")); + + AFB_API_DEBUG(source->api, "Type event, j_metadata=%s", json_object_to_json_string(j_metadata)); + } else { + AFB_API_ERROR(source->api, "Null request or event field"); + rc = ERROR; + goto _EXIT_TRACE_EVT; } - if (strcmp(type, "request") != 0) { - afb_req_fail(source->request, "Failed", "Cannot retrieve request"); - return ERROR; + if (data) { + // Serialized data (IOW an JSON object pass as a string) + cdata = json_object_to_json_string(data); + json_object_object_add(j_values, "data", json_object_new_string(cdata)); + json_object_object_add(j_values, "data_bytes", json_object_new_int((int32_t)strlen(cdata))); } - if (!json_object_object_get_ex(eventJ, "request", &request)) { - afb_req_fail(source->request, "Failed", "Cannot decode event request"); - return ERROR; + + rc = wrap_json_pack(&j_metric, "{s:s s:o s:o s:o }", + "name", "xds/supervisor/trace", + "metadata", j_metadata, + "values", j_values, + "timestamp", timestamp); + + if (rc < 0) { + AFB_API_ERROR(source->api, "Error packing j_metric, rc=%d", rc); + goto _EXIT_TRACE_EVT; } - // TODO: decode request and build trace + rc = wrap_json_pack(&j_query, "{s:s s:i s:[o] }", + "host", "localhost", // FIXME - configurable + "port", 8086, // FIXME - configurable + "metric", j_metric); + if (rc < 0) { + AFB_API_ERROR(source->api, "Error packing query, rc=%d", rc); + goto _EXIT_TRACE_EVT; + } - metric.name = "trace"; - /* FIXME string KO - metric.dataType = SPVR_DATA_STRING; - metric.data = "test1234"; - */ - metric.dataType = SPVR_DATA_INT; - int val = 54321; - metric.data = &val; + AFB_API_DEBUG(source->api, "%s write: %s", SRV_HARVESTER_NAME, + json_object_to_json_string(j_query)); - rc = harvester_post_data(source, &metric); +#if 0 // SYNC_CALL - for debugging purpose + rc = afb_api_call_sync_legacy(source->api, SRV_HARVESTER_NAME, "write", j_query, &j_res); if (rc < 0) { - afb_req_fail(source->request, "Failed", "ERROR harvester_post_data: rc %d", rc); - return rc; + AFB_API_ERROR(source->api, "Error %s write : rc=%d, j_res=%s", SRV_HARVESTER_NAME, rc, + json_object_to_json_string(j_res)); + goto _EXIT_TRACE_EVT; } +#else + afb_api_call_legacy(source->api, SRV_HARVESTER_NAME, "write", j_query, cb_harvester_write, &j_res); +#endif - return 0; + // No error + rc = 0; + +_EXIT_TRACE_EVT: + // no need to free type, tag and cdata (already managed by json_object_generic_delete) + + return rc; } -#endif diff --git a/src/plugins/supervisor-api.h b/src/plugins/supervisor-api.h index fddaf98..96b00c8 100644 --- a/src/plugins/supervisor-api.h +++ b/src/plugins/supervisor-api.h @@ -28,17 +28,3 @@ #ifndef ERROR #define ERROR -1 #endif - -typedef enum { - SPVR_DATA_STRING = 0, - SPVR_DATA_INT, - SPVR_DATA_BOOL, - SPVR_DATA_FLOAT, -} SpvrDataTypeT; - -typedef struct metric_t { - char* name; - SpvrDataTypeT dataType; - void* data; - uint64_t timestamp; -} METRIC_T; |