Skip to content

Commit 875ce0f

Browse files
SimonSerranoclayton-cornellcristiangreco
authored
postgres_exporter: expose stat_statements.include_query (#4372)
* refactor: added stat_statements component configuration * test: added stat_statements to unmarshal test * refactor: override kingpin flags to pass stat_statements flags * docs(changelog): updated changelog (#4004) * refactor: added stat_statements component configuration * test: added stat_statements to unmarshal test * refactor: override kingpin flags to pass stat_statements flags * docs(changelog): updated changelog (#4004) * docs(changelog): refined changelog entry (#4004) * docs(reference): added stat_statements documentation * docs(prostgres exporter): query length default is 120 * refactor(postgres exporter): namespaced stat statements config for integration #4004 * refactor(postgres exporter): renamed variable for clarity #4004 * test(postgres exporter): indent using spaces #4004 * docs(postgres exporter): format documentation #4004 Co-authored-by: Clayton Cornell <[email protected]> * docs(postgres exporter): format documentation #4004 Co-authored-by: Clayton Cornell <[email protected]> * docs(postgres exporter): format array * refactor: added stat_statements component configuration * test: added stat_statements to unmarshal test * refactor: override kingpin flags to pass stat_statements flags * docs(changelog): updated changelog (#4004) * docs(changelog): refined changelog entry (#4004) * docs(reference): added stat_statements documentation * docs(prostgres exporter): query length default is 120 * refactor(postgres exporter): namespaced stat statements config for integration #4004 * refactor(postgres exporter): renamed variable for clarity #4004 * test(postgres exporter): indent using spaces #4004 * docs(postgres exporter): format array * docs(postgres exporter): format documentation #4004 Co-authored-by: Clayton Cornell <[email protected]> * test(postgres exporter): indent using tabs * refactor(postgres exporter): check for errors when setting kingpin flags #4004 * docs(postgres exporter): typo in code documentation #4004 Co-authored-by: Cristian Greco <[email protected]> * test(postgres exporter): added QueryLength default to expected postgres exporter integration config * test(postgres exporter): converter includes empty stat_statements by default * Revert "test(postgres exporter): converter includes empty stat_statements by default" This reverts commit 2bc5c1a. * refactor(postgres exporter): flattened namespaced config to match other components of same types #4004 * Revert "refactor(postgres exporter): flattened namespaced config to match other components of same types #4004" This reverts commit 672fadc. * make field nullable * docs(postgres exporter): removed duplicate line #4004 * docs(changelog): moved to unreleased section --------- Co-authored-by: Clayton Cornell <[email protected]> Co-authored-by: Cristian Greco <[email protected]>
1 parent 612bbb3 commit 875ce0f

File tree

5 files changed

+78
-7
lines changed

5 files changed

+78
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Main (unreleased)
3333

3434
- Send remote config status to the remote server for the remotecfg service. (@erikbaranowski)
3535

36+
- Add a `stat_statements` configuration block to the `prometheus.exporter.postgres` component to enable selecting both the query ID and the full SQL statement. The new block includes one option to enable statement selection, and another to configure the maximum length of the statement text. (@SimonSerrano)
37+
3638
### Enhancements
3739

3840
- Add support of `tls` in components `loki.source.(awsfirehose|gcplog|heroku|api)` and `prometheus.receive_http` and `pyroscope.receive_http`. (@fgouteroux)

docs/sources/reference/components/prometheus/prometheus.exporter.postgres.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ If `enabled` is set to `true` and no allowlist or denylist is specified, the exp
126126

127127
If `autodiscovery` is disabled, neither `database_allowlist` nor `database_denylist` has any effect.
128128

129+
### `stat_statements`
130+
131+
The `stat_statements` block configures the selection of both the query ID and the full SQL statement.
132+
This configuration takes effect only when the `stat_statements` collector is enabled.
133+
134+
The following arguments are supported:
135+
| Name | Type | Description | Default | Required |
136+
| --------------- | -------- | --------------------------------------------------- | ------- | -------- |
137+
| `include_query` | `bool` | Enable the selection of query ID and SQL statement. | `false` | no |
138+
| `query_length` | `number` | Maximum length of the statement query text. | `120` | no |
139+
129140
## Exported fields
130141

131142
{{< docs/shared lookup="reference/components/exporter-component-exports.md" source="alloy" version="<ALLOY_VERSION>" >}}

internal/component/prometheus/exporter/postgres/postgres.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var DefaultArguments = Arguments{
3838
},
3939
DisableDefaultMetrics: false,
4040
CustomQueriesConfigPath: "",
41+
StatStatementFlags: nil,
4142
}
4243

4344
// Arguments configures the prometheus.exporter.postgres component
@@ -54,7 +55,8 @@ type Arguments struct {
5455
EnabledCollectors []string `alloy:"enabled_collectors,attr,optional"`
5556

5657
// Blocks
57-
AutoDiscovery AutoDiscovery `alloy:"autodiscovery,block,optional"`
58+
AutoDiscovery AutoDiscovery `alloy:"autodiscovery,block,optional"`
59+
StatStatementFlags *StatStatementFlags `alloy:"stat_statements,block,optional"`
5860
}
5961

6062
func (a *Arguments) Validate() error {
@@ -74,6 +76,12 @@ type AutoDiscovery struct {
7476
DatabaseDenylist []string `alloy:"database_denylist,attr,optional"`
7577
}
7678

79+
// StatStatementFlags describe the flags to pass along the activation of the stat_statements collector
80+
type StatStatementFlags struct {
81+
IncludeQuery bool `alloy:"include_query,attr,optional"`
82+
QueryLength uint `alloy:"query_length,attr,optional"`
83+
}
84+
7785
// SetToDefault implements syntax.Defaulter.
7886
func (a *Arguments) SetToDefault() {
7987
*a = DefaultArguments
@@ -90,6 +98,17 @@ func (a *Arguments) convert(instanceName string) *postgres_exporter.Config {
9098
QueryPath: a.CustomQueriesConfigPath,
9199
Instance: instanceName,
92100
EnabledCollectors: a.EnabledCollectors,
101+
StatStatementFlags: a.StatStatementFlags.Convert(),
102+
}
103+
}
104+
105+
func (s *StatStatementFlags) Convert() *postgres_exporter.StatStatementFlags {
106+
if s == nil {
107+
return nil
108+
}
109+
return &postgres_exporter.StatStatementFlags{
110+
IncludeQuery: s.IncludeQuery,
111+
QueryLength: s.QueryLength,
93112
}
94113
}
95114

internal/component/prometheus/exporter/postgres/postgres_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@ import (
1111
)
1212

1313
func TestAlloyConfigUnmarshal(t *testing.T) {
14-
var exampleAlloyConfig = `
14+
exampleAlloyConfig := `
1515
data_source_names = ["postgresql://username:password@localhost:5432/database?sslmode=disable"]
1616
disable_settings_metrics = true
1717
disable_default_metrics = true
1818
custom_queries_config_path = "/tmp/queries.yaml"
19-
19+
20+
stat_statements {
21+
include_query = true
22+
query_length = 200
23+
}
24+
2025
autodiscovery {
2126
enabled = false
2227
database_allowlist = ["include1"]
@@ -37,19 +42,23 @@ func TestAlloyConfigUnmarshal(t *testing.T) {
3742
},
3843
DisableDefaultMetrics: true,
3944
CustomQueriesConfigPath: "/tmp/queries.yaml",
45+
StatStatementFlags: &StatStatementFlags{
46+
IncludeQuery: true,
47+
QueryLength: 200,
48+
},
4049
}
4150

4251
require.Equal(t, expected, args)
4352
}
4453

4554
func TestAlloyConfigConvert(t *testing.T) {
46-
var exampleAlloyConfig = `
55+
exampleAlloyConfig := `
4756
data_source_names = ["postgresql://username:password@localhost:5432/database?sslmode=disable"]
4857
disable_settings_metrics = true
4958
disable_default_metrics = false
5059
custom_queries_config_path = "/tmp/queries.yaml"
5160
enabled_collectors = ["collector1", "collector2"]
52-
61+
5362
autodiscovery {
5463
enabled = false
5564
database_allowlist = ["include1"]
@@ -72,12 +81,13 @@ func TestAlloyConfigConvert(t *testing.T) {
7281
QueryPath: "/tmp/queries.yaml",
7382
Instance: "test-instance",
7483
EnabledCollectors: []string{"collector1", "collector2"},
84+
StatStatementFlags: nil,
7585
}
7686
require.Equal(t, expected, *c)
7787
}
7888

7989
func TestRiverConfigValidate(t *testing.T) {
80-
var tc = []struct {
90+
tc := []struct {
8191
name string
8292
args Arguments
8393
expectedErr string

internal/static/integrations/postgres_exporter/postgres_exporter.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"strings"
99

10+
"github.com/alecthomas/kingpin/v2"
1011
"github.com/go-kit/log"
1112
"github.com/grafana/alloy/internal/runtime/logging"
1213
"github.com/grafana/alloy/internal/static/integrations"
@@ -38,7 +39,14 @@ type Config struct {
3839
// EnabledCollectors is a list of additional collectors to enable. NOTE: Due to limitations of the postgres_exporter,
3940
// this is only used for the first DSN provided and only some collectors can be enabled/disabled this way. See the
4041
// user-facing docs for more information.
41-
EnabledCollectors []string
42+
EnabledCollectors []string
43+
StatStatementFlags *StatStatementFlags
44+
}
45+
46+
// Config for the stat_statement collector flags
47+
type StatStatementFlags struct {
48+
IncludeQuery bool
49+
QueryLength uint
4250
}
4351

4452
// Name returns the name of the integration this config is for.
@@ -173,6 +181,27 @@ func New(log log.Logger, cfg *Config) (integrations.Integration, error) {
173181
return integrations.NewCollectorIntegration(cfg.Name(), integrations.WithCollectors(e)), nil
174182
}
175183

184+
// This is a hack to force the command line flag values for the stat_statements collector.
185+
// These flags are not exposed outside the package and cannot be mutated afterwards.
186+
if cfg.StatStatementFlags != nil && cfg.StatStatementFlags.IncludeQuery {
187+
includeQueryFlag := kingpin.CommandLine.GetFlag("collector.stat_statements.include_query")
188+
queryLengthFlag := kingpin.CommandLine.GetFlag("collector.stat_statements.query_length")
189+
190+
if includeQueryFlag == nil || queryLengthFlag == nil {
191+
return nil, fmt.Errorf("failed to find collector.stat_statements.include_query or collector.stat_statements.query_length in postgres_exporter")
192+
}
193+
194+
err := includeQueryFlag.Model().Value.Set("true")
195+
if err != nil {
196+
return nil, fmt.Errorf("failed to set include query flag using Kingpin : %w", err)
197+
}
198+
199+
err = queryLengthFlag.Model().Value.Set(fmt.Sprintf("%d", cfg.StatStatementFlags.QueryLength))
200+
if err != nil {
201+
return nil, fmt.Errorf("failed to set query length flag using Kingpin : %w", err)
202+
}
203+
}
204+
176205
// On top of the exporter's metrics, the postgres exporter also has metrics exposed via collector package.
177206
// However, these can only work for the first DSN provided. This matches the current implementation of the exporter.
178207
// TODO: Once https://github.com/prometheus-community/postgres_exporter/issues/999 is addressed, update the exporter

0 commit comments

Comments
 (0)