Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/prometheus/exporter/postgres/postgres.go
4096 views
1
package postgres
2
3
import (
4
"github.com/grafana/agent/component"
5
"github.com/grafana/agent/component/prometheus/exporter"
6
"github.com/grafana/agent/pkg/integrations"
7
"github.com/grafana/agent/pkg/integrations/postgres_exporter"
8
"github.com/grafana/agent/pkg/river/rivertypes"
9
config_util "github.com/prometheus/common/config"
10
)
11
12
func init() {
13
component.Register(component.Registration{
14
Name: "prometheus.exporter.postgres",
15
Args: Arguments{},
16
Exports: exporter.Exports{},
17
Build: exporter.New(createExporter, "postgres"),
18
})
19
}
20
21
func createExporter(opts component.Options, args component.Arguments) (integrations.Integration, error) {
22
a := args.(Arguments)
23
return a.Convert().NewIntegration(opts.Logger)
24
}
25
26
// DefaultArguments holds the default arguments for the prometheus.exporter.postgres
27
// component.
28
var DefaultArguments = Arguments{
29
DisableSettingsMetrics: false,
30
AutoDiscovery: AutoDiscovery{
31
Enabled: false,
32
},
33
DisableDefaultMetrics: false,
34
CustomQueriesConfigPath: "",
35
}
36
37
// Arguments configures the prometheus.exporter.postgres component
38
type Arguments struct {
39
// DataSourceNames to use to connect to Postgres. This is marked optional because it
40
// may also be supplied by the POSTGRES_EXPORTER_DATA_SOURCE_NAME env var,
41
// though it is not recommended to do so.
42
DataSourceNames []rivertypes.Secret `river:"data_source_names,attr,optional"`
43
44
// Attributes
45
DisableSettingsMetrics bool `river:"disable_settings_metrics,attr,optional"`
46
DisableDefaultMetrics bool `river:"disable_default_metrics,attr,optional"`
47
CustomQueriesConfigPath string `river:"custom_queries_config_path,attr,optional"`
48
49
// Blocks
50
AutoDiscovery AutoDiscovery `river:"autodiscovery,block,optional"`
51
}
52
53
// Autodiscovery controls discovery of databases outside any specified in DataSourceNames.
54
type AutoDiscovery struct {
55
Enabled bool `river:"enabled,attr,optional"`
56
DatabaseAllowlist []string `river:"database_allowlist,attr,optional"`
57
DatabaseDenylist []string `river:"database_denylist,attr,optional"`
58
}
59
60
// UnmarshalRiver implements River unmarshalling for Arguments.
61
func (a *Arguments) UnmarshalRiver(f func(interface{}) error) error {
62
*a = DefaultArguments
63
64
type args Arguments
65
return f((*args)(a))
66
}
67
68
func (a *Arguments) Convert() *postgres_exporter.Config {
69
return &postgres_exporter.Config{
70
DataSourceNames: a.convertDataSourceNames(),
71
DisableSettingsMetrics: a.DisableSettingsMetrics,
72
AutodiscoverDatabases: a.AutoDiscovery.Enabled,
73
ExcludeDatabases: a.AutoDiscovery.DatabaseDenylist,
74
IncludeDatabases: a.AutoDiscovery.DatabaseAllowlist,
75
DisableDefaultMetrics: a.DisableDefaultMetrics,
76
QueryPath: a.CustomQueriesConfigPath,
77
}
78
}
79
80
func (a *Arguments) convertDataSourceNames() []config_util.Secret {
81
dataSourceNames := make([]config_util.Secret, len(a.DataSourceNames))
82
for i, dataSourceName := range a.DataSourceNames {
83
dataSourceNames[i] = config_util.Secret(dataSourceName)
84
}
85
return dataSourceNames
86
}
87
88