pmetrics 0.1
PostgreSQL metrics instrumentation extension
Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
pmetrics.c File Reference
#include "postgres.h"
#include "pmetrics.h"
#include "common/hashfn.h"
#include "fmgr.h"
#include "funcapi.h"
#include "lib/dshash.h"
#include "miscadmin.h"
#include "storage/ipc.h"
#include "storage/lwlock.h"
#include "storage/shmem.h"
#include "utils/builtins.h"
#include "utils/dsa.h"
#include "utils/guc.h"
#include "utils/hsearch.h"
#include "utils/jsonb.h"
#include "math.h"
#include <stdio.h>
Include dependency graph for pmetrics.c:

Go to the source code of this file.

Data Structures

struct  PMetricsSharedState
 
struct  MetricKey
 
struct  Metric
 

Macros

#define LWTRANCHE_PMETRICS_DSA   43001
 
#define LWTRANCHE_PMETRICS   43002
 
#define DEFAULT_ENABLED   true
 
#define DEFAULT_BUCKET_VARIABILITY   0.1
 
#define DEFAULT_BUCKETS_UPPER_BOUND   30000
 

Typedefs

typedef enum MetricType MetricType
 
typedef struct PMetricsSharedState PMetricsSharedState
 
typedef enum LabelsLocation LabelsLocation
 

Enumerations

enum  MetricType { METRIC_TYPE_COUNTER = 0 , METRIC_TYPE_GAUGE = 1 , METRIC_TYPE_HISTOGRAM = 2 , METRIC_TYPE_HISTOGRAM_SUM = 3 }
 
enum  LabelsLocation { LABELS_NONE = 0 , LABELS_LOCAL = 1 , LABELS_DSA = 2 }
 

Functions

void _PG_init (void)
 
static void metrics_shmem_request (void)
 
static void metrics_shmem_startup (void)
 
static dshash_table * get_metrics_table (void)
 
static void cleanup_metrics_backend (int code, Datum arg)
 
static void validate_inputs (const char *name)
 
static void init_metric_key (MetricKey *key, const char *name, Jsonb *labels_jsonb, MetricType type, int bucket)
 
static int bucket_for (double value)
 
static int64 increment_by (const char *name_str, Jsonb *labels_jsonb, MetricType type, int bucket, int64 amount)
 
static int64 delete_metrics_by_name_labels (const char *name_str, Jsonb *labels_jsonb)
 
static void extract_metric_args (FunctionCallInfo fcinfo, int name_arg, int labels_arg, char **name_out, Jsonb **labels_out)
 
static Jsonb * get_labels_jsonb (const MetricKey *key, dsa_area *dsa)
 
static uint32 metric_hash_dshash (const void *key, size_t key_size, void *arg)
 
static int metric_compare_dshash (const void *a, const void *b, size_t key_size, void *arg)
 
static void metric_key_copy (void *dst, const void *src, size_t key_size, void *arg)
 
 PG_FUNCTION_INFO_V1 (increment_counter)
 
Datum increment_counter (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (increment_counter_by)
 
Datum increment_counter_by (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (set_gauge)
 
Datum set_gauge (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (add_to_gauge)
 
Datum add_to_gauge (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (list_metrics)
 
Datum list_metrics (PG_FUNCTION_ARGS)
 
 __attribute__ ((visibility("default")))
 
 PG_FUNCTION_INFO_V1 (record_to_histogram)
 
Datum record_to_histogram (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (list_histogram_buckets)
 
Datum list_histogram_buckets (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (clear_metrics)
 
Datum clear_metrics (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (delete_metric)
 
Datum delete_metric (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 
static PMetricsSharedStateshared_state = NULL
 
static dsa_area * local_dsa = NULL
 
static dshash_table * local_metrics_table = NULL
 
static shmem_startup_hook_type prev_shmem_startup_hook = NULL
 
static shmem_request_hook_type prev_shmem_request_hook = NULL
 
static bool pmetrics_enabled = DEFAULT_ENABLED
 
static double bucket_variability = DEFAULT_BUCKET_VARIABILITY
 
static int buckets_upper_bound = DEFAULT_BUCKETS_UPPER_BOUND
 
static double gamma_val = 0
 
static double log_gamma = 0
 
static const dshash_parameters metrics_params
 

Macro Definition Documentation

◆ DEFAULT_BUCKET_VARIABILITY

#define DEFAULT_BUCKET_VARIABILITY   0.1

Definition at line 52 of file pmetrics.c.

◆ DEFAULT_BUCKETS_UPPER_BOUND

#define DEFAULT_BUCKETS_UPPER_BOUND   30000

Definition at line 53 of file pmetrics.c.

◆ DEFAULT_ENABLED

#define DEFAULT_ENABLED   true

Definition at line 51 of file pmetrics.c.

◆ LWTRANCHE_PMETRICS

#define LWTRANCHE_PMETRICS   43002

Definition at line 48 of file pmetrics.c.

◆ LWTRANCHE_PMETRICS_DSA

#define LWTRANCHE_PMETRICS_DSA   43001

Definition at line 47 of file pmetrics.c.

Typedef Documentation

◆ LabelsLocation

◆ MetricType

typedef enum MetricType MetricType

◆ PMetricsSharedState

Enumeration Type Documentation

◆ LabelsLocation

Enumerator
LABELS_NONE 
LABELS_LOCAL 
LABELS_DSA 

Definition at line 71 of file pmetrics.c.

◆ MetricType

enum MetricType
Enumerator
METRIC_TYPE_COUNTER 
METRIC_TYPE_GAUGE 
METRIC_TYPE_HISTOGRAM 
METRIC_TYPE_HISTOGRAM_SUM 

Definition at line 56 of file pmetrics.c.

Function Documentation

◆ __attribute__()

__attribute__ ( (visibility("default"))  )

Definition at line 659 of file pmetrics.c.

◆ _PG_init()

void _PG_init ( void  )

Definition at line 197 of file pmetrics.c.

◆ add_to_gauge()

Datum add_to_gauge ( PG_FUNCTION_ARGS  )

Definition at line 490 of file pmetrics.c.

◆ bucket_for()

static int bucket_for ( double  value)
static

Definition at line 971 of file pmetrics.c.

◆ cleanup_metrics_backend()

static void cleanup_metrics_backend ( int  code,
Datum  arg 
)
static

Definition at line 299 of file pmetrics.c.

◆ clear_metrics()

Datum clear_metrics ( PG_FUNCTION_ARGS  )

Definition at line 855 of file pmetrics.c.

◆ delete_metric()

Datum delete_metric ( PG_FUNCTION_ARGS  )

Definition at line 919 of file pmetrics.c.

◆ delete_metrics_by_name_labels()

static int64 delete_metrics_by_name_labels ( const char *  name_str,
Jsonb *  labels_jsonb 
)
static

Definition at line 864 of file pmetrics.c.

◆ extract_metric_args()

static void extract_metric_args ( FunctionCallInfo  fcinfo,
int  name_arg,
int  labels_arg,
char **  name_out,
Jsonb **  labels_out 
)
static

Definition at line 263 of file pmetrics.c.

◆ get_labels_jsonb()

static Jsonb * get_labels_jsonb ( const MetricKey key,
dsa_area *  dsa 
)
static

Definition at line 996 of file pmetrics.c.

◆ get_metrics_table()

static dshash_table * get_metrics_table ( void  )
static

Definition at line 319 of file pmetrics.c.

◆ increment_by()

static int64 increment_by ( const char *  name_str,
Jsonb *  labels_jsonb,
MetricType  type,
int  bucket,
int64  amount 
)
static

Definition at line 368 of file pmetrics.c.

◆ increment_counter()

Datum increment_counter ( PG_FUNCTION_ARGS  )

Definition at line 397 of file pmetrics.c.

◆ increment_counter_by()

Datum increment_counter_by ( PG_FUNCTION_ARGS  )

Definition at line 425 of file pmetrics.c.

◆ init_metric_key()

static void init_metric_key ( MetricKey key,
const char *  name,
Jsonb *  labels_jsonb,
MetricType  type,
int  bucket 
)
static

Definition at line 278 of file pmetrics.c.

◆ list_histogram_buckets()

Datum list_histogram_buckets ( PG_FUNCTION_ARGS  )

Definition at line 767 of file pmetrics.c.

◆ list_metrics()

Datum list_metrics ( PG_FUNCTION_ARGS  )

Definition at line 525 of file pmetrics.c.

◆ metric_compare_dshash()

static int metric_compare_dshash ( const void *  a,
const void *  b,
size_t  key_size,
void *  arg 
)
static

Definition at line 1038 of file pmetrics.c.

◆ metric_hash_dshash()

static uint32 metric_hash_dshash ( const void *  key,
size_t  key_size,
void *  arg 
)
static

Definition at line 1015 of file pmetrics.c.

◆ metric_key_copy()

static void metric_key_copy ( void *  dst,
const void *  src,
size_t  key_size,
void *  arg 
)
static

Definition at line 1098 of file pmetrics.c.

◆ metrics_shmem_request()

static void metrics_shmem_request ( void  )
static

Definition at line 145 of file pmetrics.c.

◆ metrics_shmem_startup()

static void metrics_shmem_startup ( void  )
static

Definition at line 154 of file pmetrics.c.

◆ PG_FUNCTION_INFO_V1() [1/9]

PG_FUNCTION_INFO_V1 ( add_to_gauge  )

◆ PG_FUNCTION_INFO_V1() [2/9]

PG_FUNCTION_INFO_V1 ( clear_metrics  )

◆ PG_FUNCTION_INFO_V1() [3/9]

PG_FUNCTION_INFO_V1 ( delete_metric  )

◆ PG_FUNCTION_INFO_V1() [4/9]

PG_FUNCTION_INFO_V1 ( increment_counter  )

◆ PG_FUNCTION_INFO_V1() [5/9]

PG_FUNCTION_INFO_V1 ( increment_counter_by  )

◆ PG_FUNCTION_INFO_V1() [6/9]

PG_FUNCTION_INFO_V1 ( list_histogram_buckets  )

◆ PG_FUNCTION_INFO_V1() [7/9]

PG_FUNCTION_INFO_V1 ( list_metrics  )

◆ PG_FUNCTION_INFO_V1() [8/9]

PG_FUNCTION_INFO_V1 ( record_to_histogram  )

◆ PG_FUNCTION_INFO_V1() [9/9]

PG_FUNCTION_INFO_V1 ( set_gauge  )

◆ record_to_histogram()

Datum record_to_histogram ( PG_FUNCTION_ARGS  )

Definition at line 737 of file pmetrics.c.

◆ set_gauge()

Datum set_gauge ( PG_FUNCTION_ARGS  )

Definition at line 459 of file pmetrics.c.

◆ validate_inputs()

static void validate_inputs ( const char *  name)
static

Definition at line 250 of file pmetrics.c.

Variable Documentation

◆ bucket_variability

double bucket_variability = DEFAULT_BUCKET_VARIABILITY
static

Definition at line 106 of file pmetrics.c.

◆ buckets_upper_bound

int buckets_upper_bound = DEFAULT_BUCKETS_UPPER_BOUND
static

Definition at line 107 of file pmetrics.c.

◆ gamma_val

double gamma_val = 0
static

Definition at line 109 of file pmetrics.c.

◆ local_dsa

dsa_area* local_dsa = NULL
static

Definition at line 96 of file pmetrics.c.

◆ local_metrics_table

dshash_table* local_metrics_table = NULL
static

Definition at line 97 of file pmetrics.c.

◆ log_gamma

double log_gamma = 0
static

Definition at line 110 of file pmetrics.c.

◆ metrics_params

const dshash_parameters metrics_params
static
Initial value:
= {
.key_size = sizeof(MetricKey),
.entry_size = sizeof(Metric),
.compare_function = metric_compare_dshash,
.hash_function = metric_hash_dshash,
.copy_function = metric_key_copy,
.tranche_id = LWTRANCHE_PMETRICS}
static int metric_compare_dshash(const void *a, const void *b, size_t key_size, void *arg)
Definition pmetrics.c:1038
static void metric_key_copy(void *dst, const void *src, size_t key_size, void *arg)
Definition pmetrics.c:1098
static uint32 metric_hash_dshash(const void *key, size_t key_size, void *arg)
Definition pmetrics.c:1015
#define LWTRANCHE_PMETRICS
Definition pmetrics.c:48

Definition at line 137 of file pmetrics.c.

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 44 of file pmetrics.c.

◆ pmetrics_enabled

bool pmetrics_enabled = DEFAULT_ENABLED
static

Definition at line 105 of file pmetrics.c.

◆ prev_shmem_request_hook

shmem_request_hook_type prev_shmem_request_hook = NULL
static

Definition at line 101 of file pmetrics.c.

◆ prev_shmem_startup_hook

shmem_startup_hook_type prev_shmem_startup_hook = NULL
static

Definition at line 100 of file pmetrics.c.

◆ shared_state

PMetricsSharedState* shared_state = NULL
static

Definition at line 93 of file pmetrics.c.