2022-02-15 02:12:27 +00:00
|
|
|
// Copyright 2022 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 collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2023-03-21 07:21:49 +00:00
|
|
|
"github.com/alecthomas/kingpin/v2"
|
2022-02-15 02:12:27 +00:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-10-04 02:30:07 +00:00
|
|
|
factories = make(map[string]func(collectorConfig) (Collector, error))
|
2022-02-15 02:12:27 +00:00
|
|
|
initiatedCollectorsMtx = sync.Mutex{}
|
|
|
|
initiatedCollectors = make(map[string]Collector)
|
|
|
|
collectorState = make(map[string]*bool)
|
|
|
|
forcedCollectors = map[string]bool{} // collectors which have been explicitly enabled or disabled
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Namespace for all metrics.
|
|
|
|
namespace = "pg"
|
|
|
|
|
2023-06-08 21:28:04 +00:00
|
|
|
defaultEnabled = true
|
|
|
|
defaultDisabled = false
|
2022-02-15 02:12:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
scrapeDurationDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "scrape", "collector_duration_seconds"),
|
|
|
|
"postgres_exporter: Duration of a collector scrape.",
|
|
|
|
[]string{"collector"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
scrapeSuccessDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "scrape", "collector_success"),
|
|
|
|
"postgres_exporter: Whether a collector succeeded.",
|
|
|
|
[]string{"collector"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
type Collector interface {
|
2023-03-30 01:25:27 +00:00
|
|
|
Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error
|
2022-02-15 02:12:27 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 02:30:07 +00:00
|
|
|
type collectorConfig struct {
|
|
|
|
logger log.Logger
|
|
|
|
excludeDatabases []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerCollector(name string, isDefaultEnabled bool, createFunc func(collectorConfig) (Collector, error)) {
|
2022-02-15 02:12:27 +00:00
|
|
|
var helpDefaultState string
|
|
|
|
if isDefaultEnabled {
|
|
|
|
helpDefaultState = "enabled"
|
|
|
|
} else {
|
|
|
|
helpDefaultState = "disabled"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create flag for this collector
|
|
|
|
flagName := fmt.Sprintf("collector.%s", name)
|
|
|
|
flagHelp := fmt.Sprintf("Enable the %s collector (default: %s).", name, helpDefaultState)
|
|
|
|
defaultValue := fmt.Sprintf("%v", isDefaultEnabled)
|
|
|
|
|
|
|
|
flag := kingpin.Flag(flagName, flagHelp).Default(defaultValue).Action(collectorFlagAction(name)).Bool()
|
|
|
|
collectorState[name] = flag
|
|
|
|
|
|
|
|
// Register the create function for this collector
|
|
|
|
factories[name] = createFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostgresCollector implements the prometheus.Collector interface.
|
|
|
|
type PostgresCollector struct {
|
|
|
|
Collectors map[string]Collector
|
|
|
|
logger log.Logger
|
|
|
|
|
2023-03-30 01:25:27 +00:00
|
|
|
instance *instance
|
2022-02-15 02:12:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 02:07:23 +00:00
|
|
|
type Option func(*PostgresCollector) error
|
|
|
|
|
2022-02-15 02:12:27 +00:00
|
|
|
// NewPostgresCollector creates a new PostgresCollector.
|
2022-10-04 02:30:07 +00:00
|
|
|
func NewPostgresCollector(logger log.Logger, excludeDatabases []string, dsn string, filters []string, options ...Option) (*PostgresCollector, error) {
|
2021-07-07 02:07:23 +00:00
|
|
|
p := &PostgresCollector{
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
// Apply options to customize the collector
|
|
|
|
for _, o := range options {
|
|
|
|
err := o(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-15 02:12:27 +00:00
|
|
|
f := make(map[string]bool)
|
|
|
|
for _, filter := range filters {
|
|
|
|
enabled, exist := collectorState[filter]
|
|
|
|
if !exist {
|
|
|
|
return nil, fmt.Errorf("missing collector: %s", filter)
|
|
|
|
}
|
|
|
|
if !*enabled {
|
|
|
|
return nil, fmt.Errorf("disabled collector: %s", filter)
|
|
|
|
}
|
|
|
|
f[filter] = true
|
|
|
|
}
|
|
|
|
collectors := make(map[string]Collector)
|
|
|
|
initiatedCollectorsMtx.Lock()
|
|
|
|
defer initiatedCollectorsMtx.Unlock()
|
|
|
|
for key, enabled := range collectorState {
|
|
|
|
if !*enabled || (len(f) > 0 && !f[key]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if collector, ok := initiatedCollectors[key]; ok {
|
|
|
|
collectors[key] = collector
|
|
|
|
} else {
|
2022-10-04 02:30:07 +00:00
|
|
|
collector, err := factories[key](collectorConfig{
|
|
|
|
logger: log.With(logger, "collector", key),
|
|
|
|
excludeDatabases: excludeDatabases,
|
|
|
|
})
|
2022-02-15 02:12:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
collectors[key] = collector
|
|
|
|
initiatedCollectors[key] = collector
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 02:07:23 +00:00
|
|
|
p.Collectors = collectors
|
|
|
|
|
2022-02-25 16:45:15 +00:00
|
|
|
if dsn == "" {
|
|
|
|
return nil, errors.New("empty dsn")
|
|
|
|
}
|
2021-07-07 02:07:23 +00:00
|
|
|
|
2023-03-30 01:25:27 +00:00
|
|
|
instance, err := newInstance(dsn)
|
2022-02-25 16:45:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-15 02:12:27 +00:00
|
|
|
}
|
2023-03-30 01:25:27 +00:00
|
|
|
p.instance = instance
|
2021-07-07 02:07:23 +00:00
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2022-02-15 02:12:27 +00:00
|
|
|
// Describe implements the prometheus.Collector interface.
|
2021-07-07 02:07:23 +00:00
|
|
|
func (p PostgresCollector) Describe(ch chan<- *prometheus.Desc) {
|
2022-02-15 02:12:27 +00:00
|
|
|
ch <- scrapeDurationDesc
|
|
|
|
ch <- scrapeSuccessDesc
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect implements the prometheus.Collector interface.
|
2021-07-07 02:07:23 +00:00
|
|
|
func (p PostgresCollector) Collect(ch chan<- prometheus.Metric) {
|
2022-02-15 02:12:27 +00:00
|
|
|
ctx := context.TODO()
|
|
|
|
wg := sync.WaitGroup{}
|
2021-07-07 02:07:23 +00:00
|
|
|
wg.Add(len(p.Collectors))
|
|
|
|
for name, c := range p.Collectors {
|
2022-02-15 02:12:27 +00:00
|
|
|
go func(name string, c Collector) {
|
2023-03-30 01:25:27 +00:00
|
|
|
execute(ctx, name, c, p.instance, ch, p.logger)
|
2022-02-15 02:12:27 +00:00
|
|
|
wg.Done()
|
|
|
|
}(name, c)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2023-03-30 01:25:27 +00:00
|
|
|
func execute(ctx context.Context, name string, c Collector, instance *instance, ch chan<- prometheus.Metric, logger log.Logger) {
|
2022-02-15 02:12:27 +00:00
|
|
|
begin := time.Now()
|
2023-03-30 01:25:27 +00:00
|
|
|
err := c.Update(ctx, instance, ch)
|
2022-02-15 02:12:27 +00:00
|
|
|
duration := time.Since(begin)
|
|
|
|
var success float64
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if IsNoDataError(err) {
|
|
|
|
level.Debug(logger).Log("msg", "collector returned no data", "name", name, "duration_seconds", duration.Seconds(), "err", err)
|
|
|
|
} else {
|
|
|
|
level.Error(logger).Log("msg", "collector failed", "name", name, "duration_seconds", duration.Seconds(), "err", err)
|
|
|
|
}
|
|
|
|
success = 0
|
|
|
|
} else {
|
|
|
|
level.Debug(logger).Log("msg", "collector succeeded", "name", name, "duration_seconds", duration.Seconds())
|
|
|
|
success = 1
|
|
|
|
}
|
|
|
|
ch <- prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, duration.Seconds(), name)
|
|
|
|
ch <- prometheus.MustNewConstMetric(scrapeSuccessDesc, prometheus.GaugeValue, success, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// collectorFlagAction generates a new action function for the given collector
|
|
|
|
// to track whether it has been explicitly enabled or disabled from the command line.
|
|
|
|
// A new action function is needed for each collector flag because the ParseContext
|
|
|
|
// does not contain information about which flag called the action.
|
|
|
|
// See: https://github.com/alecthomas/kingpin/issues/294
|
|
|
|
func collectorFlagAction(collector string) func(ctx *kingpin.ParseContext) error {
|
|
|
|
return func(ctx *kingpin.ParseContext) error {
|
|
|
|
forcedCollectors[collector] = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrNoData indicates the collector found no data to collect, but had no other error.
|
|
|
|
var ErrNoData = errors.New("collector returned no data")
|
|
|
|
|
|
|
|
func IsNoDataError(err error) bool {
|
|
|
|
return err == ErrNoData
|
|
|
|
}
|