dyn-stats values are now reported (in json, json-es and cee formats) under a key "values"

This commit is contained in:
Janmejay Singh 2016-03-31 00:16:17 +05:30
parent 4594150072
commit 90019a681a
10 changed files with 101 additions and 43 deletions

View File

@ -252,6 +252,7 @@ dynstats_initNewBucketStats(dynstats_bucket_t *b) {
CHKiRet(statsobj.Construct(&b->stats));
CHKiRet(statsobj.SetOrigin(b->stats, UCHAR_CONSTANT("dynstats.bucket")));
CHKiRet(statsobj.SetName(b->stats, b->name));
CHKiRet(statsobj.SetReportingNamespace(b->stats, "values"));
statsobj.SetReadNotifier(b->stats, dynstats_readCallback, b);
CHKiRet(statsobj.ConstructFinalize(b->stats));
@ -374,6 +375,7 @@ dynstats_initCnf(dynstats_buckets_t *bkts) {
CHKiRet(statsobj.Construct(&bkts->global_stats));
CHKiRet(statsobj.SetOrigin(bkts->global_stats, UCHAR_CONSTANT("dynstats")));
CHKiRet(statsobj.SetName(bkts->global_stats, UCHAR_CONSTANT("global")));
CHKiRet(statsobj.SetReportingNamespace(bkts->global_stats, UCHAR_CONSTANT("values")));
CHKiRet(statsobj.ConstructFinalize(bkts->global_stats));
pthread_rwlock_init(&bkts->lock, NULL);

View File

@ -31,6 +31,7 @@
#include <errno.h>
#include <time.h>
#include <assert.h>
#include <json.h>
#include "rsyslog.h"
#include "unicode-helper.h"
@ -168,6 +169,14 @@ finalize_it:
RETiRet;
}
static rsRetVal
setReportingNamespace(statsobj_t *pThis, uchar *ns)
{
DEFiRet;
CHKmalloc(pThis->reporting_ns = ustrdup(ns));
finalize_it:
RETiRet;
}
/* add a counter to an object
* ctrName is duplicated, caller must free it if requried
@ -264,43 +273,89 @@ resetResettableCtr(ctr_t *pCtr, int8_t bResetCtrs)
}
}
static rsRetVal
addCtrForReporting(json_object *obj, const char* field_name, intctr_t value) {
json_object *v = NULL;
DEFiRet;
/*We should migrate libfastjson to support uint64_t in addition to int64_t.
Although no counter is likely to grow to int64 max-value, this is theoritically
incorrect (as intctr_t is uint64)*/
CHKmalloc(v = json_object_new_int64((int64_t) value));
json_object_object_add(obj, field_name, v);
finalize_it:
if (iRet != RS_RET_OK) {
if (v != NULL) {
json_object_put(v);
}
}
RETiRet;
}
static rsRetVal
addContextForReporting(json_object *obj, const char* field_name, const char* value) {
json_object *v = NULL;
DEFiRet;
CHKmalloc(v = json_object_new_string(value));
json_object_object_add(obj, field_name, v);
finalize_it:
if (iRet != RS_RET_OK) {
if (v != NULL) {
json_object_put(v);
}
}
RETiRet;
}
static intctr_t
accumulatedValue(ctr_t *pCtr) {
switch(pCtr->ctrType) {
case ctrType_IntCtr:
return *(pCtr->val.pIntCtr);
case ctrType_Int:
return *(pCtr->val.pInt);
}
return -1;
}
/* get all the object's countes together as CEE. */
static rsRetVal
getStatsLineCEE(statsobj_t *pThis, cstr_t **ppcstr, const statsFmtType_t fmt, const int8_t bResetCtrs)
{
cstr_t *pcstr;
ctr_t *pCtr;
json_object *root, *values;
DEFiRet;
root = values = NULL;
CHKiRet(cstrConstruct(&pcstr));
if (fmt == statsFmt_CEE)
rsCStrAppendStrWithLen(pcstr, UCHAR_CONSTANT("@cee: "), 6);
rsCStrAppendStrWithLen(pcstr, UCHAR_CONSTANT("{"), 1);
rsCStrAppendStrWithLen(pcstr, UCHAR_CONSTANT("\""), 1);
rsCStrAppendStrWithLen(pcstr, UCHAR_CONSTANT("name"), 4);
rsCStrAppendStrWithLen(pcstr, UCHAR_CONSTANT("\""), 1);
rsCStrAppendStrWithLen(pcstr, UCHAR_CONSTANT(":"), 1);
rsCStrAppendStrWithLen(pcstr, UCHAR_CONSTANT("\""), 1);
rsCStrAppendStr(pcstr, pThis->name);
rsCStrAppendStrWithLen(pcstr, UCHAR_CONSTANT("\""), 1);
CHKiRet(rsCStrAppendStrWithLen(pcstr, UCHAR_CONSTANT("@cee: "), 6));
CHKmalloc(root = json_object_new_object());
CHKiRet(addContextForReporting(root, "name", pThis->name));
if(pThis->origin != NULL) {
rsCStrAppendStrWithLen(pcstr, UCHAR_CONSTANT(","), 1);
rsCStrAppendStrWithLen(pcstr, UCHAR_CONSTANT("\""), 1);
rsCStrAppendStrWithLen(pcstr, UCHAR_CONSTANT("origin"), 6);
rsCStrAppendStrWithLen(pcstr, UCHAR_CONSTANT("\""), 1);
rsCStrAppendStrWithLen(pcstr, UCHAR_CONSTANT(":"), 1);
rsCStrAppendStrWithLen(pcstr, UCHAR_CONSTANT("\""), 1);
rsCStrAppendStr(pcstr, pThis->origin);
rsCStrAppendStrWithLen(pcstr, UCHAR_CONSTANT("\""), 1);
CHKiRet(addContextForReporting(root, "origin", pThis->origin));
}
if (pThis->reporting_ns == NULL) {
values = json_object_get(root);
} else {
CHKmalloc(values = json_object_new_object());
json_object_object_add(root, pThis->reporting_ns, json_object_get(values));
}
/* now add all counters to this line */
pthread_mutex_lock(&pThis->mutCtr);
for(pCtr = pThis->ctrRoot ; pCtr != NULL ; pCtr = pCtr->next) {
rsCStrAppendStrWithLen(pcstr, UCHAR_CONSTANT(",\""), 2);
if (fmt == statsFmt_JSON_ES) {
/* work-around for broken Elasticsearch JSON implementation:
* we need to replace dots by a different char, we use bang.
@ -313,29 +368,26 @@ getStatsLineCEE(statsobj_t *pThis, cstr_t **ppcstr, const statsFmtType_t fmt, co
if(*c == '.')
*c = '!';
}
rsCStrAppendStr(pcstr, esbuf);
CHKiRet(addCtrForReporting(values, esbuf, accumulatedValue(pCtr)));
} else {
rsCStrAppendStr(pcstr, pCtr->name);
}
rsCStrAppendStrWithLen(pcstr, UCHAR_CONSTANT("\""), 1);
cstrAppendChar(pcstr, ':');
switch(pCtr->ctrType) {
case ctrType_IntCtr:
rsCStrAppendInt(pcstr, *(pCtr->val.pIntCtr)); // TODO: OK?????
break;
case ctrType_Int:
rsCStrAppendInt(pcstr, *(pCtr->val.pInt));
break;
CHKiRet(addCtrForReporting(values, pCtr->name, accumulatedValue(pCtr)));
}
resetResettableCtr(pCtr, bResetCtrs);
}
pthread_mutex_unlock(&pThis->mutCtr);
cstrAppendChar(pcstr, '}');
CHKiRet(rsCStrAppendStr(pcstr, json_object_to_json_string(root)));
CHKiRet(cstrFinalize(pcstr));
*ppcstr = pcstr;
finalize_it:
if (root != NULL) {
json_object_put(root);
}
if (values != NULL) {
json_object_put(values);
}
RETiRet;
}
@ -596,6 +648,7 @@ CODESTARTobjDestruct(statsobj)
pthread_mutex_destroy(&pThis->mutCtr);
free(pThis->name);
free(pThis->origin);
free(pThis->reporting_ns);
ENDobjDestruct(statsobj)
@ -626,6 +679,7 @@ CODESTARTobjQueryInterface(statsobj)
pIf->SetName = setName;
pIf->SetOrigin = setOrigin;
pIf->SetReadNotifier = setReadNotifier;
pIf->SetReportingNamespace = setReportingNamespace;
pIf->GetAllStatsLines = getAllStatsLines;
pIf->AddCounter = addCounter;
pIf->AddManagedCounter = addManagedCounter;

View File

@ -72,6 +72,7 @@ struct statsobj_s {
BEGINobjInstance; /* Data to implement generic object - MUST be the first data element! */
uchar *name;
uchar *origin;
uchar *reporting_ns;
statsobj_read_notifier_t read_notifier;
void *read_notifier_ctx;
pthread_mutex_t mutCtr; /* to guard counter linked-list ops */
@ -98,6 +99,7 @@ BEGINinterface(statsobj) /* name must also be changed in ENDinterface macro! */
rsRetVal (*SetName)(statsobj_t *pThis, uchar *name);
rsRetVal (*SetOrigin)(statsobj_t *pThis, uchar *name); /* added v12, 2014-09-08 */
rsRetVal (*SetReadNotifier)(statsobj_t *pThis, statsobj_read_notifier_t notifier, void* ctx);
rsRetVal (*SetReportingNamespace)(statsobj_t *pThis, uchar *ns);
//rsRetVal (*GetStatsLine)(statsobj_t *pThis, cstr_t **ppcstr);
rsRetVal (*GetAllStatsLines)(rsRetVal(*cb)(void*, cstr_t*), void *usrptr, statsFmtType_t fmt, int8_t bResetCtr);
rsRetVal (*AddCounter)(statsobj_t *pThis, const uchar *ctrName, statsCtrType_t ctrType, int8_t flags, void *pCtr);

View File

@ -265,7 +265,7 @@ finalize_it:
* I optimized this function to use memcpy(), among others. Consider it a
* rewrite (which may be good to know in case of bugs) -- rgerhards, 2008-01-07
*/
rsRetVal rsCStrAppendStrWithLen(cstr_t *pThis, uchar* psz, size_t iStrLen)
rsRetVal rsCStrAppendStrWithLen(cstr_t *pThis, const uchar* psz, size_t iStrLen)
{
DEFiRet;
@ -291,7 +291,7 @@ finalize_it:
* need to change existing code.
* rgerhards, 2007-07-03
*/
rsRetVal rsCStrAppendStr(cstr_t *pThis, uchar* psz)
rsRetVal rsCStrAppendStr(cstr_t *pThis, const uchar* psz)
{
return rsCStrAppendStrWithLen(pThis, psz, strlen((char*) psz));
}

View File

@ -163,7 +163,7 @@ rsRetVal cstrTrimTrailingWhiteSpace(cstr_t *pThis);
*
* \param psz pointer to string to be appended. Must not be NULL.
*/
rsRetVal rsCStrAppendStr(cstr_t *pThis, uchar* psz);
rsRetVal rsCStrAppendStr(cstr_t *pThis, const uchar* psz);
/**
* Append a string to the buffer.
@ -171,7 +171,7 @@ rsRetVal rsCStrAppendStr(cstr_t *pThis, uchar* psz);
* \param psz pointer to string to be appended. Must not be NULL.
* \param iStrLen the length of the string pointed to by psz
*/
rsRetVal rsCStrAppendStrWithLen(cstr_t *pThis, uchar* psz, size_t iStrLen);
rsRetVal rsCStrAppendStrWithLen(cstr_t *pThis, const uchar* psz, size_t iStrLen);
/**
* Append a printf-style formated string to the buffer.

View File

@ -13,7 +13,7 @@ echo doing shutdown
. $srcdir/diag.sh shutdown-when-empty
echo wait on shutdown
. $srcdir/diag.sh wait-shutdown
. $srcdir/diag.sh custom-content-check '{"name":"global","origin":"dynstats","stats_one.ops_overflow":0,"stats_one.new_metric_add":1,"stats_one.no_metric":0,"stats_one.metrics_purged":0,"stats_one.ops_ignored":0,"stats_one.purge_triggered":0,"stats_two.ops_overflow":0,"stats_two.new_metric_add":1,"stats_two.no_metric":0,"stats_two.metrics_purged":0,"stats_two.ops_ignored":0,"stats_two.purge_triggered":0}' 'rsyslog.out.stats.log'
. $srcdir/diag.sh custom-content-check '{"name":"stats_one","origin":"dynstats.bucket","foo":1}' 'rsyslog.out.stats.log'
. $srcdir/diag.sh custom-content-check '{"name":"stats_two","origin":"dynstats.bucket","foo":1}' 'rsyslog.out.stats.log'
. $srcdir/diag.sh custom-content-check '{ "name": "global", "origin": "dynstats", "values": { "stats_one.ops_overflow": 0, "stats_one.new_metric_add": 1, "stats_one.no_metric": 0, "stats_one.metrics_purged": 0, "stats_one.ops_ignored": 0, "stats_one.purge_triggered": 0, "stats_two.ops_overflow": 0, "stats_two.new_metric_add": 1, "stats_two.no_metric": 0, "stats_two.metrics_purged": 0, "stats_two.ops_ignored": 0, "stats_two.purge_triggered": 0 } }' 'rsyslog.out.stats.log'
. $srcdir/diag.sh custom-content-check '{ "name": "stats_one", "origin": "dynstats.bucket", "values": { "foo": 1 } }' 'rsyslog.out.stats.log'
. $srcdir/diag.sh custom-content-check '{ "name": "stats_two", "origin": "dynstats.bucket", "values": { "foo": 1 } }' 'rsyslog.out.stats.log'
. $srcdir/diag.sh exit

View File

@ -10,5 +10,5 @@ echo doing shutdown
. $srcdir/diag.sh shutdown-when-empty
echo wait on shutdown
. $srcdir/diag.sh wait-shutdown
. $srcdir/diag.sh custom-content-check '{"name":"global","origin":"dynstats"}' 'rsyslog.out.stats.log'
. $srcdir/diag.sh custom-content-check '{ "name": "global", "origin": "dynstats", "values": { } }' 'rsyslog.out.stats.log'
. $srcdir/diag.sh exit

View File

@ -12,5 +12,5 @@ echo doing shutdown
. $srcdir/diag.sh shutdown-when-empty
echo wait on shutdown
. $srcdir/diag.sh wait-shutdown
. $srcdir/diag.sh custom-content-check '@cee: {"name":"an_action_that_is_never_called","origin":"core.action","processed":0,"failed":0,"suspended":0,"suspended.duration":0,"resumed":0}' 'rsyslog.out.stats.log'
. $srcdir/diag.sh custom-content-check '@cee: { "name": "an_action_that_is_never_called", "origin": "core.action", "processed": 0, "failed": 0, "suspended": 0, "suspended.duration": 0, "resumed": 0 }' 'rsyslog.out.stats.log'
. $srcdir/diag.sh exit

View File

@ -12,6 +12,6 @@ echo doing shutdown
. $srcdir/diag.sh shutdown-when-empty
echo wait on shutdown
. $srcdir/diag.sh wait-shutdown
. $srcdir/diag.sh custom-content-check '{"name":"an_action_that_is_never_called","origin":"core.action","processed":0,"failed":0,"suspended":0,"suspended!duration":0,"resumed":0}' 'rsyslog.out.stats.log'
. $srcdir/diag.sh custom-content-check '{ "name": "an_action_that_is_never_called", "origin": "core.action", "processed": 0, "failed": 0, "suspended": 0, "suspended!duration": 0, "resumed": 0 }' 'rsyslog.out.stats.log'
. $srcdir/diag.sh custom-assert-content-missing '@cee' 'rsyslog.out.stats.log'
. $srcdir/diag.sh exit

View File

@ -12,6 +12,6 @@ echo doing shutdown
. $srcdir/diag.sh shutdown-when-empty
echo wait on shutdown
. $srcdir/diag.sh wait-shutdown
. $srcdir/diag.sh custom-content-check '{"name":"an_action_that_is_never_called","origin":"core.action","processed":0,"failed":0,"suspended":0,"suspended.duration":0,"resumed":0}' 'rsyslog.out.stats.log'
. $srcdir/diag.sh custom-content-check '{ "name": "an_action_that_is_never_called", "origin": "core.action", "processed": 0, "failed": 0, "suspended": 0, "suspended.duration": 0, "resumed": 0 }' 'rsyslog.out.stats.log'
. $srcdir/diag.sh custom-assert-content-missing '@cee' 'rsyslog.out.stats.log'
. $srcdir/diag.sh exit