Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Versions in queries.yaml #553

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/postgres_exporter/postgres_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func (s *FunctionalSuite) TestBooleanConversionToValueAndString(c *C) {
func (s *FunctionalSuite) TestParseUserQueries(c *C) {
userQueriesData, err := ioutil.ReadFile("./tests/user_queries_ok.yaml")
if err == nil {
metricMaps, newQueryOverrides, err := parseUserQueries(userQueriesData)
metricMaps, newQueryOverrides, err := parseUserQueries(userQueriesData, semver.Version{Major: 13})
c.Assert(err, Equals, nil)
c.Assert(metricMaps, NotNil)
c.Assert(newQueryOverrides, NotNil)
Expand Down
37 changes: 29 additions & 8 deletions cmd/postgres_exporter/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,32 @@ import (

// UserQuery represents a user defined query
type UserQuery struct {
Query string `yaml:"query"`
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
Query string `yaml:"query"`
Metrics []Mapping `yaml:"metrics"`
VersionQueries []VersionQ `yaml:"versionQueries"`
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
}

// UserQueries represents a set of UserQuery objects
type UserQueries map[string]UserQuery

func (uq *UserQuery) getVersionedQuery(pgVersion semver.Version) (string, error) {
r := uq.Query
if len(uq.VersionQueries) != 0 {
for i := range uq.VersionQueries {
if err := uq.VersionQueries[i].parseVerTolerant(); err != nil {
return "", err
}
if pgVersion.GE(uq.VersionQueries[i].ver) {
r = uq.VersionQueries[i].Query
}
}
}
return r, nil
}

// OverrideQuery 's are run in-place of simple namespace look ups, and provide
// advanced functionality. But they have a tendency to postgres version specific.
// There aren't too many versions, so we simply store customized versions using
Expand Down Expand Up @@ -197,7 +213,7 @@ func makeQueryOverrideMap(pgVersion semver.Version, queryOverrides map[string][]
return resultMap
}

func parseUserQueries(content []byte) (map[string]intermediateMetricMap, map[string]string, error) {
func parseUserQueries(content []byte, pgVersion semver.Version) (map[string]intermediateMetricMap, map[string]string, error) {
var userQueries UserQueries

err := yaml.Unmarshal(content, &userQueries)
Expand All @@ -211,7 +227,12 @@ func parseUserQueries(content []byte) (map[string]intermediateMetricMap, map[str

for metric, specs := range userQueries {
level.Debug(logger).Log("msg", "New user metric namespace from YAML metric", "metric", metric, "cache_seconds", specs.CacheSeconds)
newQueryOverrides[metric] = specs.Query
versionQuery, err := specs.getVersionedQuery(pgVersion)
if err != nil {
return nil, nil, err
}
newQueryOverrides[metric] = versionQuery

metricMap, ok := metricMaps[metric]
if !ok {
// Namespace for metric not found - add it.
Expand Down Expand Up @@ -251,7 +272,7 @@ func parseUserQueries(content []byte) (map[string]intermediateMetricMap, map[str
// TODO: test code for all cu.
// TODO: the YAML this supports is "non-standard" - we should move away from it.
func addQueries(content []byte, pgVersion semver.Version, server *Server) error {
metricMaps, newQueryOverrides, err := parseUserQueries(content)
metricMaps, newQueryOverrides, err := parseUserQueries(content, pgVersion)
if err != nil {
return err
}
Expand Down
32 changes: 32 additions & 0 deletions cmd/postgres_exporter/versionq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2021 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main

import (
"github.com/blang/semver"
)

type VersionQ struct {
Version string `yaml:"version"`
ver semver.Version
Query string `yaml:"query"`
}

func (v *VersionQ) parseVerTolerant() error {
bcVer, err := semver.ParseTolerant(v.Version)
if err != nil {
return err
}
v.ver = bcVer
return nil
}
4 changes: 4 additions & 0 deletions queries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ pg_database:
description: "Disk space used by the database"

pg_stat_statements:
versionQueries:
# should be in asc order by version
- version: '13.0.0'
query: "SELECT t2.rolname, t3.datname, queryid, calls, total_exec_time / 1000 as total_time_seconds, min_exec_time / 1000 as min_time_seconds, max_exec_time / 1000 as max_time_seconds, mean_exec_time / 1000 as mean_time_seconds, stddev_exec_time / 1000 as stddev_time_seconds, rows, shared_blks_hit, shared_blks_read, shared_blks_dirtied, shared_blks_written, local_blks_hit, local_blks_read, local_blks_dirtied, local_blks_written, temp_blks_read, temp_blks_written, blk_read_time / 1000 as blk_read_time_seconds, blk_write_time / 1000 as blk_write_time_seconds FROM pg_stat_statements t1 JOIN pg_roles t2 ON (t1.userid=t2.oid) JOIN pg_database t3 ON (t1.dbid=t3.oid) WHERE t2.rolname != 'rdsadmin'"
query: "SELECT t2.rolname, t3.datname, queryid, calls, total_time / 1000 as total_time_seconds, min_time / 1000 as min_time_seconds, max_time / 1000 as max_time_seconds, mean_time / 1000 as mean_time_seconds, stddev_time / 1000 as stddev_time_seconds, rows, shared_blks_hit, shared_blks_read, shared_blks_dirtied, shared_blks_written, local_blks_hit, local_blks_read, local_blks_dirtied, local_blks_written, temp_blks_read, temp_blks_written, blk_read_time / 1000 as blk_read_time_seconds, blk_write_time / 1000 as blk_write_time_seconds FROM pg_stat_statements t1 JOIN pg_roles t2 ON (t1.userid=t2.oid) JOIN pg_database t3 ON (t1.dbid=t3.oid) WHERE t2.rolname != 'rdsadmin'"
master: true
metrics:
Expand Down