Fix activity query

* Modify the activity query to ensure each state is always reported.

* Fix the activity preprocessor to unwrap the value from the array
  containing it.
This commit is contained in:
James Campbell 2025-10-18 12:49:09 -04:00
parent 175fd85f9f
commit 8182ad75b9
Signed by: james
GPG Key ID: 2287C33A40DC906A
2 changed files with 37 additions and 21 deletions

View File

@ -196,12 +196,28 @@ metrics:
type: set
query:
0: >
SELECT state,
count(*) AS backend_count,
COALESCE(EXTRACT(EPOCH FROM max(now() - state_change)), 0) AS max_state_time
FROM pg_stat_activity
WHERE datname = %(dbname)s
GROUP BY state
SELECT
states.state,
COALESCE(a.backend_count, 0) AS backend_count,
COALESCE(a.max_state_time, 0) AS max_state_time
FROM (
SELECT state,
count(*) AS backend_count,
COALESCE(EXTRACT(EPOCH FROM now() - min(state_change)), 0) AS max_state_time
FROM pg_stat_activity
WHERE datname = %(dbname)s
GROUP BY state
) AS a
RIGHT JOIN
unnest(ARRAY[
'active',
'idle',
'idle in transaction',
'idle in transaction (aborted)',
'fastpath function call',
'disabled'
]) AS states(state)
USING (state);
test_args:
dbname: postgres
@ -235,7 +251,7 @@ metrics:
locks:
type: row
query:
0:
0: >
SELECT COUNT(*) AS total,
SUM(CASE WHEN granted THEN 1 ELSE 0 END) AS granted
FROM pg_locks

View File

@ -411,7 +411,7 @@ zabbix_export:
preprocessing:
- type: JSONPATH
parameters:
- '$[?(@.state == "active")].max_state_time'
- '$[?(@.state == "active")].max_state_time.first()'
master_item:
key: 'pgmon_connection_states[{#DBNAME}]'
tags:
@ -429,7 +429,7 @@ zabbix_export:
preprocessing:
- type: JSONPATH
parameters:
- '$[?(@.state == "active")].backend_count'
- '$[?(@.state == "active")].backend_count.first()'
master_item:
key: 'pgmon_connection_states[{#DBNAME}]'
tags:
@ -448,7 +448,7 @@ zabbix_export:
preprocessing:
- type: JSONPATH
parameters:
- '$[?(@.state == "disabled")].max_state_time'
- '$[?(@.state == "disabled")].max_state_time.first()'
master_item:
key: 'pgmon_connection_states[{#DBNAME}]'
tags:
@ -466,7 +466,7 @@ zabbix_export:
preprocessing:
- type: JSONPATH
parameters:
- '$[?(@.state == "disabled")].backend_count'
- '$[?(@.state == "disabled")].backend_count.first()'
master_item:
key: 'pgmon_connection_states[{#DBNAME}]'
tags:
@ -485,7 +485,7 @@ zabbix_export:
preprocessing:
- type: JSONPATH
parameters:
- '$[?(@.state == "fastpath function call")].max_state_time'
- '$[?(@.state == "fastpath function call")].max_state_time.first()'
master_item:
key: 'pgmon_connection_states[{#DBNAME}]'
tags:
@ -503,7 +503,7 @@ zabbix_export:
preprocessing:
- type: JSONPATH
parameters:
- '$[?(@.state == "fastpath function call")].backend_count'
- '$[?(@.state == "fastpath function call")].backend_count.first()'
master_item:
key: 'pgmon_connection_states[{#DBNAME}]'
tags:
@ -522,7 +522,7 @@ zabbix_export:
preprocessing:
- type: JSONPATH
parameters:
- '$[?(@.state == "idle")].max_state_time'
- '$[?(@.state == "idle")].max_state_time.first()'
master_item:
key: 'pgmon_connection_states[{#DBNAME}]'
tags:
@ -540,7 +540,7 @@ zabbix_export:
preprocessing:
- type: JSONPATH
parameters:
- '$[?(@.state == "idle")].backend_count'
- '$[?(@.state == "idle")].backend_count.first()'
master_item:
key: 'pgmon_connection_states[{#DBNAME}]'
tags:
@ -559,7 +559,7 @@ zabbix_export:
preprocessing:
- type: JSONPATH
parameters:
- '$[?(@.state == "idle in transaction (aborted)")].max_state_time'
- '$[?(@.state == "idle in transaction (aborted)")].max_state_time.first()'
master_item:
key: 'pgmon_connection_states[{#DBNAME}]'
tags:
@ -577,7 +577,7 @@ zabbix_export:
preprocessing:
- type: JSONPATH
parameters:
- '$[?(@.state == "idle in transaction (aborted)")].backend_count'
- '$[?(@.state == "idle in transaction (aborted)")].backend_count.first()'
master_item:
key: 'pgmon_connection_states[{#DBNAME}]'
tags:
@ -596,7 +596,7 @@ zabbix_export:
preprocessing:
- type: JSONPATH
parameters:
- '$[?(@.state == "idle in transaction")].max_state_time'
- '$[?(@.state == "idle in transaction")].max_state_time.first()'
master_item:
key: 'pgmon_connection_states[{#DBNAME}]'
tags:
@ -614,7 +614,7 @@ zabbix_export:
preprocessing:
- type: JSONPATH
parameters:
- '$[?(@.state == "idle in transaction")].backend_count'
- '$[?(@.state == "idle in transaction")].backend_count.first()'
master_item:
key: 'pgmon_connection_states[{#DBNAME}]'
tags:
@ -633,7 +633,7 @@ zabbix_export:
preprocessing:
- type: JSONPATH
parameters:
- '$[?(@.state == "starting")].max_state_time'
- '$[?(@.state == "starting")].max_state_time.first()'
master_item:
key: 'pgmon_connection_states[{#DBNAME}]'
tags:
@ -651,7 +651,7 @@ zabbix_export:
preprocessing:
- type: JSONPATH
parameters:
- '$[?(@.state == "starting")].backend_count'
- '$[?(@.state == "starting")].backend_count.first()'
master_item:
key: 'pgmon_connection_states[{#DBNAME}]'
tags: