| 1 |
/* The statsobj object. |
| 2 |
* |
| 3 |
* Copyright 2010-2026 Rainer Gerhards and Adiscon GmbH. |
| 4 |
* |
| 5 |
* This file is part of the rsyslog runtime library. |
| 6 |
* |
| 7 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 |
* you may not use this file except in compliance with the License. |
| 9 |
* You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* -or- |
| 13 |
* see COPYING.ASL20 in the source distribution |
| 14 |
* |
| 15 |
* Unless required by applicable law or agreed to in writing, software |
| 16 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 17 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 |
* See the License for the specific language governing permissions and |
| 19 |
* limitations under the License. |
| 20 |
*/ |
| 21 |
/** |
| 22 |
* @file statsobj.h |
| 23 |
* @brief Interface of the rsyslog statistics object |
| 24 |
* |
| 25 |
* A statistics object (statsobj_t) represents a set of counters that |
| 26 |
* describe activity within a subsystem. Each object keeps a doubly |
| 27 |
* linked list of counters and all objects are maintained in a global |
| 28 |
* list protected by a mutex. The counters are either 64 bit integers |
| 29 |
* (ctrType_IntCtr) or plain int values (ctrType_Int) and can be marked |
| 30 |
* resettable. Statistics are only gathered while the global |
| 31 |
* ::GatherStats flag is non-zero. |
| 32 |
* |
| 33 |
* Counters can be reported in multiple formats via the GetAllStatsLines() |
| 34 |
* interface: |
| 35 |
* - ::statsFmt_Legacy classic key=value pairs |
| 36 |
* - ::statsFmt_JSON valid JSON structures |
| 37 |
* - ::statsFmt_JSON_ES JSON with dot replacement for Elasticsearch |
| 38 |
* - ::statsFmt_CEE Project Lumberjack / CEE format |
| 39 |
* - ::statsFmt_Prometheus Prometheus text exposition format |
| 40 |
* |
| 41 |
* The caller supplies a callback that receives each generated line. |
| 42 |
* After emission counters flagged with CTR_FLAG_RESETTABLE may be |
| 43 |
* cleared if requested. |
| 44 |
*/ |
| 45 |
#ifndef INCLUDED_STATSOBJ_H |
| 46 |
#define INCLUDED_STATSOBJ_H |
| 47 |
|
| 48 |
#include "atomic.h" |
| 49 |
|
| 50 |
/* The following data item is somewhat dirty, in that it does not follow |
| 51 |
* our usual object calling conventions. However, much like with "Debug", we |
| 52 |
* do this to gain speed. If we finally come to a platform that does not |
| 53 |
* provide resolution of names for dynamically loaded modules, we need to find |
| 54 |
* a work-around, but until then, we use the direct access. |
| 55 |
* If set to 0, statistics are not gathered, otherwise they are. |
| 56 |
*/ |
| 57 |
extern int GatherStats; |
| 58 |
|
| 59 |
/* our basic counter type -- need 32 bit on 32 bit platform. |
| 60 |
* IMPORTANT: this type *MUST* be supported by atomic instructions! |
| 61 |
*/ |
| 62 |
typedef uint64 intctr_t; |
| 63 |
|
| 64 |
/* counter types */ |
| 65 |
typedef enum statsCtrType_e { ctrType_IntCtr, ctrType_Int } statsCtrType_t; |
| 66 |
|
| 67 |
/* stats line format types */ |
| 68 |
typedef enum statsFmtType_e { |
| 69 |
statsFmt_Legacy, |
| 70 |
statsFmt_JSON, |
| 71 |
statsFmt_JSON_ES, |
| 72 |
statsFmt_CEE, |
| 73 |
/** |
| 74 |
* Prometheus text‐exposition format: |
| 75 |
* For each counter, emit: |
| 76 |
* # HELP <obj>_<ctr> (optional generic text) |
| 77 |
* # TYPE <obj>_<ctr> counter |
| 78 |
* <obj>_<ctr> <value> |
| 79 |
*/ |
| 80 |
statsFmt_Prometheus |
| 81 |
} statsFmtType_t; |
| 82 |
|
| 83 |
/* counter flags */ |
| 84 |
#define CTR_FLAG_NONE 0 |
| 85 |
#define CTR_FLAG_RESETTABLE 1 |
| 86 |
#define CTR_FLAG_MUST_RESET 2 |
| 87 |
|
| 88 |
/* statsobj flags */ |
| 89 |
#define STATSOBJ_FLAG_NONE 0 |
| 90 |
#define STATSOBJ_FLAG_DO_PREPEND 1 |
| 91 |
|
| 92 |
/** |
| 93 |
* Callback type for native counter iteration via GetAllCounters(). |
| 94 |
* |
| 95 |
* @param ctx User context pointer passed to GetAllCounters() |
| 96 |
* @param obj_name Name of the statsobj instance (may be NULL/empty) |
| 97 |
* @param obj_origin Origin of the statsobj (e.g., "resource-usage", "core.queue") |
| 98 |
* @param ctr_name Name of the counter |
| 99 |
* @param ctr_type Type of counter (ctrType_IntCtr or ctrType_Int) |
| 100 |
* @param value Current counter value (read atomically for IntCtr, best-effort for Int) |
| 101 |
* @param flags Counter flags (CTR_FLAG_RESETTABLE, etc.) |
| 102 |
* @return RS_RET_OK to continue iteration, error code to abort |
| 103 |
* |
| 104 |
* THREAD SAFETY: |
| 105 |
* - ctrType_IntCtr: value is read atomically (uint64 on aligned address) |
| 106 |
* - ctrType_Int: value is read without application lock; may be stale but acceptable |
| 107 |
* for monitoring. These counters are typically gauges (queue size, open files) |
| 108 |
* protected by application-level mutexes that the stats system cannot acquire. |
| 109 |
* |
| 110 |
* LOCKING CONSTRAINT: |
| 111 |
* - The counter list mutex is HELD during callback invocation to prevent concurrent |
| 112 |
* list modification. Callbacks MUST complete quickly and MUST NOT call back into |
| 113 |
* statsobj or deadlock will occur. |
| 114 |
*/ |
| 115 |
typedef rsRetVal (*statsobj_counter_cb_t)(void *ctx, |
| 116 |
const uchar *obj_name, |
| 117 |
const uchar *obj_origin, |
| 118 |
const uchar *ctr_name, |
| 119 |
statsCtrType_t ctr_type, |
| 120 |
uint64_t value, |
| 121 |
int8_t flags); |
| 122 |
|
| 123 |
/* helper entity, the counter */ |
| 124 |
typedef struct ctr_s { |
| 125 |
uchar *name; |
| 126 |
statsCtrType_t ctrType; |
| 127 |
union { |
| 128 |
intctr_t *pIntCtr; |
| 129 |
int *pInt; |
| 130 |
} val; |
| 131 |
int8_t flags; |
| 132 |
struct ctr_s *next, *prev; |
| 133 |
} ctr_t; |
| 134 |
|
| 135 |
/* the statsobj object */ |
| 136 |
struct statsobj_s { |
| 137 |
BEGINobjInstance |
| 138 |
; /* Data to implement generic object - MUST be the first data element! */ |
| 139 |
uchar *name; |
| 140 |
uchar *origin; |
| 141 |
uchar *reporting_ns; |
| 142 |
statsobj_read_notifier_t read_notifier; |
| 143 |
void *read_notifier_ctx; |
| 144 |
pthread_mutex_t mutCtr; /* to guard counter linked-list ops */ |
| 145 |
ctr_t *ctrRoot; /* doubly-linked list of statsobj counters */ |
| 146 |
ctr_t *ctrLast; |
| 147 |
int flags; |
| 148 |
/* used to link ourselves together */ |
| 149 |
statsobj_t *prev; |
| 150 |
statsobj_t *next; |
| 151 |
}; |
| 152 |
|
| 153 |
struct sender_stats { |
| 154 |
const uchar *sender; |
| 155 |
uint64_t nMsgs; |
| 156 |
time_t lastSeen; |
| 157 |
}; |
| 158 |
|
| 159 |
|
| 160 |
/* interfaces */ |
| 161 |
BEGINinterface(statsobj) /* name must also be changed in ENDinterface macro! */ |
| 162 |
INTERFACEObjDebugPrint(statsobj); |
| 163 |
rsRetVal (*Construct)(statsobj_t **ppThis); |
| 164 |
rsRetVal (*ConstructFinalize)(statsobj_t *pThis); |
| 165 |
rsRetVal (*Destruct)(statsobj_t **ppThis); |
| 166 |
rsRetVal (*SetName)(statsobj_t *pThis, uchar *name); |
| 167 |
rsRetVal (*SetOrigin)(statsobj_t *pThis, uchar *name); /* added v12, 2014-09-08 */ |
| 168 |
rsRetVal (*SetReadNotifier)(statsobj_t *pThis, statsobj_read_notifier_t notifier, void *ctx); |
| 169 |
rsRetVal (*SetReportingNamespace)(statsobj_t *pThis, uchar *ns); |
| 170 |
void (*SetStatsObjFlags)(statsobj_t *pThis, int flags); |
| 171 |
rsRetVal (*GetAllStatsLines)(rsRetVal (*cb)(void *, const char *), void *usrptr, statsFmtType_t fmt, |
| 172 |
int8_t bResetCtr); |
| 173 |
rsRetVal (*GetAllCounters)(statsobj_counter_cb_t cb, void *ctx); |
| 174 |
/** |
| 175 |
* @brief Register a counter value with a stats object. |
| 176 |
* |
| 177 |
* The counter name is duplicated internally; the caller retains ownership |
| 178 |
* of @p ctrName and may free/modify it after this call returns. |
| 179 |
* |
| 180 |
* @param pThis target stats object |
| 181 |
* @param ctrName counter name (copied internally) |
| 182 |
* @param ctrType counter type |
| 183 |
* @param flags counter flags |
| 184 |
* @param pCtr pointer to caller-owned counter storage |
| 185 |
* |
| 186 |
* @note The storage referenced by @p pCtr must remain valid for at least |
| 187 |
* the lifetime of the registered counter entry in @p pThis. |
| 188 |
*/ |
| 189 |
rsRetVal (*AddCounter)(statsobj_t *pThis, const uchar *ctrName, statsCtrType_t ctrType, int8_t flags, void *pCtr); |
| 190 |
/** |
| 191 |
* @brief Register a managed counter entry and optionally link it into |
| 192 |
* the stats object's counter list. |
| 193 |
* |
| 194 |
* The counter name is duplicated internally; the caller retains ownership |
| 195 |
* of @p ctrName and may free/modify it after this call returns. |
| 196 |
* |
| 197 |
* On success, @p ref receives the created entry handle, which can be used |
| 198 |
* with `DestructCounter()` (if linked) or `DestructUnlinkedCounter()` |
| 199 |
* (if not linked) according to the chosen lifecycle. |
| 200 |
* |
| 201 |
* @param pThis target stats object |
| 202 |
* @param ctrName counter name (copied internally) |
| 203 |
* @param ctrType counter type |
| 204 |
* @param flags counter flags |
| 205 |
* @param pCtr pointer to caller-owned counter storage |
| 206 |
* @param ref out: created counter entry handle |
| 207 |
* @param linked if non-zero, entry is linked into @p pThis and follows |
| 208 |
* object lifecycle unless explicitly destructed |
| 209 |
* |
| 210 |
* @note The storage referenced by @p pCtr must remain valid for at least |
| 211 |
* the lifetime of the created counter entry. |
| 212 |
*/ |
| 213 |
rsRetVal (*AddManagedCounter)(statsobj_t *pThis, const uchar *ctrName, statsCtrType_t ctrType, int8_t flags, |
| 214 |
void *pCtr, ctr_t **ref, int8_t linked); |
| 215 |
void (*AddPreCreatedCtr)(statsobj_t *pThis, ctr_t *ctr); |
| 216 |
void (*DestructCounter)(statsobj_t *pThis, ctr_t *ref); |
| 217 |
void (*DestructUnlinkedCounter)(ctr_t *ctr); |
| 218 |
ctr_t *(*UnlinkAllCounters)(statsobj_t *pThis); |
| 219 |
rsRetVal (*EnableStats)(void); |
| 220 |
ENDinterface(statsobj) |
| 221 |
#define statsobjCURR_IF_VERSION 14 /* increment whenever you change the interface structure! */ |
| 222 |
/* Changes |
| 223 |
* v2-v9 rserved for future use in "older" version branches |
| 224 |
* v10, 2012-04-01: GetAllStatsLines got fmt parameter |
| 225 |
* v11, 2013-09-07: - add "flags" to AddCounter API |
| 226 |
* - GetAllStatsLines got parameter telling if ctrs shall be reset |
| 227 |
* v13, 2016-05-19: GetAllStatsLines cb data type changed (char* instead of cstr) |
| 228 |
* v14, 2026-02-01: Add GetAllCounters() native counter iteration API |
| 229 |
*/ |
| 230 |
|
| 231 |
|
| 232 |
/* prototypes */ |
| 233 |
PROTOTYPEObjFull(statsobj); |
| 234 |
PROTOTYPEObjDebugPrint(statsobj); |
| 235 |
|
| 236 |
rsRetVal statsRecordSender(const uchar *sender, unsigned nMsgs, time_t lastSeen); |
| 237 |
/* checkGoneAwaySenders() is part of this module because all it needs is |
| 238 |
* done by this module, so even though it's own processing is not directly |
| 239 |
* related to stats, it makes sense to do it here... -- rgerhards, 2016-02-01 |
| 240 |
*/ |
| 241 |
void checkGoneAwaySenders(time_t); |
| 242 |
|
| 243 |
/* macros to handle stats counters |
| 244 |
* These are to be used by "counter providers". Note that we MUST |
| 245 |
* specify the mutex name, even though at first it looks like it |
| 246 |
* could be automatically be generated via e.g. "mut##ctr". |
| 247 |
* Unfortunately, this does not work if counter is e.g. "pThis->ctr". |
| 248 |
* So we decided, for clarity, to always insist on specifying the mutex |
| 249 |
* name (after all, it's just a few more keystrokes...). |
| 250 |
* -------------------------------------------------------------------- |
| 251 |
* NOTE WELL |
| 252 |
* -------------------------------------------------------------------- |
| 253 |
* There are actually two types of stats counters: "regular" counters, |
| 254 |
* which are only used for stats purposes and "dual" counters, which |
| 255 |
* are primarily used for other purposes but can be included in stats |
| 256 |
* as well. ALL regular counters MUST be initialized with |
| 257 |
* STATSCOUNTER_INIT and only be modified by STATSCOUNTER_* functions. |
| 258 |
* They MUST NOT be used for any other purpose (if this seems to make |
| 259 |
* sense, consider changing it to a dual counter). |
| 260 |
* Dual counters are somewhat dangerous in that a single variable is |
| 261 |
* used for two purposes: the actual application need and stats |
| 262 |
* counting. However, this is supported for performance reasons, as it |
| 263 |
* provides insight into the inner engine workings without need for |
| 264 |
* additional counters (and their maintenance code). Dual counters |
| 265 |
* MUST NOT be modified by STATSCOUNTER_* functions. Most importantly, |
| 266 |
* it is expected that the actua application code provides proper |
| 267 |
* (enough) synchronized access to these counters. Most importantly, |
| 268 |
* this means they have NO stats-system mutex associated to them. |
| 269 |
* |
| 270 |
* The interface function AddCounter() is a read-only function. It |
| 271 |
* only provides the stats subsystem with a reference to a counter. |
| 272 |
* It is irrelevant if the counter is a regular or dual one. For that |
| 273 |
* reason, AddCounter() must not modify the counter contents, as in |
| 274 |
* the case of a dual counter application code may be broken. |
| 275 |
*/ |
| 276 |
#define STATSCOUNTER_DEF(ctr, mut) \ |
| 277 |
intctr_t ctr; \ |
| 278 |
DEF_ATOMIC_HELPER_MUT64(mut); |
| 279 |
|
| 280 |
#define STATSCOUNTER_INIT(ctr, mut) \ |
| 281 |
INIT_ATOMIC_HELPER_MUT64(mut); \ |
| 282 |
ctr = 0; |
| 283 |
|
| 284 |
#define STATSCOUNTER_INC(ctr, mut) \ |
| 285 |
if (GatherStats) ATOMIC_INC_uint64(&ctr, &mut); |
| 286 |
|
| 287 |
#define STATSCOUNTER_ADD(ctr, mut, delta) \ |
| 288 |
if (GatherStats) ATOMIC_ADD_uint64(&ctr, &mut, delta); |
| 289 |
|
| 290 |
#define STATSCOUNTER_DEC(ctr, mut) \ |
| 291 |
if (GatherStats) ATOMIC_DEC_uint64(&ctr, &mut); |
| 292 |
|
| 293 |
/* the next macro works only if the variable is already guarded |
| 294 |
* by mutex (or the users risks a wrong result). It is assumed |
| 295 |
* that there are not concurrent operations that modify the counter. |
| 296 |
*/ |
| 297 |
#define STATSCOUNTER_SETMAX_NOMUT(ctr, newmax) \ |
| 298 |
if (GatherStats && ((newmax) > (ctr))) ctr = newmax; |
| 299 |
|
| 300 |
#endif /* #ifndef INCLUDED_STATSOBJ_H */ |