raytracer-c/module_utilities/metrics.h
2023-12-10 00:02:45 -05:00

83 lines
3.0 KiB
C

#ifndef SIMPLE_RAYTRACER_METRICS_H
#define SIMPLE_RAYTRACER_METRICS_H
/*
* TODO:
* - [X] Add registry for metrics
* - [ ] Add metric types:
* - [X] Counter - incrementing and decrementing
* - [X] Gauge - measure an instantaneous value
* - [ ] Meter - measure a rate of events
* - [ ] Timer - measure the duration of an event
* - [X] Histogram - measure the distribution of values
* - [ ] Add reporter implementation
* - [X] Add default registry
* - [ ] Improve histogram - double instead of long? Add percentiles? Options for sliding window vs. cumulative?
*/
typedef struct METRICS_Registry METRICS_Registry;
METRICS_Registry *METRICS_Registry_new(void);
void METRICS_Registry_delete(METRICS_Registry *registry);
/**
* Increments the counter by 1
* @param registry
* @param name
* @return pre-increment value
*/
long METRICS_Counter_inc(METRICS_Registry *registry, const char *name);
/**
* Decrements the counter by 1
* @param registry
* @param name
* @return pre-decrement value
*/
long METRICS_Counter_dec(METRICS_Registry *registry, const char *name);
/**
* Returns the current value of the counter
* @param registry
* @param name
* @return
*/
long METRICS_Counter_value(METRICS_Registry *registry, const char *name);
long METRICS_Gauge_value(METRICS_Registry *registry, const char *name);
typedef long (*METRICS_Gauge_measure)(void *state);
void METRICS_Gauge_register(METRICS_Registry *registry, const char *name, METRICS_Gauge_measure measure, void *state);
typedef struct METRICS_Histogram {
long min, max, count;
double mean;
} METRICS_Histogram;
void METRICS_Histogram_update(METRICS_Registry *registry, const char *name, long value);
METRICS_Histogram METRICS_Histogram_value(METRICS_Registry *registry, const char *name);
void METRICS_Timer_start(METRICS_Registry *registry, const char *name);
void METRICS_Timer_stop(METRICS_Registry *registry, const char *name);
/**
* Dumps metrics to stdout, generally for debugging purposes
* @param registry
*/
void METRICS_dump_registry(METRICS_Registry *registry);
/* Helpers for default registry */
extern METRICS_Registry *METRICS_DEFAULT_REGISTRY;
#define METRICS_INIT_DEFAULT_REGISTRY() METRICS_DEFAULT_REGISTRY = METRICS_Registry_new()
#define METRICS_DESTROY_DEFAULT_REGISTRY() METRICS_Registry_delete(METRICS_DEFAULT_REGISTRY)
#define METRICS_COUNTER_INC(name) METRICS_Counter_inc(METRICS_DEFAULT_REGISTRY, (name))
#define METRICS_COUNTER_DEC(name) METRICS_Counter_dec(METRICS_DEFAULT_REGISTRY, (name))
#define METRICS_COUNTER_VALUE(name) METRICS_Counter_value(METRICS_DEFAULT_REGISTRY, (name))
#define METRICS_GAUGE_REGISTER(name, measure, state) METRICS_Gauge_register(METRICS_DEFAULT_REGISTRY, (name), (measure), (state))
#define METRICS_GAUGE_VALUE(name) METRICS_Gauge_value(METRICS_DEFAULT_REGISTRY, (name))
#define METRICS_HISTOGRAM_UPDATE(name, value) METRICS_Histogram_update(METRICS_DEFAULT_REGISTRY, (name), (value))
#define METRICS_HISTOGRAM_VALUE(name) METRICS_Histogram_value(METRICS_DEFAULT_REGISTRY, (name))
#endif // SIMPLE_RAYTRACER_METRICS_H