Skip to content

Commit

Permalink
User defined configuration to run the query for different database se…
Browse files Browse the repository at this point in the history
…rver 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
  • Loading branch information
neel5481 committed Feb 17, 2021
1 parent 301976c commit 8531aba
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions cmd/postgres_exporter/postgres_exporter.go
Expand Up @@ -117,6 +117,7 @@ type UserQuery struct {
Metrics []Mapping `yaml:"metrics"`
Master bool `yaml:"master"` // Querying only for master database
CacheSeconds uint64 `yaml:"cache_seconds"` // Number of seconds to cache the namespace result metrics for.
RunOnServer string `yaml:"runonserver"` // Querying to run on which server version
}

// nolint: golint
Expand Down Expand Up @@ -922,9 +923,10 @@ type cachedMetrics struct {
// Server describes a connection to Postgres.
// Also it contains metrics map and query overrides.
type Server struct {
db *sql.DB
labels prometheus.Labels
master bool
db *sql.DB
labels prometheus.Labels
master bool
runonserver string

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

// check if the query is to be run on specific database server version range or not
if len(server.runonserver) > 0 {
serVersion, _ := semver.Parse(server.lastMapVersion.String())
runServerRange, _ := semver.ParseRange(server.runonserver)
if !runServerRange(serVersion) {
log.Debugln("Query skipped for database version: ", server.lastMapVersion.String(), " as it should be run on database server version: ", server.runonserver)
continue
}
}

scrapeMetric := false
// Check if the metric is cached
server.cacheMtx.Lock()
Expand Down

0 comments on commit 8531aba

Please sign in to comment.