-
Notifications
You must be signed in to change notification settings - Fork 485
/
postgres_exporter.go
70 lines (58 loc) · 2.32 KB
/
postgres_exporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Package postgres_exporter embeds https://github.com/prometheus/postgres_exporter
package postgres_exporter //nolint:golint
import (
"fmt"
"os"
"strings"
"github.com/go-kit/kit/log"
"github.com/grafana/agent/pkg/integrations"
"github.com/grafana/agent/pkg/integrations/config"
"github.com/wrouesnel/postgres_exporter/exporter"
)
// Config controls the postgres_exporter integration.
type Config struct {
Common config.Common `yaml:",inline"`
// DataSourceNames to use to connect to Postgres.
DataSourceNames []string `yaml:"data_source_names,omitempty"`
DisableSettingsMetrics bool `yaml:"disable_settings_metrics,omitempty"`
AutodiscoverDatabases bool `yaml:"autodiscover_databases,omitempty"`
ExcludeDatabases []string `yaml:"exclude_databases,omitempty"`
DisableDefaultMetrics bool `yaml:"disable_default_metrics,omitempty"`
QueryPath string `yaml:"query_path,omitempty"`
}
// Name returns the name of the integration this config is for.
func (c *Config) Name() string {
return "postgres_exporter"
}
// CommonConfig returns the common set of options shared across all configs for
// integrations.
func (c *Config) CommonConfig() config.Common {
return c.Common
}
// NewIntegration converts this config into an instance of a configuration.
func (c *Config) NewIntegration(l log.Logger) (integrations.Integration, error) {
return New(l, c)
}
func init() {
integrations.RegisterIntegration(&Config{})
}
// New creates a new postgres_exporter integration. The integration scrapes
// metrics from a postgres process.
func New(log log.Logger, c *Config) (integrations.Integration, error) {
dsn := c.DataSourceNames
if len(dsn) == 0 {
dsn = strings.Split(os.Getenv("POSTGRES_EXPORTER_DATA_SOURCE_NAME"), ",")
}
if len(dsn) == 0 {
return nil, fmt.Errorf("cannot create postgres_exporter; neither postgres_exporter.data_source_name or $POSTGRES_EXPORTER_DATA_SOURCE_NAME is set")
}
e := exporter.NewExporter(
dsn,
exporter.DisableDefaultMetrics(c.DisableDefaultMetrics),
exporter.WithUserQueriesPath(c.QueryPath),
exporter.DisableSettingsMetrics(c.DisableSettingsMetrics),
exporter.AutoDiscoverDatabases(c.AutodiscoverDatabases),
exporter.ExcludeDatabases(strings.Join(c.ExcludeDatabases, ",")),
)
return integrations.NewCollectorIntegration(c.Name(), integrations.WithCollectors(e)), nil
}