tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

metrics_common.h (2019B)


      1 /* Copyright (c) 2020-2021, The Tor Project, Inc. */
      2 /* See LICENSE for licensing information */
      3 
      4 /**
      5 * @file metrics_common.h
      6 * @brief Header for lib/metrics/metrics_common.c
      7 **/
      8 
      9 #ifndef TOR_LIB_METRICS_METRICS_COMMON_H
     10 #define TOR_LIB_METRICS_METRICS_COMMON_H
     11 
     12 #include "lib/cc/torint.h"
     13 #include "lib/container/smartlist.h"
     14 
     15 /** Helper macro that must be used to construct the right namespaced metrics
     16 * name. A name is a string so stringify the result. */
     17 #define METRICS_STR(val) #val
     18 #define METRICS_NAME(name) METRICS_STR(tor_ ## name)
     19 
     20 /** Format output type. */
     21 typedef enum {
     22  /** Prometheus data output format. */
     23  METRICS_FORMAT_PROMETHEUS = 1,
     24 } metrics_format_t;
     25 
     26 /** Metric type. */
     27 typedef enum {
     28  /* Increment only. */
     29  METRICS_TYPE_COUNTER,
     30  /* Can go up or down. */
     31  METRICS_TYPE_GAUGE,
     32  /* Cumulative counters for multiple observation buckets. */
     33  METRICS_TYPE_HISTOGRAM,
     34 } metrics_type_t;
     35 
     36 typedef struct metrics_histogram_bucket_t {
     37  /* The value of the counter of this bucket. */
     38  uint64_t value;
     39  /* Technically, this should be a floating point value, but in practice, we
     40   * can make do with integer buckets. */
     41  int64_t bucket;
     42 } metrics_histogram_bucket_t;
     43 
     44 /** Metric counter object (METRICS_TYPE_COUNTER). */
     45 typedef struct metrics_counter_t {
     46  uint64_t value;
     47 } metrics_counter_t;
     48 
     49 /** Metric gauge object (METRICS_TYPE_GAUGE). */
     50 typedef struct metrics_gauge_t {
     51  int64_t value;
     52 } metrics_gauge_t;
     53 
     54 /** Metric histogram object (METRICS_TYPE_HISTOGRAM). */
     55 typedef struct metrics_histogram_t {
     56  /* The observation buckets. */
     57  metrics_histogram_bucket_t *buckets;
     58  /* The number of observation buckets. */
     59  size_t bucket_count;
     60  /* The sum of all observations */
     61  int64_t sum;
     62  /* The total number of observations */
     63  uint64_t count;
     64 } metrics_histogram_t;
     65 
     66 const char *metrics_type_to_str(const metrics_type_t type);
     67 
     68 /* Helpers. */
     69 const char *metrics_format_label(const char *key, const char *value);
     70 
     71 #endif /* !defined(TOR_LIB_METRICS_METRICS_COMMON_H) */