Skip to content

Commit 8531aba

Browse files
authored
User defined configuration to run the query for different database server version from single yml file (#428)
* 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" * Fixed the below review comments given by Ashesh Vashi Instead of having db version string from yml file, user can define the range of database server version where query is to be executed. If user want to run the query on database version greater than 10.0.0, use below format. runonserver: ">=10.0.0" Below are the example of db version range user can defined in yml file. <=10.1.0 >=12.1.0 =11.0.0 <9.6.0 || >=11.0.0 * Remove the call from unused places where 'runOnServer' is not required. Only Server type hold that value. * Fix compilation issues. * Fix the issue with Debugln to print the database server version
1 parent 301976c commit 8531aba

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

cmd/postgres_exporter/postgres_exporter.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ type UserQuery struct {
117117
Metrics []Mapping `yaml:"metrics"`
118118
Master bool `yaml:"master"` // Querying only for master database
119119
CacheSeconds uint64 `yaml:"cache_seconds"` // Number of seconds to cache the namespace result metrics for.
120+
RunOnServer string `yaml:"runonserver"` // Querying to run on which server version
120121
}
121122

122123
// nolint: golint
@@ -922,9 +923,10 @@ type cachedMetrics struct {
922923
// Server describes a connection to Postgres.
923924
// Also it contains metrics map and query overrides.
924925
type Server struct {
925-
db *sql.DB
926-
labels prometheus.Labels
927-
master bool
926+
db *sql.DB
927+
labels prometheus.Labels
928+
master bool
929+
runonserver string
928930

929931
// Last version used to calculate metric map. If mismatch on scrape,
930932
// then maps are recalculated.
@@ -1449,6 +1451,16 @@ func queryNamespaceMappings(ch chan<- prometheus.Metric, server *Server) map[str
14491451
continue
14501452
}
14511453

1454+
// check if the query is to be run on specific database server version range or not
1455+
if len(server.runonserver) > 0 {
1456+
serVersion, _ := semver.Parse(server.lastMapVersion.String())
1457+
runServerRange, _ := semver.ParseRange(server.runonserver)
1458+
if !runServerRange(serVersion) {
1459+
log.Debugln("Query skipped for database version: ", server.lastMapVersion.String(), " as it should be run on database server version: ", server.runonserver)
1460+
continue
1461+
}
1462+
}
1463+
14521464
scrapeMetric := false
14531465
// Check if the metric is cached
14541466
server.cacheMtx.Lock()

0 commit comments

Comments
 (0)