Skip to content

Commit 769f487

Browse files
committed
Run the query for specific database version if provided from yml file.
By default query will run on all the databases if "runonserver" is not provided. If user want the query to be run on multiple database versions, use below string. runonserver: "9.5, 9.6" Example yml file as below. ( e.g. below query will run only on database version 9.5 ) pg_replication: query: "SELECT EXTRACT(EPOCH FROM (now() - pg_last_xact_replay_timestamp())) as lag" master: true runonserver: "9.5" metrics: - lag: usage: "GAUGE" description: "Replication lag behind master in seconds"
1 parent e2df41f commit 769f487

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

cmd/postgres_exporter/postgres_exporter.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ type UserQuery struct {
115115
Metrics []Mapping `yaml:"metrics"`
116116
Master bool `yaml:"master"` // Querying only for master database
117117
CacheSeconds uint64 `yaml:"cache_seconds"` // Number of seconds to cache the namespace result metrics for.
118+
RunOnServer string `yaml:"runonserver"` // Querying to run on which server version
118119
}
119120

120121
// nolint: golint
@@ -155,6 +156,7 @@ type intermediateMetricMap struct {
155156
columnMappings map[string]ColumnMapping
156157
master bool
157158
cacheSeconds uint64
159+
runonserver string
158160
}
159161

160162
// MetricMapNamespace groups metric maps under a shared set of labels.
@@ -163,6 +165,7 @@ type MetricMapNamespace struct {
163165
columnMappings map[string]MetricMap // Column mappings in this namespace
164166
master bool // Call query only for master database
165167
cacheSeconds uint64 // Number of seconds this metric namespace can be cached. 0 disables.
168+
runonserver string // Run the query on which server version
166169
}
167170

168171
// MetricMap stores the prometheus metric description which a given column will
@@ -221,6 +224,7 @@ var builtinMetricMaps = map[string]intermediateMetricMap{
221224
},
222225
true,
223226
0,
227+
"all",
224228
},
225229
"pg_stat_database": {
226230
map[string]ColumnMapping{
@@ -246,6 +250,7 @@ var builtinMetricMaps = map[string]intermediateMetricMap{
246250
},
247251
true,
248252
0,
253+
"all",
249254
},
250255
"pg_stat_database_conflicts": {
251256
map[string]ColumnMapping{
@@ -259,6 +264,7 @@ var builtinMetricMaps = map[string]intermediateMetricMap{
259264
},
260265
true,
261266
0,
267+
"all",
262268
},
263269
"pg_locks": {
264270
map[string]ColumnMapping{
@@ -268,6 +274,7 @@ var builtinMetricMaps = map[string]intermediateMetricMap{
268274
},
269275
true,
270276
0,
277+
"all",
271278
},
272279
"pg_stat_replication": {
273280
map[string]ColumnMapping{
@@ -314,6 +321,7 @@ var builtinMetricMaps = map[string]intermediateMetricMap{
314321
},
315322
true,
316323
0,
324+
"all",
317325
},
318326
"pg_stat_archiver": {
319327
map[string]ColumnMapping{
@@ -328,6 +336,7 @@ var builtinMetricMaps = map[string]intermediateMetricMap{
328336
},
329337
true,
330338
0,
339+
"all",
331340
},
332341
"pg_stat_activity": {
333342
map[string]ColumnMapping{
@@ -338,6 +347,7 @@ var builtinMetricMaps = map[string]intermediateMetricMap{
338347
},
339348
true,
340349
0,
350+
"all",
341351
},
342352
}
343353

@@ -494,6 +504,7 @@ func parseUserQueries(content []byte) (map[string]intermediateMetricMap, map[str
494504
return nil, nil, err
495505
}
496506

507+
497508
// Stores the loaded map representation
498509
metricMaps := make(map[string]intermediateMetricMap)
499510
newQueryOverrides := make(map[string]string)
@@ -509,6 +520,7 @@ func parseUserQueries(content []byte) (map[string]intermediateMetricMap, map[str
509520
columnMappings: newMetricMap,
510521
master: specs.Master,
511522
cacheSeconds: specs.CacheSeconds,
523+
runonserver: specs.RunOnServer,
512524
}
513525
metricMaps[metric] = metricMap
514526
}
@@ -678,7 +690,7 @@ func makeDescMap(pgVersion semver.Version, serverLabels prometheus.Labels, metri
678690
}
679691
}
680692

681-
metricMap[namespace] = MetricMapNamespace{variableLabels, thisMap, intermediateMappings.master, intermediateMappings.cacheSeconds}
693+
metricMap[namespace] = MetricMapNamespace{variableLabels, thisMap, intermediateMappings.master, intermediateMappings.cacheSeconds, intermediateMappings.runonserver}
682694
}
683695

684696
return metricMap
@@ -835,6 +847,8 @@ type Server struct {
835847
db *sql.DB
836848
labels prometheus.Labels
837849
master bool
850+
runonserver string
851+
serverversion string
838852

839853
// Last version used to calculate metric map. If mismatch on scrape,
840854
// then maps are recalculated.
@@ -880,6 +894,7 @@ func NewServer(dsn string, opts ...ServerOpt) (*Server, error) {
880894
s := &Server{
881895
db: db,
882896
master: false,
897+
runonserver: "all",
883898
labels: prometheus.Labels{
884899
serverLabelName: fingerprint,
885900
},
@@ -1327,6 +1342,12 @@ func queryNamespaceMappings(ch chan<- prometheus.Metric, server *Server) map[str
13271342
continue
13281343
}
13291344

1345+
// check if the query is to be run on specific server version or not
1346+
if len(mapping.runonserver) > 0 && !strings.Contains(mapping.runonserver, "all") && !strings.Contains(server.serverversion, mapping.runonserver) {
1347+
log.Debugln("Query skipped for database version: [%s] as it should be run on database server version: [%s]", server.serverversion, mapping.runonserver)
1348+
continue
1349+
}
1350+
13301351
scrapeMetric := false
13311352
// Check if the metric is cached
13321353
server.cacheMtx.Lock()
@@ -1415,6 +1436,7 @@ func (e *Exporter) checkMapVersions(ch chan<- prometheus.Metric, server *Server)
14151436
}
14161437

14171438
server.lastMapVersion = semanticVersion
1439+
server.serverversion = semanticVersion.String()
14181440

14191441
if e.userQueriesPath != "" {
14201442
// Clear the metric while a reload is happening

0 commit comments

Comments
 (0)