Skip to content

Commit 36f241a

Browse files
authored
Update postgres exporter, add podmonitor support (#63)
1 parent 70a98f1 commit 36f241a

File tree

9 files changed

+84
-301
lines changed

9 files changed

+84
-301
lines changed

charts/db-instances/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ apiVersion: v1
22
appVersion: "1.0"
33
description: Database Instances for db operator
44
name: db-instances
5-
version: 2.3.4
5+
version: 2.4.0

charts/db-instances/README.md

Lines changed: 1 addition & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -10,102 +10,7 @@ Database Instances for db operator
1010
|-----|------|---------|-------------|
1111
| dbinstances | object | `{}` | |
1212
| nodeSelector | object | `{}` | |
13-
| exporter.postgres.image | string | `"wrouesnel/postgres_exporter:latest"` | |
14-
| exporter.postgres.query.pg_postmaster.query | string | `"SELECT pg_postmaster_start_time as start_time_seconds from pg_postmaster_start_time()"` | |
15-
| exporter.postgres.query.pg_postmaster.metrics[0].start_time_seconds.usage | string | `"GAUGE"` | |
16-
| exporter.postgres.query.pg_postmaster.metrics[0].start_time_seconds.description | string | `"Time at which postmaster started"` | |
17-
| exporter.postgres.query.pg_stat_user_tables.query | string | `"SELECT schemaname, relname, seq_scan, seq_tup_read, idx_scan, idx_tup_fetch, n_tup_ins, n_tup_upd, n_tup_del, n_tup_hot_upd, n_live_tup, n_dead_tup, n_mod_since_analyze, last_vacuum, last_autovacuum, last_analyze, last_autoanalyze, vacuum_count, autovacuum_count, analyze_count, autoanalyze_count FROM pg_stat_user_tables"` | |
18-
| exporter.postgres.query.pg_stat_user_tables.metrics[0].schemaname.usage | string | `"LABEL"` | |
19-
| exporter.postgres.query.pg_stat_user_tables.metrics[0].schemaname.description | string | `"Name of the schema that this table is in"` | |
20-
| exporter.postgres.query.pg_stat_user_tables.metrics[1].relname.usage | string | `"LABEL"` | |
21-
| exporter.postgres.query.pg_stat_user_tables.metrics[1].relname.description | string | `"Name of this table"` | |
22-
| exporter.postgres.query.pg_stat_user_tables.metrics[2].seq_scan.usage | string | `"COUNTER"` | |
23-
| exporter.postgres.query.pg_stat_user_tables.metrics[2].seq_scan.description | string | `"Number of sequential scans initiated on this table"` | |
24-
| exporter.postgres.query.pg_stat_user_tables.metrics[3].seq_tup_read.usage | string | `"COUNTER"` | |
25-
| exporter.postgres.query.pg_stat_user_tables.metrics[3].seq_tup_read.description | string | `"Number of live rows fetched by sequential scans"` | |
26-
| exporter.postgres.query.pg_stat_user_tables.metrics[4].idx_scan.usage | string | `"COUNTER"` | |
27-
| exporter.postgres.query.pg_stat_user_tables.metrics[4].idx_scan.description | string | `"Number of index scans initiated on this table"` | |
28-
| exporter.postgres.query.pg_stat_user_tables.metrics[5].idx_tup_fetch.usage | string | `"COUNTER"` | |
29-
| exporter.postgres.query.pg_stat_user_tables.metrics[5].idx_tup_fetch.description | string | `"Number of live rows fetched by index scans"` | |
30-
| exporter.postgres.query.pg_stat_user_tables.metrics[6].n_tup_ins.usage | string | `"COUNTER"` | |
31-
| exporter.postgres.query.pg_stat_user_tables.metrics[6].n_tup_ins.description | string | `"Number of rows inserted"` | |
32-
| exporter.postgres.query.pg_stat_user_tables.metrics[7].n_tup_upd.usage | string | `"COUNTER"` | |
33-
| exporter.postgres.query.pg_stat_user_tables.metrics[7].n_tup_upd.description | string | `"Number of rows updated"` | |
34-
| exporter.postgres.query.pg_stat_user_tables.metrics[8].n_tup_del.usage | string | `"COUNTER"` | |
35-
| exporter.postgres.query.pg_stat_user_tables.metrics[8].n_tup_del.description | string | `"Number of rows deleted"` | |
36-
| exporter.postgres.query.pg_stat_user_tables.metrics[9].n_tup_hot_upd.usage | string | `"COUNTER"` | |
37-
| exporter.postgres.query.pg_stat_user_tables.metrics[9].n_tup_hot_upd.description | string | `"Number of rows HOT updated (i.e., with no separate index update required)"` | |
38-
| exporter.postgres.query.pg_stat_user_tables.metrics[10].n_live_tup.usage | string | `"GAUGE"` | |
39-
| exporter.postgres.query.pg_stat_user_tables.metrics[10].n_live_tup.description | string | `"Estimated number of live rows"` | |
40-
| exporter.postgres.query.pg_stat_user_tables.metrics[11].n_dead_tup.usage | string | `"GAUGE"` | |
41-
| exporter.postgres.query.pg_stat_user_tables.metrics[11].n_dead_tup.description | string | `"Estimated number of dead rows"` | |
42-
| exporter.postgres.query.pg_stat_user_tables.metrics[12].n_mod_since_analyze.usage | string | `"GAUGE"` | |
43-
| exporter.postgres.query.pg_stat_user_tables.metrics[12].n_mod_since_analyze.description | string | `"Estimated number of rows changed since last analyze"` | |
44-
| exporter.postgres.query.pg_stat_user_tables.metrics[13].last_vacuum.usage | string | `"GAUGE"` | |
45-
| exporter.postgres.query.pg_stat_user_tables.metrics[13].last_vacuum.description | string | `"Last time at which this table was manually vacuumed (not counting VACUUM FULL)"` | |
46-
| exporter.postgres.query.pg_stat_user_tables.metrics[14].last_autovacuum.usage | string | `"GAUGE"` | |
47-
| exporter.postgres.query.pg_stat_user_tables.metrics[14].last_autovacuum.description | string | `"Last time at which this table was vacuumed by the autovacuum daemon"` | |
48-
| exporter.postgres.query.pg_stat_user_tables.metrics[15].last_analyze.usage | string | `"GAUGE"` | |
49-
| exporter.postgres.query.pg_stat_user_tables.metrics[15].last_analyze.description | string | `"Last time at which this table was manually analyzed"` | |
50-
| exporter.postgres.query.pg_stat_user_tables.metrics[16].last_autoanalyze.usage | string | `"GAUGE"` | |
51-
| exporter.postgres.query.pg_stat_user_tables.metrics[16].last_autoanalyze.description | string | `"Last time at which this table was analyzed by the autovacuum daemon"` | |
52-
| exporter.postgres.query.pg_stat_user_tables.metrics[17].vacuum_count.usage | string | `"COUNTER"` | |
53-
| exporter.postgres.query.pg_stat_user_tables.metrics[17].vacuum_count.description | string | `"Number of times this table has been manually vacuumed (not counting VACUUM FULL)"` | |
54-
| exporter.postgres.query.pg_stat_user_tables.metrics[18].autovacuum_count.usage | string | `"COUNTER"` | |
55-
| exporter.postgres.query.pg_stat_user_tables.metrics[18].autovacuum_count.description | string | `"Number of times this table has been vacuumed by the autovacuum daemon"` | |
56-
| exporter.postgres.query.pg_stat_user_tables.metrics[19].analyze_count.usage | string | `"COUNTER"` | |
57-
| exporter.postgres.query.pg_stat_user_tables.metrics[19].analyze_count.description | string | `"Number of times this table has been manually analyzed"` | |
58-
| exporter.postgres.query.pg_stat_user_tables.metrics[20].autoanalyze_count.usage | string | `"COUNTER"` | |
59-
| exporter.postgres.query.pg_stat_user_tables.metrics[20].autoanalyze_count.description | string | `"Number of times this table has been analyzed by the autovacuum daemon"` | |
60-
| exporter.postgres.query.pg_database.query | string | `"SELECT pg_database.datname, pg_database_size(pg_database.datname) as size FROM pg_database"` | |
61-
| exporter.postgres.query.pg_database.metrics[0].datname.usage | string | `"LABEL"` | |
62-
| exporter.postgres.query.pg_database.metrics[0].datname.description | string | `"Name of the database"` | |
63-
| exporter.postgres.query.pg_database.metrics[1].size.usage | string | `"GAUGE"` | |
64-
| exporter.postgres.query.pg_database.metrics[1].size.description | string | `"Disk space used by the database"` | |
65-
| exporter.postgres.query.pg_stat_database.query | string | `"SELECT datname, numbackends, xact_commit, xact_rollback, blks_read, blks_hit, tup_fetched, tup_inserted, tup_updated, tup_deleted, temp_bytes, deadlocks FROM pg_catalog.pg_stat_database;"` | |
66-
| exporter.postgres.query.pg_stat_database.metrics[0].datname.usage | string | `"LABEL"` | |
67-
| exporter.postgres.query.pg_stat_database.metrics[0].datname.description | string | `"database NAME"` | |
68-
| exporter.postgres.query.pg_stat_database.metrics[1].numbackends.usage | string | `"COUNTER"` | |
69-
| exporter.postgres.query.pg_stat_database.metrics[1].numbackends.description | string | `"Number of backends currently connected to this database."` | |
70-
| exporter.postgres.query.pg_stat_database.metrics[2].xact_commit.usage | string | `"COUNTER"` | |
71-
| exporter.postgres.query.pg_stat_database.metrics[2].xact_commit.description | string | `"Number of transactions in this database that have been committed"` | |
72-
| exporter.postgres.query.pg_stat_database.metrics[3].xact_rollback.usage | string | `"COUNTER"` | |
73-
| exporter.postgres.query.pg_stat_database.metrics[3].xact_rollback.description | string | `"Number of transactions in this database that have been rolled back"` | |
74-
| exporter.postgres.query.pg_stat_database.metrics[4].blks_read.usage | string | `"GAUGE"` | |
75-
| exporter.postgres.query.pg_stat_database.metrics[4].blks_read.description | string | `"Number of disk blocks read in this database"` | |
76-
| exporter.postgres.query.pg_stat_database.metrics[5].blks_hit.usage | string | `"GAUGE"` | |
77-
| exporter.postgres.query.pg_stat_database.metrics[5].blks_hit.description | string | `"Number of times disk blocks were found already in the buffer cache, so that a read was not necessary"` | |
78-
| exporter.postgres.query.pg_stat_database.metrics[6].tup_fetched.usage | string | `"COUNTER"` | |
79-
| exporter.postgres.query.pg_stat_database.metrics[6].tup_fetched.description | string | `"Number of rows fetched by queries in this database"` | |
80-
| exporter.postgres.query.pg_stat_database.metrics[7].tup_inserted.usage | string | `"COUNTER"` | |
81-
| exporter.postgres.query.pg_stat_database.metrics[7].tup_inserted.description | string | `"Number of rows inserted by queries in this database"` | |
82-
| exporter.postgres.query.pg_stat_database.metrics[8].tup_updated.usage | string | `"COUNTER"` | |
83-
| exporter.postgres.query.pg_stat_database.metrics[8].tup_updated.description | string | `"Number of rows updated by queries in this database"` | |
84-
| exporter.postgres.query.pg_stat_database.metrics[9].tup_deleted.usage | string | `"COUNTER"` | |
85-
| exporter.postgres.query.pg_stat_database.metrics[9].tup_deleted.description | string | `"Number of rows deleted by queries in this database"` | |
86-
| exporter.postgres.query.pg_stat_database.metrics[10].temp_bytes.usage | string | `"GAUGE"` | |
87-
| exporter.postgres.query.pg_stat_database.metrics[10].temp_bytes.description | string | `"Total amount of data written to temporary files by queries in this database."` | |
88-
| exporter.postgres.query.pg_stat_database.metrics[11].deadlocks.usage | string | `"COUNTER"` | |
89-
| exporter.postgres.query.pg_stat_database.metrics[11].deadlocks.description | string | `"Number of deadlocks detected in this database"` | |
90-
| exporter.postgres.query.pg_stat_statements.query | string | `"SELECT userid, pgss.dbid, pgdb.datname, queryid, query, calls, total_time, mean_time, rows FROM pg_stat_statements pgss LEFT JOIN (select oid as dbid, datname from pg_database) as pgdb on pgdb.dbid = pgss.dbid WHERE not queryid isnull ORDER BY mean_time desc limit 20"` | |
91-
| exporter.postgres.query.pg_stat_statements.metrics[0].userid.usage | string | `"LABEL"` | |
92-
| exporter.postgres.query.pg_stat_statements.metrics[0].userid.description | string | `"User ID"` | |
93-
| exporter.postgres.query.pg_stat_statements.metrics[1].dbid.usage | string | `"LABEL"` | |
94-
| exporter.postgres.query.pg_stat_statements.metrics[1].dbid.description | string | `"database ID"` | |
95-
| exporter.postgres.query.pg_stat_statements.metrics[2].datname.usage | string | `"LABEL"` | |
96-
| exporter.postgres.query.pg_stat_statements.metrics[2].datname.description | string | `"database NAME"` | |
97-
| exporter.postgres.query.pg_stat_statements.metrics[3].queryid.usage | string | `"LABEL"` | |
98-
| exporter.postgres.query.pg_stat_statements.metrics[3].queryid.description | string | `"Query unique Hash Code"` | |
99-
| exporter.postgres.query.pg_stat_statements.metrics[4].query.usage | string | `"LABEL"` | |
100-
| exporter.postgres.query.pg_stat_statements.metrics[4].query.description | string | `"Query class"` | |
101-
| exporter.postgres.query.pg_stat_statements.metrics[5].calls.usage | string | `"COUNTER"` | |
102-
| exporter.postgres.query.pg_stat_statements.metrics[5].calls.description | string | `"Number of times executed"` | |
103-
| exporter.postgres.query.pg_stat_statements.metrics[6].total_time.usage | string | `"COUNTER"` | |
104-
| exporter.postgres.query.pg_stat_statements.metrics[6].total_time.description | string | `"Total time spent in the statement, in milliseconds"` | |
105-
| exporter.postgres.query.pg_stat_statements.metrics[7].mean_time.usage | string | `"GAUGE"` | |
106-
| exporter.postgres.query.pg_stat_statements.metrics[7].mean_time.description | string | `"Mean time spent in the statement, in milliseconds"` | |
107-
| exporter.postgres.query.pg_stat_statements.metrics[8].rows.usage | string | `"COUNTER"` | |
108-
| exporter.postgres.query.pg_stat_statements.metrics[8].rows.description | string | `"Total number of rows retrieved or affected by the statement"` | |
13+
| exporter.postgres.image | string | `"prometheuscommunity/postgres-exporter:v0.15.0"` | |
10914
| mysql.enabled | bool | `false` | |
11015
| postgresql.enabled | bool | `false` | |
11116
| tests | object | `{"serviceMonitor":{"enabled":false}}` | ------------------------------------------------------------------- |

charts/db-instances/ci/ci-1-values.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ dbinstances:
22
instance1:
33
serviceMonitor:
44
enabled: true
5+
podMonitor:
6+
enabled: true
57
engine: postgres
68
monitoring:
79
enabled: true

charts/db-instances/ci/ci-gen-test-service-monitor-values.yaml.tmpl renamed to charts/db-instances/ci/ci-gen-test-monitoring-values.yaml.tmpl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ dbinstances:
22
instance1:
33
serviceMonitor:
44
enabled: true
5+
podMonitor:
6+
enabled: true
57
engine: postgres
68
monitoring:
79
autodiscovery: true
@@ -13,6 +15,5 @@ dbinstances:
1315
adminUser: postgres
1416
adminPassword: 123123!!
1517
tests:
16-
serviceMonitor:
18+
monitoring:
1719
enabled: true
18-
instance: instance1

charts/db-instances/templates/postgres_exporter.yaml

Lines changed: 69 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{{- $root := . }}
22
{{- $nodeSelector := .Values.nodeSelector }}
3-
{{- $configSha := include (print $.Template.BasePath "/postgres_exporter_query.yaml") . | sha256sum }}
43
{{- $exporter := .Values.exporter.postgres }}
54
{{- $fullName := include "db-instances.fullname" . }}
65
{{- if .Values.dbinstances }}
@@ -16,16 +15,12 @@ spec:
1615
replicas: 1
1716
selector:
1817
matchLabels:
19-
{{- include "db-instances.selectorLabels" $ | nindent 6 }}
2018
db-instance: {{ $name }}
19+
{{- include "db-instances.selectorLabels" $ | nindent 6 }}
2120
strategy:
2221
type: Recreate
2322
template:
2423
metadata:
25-
annotations:
26-
prometheus.io/port: "60000"
27-
prometheus.io/scrape: "true"
28-
checksum/config: {{ $configSha }}
2924
labels:
3025
{{- include "db-instances.labels" $ | nindent 8 }}
3126
db-instance: {{ $name }}
@@ -41,28 +36,15 @@ spec:
4136
value: /run/secrets/db-secrets/{{ $value.monitoring.passwordKey | default "password" }}
4237
- name: DATA_SOURCE_USER_FILE
4338
value: /run/secrets/db-secrets/{{ $value.monitoring.usernameKey | default "user" }}
44-
- name: PG_EXPORTER_WEB_LISTEN_ADDRESS
45-
value: :60000
46-
- name: PG_EXPORTER_EXTEND_QUERY_PATH
47-
value: /run/cm/queries/queries.yaml
48-
- name: PG_EXPORTER_CONSTANT_LABELS
49-
value: dbinstance={{ $name }}
50-
{{- if $value.monitoring.autodiscovery }}
51-
- name: PG_EXPORTER_AUTO_DISCOVERY
52-
value: "true"
53-
{{- end }}
5439
image: {{ $exporter.image }}
5540
imagePullPolicy: Always
5641
name: exporter
5742
ports:
58-
- name: metrics
59-
containerPort: 60000
43+
- name: http
44+
containerPort: 9187
6045
volumeMounts:
6146
- mountPath: /run/secrets/db-secrets
6247
name: db-secrets
63-
- mountPath: /run/cm/queries/queries.yaml
64-
name: queries
65-
subPath: queries.yaml
6648
nodeSelector:
6749
{{ toYaml $nodeSelector | nindent 8 }}
6850
volumes:
@@ -74,35 +56,33 @@ spec:
7456
{{- else }}
7557
secretName: {{ $name }}-admin-secret
7658
{{- end }}
77-
- configMap:
78-
defaultMode: 420
79-
name: {{ $fullName }}-pgexporter-query
80-
name: queries
81-
{{- if $value.serviceMonitor.enabled }}
8259
---
8360
apiVersion: v1
8461
kind: Service
8562
metadata:
8663
name: dbinstance-{{ $name }}-pgexporter
8764
labels:
65+
db-instance: {{ $name }}
8866
{{- include "db-instances.labels" $ | nindent 4 }}
8967
spec:
9068
type: ClusterIP
9169
ports:
92-
- port: 8080
93-
targetPort: metrics
70+
- port: 80
71+
targetPort: http
9472
protocol: TCP
95-
name: metrics
73+
name: http
9674
selector:
9775
{{- include "db-instances.selectorLabels" $ | nindent 4 }}
9876
db-instance: {{ $name }}
99-
---
10077
{{- if $.Capabilities.APIVersions.Has "monitoring.coreos.com/v1" }}
78+
{{- if ($value.serviceMonitor).enabled }}
79+
---
10180
apiVersion: monitoring.coreos.com/v1
10281
kind: ServiceMonitor
10382
metadata:
10483
name: dbinstance-{{ $name }}-sm
10584
labels:
85+
db-instance: {{ $name }}
10686
{{- include "db-instances.labels" $ | nindent 4 }}
10787
{{- with $value.serviceMonitor.selector }}
10888
{{- toYaml . | nindent 4 }}
@@ -112,7 +92,7 @@ spec:
11292
jobLabel: {{ $value.serviceMonitor.jobLabel }}
11393
{{- end }}
11494
endpoints:
115-
- port: metrics
95+
- port: http
11696
{{- with $value.serviceMonitor.interval }}
11797
interval: {{ . }}
11898
{{- end }}
@@ -136,10 +116,67 @@ spec:
136116
- {{ $.Release.Namespace }}
137117
selector:
138118
matchLabels:
139-
{{- include "db-instances.selectorLabels" $ | nindent 6 }}
119+
db-instance: {{ $name }}
120+
{{- if $value.serviceMonitor.targetLabels }}
121+
targetLabels:
122+
{{- range $value.serviceMonitor.targetLabels }}
123+
- {{ . }}
124+
{{- end }}
125+
{{- end }}
126+
{{- end }}
127+
{{- end }}
128+
{{- if $.Capabilities.APIVersions.Has "monitoring.coreos.com/v1" }}
129+
{{- if ($value.podMonitor).enabled }}
130+
---
131+
apiVersion: monitoring.coreos.com/v1
132+
kind: PodMonitor
133+
metadata:
134+
name: dbinstance-{{ $name }}-pm
135+
labels:
136+
db-instance: {{ $name }}
137+
{{- include "db-instances.labels" $ | nindent 4 }}
138+
{{- with $value.podMonitor.selector }}
139+
{{- toYaml . | nindent 4 }}
140+
{{- end }}
141+
spec:
142+
{{- if $value.podMonitor.jobLabel }}
143+
jobLabel: {{ $value.podMonitor.jobLabel }}
144+
{{- end }}
145+
podMetricsEndpoints:
146+
- port: http
147+
{{- with $value.podMonitor.interval }}
148+
interval: {{ . }}
149+
{{- end }}
150+
{{- with $value.podMonitor.scrapeTimeout }}
151+
scrapeTimeout: {{ . }}
152+
{{- end }}
153+
{{- with $value.podMonitor.metricRelabelings }}
154+
metricRelabelings:
155+
{{- if kindIs "string" . }}
156+
{{- tpl . $ | nindent 8 }}
157+
{{- else }}
158+
{{- toYaml . | nindent 8 }}
159+
{{- end }}
160+
{{- end }}
161+
{{- with $value.podMonitor.relabelings }}
162+
relabelings:
163+
{{- toYaml . | nindent 8 }}
164+
{{- end }}
165+
namespaceSelector:
166+
matchNames:
167+
- {{ $.Release.Namespace }}
168+
selector:
169+
matchLabels:
170+
db-instance: {{ $name }}
171+
{{- if $value.podMonitor.podTargetLabels }}
172+
podTargetLabels:
173+
{{- range $value.podMonitor.podTargetLabels }}
174+
- {{ . }}
175+
{{- end }}
140176
{{- end }}
141177
{{- end }}
142178
{{- end }}
143179
{{- end }}
144180
{{- end }}
145181
{{- end }}
182+
{{- end }}

charts/db-instances/templates/postgres_exporter_query.yaml

Lines changed: 0 additions & 10 deletions
This file was deleted.

charts/db-instances/templates/tests/test_service_monitor.yaml renamed to charts/db-instances/templates/tests/test_monitoring.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
{{- if (((.Values.tests).serviceMonitor).enabled) }}
1+
{{- if (.Values.tests.monitoring).enabled }}
22
# ---------------------------------------------------------------------
3-
# -- A config map with a script to test serviceMonitor
3+
# -- A config map with a script to test monitoring
44
# ---------------------------------------------------------------------
55
---
66
apiVersion: v1
@@ -40,9 +40,9 @@ spec:
4040
volumeMounts:
4141
- name: test-script
4242
readOnly: true
43-
mountPath: /test_service_monitor
44-
subPath: test_service_monitor
43+
mountPath: /test_monitoring
44+
subPath: test_monitoring
4545
args:
46-
- /test.sh
46+
- /test_monitoring
4747
restartPolicy: Never
4848
{{- end}}

0 commit comments

Comments
 (0)