Skip to content

Commit

Permalink
Enhance docs and test
Browse files Browse the repository at this point in the history
  • Loading branch information
thampiotr committed Apr 2, 2024
1 parent 1e0d149 commit f0d45a9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,35 @@ prometheus.exporter.postgres "LABEL" {
The following arguments are supported:

| Name | Type | Description | Default | Required |
| ---------------------------- | -------------- | ----------------------------------------------------------------------------- | ------- | -------- |
|------------------------------|----------------|-------------------------------------------------------------------------------|---------|----------|
| `data_source_names` | `list(secret)` | Specifies the Postgres server(s) to connect to. | | yes |
| `disable_settings_metrics` | `bool` | Disables collection of metrics from pg_settings. | `false` | no |
| `disable_default_metrics` | `bool` | When `true`, only exposes metrics supplied from `custom_queries_config_path`. | `false` | no |
| `custom_queries_config_path` | `string` | Path to YAML file containing custom queries to expose as metrics. | "" | no |
| `enabled_collectors` | `list(string)` | List of collectors to enable. See below for more detail. | [] | no |

The format for connection strings in `data_source_names` can be found in the [official postgresql documentation](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING).

See examples for the `custom_queries_config_path` file in the [postgres_exporter repository](https://github.com/prometheus-community/postgres_exporter/blob/master/queries.yaml).

**NOTE**: There are a number of environment variables that are not recommended for use, as they will affect _all_ `prometheus.exporter.postgres` components. A full list can be found in the [postgres_exporter repository](https://github.com/prometheus-community/postgres_exporter#environment-variables).

By default, the same set of metrics is enabled as in the upstream [postgres_exporter](https://github.com/prometheus-community/postgres_exporter/). If `custom_queries_config_path` is set, additional metrics defined in the given config file will be exposed.
If `disable_default_metrics` is set to `true`, only the metrics defined in the `custom_queries_config_path` file will be exposed.

A subset of metrics collectors can be controlled by setting the `enabled_collectors` argument. The following collectors are available for selection:
`database`, `database_wraparound`, `locks`, `long_running_transactions`, `postmaster`, `process_idle`,
`replication`, `replication_slot`, `stat_activity_autovacuum`, `stat_bgwriter`, `stat_database`,
`stat_statements`, `stat_user_tables`, `stat_wal_receiver`, `statio_user_indexes`, `statio_user_tables`,
`wal`, `xlog_location`.

By default, the following collectors are enabled: `database`, `locks`, `replication`, `replication_slot`, `stat_bgwriter`, `stat_database`,
`stat_user_tables`, `statio_user_tables`, `wal`.

{{< admonition type="note" >}}
Due to a limitation of the upstream exporter, when multiple `data_source_names` are used, the collectors that are controlled via the `enabled_collectors` argument will only be applied to the first data source in the list.
{{< /admonition >}}

## Blocks

The following blocks are supported:
Expand Down
7 changes: 3 additions & 4 deletions internal/component/prometheus/exporter/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func init() {

func createExporter(opts component.Options, args component.Arguments, defaultInstanceKey string) (integrations.Integration, string, error) {
a := args.(Arguments)
return integrations.NewIntegrationWithInstanceKey(opts.Logger, a.Convert(), defaultInstanceKey)
return integrations.NewIntegrationWithInstanceKey(opts.Logger, a.convert(opts.ID), defaultInstanceKey)
}

func parsePostgresURL(url string) (map[string]string, error) {
Expand Down Expand Up @@ -83,7 +83,6 @@ type Arguments struct {
DisableSettingsMetrics bool `river:"disable_settings_metrics,attr,optional"`
DisableDefaultMetrics bool `river:"disable_default_metrics,attr,optional"`
CustomQueriesConfigPath string `river:"custom_queries_config_path,attr,optional"`
Instance string `river:"instance,attr,optional"`
EnabledCollectors []string `river:"enabled_collectors,attr,optional"`

// Blocks
Expand Down Expand Up @@ -112,7 +111,7 @@ func (a *Arguments) SetToDefault() {
*a = DefaultArguments
}

func (a *Arguments) Convert() *postgres_exporter.Config {
func (a *Arguments) convert(instanceName string) *postgres_exporter.Config {
return &postgres_exporter.Config{
DataSourceNames: a.convertDataSourceNames(),
DisableSettingsMetrics: a.DisableSettingsMetrics,
Expand All @@ -121,7 +120,7 @@ func (a *Arguments) Convert() *postgres_exporter.Config {
IncludeDatabases: a.AutoDiscovery.DatabaseAllowlist,
DisableDefaultMetrics: a.DisableDefaultMetrics,
QueryPath: a.CustomQueriesConfigPath,
Instance: a.Instance,
Instance: instanceName,
EnabledCollectors: a.EnabledCollectors,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ func TestRiverConfigConvert(t *testing.T) {
var exampleRiverConfig = `
data_source_names = ["postgresql://username:password@localhost:5432/database?sslmode=disable"]
disable_settings_metrics = true
disable_default_metrics = true
disable_default_metrics = false
custom_queries_config_path = "/tmp/queries.yaml"
enabled_collectors = ["collector1", "collector2"]
autodiscovery {
enabled = false
Expand All @@ -59,16 +60,18 @@ func TestRiverConfigConvert(t *testing.T) {
err := river.Unmarshal([]byte(exampleRiverConfig), &args)
require.NoError(t, err)

c := args.Convert()
c := args.convert("test-instance")

expected := postgres_exporter.Config{
DataSourceNames: []config_util.Secret{config_util.Secret("postgresql://username:password@localhost:5432/database?sslmode=disable")},
DisableSettingsMetrics: true,
AutodiscoverDatabases: false,
ExcludeDatabases: []string{"exclude1", "exclude2"},
IncludeDatabases: []string{"include1"},
DisableDefaultMetrics: true,
DisableDefaultMetrics: false,
QueryPath: "/tmp/queries.yaml",
Instance: "test-instance",
EnabledCollectors: []string{"collector1", "collector2"},
}
require.Equal(t, expected, *c)
}
Expand Down

0 comments on commit f0d45a9

Please sign in to comment.