Skip to content

Commit

Permalink
Use correct SemVer range condition. Fixes #38 (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikschulz authored and wrouesnel committed Nov 28, 2016
1 parent 37e6dd3 commit 8f30886
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions postgres_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@ package main

import (
"database/sql"
"errors"
"flag"
"fmt"
"io/ioutil"
"math"
"net/http"
"os"
"strconv"
"time"
"regexp"
"errors"
"strconv"
"sync"
"time"

"gopkg.in/yaml.v2"

"github.com/blang/semver"
_ "github.com/lib/pq"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"github.com/blang/semver"

)

var Version string = "0.0.1"
Expand Down Expand Up @@ -116,10 +115,10 @@ func parseVersion(versionString string) (semver.Version, error) {

// User-friendly representation of a prometheus descriptor map
type ColumnMapping struct {
usage ColumnUsage `yaml:"usage"`
description string `yaml:"description"`
mapping map[string]float64 `yaml:"metric_mapping"` // Optional column mapping for MAPPEDMETRIC
supportedVersions semver.Range `yaml:"pg_version"` // Semantic version ranges which are supported. Unsupported columns are not queried (internally converted to DISCARD).
usage ColumnUsage `yaml:"usage"`
description string `yaml:"description"`
mapping map[string]float64 `yaml:"metric_mapping"` // Optional column mapping for MAPPEDMETRIC
supportedVersions semver.Range `yaml:"pg_version"` // Semantic version ranges which are supported. Unsupported columns are not queried (internally converted to DISCARD).
}

func (this *ColumnMapping) UnmarshalYAML(unmarshal func(interface{}) error) error {
Expand Down Expand Up @@ -232,7 +231,7 @@ var metricMaps = map[string]map[string]ColumnMapping{
"count": {GAUGE, "Number of locks", nil, nil},
},
"pg_stat_replication": map[string]ColumnMapping{
"procpid": {DISCARD, "Process ID of a WAL sender process", nil, semver.MustParseRange("<9.2.0")},
"procpid": {DISCARD, "Process ID of a WAL sender process", nil, semver.MustParseRange("<9.2.0")},
"pid": {DISCARD, "Process ID of a WAL sender process", nil, semver.MustParseRange(">=9.2.0")},
"usesysid": {DISCARD, "OID of the user logged into this WAL sender process", nil, nil},
"usename": {DISCARD, "Name of the user logged into this WAL sender process", nil, nil},
Expand Down Expand Up @@ -276,7 +275,7 @@ var metricMaps = map[string]map[string]ColumnMapping{
// the semver matching we do for columns.
type OverrideQuery struct {
versionRange semver.Range
query string
query string
}

// Overriding queries for namespaces above.
Expand Down Expand Up @@ -358,7 +357,6 @@ var queryOverrides = map[string][]OverrideQuery{
},
// No query is applicable for 9.1 that gives any sensible data.
},

}

// Convert the query override file to the version-specific query override file
Expand Down Expand Up @@ -509,7 +507,7 @@ func makeDescMap(pgVersion semver.Version, metricMaps map[string]map[string]Colu
// Check column version compatibility for the current map
// Force to discard if not compatible.
if columnMapping.supportedVersions != nil {
if columnMapping.supportedVersions(pgVersion) {
if !columnMapping.supportedVersions(pgVersion) {
thisMap[columnName] = MetricMap{
discard: true,
conversion: func(in interface{}) (float64, bool) {
Expand Down Expand Up @@ -683,26 +681,26 @@ func dbToString(t interface{}) (string, bool) {
// Exporter collects Postgres metrics. It implements prometheus.Collector.
type Exporter struct {
dsn string
userQueriesPath string
userQueriesPath string
duration, error prometheus.Gauge
totalScrapes prometheus.Counter

// Last version used to calculate metric map. If mismatch on scrape,
// then maps are recalculated.
lastMapVersion semver.Version
lastMapVersion semver.Version
// Currently active variable map
variableMap map[string]MetricMapNamespace
variableMap map[string]MetricMapNamespace
// Currently active metric map
metricMap map[string]MetricMapNamespace
metricMap map[string]MetricMapNamespace
// Currently active query overrides
queryOverrides map[string]string
mappingMtx sync.RWMutex
queryOverrides map[string]string
mappingMtx sync.RWMutex
}

// NewExporter returns a new PostgreSQL exporter for the provided DSN.
func NewExporter(dsn string, userQueriesPath string) *Exporter {
return &Exporter{
dsn: dsn,
dsn: dsn,
userQueriesPath: userQueriesPath,
duration: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Expand All @@ -722,8 +720,8 @@ func NewExporter(dsn string, userQueriesPath string) *Exporter {
Name: "last_scrape_error",
Help: "Whether the last scrape of metrics from PostgreSQL resulted in an error (1 for error, 0 for success).",
}),
variableMap: nil,
metricMap: nil,
variableMap: nil,
metricMap: nil,
queryOverrides: nil,
}
}
Expand Down Expand Up @@ -948,7 +946,7 @@ func (e *Exporter) checkMapVersions(ch chan<- prometheus.Metric, db *sql.DB) err
e.lastMapVersion = semanticVersion

if e.userQueriesPath != "" {
if err := addQueries(e.userQueriesPath, semanticVersion, e.metricMap, e.queryOverrides) ; err != nil {
if err := addQueries(e.userQueriesPath, semanticVersion, e.metricMap, e.queryOverrides); err != nil {
log.Errorln("Failed to reload user queries:", e.userQueriesPath, err)
}
}
Expand Down

0 comments on commit 8f30886

Please sign in to comment.