| 1 |
/* The statsobj object. |
| 2 |
* |
| 3 |
* This object provides a statistics-gathering facility inside rsyslog. This |
| 4 |
* functionality will be pragmatically implemented and extended. |
| 5 |
* |
| 6 |
* Copyright 2010-2021 Adiscon GmbH. |
| 7 |
* |
| 8 |
* This file is part of the rsyslog runtime library. |
| 9 |
* |
| 10 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 |
* you may not use this file except in compliance with the License. |
| 12 |
* You may obtain a copy of the License at |
| 13 |
* |
| 14 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 15 |
* -or- |
| 16 |
* see COPYING.ASL20 in the source distribution |
| 17 |
* |
| 18 |
* Unless required by applicable law or agreed to in writing, software |
| 19 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 20 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 |
* See the License for the specific language governing permissions and |
| 22 |
* limitations under the License. |
| 23 |
*/ |
| 24 |
/** |
| 25 |
* @file statsobj.c |
| 26 |
* @brief Implementation of the rsyslog statistics object |
| 27 |
* |
| 28 |
* Each statsobj_t instance maintains a name, origin and a set of counters. |
| 29 |
* All instances are linked together so that GetAllStatsLines() can iterate |
| 30 |
* through them and emit their values. Counters may be 64 bit integers or |
| 31 |
* plain ints and are modified with atomic helpers when required. |
| 32 |
* |
| 33 |
* The module can output collected counters in several formats: |
| 34 |
* - legacy key=value lines |
| 35 |
* - JSON or JSON-ES (Elasticsearch compatible) |
| 36 |
* - CEE / lumberjack records |
| 37 |
* - Prometheus text exposition |
| 38 |
* |
| 39 |
* Statistics gathering is controlled by the ::GatherStats flag and can be |
| 40 |
* enabled via EnableStats(). |
| 41 |
*/ |
| 42 |
|
| 43 |
#include "config.h" |
| 44 |
#include <stdio.h> |
| 45 |
#include <stdlib.h> |
| 46 |
#include <stdarg.h> |
| 47 |
#include <inttypes.h> |
| 48 |
#include <pthread.h> |
| 49 |
#include <errno.h> |
| 50 |
#include <time.h> |
| 51 |
#include <assert.h> |
| 52 |
#include <json.h> |
| 53 |
|
| 54 |
#include "rsyslog.h" |
| 55 |
#include "unicode-helper.h" |
| 56 |
#include "obj.h" |
| 57 |
#include "statsobj.h" |
| 58 |
#include "srUtils.h" |
| 59 |
#include "stringbuf.h" |
| 60 |
#include "errmsg.h" |
| 61 |
#include "hashtable.h" |
| 62 |
#include "hashtable_itr.h" |
| 63 |
#include "rsconf.h" |
| 64 |
|
| 65 |
|
| 66 |
/* externally-visiable data (see statsobj.h for explanation) */ |
| 67 |
int GatherStats = 0; |
| 68 |
|
| 69 |
/* static data */ |
| 70 |
DEFobjStaticHelpers; |
| 71 |
|
| 72 |
/* doubly linked list of stats objects. Object is automatically linked to it |
| 73 |
* upon construction. Enqueue always happens at the front (simplifies logic). |
| 74 |
*/ |
| 75 |
static statsobj_t *objRoot = NULL; |
| 76 |
static statsobj_t *objLast = NULL; |
| 77 |
|
| 78 |
static pthread_mutex_t mutStats; |
| 79 |
static pthread_mutex_t mutSenders; |
| 80 |
|
| 81 |
static struct hashtable *stats_senders = NULL; |
| 82 |
|
| 83 |
/* ------------------------------ statsobj linked list maintenance ------------------------------ */ |
| 84 |
|
| 85 |
static void addToObjList(statsobj_t *pThis) { |
| 86 |
pthread_mutex_lock(&mutStats); |
| 87 |
if (pThis->flags && STATSOBJ_FLAG_DO_PREPEND) { |
| 88 |
pThis->next = objRoot; |
| 89 |
if (objRoot != NULL) { |
| 90 |
objRoot->prev = pThis; |
| 91 |
} |
| 92 |
objRoot = pThis; |
| 93 |
if (objLast == NULL) objLast = pThis; |
| 94 |
} else { |
| 95 |
pThis->prev = objLast; |
| 96 |
if (objLast != NULL) objLast->next = pThis; |
| 97 |
objLast = pThis; |
| 98 |
if (objRoot == NULL) objRoot = pThis; |
| 99 |
} |
| 100 |
pthread_mutex_unlock(&mutStats); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
static void removeFromObjList(statsobj_t *pThis) { |
| 105 |
pthread_mutex_lock(&mutStats); |
| 106 |
if (pThis->prev != NULL) pThis->prev->next = pThis->next; |
| 107 |
if (pThis->next != NULL) pThis->next->prev = pThis->prev; |
| 108 |
if (objLast == pThis) objLast = pThis->prev; |
| 109 |
if (objRoot == pThis) objRoot = pThis->next; |
| 110 |
pthread_mutex_unlock(&mutStats); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
static void addCtrToList(statsobj_t *pThis, ctr_t *pCtr) { |
| 115 |
pthread_mutex_lock(&pThis->mutCtr); |
| 116 |
pCtr->prev = pThis->ctrLast; |
| 117 |
if (pThis->ctrLast != NULL) pThis->ctrLast->next = pCtr; |
| 118 |
pThis->ctrLast = pCtr; |
| 119 |
if (pThis->ctrRoot == NULL) pThis->ctrRoot = pCtr; |
| 120 |
pthread_mutex_unlock(&pThis->mutCtr); |
| 121 |
} |
| 122 |
|
| 123 |
/* ------------------------------ methods ------------------------------ */ |
| 124 |
|
| 125 |
|
| 126 |
/* Standard-Constructor |
| 127 |
*/ |
| 128 |
BEGINobjConstruct(statsobj) /* be sure to specify the object type also in END macro! */ |
| 129 |
pthread_mutex_init(&pThis->mutCtr, NULL); |
| 130 |
pThis->ctrLast = NULL; |
| 131 |
pThis->ctrRoot = NULL; |
| 132 |
pThis->read_notifier = NULL; |
| 133 |
pThis->flags = 0; |
| 134 |
ENDobjConstruct(statsobj) |
| 135 |
|
| 136 |
|
| 137 |
/* ConstructionFinalizer |
| 138 |
*/ |
| 139 |
static rsRetVal statsobjConstructFinalize(statsobj_t *pThis) { |
| 140 |
DEFiRet; |
| 141 |
ISOBJ_TYPE_assert(pThis, statsobj); |
| 142 |
addToObjList(pThis); |
| 143 |
RETiRet; |
| 144 |
} |
| 145 |
|
| 146 |
/* set read_notifier (a function which is invoked after stats are read). |
| 147 |
*/ |
| 148 |
static rsRetVal setReadNotifier(statsobj_t *pThis, statsobj_read_notifier_t notifier, void *ctx) { |
| 149 |
DEFiRet; |
| 150 |
pThis->read_notifier = notifier; |
| 151 |
pThis->read_notifier_ctx = ctx; |
| 152 |
RETiRet; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
/* set origin (module name, etc). |
| 157 |
* Note that we make our own copy of the memory, caller is |
| 158 |
* responsible to free up name it passes in (if required). |
| 159 |
*/ |
| 160 |
static rsRetVal setOrigin(statsobj_t *pThis, uchar *origin) { |
| 161 |
DEFiRet; |
| 162 |
CHKmalloc(pThis->origin = ustrdup(origin)); |
| 163 |
finalize_it: |
| 164 |
RETiRet; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
/* set name. Note that we make our own copy of the memory, caller is |
| 169 |
* responsible to free up name it passes in (if required). |
| 170 |
*/ |
| 171 |
static rsRetVal setName(statsobj_t *pThis, uchar *name) { |
| 172 |
DEFiRet; |
| 173 |
CHKmalloc(pThis->name = ustrdup(name)); |
| 174 |
finalize_it: |
| 175 |
RETiRet; |
| 176 |
} |
| 177 |
|
| 178 |
static void setStatsObjFlags(statsobj_t *pThis, int flags) { |
| 179 |
pThis->flags = flags; |
| 180 |
} |
| 181 |
|
| 182 |
static rsRetVal setReportingNamespace(statsobj_t *pThis, uchar *ns) { |
| 183 |
DEFiRet; |
| 184 |
CHKmalloc(pThis->reporting_ns = ustrdup(ns)); |
| 185 |
finalize_it: |
| 186 |
RETiRet; |
| 187 |
} |
| 188 |
|
| 189 |
/* add a counter to an object |
| 190 |
* ctrName is duplicated, caller must free it if requried |
| 191 |
* NOTE: The counter is READ-ONLY and MUST NOT be modified (most |
| 192 |
* importantly, it must not be initialized, so the caller must |
| 193 |
* ensure the counter is properly initialized before AddCounter() |
| 194 |
* is called. |
| 195 |
*/ |
| 196 |
static rsRetVal addManagedCounter(statsobj_t *pThis, |
| 197 |
const uchar *ctrName, |
| 198 |
statsCtrType_t ctrType, |
| 199 |
int8_t flags, |
| 200 |
void *pCtr, |
| 201 |
ctr_t **entryRef, |
| 202 |
int8_t linked) { |
| 203 |
ctr_t *ctr; |
| 204 |
DEFiRet; |
| 205 |
|
| 206 |
*entryRef = NULL; |
| 207 |
|
| 208 |
CHKmalloc(ctr = calloc(1, sizeof(ctr_t))); |
| 209 |
ctr->next = NULL; |
| 210 |
ctr->prev = NULL; |
| 211 |
if ((ctr->name = ustrdup(ctrName)) == NULL) { |
| 212 |
DBGPRINTF("addCounter: OOM in strdup()\n"); |
| 213 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 214 |
} |
| 215 |
ctr->flags = flags; |
| 216 |
ctr->ctrType = ctrType; |
| 217 |
switch (ctrType) { |
| 218 |
case ctrType_IntCtr: |
| 219 |
ctr->val.pIntCtr = (intctr_t *)pCtr; |
| 220 |
break; |
| 221 |
case ctrType_Int: |
| 222 |
ctr->val.pInt = (int *)pCtr; |
| 223 |
break; |
| 224 |
default: |
| 225 |
// No action needed for other cases |
| 226 |
break; |
| 227 |
} |
| 228 |
if (linked) { |
| 229 |
addCtrToList(pThis, ctr); |
| 230 |
} |
| 231 |
*entryRef = ctr; |
| 232 |
|
| 233 |
finalize_it: |
| 234 |
if (iRet != RS_RET_OK) { |
| 235 |
if (ctr != NULL) { |
| 236 |
free(ctr->name); |
| 237 |
free(ctr); |
| 238 |
} |
| 239 |
} |
| 240 |
RETiRet; |
| 241 |
} |
| 242 |
|
| 243 |
static void addPreCreatedCounter(statsobj_t *pThis, ctr_t *pCtr) { |
| 244 |
pCtr->next = NULL; |
| 245 |
pCtr->prev = NULL; |
| 246 |
addCtrToList(pThis, pCtr); |
| 247 |
} |
| 248 |
|
| 249 |
static rsRetVal addCounter(statsobj_t *pThis, const uchar *ctrName, statsCtrType_t ctrType, int8_t flags, void *pCtr) { |
| 250 |
ctr_t *ctr; |
| 251 |
DEFiRet; |
| 252 |
iRet = addManagedCounter(pThis, ctrName, ctrType, flags, pCtr, &ctr, 1); |
| 253 |
RETiRet; |
| 254 |
} |
| 255 |
|
| 256 |
static void destructUnlinkedCounter(ctr_t *ctr) { |
| 257 |
free(ctr->name); |
| 258 |
free(ctr); |
| 259 |
} |
| 260 |
|
| 261 |
static void destructCounter(statsobj_t *pThis, ctr_t *pCtr) { |
| 262 |
pthread_mutex_lock(&pThis->mutCtr); |
| 263 |
if (pCtr->prev != NULL) { |
| 264 |
pCtr->prev->next = pCtr->next; |
| 265 |
} |
| 266 |
if (pCtr->next != NULL) { |
| 267 |
pCtr->next->prev = pCtr->prev; |
| 268 |
} |
| 269 |
if (pThis->ctrLast == pCtr) { |
| 270 |
pThis->ctrLast = pCtr->prev; |
| 271 |
} |
| 272 |
if (pThis->ctrRoot == pCtr) { |
| 273 |
pThis->ctrRoot = pCtr->next; |
| 274 |
} |
| 275 |
pthread_mutex_unlock(&pThis->mutCtr); |
| 276 |
destructUnlinkedCounter(pCtr); |
| 277 |
} |
| 278 |
|
| 279 |
static void resetResettableCtr(ctr_t *pCtr, int8_t bResetCtrs) { |
| 280 |
if ((bResetCtrs && (pCtr->flags & CTR_FLAG_RESETTABLE)) || (pCtr->flags & CTR_FLAG_MUST_RESET)) { |
| 281 |
switch (pCtr->ctrType) { |
| 282 |
case ctrType_IntCtr: |
| 283 |
*(pCtr->val.pIntCtr) = 0; |
| 284 |
break; |
| 285 |
case ctrType_Int: |
| 286 |
*(pCtr->val.pInt) = 0; |
| 287 |
break; |
| 288 |
default: |
| 289 |
// No action needed for other cases |
| 290 |
break; |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
static rsRetVal addCtrForReporting(json_object *to, const uchar *field_name, intctr_t value) { |
| 296 |
json_object *v; |
| 297 |
DEFiRet; |
| 298 |
|
| 299 |
/*We should migrate libfastjson to support uint64_t in addition to int64_t. |
| 300 |
Although no counter is likely to grow to int64 max-value, this is theoritically |
| 301 |
incorrect (as intctr_t is uint64)*/ |
| 302 |
CHKmalloc(v = json_object_new_int64((int64_t)value)); |
| 303 |
|
| 304 |
json_object_object_add(to, (const char *)field_name, v); |
| 305 |
finalize_it: |
| 306 |
/* v cannot be NULL in error case, as this would only happen during malloc fail, |
| 307 |
* which itself sets it to NULL -- so not doing cleanup here. |
| 308 |
*/ |
| 309 |
RETiRet; |
| 310 |
} |
| 311 |
|
| 312 |
static rsRetVal addContextForReporting(json_object *to, const uchar *field_name, const uchar *value) { |
| 313 |
json_object *v; |
| 314 |
DEFiRet; |
| 315 |
|
| 316 |
CHKmalloc(v = json_object_new_string((const char *)value)); |
| 317 |
|
| 318 |
json_object_object_add(to, (const char *)field_name, v); |
| 319 |
finalize_it: |
| 320 |
RETiRet; |
| 321 |
} |
| 322 |
|
| 323 |
static intctr_t accumulatedValue(ctr_t *pCtr) { |
| 324 |
switch (pCtr->ctrType) { |
| 325 |
case ctrType_IntCtr: |
| 326 |
return *(pCtr->val.pIntCtr); |
| 327 |
case ctrType_Int: |
| 328 |
return *(pCtr->val.pInt); |
| 329 |
default: |
| 330 |
// No action needed for other cases |
| 331 |
break; |
| 332 |
} |
| 333 |
return -1; |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
/* get all the object's countes together as CEE. */ |
| 338 |
static rsRetVal getStatsLineCEE(statsobj_t *pThis, cstr_t **ppcstr, const statsFmtType_t fmt, const int8_t bResetCtrs) { |
| 339 |
cstr_t *pcstr = NULL; |
| 340 |
ctr_t *pCtr; |
| 341 |
json_object *root, *values; |
| 342 |
int locked = 0; |
| 343 |
DEFiRet; |
| 344 |
|
| 345 |
root = values = NULL; |
| 346 |
|
| 347 |
CHKiRet(cstrConstruct(&pcstr)); |
| 348 |
|
| 349 |
if (fmt == statsFmt_CEE) |
| 350 |
CHKiRet(rsCStrAppendStrWithLen(pcstr, UCHAR_CONSTANT(CONST_CEE_COOKIE " "), CONST_LEN_CEE_COOKIE + 1)); |
| 351 |
|
| 352 |
CHKmalloc(root = json_object_new_object()); |
| 353 |
|
| 354 |
CHKiRet(addContextForReporting(root, UCHAR_CONSTANT("name"), pThis->name)); |
| 355 |
|
| 356 |
if (pThis->origin != NULL) { |
| 357 |
CHKiRet(addContextForReporting(root, UCHAR_CONSTANT("origin"), pThis->origin)); |
| 358 |
} |
| 359 |
|
| 360 |
if (pThis->reporting_ns == NULL) { |
| 361 |
values = json_object_get(root); |
| 362 |
} else { |
| 363 |
CHKmalloc(values = json_object_new_object()); |
| 364 |
json_object_object_add(root, (const char *)pThis->reporting_ns, json_object_get(values)); |
| 365 |
} |
| 366 |
|
| 367 |
/* now add all counters to this line */ |
| 368 |
pthread_mutex_lock(&pThis->mutCtr); |
| 369 |
locked = 1; |
| 370 |
for (pCtr = pThis->ctrRoot; pCtr != NULL; pCtr = pCtr->next) { |
| 371 |
if (fmt == statsFmt_JSON_ES) { |
| 372 |
/* work-around for broken Elasticsearch JSON implementation: |
| 373 |
* we need to replace dots by a different char, we use bang. |
| 374 |
* Note: ES 2.0 does not longer accept dot in name |
| 375 |
*/ |
| 376 |
uchar esbuf[256]; |
| 377 |
strncpy((char *)esbuf, (char *)pCtr->name, sizeof(esbuf) - 1); |
| 378 |
esbuf[sizeof(esbuf) - 1] = '\0'; |
| 379 |
for (uchar *c = esbuf; *c; ++c) { |
| 380 |
if (*c == '.') *c = '!'; |
| 381 |
} |
| 382 |
CHKiRet(addCtrForReporting(values, esbuf, accumulatedValue(pCtr))); |
| 383 |
} else { |
| 384 |
CHKiRet(addCtrForReporting(values, pCtr->name, accumulatedValue(pCtr))); |
| 385 |
} |
| 386 |
resetResettableCtr(pCtr, bResetCtrs); |
| 387 |
} |
| 388 |
pthread_mutex_unlock(&pThis->mutCtr); |
| 389 |
locked = 0; |
| 390 |
CHKiRet(rsCStrAppendStr(pcstr, (const uchar *)json_object_to_json_string(root))); |
| 391 |
|
| 392 |
cstrFinalize(pcstr); |
| 393 |
*ppcstr = pcstr; |
| 394 |
pcstr = NULL; |
| 395 |
|
| 396 |
finalize_it: |
| 397 |
if (locked) { |
| 398 |
pthread_mutex_unlock(&pThis->mutCtr); |
| 399 |
} |
| 400 |
|
| 401 |
if (pcstr != NULL) { |
| 402 |
cstrDestruct(&pcstr); |
| 403 |
} |
| 404 |
if (root != NULL) { |
| 405 |
json_object_put(root); |
| 406 |
} |
| 407 |
if (values != NULL) { |
| 408 |
json_object_put(values); |
| 409 |
} |
| 410 |
|
| 411 |
RETiRet; |
| 412 |
} |
| 413 |
|
| 414 |
/* get all the object's countes together with object name as one line. |
| 415 |
*/ |
| 416 |
static rsRetVal getStatsLine(statsobj_t *pThis, cstr_t **ppcstr, int8_t bResetCtrs) { |
| 417 |
cstr_t *pcstr; |
| 418 |
ctr_t *pCtr; |
| 419 |
DEFiRet; |
| 420 |
|
| 421 |
CHKiRet(cstrConstruct(&pcstr)); |
| 422 |
rsCStrAppendStr(pcstr, pThis->name); |
| 423 |
rsCStrAppendStrWithLen(pcstr, UCHAR_CONSTANT(": "), 2); |
| 424 |
|
| 425 |
if (pThis->origin != NULL) { |
| 426 |
rsCStrAppendStrWithLen(pcstr, UCHAR_CONSTANT("origin="), 7); |
| 427 |
rsCStrAppendStr(pcstr, pThis->origin); |
| 428 |
cstrAppendChar(pcstr, ' '); |
| 429 |
} |
| 430 |
|
| 431 |
/* now add all counters to this line */ |
| 432 |
pthread_mutex_lock(&pThis->mutCtr); |
| 433 |
for (pCtr = pThis->ctrRoot; pCtr != NULL; pCtr = pCtr->next) { |
| 434 |
rsCStrAppendStr(pcstr, pCtr->name); |
| 435 |
cstrAppendChar(pcstr, '='); |
| 436 |
switch (pCtr->ctrType) { |
| 437 |
case ctrType_IntCtr: |
| 438 |
rsCStrAppendInt(pcstr, *(pCtr->val.pIntCtr)); // TODO: OK????? |
| 439 |
break; |
| 440 |
case ctrType_Int: |
| 441 |
rsCStrAppendInt(pcstr, *(pCtr->val.pInt)); |
| 442 |
break; |
| 443 |
default: |
| 444 |
// No action needed for other cases |
| 445 |
break; |
| 446 |
} |
| 447 |
cstrAppendChar(pcstr, ' '); |
| 448 |
resetResettableCtr(pCtr, bResetCtrs); |
| 449 |
} |
| 450 |
pthread_mutex_unlock(&pThis->mutCtr); |
| 451 |
|
| 452 |
cstrFinalize(pcstr); |
| 453 |
*ppcstr = pcstr; |
| 454 |
|
| 455 |
finalize_it: |
| 456 |
RETiRet; |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
/* this function obtains all sender stats. hlper to getAllStatsLines() |
| 461 |
* We need to keep this looked to avoid resizing of the hash table |
| 462 |
* (what could otherwise cause a segfault). |
| 463 |
*/ |
| 464 |
static void getSenderStats(rsRetVal (*cb)(void *, const char *), |
| 465 |
void *usrptr, |
| 466 |
statsFmtType_t fmt, |
| 467 |
const int8_t bResetCtrs) { |
| 468 |
struct hashtable_itr *itr = NULL; |
| 469 |
struct sender_stats *stat; |
| 470 |
char fmtbuf[2048]; |
| 471 |
|
| 472 |
pthread_mutex_lock(&mutSenders); |
| 473 |
|
| 474 |
/* Iterator constructor only returns a valid iterator if |
| 475 |
* the hashtable is not empty |
| 476 |
*/ |
| 477 |
if (hashtable_count(stats_senders) > 0) { |
| 478 |
itr = hashtable_iterator(stats_senders); |
| 479 |
do { |
| 480 |
stat = (struct sender_stats *)hashtable_iterator_value(itr); |
| 481 |
if (fmt == statsFmt_Legacy) { |
| 482 |
snprintf(fmtbuf, sizeof(fmtbuf), "_sender_stat: sender=%s messages=%" PRIu64, stat->sender, |
| 483 |
stat->nMsgs); |
| 484 |
} else { |
| 485 |
snprintf(fmtbuf, sizeof(fmtbuf), |
| 486 |
"{ \"name\":\"_sender_stat\", " |
| 487 |
"\"origin\":\"impstats\", " |
| 488 |
"\"sender\":\"%s\", \"messages\":%" PRIu64 "}", |
| 489 |
stat->sender, stat->nMsgs); |
| 490 |
} |
| 491 |
fmtbuf[sizeof(fmtbuf) - 1] = '\0'; |
| 492 |
cb(usrptr, fmtbuf); |
| 493 |
if (bResetCtrs) stat->nMsgs = 0; |
| 494 |
} while (hashtable_iterator_advance(itr)); |
| 495 |
} |
| 496 |
|
| 497 |
free(itr); |
| 498 |
pthread_mutex_unlock(&mutSenders); |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
/** |
| 503 |
* Helper: For a single statsobj_t (named o->name), iterate its counters |
| 504 |
* and emit Prometheus lines via cb. We generate, for each counter: |
| 505 |
* # HELP <obj>_<ctr> Generic help: "<origin> object, counter <ctr>" |
| 506 |
* # TYPE <obj>_<ctr> counter |
| 507 |
* <obj>_<ctr> <value> |
| 508 |
* |
| 509 |
* If bResetCtrs=TRUE and the counter has CTR_FLAG_RESETTABLE, zero it after reading. |
| 510 |
* |
| 511 |
* Note: by rsyslog stats subsystem design decision, read and write counters is racy |
| 512 |
* because we need the performance. It is OK that the counters in question are |
| 513 |
* not 100% precise. |
| 514 |
*/ |
| 515 |
static ATTR_NO_SANITIZE_THREAD rsRetVal emitPrometheusForObject(statsobj_t *o, |
| 516 |
rsRetVal (*cb)(void *, const char *), |
| 517 |
void *usrptr, |
| 518 |
int8_t bResetCtrs) { |
| 519 |
ctr_t *pCtr; |
| 520 |
char linebuf[512]; |
| 521 |
int len; |
| 522 |
uint64_t value; |
| 523 |
const char *objName = (const char *)o->name; |
| 524 |
const char *origin = o->origin ? (const char *)o->origin : ""; |
| 525 |
|
| 526 |
/* Iterate each counter in o->ctrRoot. Lock while walking the linked list. */ |
| 527 |
pthread_mutex_lock(&o->mutCtr); |
| 528 |
for (pCtr = o->ctrRoot; pCtr != NULL; pCtr = pCtr->next) { |
| 529 |
/* 1) Read the current accumulated value. Might be IntCtr or Int. */ |
| 530 |
switch (pCtr->ctrType) { |
| 531 |
case ctrType_IntCtr: |
| 532 |
value = *(pCtr->val.pIntCtr); |
| 533 |
break; |
| 534 |
case ctrType_Int: |
| 535 |
value = (uint64_t)(*(pCtr->val.pInt)); |
| 536 |
break; |
| 537 |
default: |
| 538 |
value = 0; |
| 539 |
break; |
| 540 |
} |
| 541 |
|
| 542 |
/* 2) Optionally reset if requested and allowed. */ |
| 543 |
if ((bResetCtrs && (pCtr->flags & CTR_FLAG_RESETTABLE)) || (pCtr->flags & CTR_FLAG_MUST_RESET)) { |
| 544 |
switch (pCtr->ctrType) { |
| 545 |
case ctrType_IntCtr: |
| 546 |
*(pCtr->val.pIntCtr) = 0; |
| 547 |
break; |
| 548 |
case ctrType_Int: |
| 549 |
*(pCtr->val.pInt) = 0; |
| 550 |
break; |
| 551 |
default: |
| 552 |
break; |
| 553 |
} |
| 554 |
} |
| 555 |
pthread_mutex_unlock(&o->mutCtr); |
| 556 |
|
| 557 |
/* 3) Build the metric name: "<object>_<counter>_total" */ |
| 558 |
/* It is conventional in Prometheus to append "_total" to counters. */ |
| 559 |
len = snprintf(linebuf, sizeof(linebuf), |
| 560 |
"# HELP %s_%s_total rsyslog stats: origin=\"%s\" object=\"%s\", counter=\"%s\"\n" |
| 561 |
"# TYPE %s_%s_total counter\n" |
| 562 |
"%s_%s_total %llu\n", |
| 563 |
/* HELP */ objName, pCtr->name, origin, objName, pCtr->name, |
| 564 |
/* TYPE */ objName, pCtr->name, |
| 565 |
/* VALUE */ objName, pCtr->name, (unsigned long long)value); |
| 566 |
if (len < 0 || len >= (int)sizeof(linebuf)) { |
| 567 |
/* In case our buffer is too small, just skip emitting this counter. */ |
| 568 |
} else { |
| 569 |
/* Emit this chunk (all three lines) in one callback */ |
| 570 |
cb(usrptr, linebuf); |
| 571 |
} |
| 572 |
|
| 573 |
/* Acquire the lock again before advancing to the next counter */ |
| 574 |
pthread_mutex_lock(&o->mutCtr); |
| 575 |
} |
| 576 |
pthread_mutex_unlock(&o->mutCtr); |
| 577 |
return RS_RET_OK; |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
static rsRetVal generatePrometheusStats(rsRetVal (*cb)(void *, const char *), void *usrptr, int8_t bResetCtrs) { |
| 582 |
statsobj_t *o; |
| 583 |
DEFiRet; |
| 584 |
|
| 585 |
/* For each statsobj in our linked list, emit Prometheus lines. */ |
| 586 |
for (o = objRoot; o != NULL; o = o->next) { |
| 587 |
CHKiRet(emitPrometheusForObject(o, cb, usrptr, bResetCtrs)); |
| 588 |
/* If the object has a read_notifier, call it now */ |
| 589 |
if (o->read_notifier != NULL) { |
| 590 |
o->read_notifier(o, o->read_notifier_ctx); |
| 591 |
} |
| 592 |
} |
| 593 |
/* Optionally, handle sender stats as additional metrics: |
| 594 |
* e.g. emit "rsyslog_sender_<sender> <nMsgs>" lines. |
| 595 |
* For simplicity, we skip this, or you can extend similarly. */ |
| 596 |
finalize_it: |
| 597 |
RETiRet; |
| 598 |
} |
| 599 |
|
| 600 |
/* this function can be used to obtain all stats lines. In this case, |
| 601 |
* a callback must be provided. This module than iterates over all objects and |
| 602 |
* submits each stats line to the callback. The callback has two parameters: |
| 603 |
* the first one is a caller-provided void*, the second one the cstr_t with the |
| 604 |
* line. If the callback reports an error, processing is stopped. |
| 605 |
*/ |
| 606 |
static rsRetVal getAllStatsLines(rsRetVal (*cb)(void *, const char *), |
| 607 |
void *const usrptr, |
| 608 |
statsFmtType_t fmt, |
| 609 |
const int8_t bResetCtrs) { |
| 610 |
statsobj_t *o; |
| 611 |
cstr_t *cstr = NULL; |
| 612 |
DEFiRet; |
| 613 |
|
| 614 |
|
| 615 |
if (fmt == statsFmt_Prometheus) { |
| 616 |
CHKiRet(generatePrometheusStats(cb, usrptr, bResetCtrs)); |
| 617 |
FINALIZE; |
| 618 |
} |
| 619 |
|
| 620 |
for (o = objRoot; o != NULL; o = o->next) { |
| 621 |
switch (fmt) { |
| 622 |
case statsFmt_Legacy: |
| 623 |
CHKiRet(getStatsLine(o, &cstr, bResetCtrs)); |
| 624 |
break; |
| 625 |
case statsFmt_CEE: |
| 626 |
case statsFmt_JSON: |
| 627 |
case statsFmt_JSON_ES: |
| 628 |
CHKiRet(getStatsLineCEE(o, &cstr, fmt, bResetCtrs)); |
| 629 |
break; |
| 630 |
case statsFmt_Prometheus: |
| 631 |
/* already handled above */ |
| 632 |
break; |
| 633 |
default: |
| 634 |
// No action needed for other cases |
| 635 |
break; |
| 636 |
} |
| 637 |
CHKiRet(cb(usrptr, (const char *)cstrGetSzStrNoNULL(cstr))); |
| 638 |
rsCStrDestruct(&cstr); |
| 639 |
if (o->read_notifier != NULL) { |
| 640 |
o->read_notifier(o, o->read_notifier_ctx); |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
getSenderStats(cb, usrptr, fmt, bResetCtrs); |
| 645 |
|
| 646 |
finalize_it: |
| 647 |
if (cstr != NULL) { |
| 648 |
rsCStrDestruct(&cstr); |
| 649 |
} |
| 650 |
RETiRet; |
| 651 |
} |
| 652 |
|
| 653 |
/** |
| 654 |
* getAllCounters() - Native counter iteration API |
| 655 |
* |
| 656 |
* Iterates through all statsobj instances and their counters, invoking the callback |
| 657 |
* with raw counter values. This eliminates text serialization/parsing overhead. |
| 658 |
* |
| 659 |
* @param cb Callback function invoked for each counter |
| 660 |
* @param ctx User context passed to callback |
| 661 |
* @return RS_RET_OK on success, error code on failure |
| 662 |
*/ |
| 663 |
static rsRetVal getAllCounters(statsobj_counter_cb_t cb, void *ctx) { |
| 664 |
DEFiRet; |
| 665 |
statsobj_t *o; |
| 666 |
ctr_t *ctr; |
| 667 |
|
| 668 |
if (cb == NULL) { |
| 669 |
ABORT_FINALIZE(RS_RET_PARAM_ERROR); |
| 670 |
} |
| 671 |
|
| 672 |
/* Iterate through all statsobj instances */ |
| 673 |
for (o = objRoot; o != NULL; o = o->next) { |
| 674 |
/* Iterate through all counters in this object */ |
| 675 |
pthread_mutex_lock(&o->mutCtr); |
| 676 |
for (ctr = o->ctrRoot; ctr != NULL; ctr = ctr->next) { |
| 677 |
uint64_t value; |
| 678 |
|
| 679 |
/* Read counter value based on type */ |
| 680 |
switch (ctr->ctrType) { |
| 681 |
case ctrType_IntCtr: |
| 682 |
/* Atomic uint64 - safe to read directly */ |
| 683 |
value = *(ctr->val.pIntCtr); |
| 684 |
break; |
| 685 |
|
| 686 |
case ctrType_Int: |
| 687 |
/* Plain int - read without lock. This matches GetAllStatsLines() |
| 688 |
* behavior. Value may be stale but acceptable for monitoring. |
| 689 |
* Most ctrType_Int counters are gauges (queue size, open files) |
| 690 |
* protected by application-level mutexes. */ |
| 691 |
value = (uint64_t)(*(ctr->val.pInt)); |
| 692 |
break; |
| 693 |
|
| 694 |
default: |
| 695 |
value = 0; |
| 696 |
break; |
| 697 |
} |
| 698 |
|
| 699 |
/* Invoke callback with counter metadata and value. |
| 700 |
* Keep mutCtr locked to prevent list modification during iteration. |
| 701 |
* Callback must not call back into statsobj or deadlock may occur. */ |
| 702 |
rsRetVal localRet = cb(ctx, o->name, o->origin, ctr->name, ctr->ctrType, value, ctr->flags); |
| 703 |
|
| 704 |
if (localRet != RS_RET_OK) { |
| 705 |
pthread_mutex_unlock(&o->mutCtr); |
| 706 |
ABORT_FINALIZE(localRet); |
| 707 |
} |
| 708 |
} |
| 709 |
pthread_mutex_unlock(&o->mutCtr); |
| 710 |
|
| 711 |
/* Call read notifier after counters are read (e.g., for percentile stats) */ |
| 712 |
if (o->read_notifier != NULL) { |
| 713 |
o->read_notifier(o, o->read_notifier_ctx); |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
finalize_it: |
| 718 |
RETiRet; |
| 719 |
} |
| 720 |
|
| 721 |
/* Enable statistics gathering. currently there is no function to disable it |
| 722 |
* again, as this is right now not needed. |
| 723 |
*/ |
| 724 |
static rsRetVal enableStats(void) { |
| 725 |
GatherStats = 1; |
| 726 |
return RS_RET_OK; |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
rsRetVal statsRecordSender(const uchar *sender, unsigned nMsgs, time_t lastSeen) { |
| 731 |
struct sender_stats *stat; |
| 732 |
int mustUnlock = 0; |
| 733 |
DEFiRet; |
| 734 |
|
| 735 |
if (stats_senders == NULL) FINALIZE; /* unlikely: we could not init our hash table */ |
| 736 |
|
| 737 |
pthread_mutex_lock(&mutSenders); |
| 738 |
mustUnlock = 1; |
| 739 |
stat = hashtable_search(stats_senders, (void *)sender); |
| 740 |
if (stat == NULL) { |
| 741 |
DBGPRINTF("statsRecordSender: sender '%s' not found, adding\n", sender); |
| 742 |
CHKmalloc(stat = calloc(1, sizeof(struct sender_stats))); |
| 743 |
stat->sender = (const uchar *)strdup((const char *)sender); |
| 744 |
stat->nMsgs = 0; |
| 745 |
if (runConf->globals.reportNewSenders) { |
| 746 |
LogMsg(0, RS_RET_SENDER_APPEARED, LOG_INFO, "new sender '%s'", stat->sender); |
| 747 |
} |
| 748 |
if (hashtable_insert(stats_senders, (void *)stat->sender, (void *)stat) == 0) { |
| 749 |
LogError(errno, RS_RET_INTERNAL_ERROR, |
| 750 |
"error inserting sender '%s' into sender " |
| 751 |
"hash table", |
| 752 |
sender); |
| 753 |
ABORT_FINALIZE(RS_RET_INTERNAL_ERROR); |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
stat->nMsgs += nMsgs; |
| 758 |
stat->lastSeen = lastSeen; |
| 759 |
DBGPRINTF("DDDDD: statsRecordSender: '%s', nmsgs %u [%llu], lastSeen %llu\n", sender, nMsgs, |
| 760 |
(long long unsigned)stat->nMsgs, (long long unsigned)lastSeen); |
| 761 |
|
| 762 |
finalize_it: |
| 763 |
if (mustUnlock) pthread_mutex_unlock(&mutSenders); |
| 764 |
RETiRet; |
| 765 |
} |
| 766 |
|
| 767 |
static ctr_t *unlinkAllCounters(statsobj_t *pThis) { |
| 768 |
ctr_t *ctr; |
| 769 |
pthread_mutex_lock(&pThis->mutCtr); |
| 770 |
ctr = pThis->ctrRoot; |
| 771 |
pThis->ctrLast = NULL; |
| 772 |
pThis->ctrRoot = NULL; |
| 773 |
pthread_mutex_unlock(&pThis->mutCtr); |
| 774 |
return ctr; |
| 775 |
} |
| 776 |
|
| 777 |
static void destructUnlinkedCounters(ctr_t *ctr) { |
| 778 |
ctr_t *ctrToDel; |
| 779 |
|
| 780 |
while (ctr != NULL) { |
| 781 |
ctrToDel = ctr; |
| 782 |
ctr = ctr->next; |
| 783 |
destructUnlinkedCounter(ctrToDel); |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
/* check if a sender has not sent info to us for an extended period |
| 788 |
* of time. |
| 789 |
*/ |
| 790 |
void checkGoneAwaySenders(const time_t tCurr) { |
| 791 |
struct hashtable_itr *itr = NULL; |
| 792 |
struct sender_stats *stat; |
| 793 |
const time_t rqdLast = tCurr - runConf->globals.senderStatsTimeout; |
| 794 |
struct tm tm; |
| 795 |
|
| 796 |
pthread_mutex_lock(&mutSenders); |
| 797 |
|
| 798 |
/* Iterator constructor only returns a valid iterator if |
| 799 |
* the hashtable is not empty |
| 800 |
*/ |
| 801 |
if (hashtable_count(stats_senders) > 0) { |
| 802 |
itr = hashtable_iterator(stats_senders); |
| 803 |
do { |
| 804 |
stat = (struct sender_stats *)hashtable_iterator_value(itr); |
| 805 |
if (stat->lastSeen < rqdLast) { |
| 806 |
if (runConf->globals.reportGoneAwaySenders) { |
| 807 |
localtime_r(&stat->lastSeen, &tm); |
| 808 |
LogMsg(0, RS_RET_SENDER_GONE_AWAY, LOG_WARNING, |
| 809 |
"removing sender '%s' from connection " |
| 810 |
"table, last seen at " |
| 811 |
"%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d", |
| 812 |
stat->sender, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, |
| 813 |
tm.tm_sec); |
| 814 |
} |
| 815 |
hashtable_remove(stats_senders, (void *)stat->sender); |
| 816 |
} |
| 817 |
} while (hashtable_iterator_advance(itr)); |
| 818 |
} |
| 819 |
|
| 820 |
pthread_mutex_unlock(&mutSenders); |
| 821 |
free(itr); |
| 822 |
} |
| 823 |
|
| 824 |
/* destructor for the statsobj object */ |
| 825 |
BEGINobjDestruct(statsobj) /* be sure to specify the object type also in END and CODESTART macros! */ |
| 826 |
CODESTARTobjDestruct(statsobj); |
| 827 |
removeFromObjList(pThis); |
| 828 |
|
| 829 |
/* destruct counters */ |
| 830 |
destructUnlinkedCounters(unlinkAllCounters(pThis)); |
| 831 |
|
| 832 |
pthread_mutex_destroy(&pThis->mutCtr); |
| 833 |
free(pThis->name); |
| 834 |
free(pThis->origin); |
| 835 |
free(pThis->reporting_ns); |
| 836 |
ENDobjDestruct(statsobj) |
| 837 |
|
| 838 |
|
| 839 |
/* debugprint for the statsobj object */ |
| 840 |
BEGINobjDebugPrint(statsobj) /* be sure to specify the object type also in END and CODESTART macros! */ |
| 841 |
CODESTARTobjDebugPrint(statsobj); |
| 842 |
dbgoprint((obj_t *)pThis, "statsobj object, currently no state info available\n"); |
| 843 |
ENDobjDebugPrint(statsobj) |
| 844 |
|
| 845 |
|
| 846 |
/* queryInterface function |
| 847 |
*/ |
| 848 |
BEGINobjQueryInterface(statsobj) |
| 849 |
CODESTARTobjQueryInterface(statsobj); |
| 850 |
if (pIf->ifVersion != statsobjCURR_IF_VERSION) { /* check for current version, increment on each change */ |
| 851 |
ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED); |
| 852 |
} |
| 853 |
|
| 854 |
/* ok, we have the right interface, so let's fill it |
| 855 |
* Please note that we may also do some backwards-compatibility |
| 856 |
* work here (if we can support an older interface version - that, |
| 857 |
* of course, also affects the "if" above). |
| 858 |
*/ |
| 859 |
pIf->Construct = statsobjConstruct; |
| 860 |
pIf->ConstructFinalize = statsobjConstructFinalize; |
| 861 |
pIf->Destruct = statsobjDestruct; |
| 862 |
pIf->DebugPrint = statsobjDebugPrint; |
| 863 |
pIf->SetName = setName; |
| 864 |
pIf->SetOrigin = setOrigin; |
| 865 |
pIf->SetReadNotifier = setReadNotifier; |
| 866 |
pIf->SetReportingNamespace = setReportingNamespace; |
| 867 |
pIf->SetStatsObjFlags = setStatsObjFlags; |
| 868 |
pIf->GetAllStatsLines = getAllStatsLines; |
| 869 |
pIf->GetAllCounters = getAllCounters; |
| 870 |
pIf->AddCounter = addCounter; |
| 871 |
pIf->AddManagedCounter = addManagedCounter; |
| 872 |
pIf->AddPreCreatedCtr = addPreCreatedCounter; |
| 873 |
pIf->DestructCounter = destructCounter; |
| 874 |
pIf->DestructUnlinkedCounter = destructUnlinkedCounter; |
| 875 |
pIf->UnlinkAllCounters = unlinkAllCounters; |
| 876 |
pIf->EnableStats = enableStats; |
| 877 |
finalize_it: |
| 878 |
ENDobjQueryInterface(statsobj) |
| 879 |
|
| 880 |
|
| 881 |
/* Initialize the statsobj class. Must be called as the very first method |
| 882 |
* before anything else is called inside this class. |
| 883 |
*/ |
| 884 |
BEGINAbstractObjClassInit(statsobj, 1, OBJ_IS_CORE_MODULE) /* class, version */ |
| 885 |
/* request objects we use */ |
| 886 |
|
| 887 |
/* set our own handlers */ |
| 888 |
OBJSetMethodHandler(objMethod_DEBUGPRINT, statsobjDebugPrint); |
| 889 |
OBJSetMethodHandler(objMethod_CONSTRUCTION_FINALIZER, statsobjConstructFinalize); |
| 890 |
|
| 891 |
/* init other data items */ |
| 892 |
pthread_mutex_init(&mutStats, NULL); |
| 893 |
pthread_mutex_init(&mutSenders, NULL); |
| 894 |
|
| 895 |
if ((stats_senders = create_hashtable(100, hash_from_string, key_equals_string, NULL)) == NULL) { |
| 896 |
LogError(0, RS_RET_INTERNAL_ERROR, |
| 897 |
"error trying to initialize hash-table " |
| 898 |
"for sender table. Sender statistics and warnings are disabled."); |
| 899 |
ABORT_FINALIZE(RS_RET_INTERNAL_ERROR); |
| 900 |
} |
| 901 |
ENDObjClassInit(statsobj) |
| 902 |
|
| 903 |
/* Exit the class. |
| 904 |
*/ |
| 905 |
BEGINObjClassExit(statsobj, OBJ_IS_CORE_MODULE) /* class, version */ |
| 906 |
/* release objects we no longer need */ |
| 907 |
pthread_mutex_destroy(&mutStats); |
| 908 |
pthread_mutex_destroy(&mutSenders); |
| 909 |
hashtable_destroy(stats_senders, 1); |
| 910 |
ENDObjClassExit(statsobj) |