Skip to content

Commit

Permalink
Merge pull request #966 from criblio/feature/918-output-cribl-config
Browse files Browse the repository at this point in the history
output the cribl section when outputting config
  • Loading branch information
jrcheli authored May 25, 2022
2 parents 84d98f8 + f27f36f commit 6f33adc
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
32 changes: 30 additions & 2 deletions src/cfgutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -2481,11 +2481,36 @@ createProtocolJson(config_t* cfg)
return NULL;
}

static cJSON*
createCriblJson(config_t* cfg)
{
cJSON *root = NULL;
cJSON *transport;

if (!(root = cJSON_CreateObject())) goto err;

if (!cJSON_AddStringToObjLN(root, ENABLE_NODE,
valToStr(boolMap, cfgLogStreamEnable(cfg)))) goto err;

if (!(transport = createTransportJson(cfg, CFG_LS))) goto err;
cJSON_AddItemToObjectCS(root, TRANSPORT_NODE, transport);

// Represent NULL as an empty string
const char *token = cfgAuthToken(cfg);
token = (token) ? token : "";
if (!cJSON_AddStringToObjLN(root, AUTHTOKEN_NODE, token)) goto err;

return root;
err:
if(root) cJSON_Delete(root);
return NULL;
}

cJSON*
jsonObjectFromCfg(config_t* cfg)
{
cJSON* json_root = NULL;
cJSON* metric, *libscope, *event, *payload, *tags, *protocol;
cJSON *json_root = NULL;
cJSON *metric, *libscope, *event, *payload, *tags, *protocol, *cribl;

if (!(json_root = cJSON_CreateObject())) goto err;

Expand All @@ -2507,6 +2532,9 @@ jsonObjectFromCfg(config_t* cfg)
if (!(protocol = createProtocolJson(cfg))) goto err;
cJSON_AddItemToObjectCS(json_root, PROTOCOL_NODE, protocol);

if (!(cribl = createCriblJson(cfg))) goto err;
cJSON_AddItemToObjectCS(json_root, CRIBL_NODE, cribl);

return json_root;
err:
if (json_root) cJSON_Delete(json_root);
Expand Down
31 changes: 28 additions & 3 deletions test/cfgutilstest.c
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,10 @@ verifyDefaults(config_t* config)
// the protocol list should be empty too
assert_non_null (g_protlist);
assert_int_equal (g_prot_sequence, 0);

assert_int_equal (cfgLogStreamEnable(config), DEFAULT_LOGSTREAM_ENABLE);
assert_int_equal (cfgTransportType(config, CFG_LS), DEFAULT_LS_TYPE);
assert_null (cfgAuthToken(config));
}

static void
Expand Down Expand Up @@ -1223,8 +1227,14 @@ cfgReadGoodYaml(void** state)
" buffering: full\n"
" type: syslog\n"
"tags:\n"
" name1 : value1\n"
" name2 : value2\n"
" name1: value1\n"
" name2: value2\n"
"cribl:\n"
" enable: true\n"
" transport:\n"
" type: unix\n"
" path: '@abstractsock'\n"
" authtoken: ''\n"
"...\n";
const char* path = CFG_FILE_NAME;
writeFile(path, yamlText);
Expand Down Expand Up @@ -1277,6 +1287,10 @@ cfgReadGoodYaml(void** state)
assert_int_equal(cfgLogLevel(config), CFG_LOG_DEBUG);
assert_int_equal(cfgPayEnable(config), FALSE);
assert_string_equal(cfgPayDir(config), "/my/dir");
assert_int_equal (cfgLogStreamEnable(config), TRUE);
assert_int_equal (cfgTransportType(config, CFG_LS), CFG_UNIX);
assert_string_equal (cfgTransportPath(config, CFG_LS), "@abstractsock");
assert_null (cfgAuthToken(config));
cfgDestroy(&config);
lstDestroy(&g_protlist);
g_prot_sequence = 0;
Expand Down Expand Up @@ -1462,7 +1476,14 @@ const char* jsonText =
" },\n"
" 'protocol': [\n"
" {'name':'eg1','regex':'.*'}\n"
" ]\n"
" ],\n"
" 'cribl': {\n"
" 'enable': 'false',\n"
" 'transport': {\n"
" 'type': 'shm'\n"
" },\n"
" 'authtoken': 'shhdonotsharethistokenwithjustanyone'\n"
" }\n"
"}\n";

static void
Expand Down Expand Up @@ -1523,6 +1544,10 @@ cfgReadGoodJson(void** state)
assert_non_null (prot = lstFind(g_protlist, 1));
assert_string_equal(prot->protname, "eg1");

assert_int_equal (cfgLogStreamEnable(config), FALSE);
assert_int_equal (cfgTransportType(config, CFG_LS), CFG_SHM);
assert_string_equal (cfgAuthToken(config), "shhdonotsharethistokenwithjustanyone");

cfgDestroy(&config);
lstDestroy(&g_protlist);
g_prot_sequence = 0;
Expand Down

0 comments on commit 6f33adc

Please sign in to comment.