Skip to content

Commit

Permalink
Add new metric pgagroal_connection_query_count and new panel "Query r…
Browse files Browse the repository at this point in the history
…ate per database"

* Add new metric pgagroal_connection_query_count
* Add new panel "Query rate per database"
* Fix missing statistic of 'Q' and 'E' messages
* Change "Query rate", "Transaction rate", "Send traffic", "Receive traffic" panels' promql by irate
  • Loading branch information
An-DJ committed Aug 19, 2021
1 parent d51e3e4 commit d463bf3
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 45 deletions.
112 changes: 96 additions & 16 deletions contrib/grafana/dashboard.json
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@
"targets": [
{
"exemplar": true,
"expr": "pgagroal_query_count - (pgagroal_query_count offset 1s)",
"expr": "irate(pgagroal_query_count[1m])",
"interval": "",
"legendFormat": "",
"refId": "query rate"
Expand Down Expand Up @@ -800,7 +800,7 @@
"targets": [
{
"exemplar": true,
"expr": "pgagroal_tx_count - (pgagroal_tx_count offset 1s)",
"expr": "irate(pgagroal_tx_count[1m])",
"interval": "",
"legendFormat": "",
"refId": "A"
Expand All @@ -809,13 +809,93 @@
"title": "Transaction rate",
"type": "timeseries"
},
{
"datasource": null,
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"lineInterpolation": "linear",
"lineWidth": 1,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "auto",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
}
]
},
"unit": "reqps"
},
"overrides": []
},
"gridPos": {
"h": 8,
"w": 11,
"x": 0,
"y": 34
},
"id": 56,
"interval": "1s",
"maxDataPoints": 20000,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom"
},
"tooltip": {
"mode": "single"
}
},
"targets": [
{
"exemplar": true,
"expr": "sum(irate(pgagroal_connection_query_count{database!=\"\"}[5s])) by (database)",
"interval": "",
"legendFormat": "",
"refId": "A"
}
],
"title": "Query rate per database",
"type": "timeseries"
},
{
"datasource": null,
"gridPos": {
"h": 1,
"w": 24,
"x": 0,
"y": 34
"y": 42
},
"id": 12,
"title": "Internal",
Expand Down Expand Up @@ -874,7 +954,7 @@
"h": 8,
"w": 11,
"x": 0,
"y": 35
"y": 43
},
"id": 46,
"interval": "1s",
Expand All @@ -891,7 +971,7 @@
"targets": [
{
"exemplar": true,
"expr": "pgagroal_network_sent - (pgagroal_network_sent offset 1s)",
"expr": "irate(pgagroal_network_sent[1m])",
"instant": false,
"interval": "",
"legendFormat": "",
Expand Down Expand Up @@ -954,7 +1034,7 @@
"h": 8,
"w": 11,
"x": 11,
"y": 35
"y": 43
},
"id": 48,
"interval": "1s",
Expand All @@ -971,7 +1051,7 @@
"targets": [
{
"exemplar": true,
"expr": "pgagroal_network_received - (pgagroal_network_received offset 1s)",
"expr": "irate(pgagroal_network_received[1m])",
"interval": "",
"legendFormat": "",
"refId": "A"
Expand Down Expand Up @@ -1032,7 +1112,7 @@
"h": 8,
"w": 11,
"x": 0,
"y": 43
"y": 51
},
"id": 50,
"interval": "1s",
Expand Down Expand Up @@ -1080,7 +1160,7 @@
"h": 1,
"w": 24,
"x": 0,
"y": 51
"y": 59
},
"id": 10,
"title": "Connection",
Expand Down Expand Up @@ -1143,7 +1223,7 @@
"h": 8,
"w": 11,
"x": 0,
"y": 52
"y": 60
},
"id": 24,
"options": {
Expand Down Expand Up @@ -1292,7 +1372,7 @@
"h": 8,
"w": 12,
"x": 11,
"y": 52
"y": 60
},
"id": 52,
"options": {
Expand Down Expand Up @@ -1323,7 +1403,7 @@
"h": 1,
"w": 24,
"x": 0,
"y": 60
"y": 68
},
"id": 8,
"title": "Client",
Expand Down Expand Up @@ -1382,7 +1462,7 @@
"h": 8,
"w": 11,
"x": 0,
"y": 61
"y": 69
},
"id": 38,
"options": {
Expand Down Expand Up @@ -1422,7 +1502,7 @@
"h": 1,
"w": 24,
"x": 0,
"y": 69
"y": 77
},
"id": 6,
"panels": [],
Expand Down Expand Up @@ -1484,7 +1564,7 @@
"h": 8,
"w": 11,
"x": 0,
"y": 70
"y": 78
},
"id": 34,
"options": {
Expand Down Expand Up @@ -1566,7 +1646,7 @@
"h": 8,
"w": 12,
"x": 11,
"y": 70
"y": 78
},
"id": 36,
"options": {
Expand Down
2 changes: 1 addition & 1 deletion src/include/pgagroal.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ struct user
*/
struct prometheus_connection
{
/**< The metrics */
atomic_ullong query_count; /**< The number of queries per connection */
} __attribute__ ((aligned (64)));


Expand Down
14 changes: 14 additions & 0 deletions src/include/prometheus.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,20 @@ pgagroal_prometheus_client_active_sub(void);
void
pgagroal_prometheus_query_count_add(void);

/**
* Increase query_count for the specified connection by 1
* @param slot The connection slot
*/
void
pgagroal_prometheus_query_count_specified_add(int slot);

/**
* Reset query_count for the specified connection
* @param slot The connection slot
*/
void
pgagroal_prometheus_query_count_specified_reset(int slot);

/**
* Increase tx_count by 1
*/
Expand Down
36 changes: 34 additions & 2 deletions src/libpgagroal/pipeline_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ static void session_destroy(void*, size_t);
static void session_periodic(void);

static bool in_tx;
static int next_client_message;
static int next_server_message;

#define CLIENT_INIT 0
Expand Down Expand Up @@ -125,6 +126,7 @@ session_start(struct ev_loop *loop, struct worker_io* w)
struct client_session* client;

in_tx = false;
next_client_message = 0;
next_server_message = 0;

if (pipeline_shmem != NULL)
Expand Down Expand Up @@ -229,9 +231,39 @@ session_client(struct ev_loop *loop, struct ev_io *watcher, int revents)

if (likely(msg->kind != 'X'))
{
if (msg->kind == 'Q' || msg->kind == 'E')
int offset = 0;

while (offset < msg->length)
{
pgagroal_prometheus_query_count_add();
if (next_client_message == 0)
{
char kind = pgagroal_read_byte(msg->data + offset);
int length = pgagroal_read_int32(msg->data + offset + 1);

/* The Q and E message tell us the execute of the simple query and the prepared statement */
if (kind == 'Q' || kind == 'E')
{
pgagroal_prometheus_query_count_add();
pgagroal_prometheus_query_count_specified_add(wi->slot);
}

/* Calculate the offset to the next message */
if (offset + length + 1 <= msg->length)
{
next_client_message = 0;
offset += length + 1;
}
else
{
next_client_message = length + 1 - (msg->length - offset);
offset = msg->length;
}
}
else
{
offset = MIN(next_client_message, msg->length);
next_client_message -= offset;
}
}

status = pgagroal_write_socket_message(wi->server_fd, msg);
Expand Down
52 changes: 27 additions & 25 deletions src/libpgagroal/pipeline_transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,22 +238,17 @@ transaction_client(struct ev_loop* loop, struct ev_io* watcher, int revents)

if (likely(msg->kind != 'X'))
{
if (msg->kind == 'Q' || msg->kind == 'E')
{
pgagroal_prometheus_query_count_add();
}
int offset = 0;

if (config->track_prepared_statements)
while (offset < msg->length)
{
int offset = 0;

while (offset < msg->length)
if (next_client_message == 0)
{
if (next_client_message == 0)
{
char kind = pgagroal_read_byte(msg->data + offset);
int length = pgagroal_read_int32(msg->data + offset + 1);
char kind = pgagroal_read_byte(msg->data + offset);
int length = pgagroal_read_int32(msg->data + offset + 1);

if (config->track_prepared_statements)
{
/* The P message tell us the prepared statement */
if (kind == 'P')
{
Expand All @@ -263,25 +258,32 @@ transaction_client(struct ev_loop* loop, struct ev_io* watcher, int revents)
deallocate = true;
}
}
}

/* Calculate the offset to the next message */
if (offset + length + 1 <= msg->length)
{
next_client_message = 0;
offset += length + 1;
}
else
{
next_client_message = length + 1 - (msg->length - offset);
offset = msg->length;
}
/* The Q and E message tell us the execute of the simple query and the prepared statement */
if (kind == 'Q' || kind == 'E')
{
pgagroal_prometheus_query_count_add();
pgagroal_prometheus_query_count_specified_add(wi->slot);
}

/* Calculate the offset to the next message */
if (offset + length + 1 <= msg->length)
{
next_client_message = 0;
offset += length + 1;
}
else
{
offset = MIN(next_client_message, msg->length);
next_client_message -= offset;
next_client_message = length + 1 - (msg->length - offset);
offset = msg->length;
}
}
else
{
offset = MIN(next_client_message, msg->length);
next_client_message -= offset;
}
}

status = pgagroal_write_socket_message(wi->server_fd, msg);
Expand Down
Loading

0 comments on commit d463bf3

Please sign in to comment.