Skip to content

Commit bb08aee

Browse files
matthewnolfclaude
andcommitted
feat(database_observability): default to embedded exporter when targets is omitted
Makes `targets` optional for both `database_observability.mysql` and `database_observability.postgres`. When no targets are provided, the embedded exporter is started automatically using default args. An explicit `prometheus_exporter` block can still be used to customise the embedded exporter. Providing both `prometheus_exporter` and `targets` remains a validation error. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 0f3bfe9 commit bb08aee

6 files changed

Lines changed: 47 additions & 30 deletions

File tree

docs/sources/reference/components/database_observability/database_observability.mysql.md

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ It forwards this data as log entries to Loki receivers and exports targets for P
2020
database_observability.mysql "<LABEL>" {
2121
data_source_name = <DATA_SOURCE_NAME>
2222
forward_to = [<LOKI_RECEIVERS>]
23-
targets = "<TARGET_LIST>"
2423
}
2524
```
2625

@@ -32,7 +31,7 @@ You can use the following arguments with `database_observability.mysql`:
3231
|--------------------------------------------|----------------------|-----------------------------------------------------------------------------|---------|----------|
3332
| `data_source_name` | `secret` | [Data Source Name][] for the MySQL server to connect to. | | yes |
3433
| `forward_to` | `list(LogsReceiver)` | Where to forward log entries after processing. | | yes |
35-
| `targets` | `list(map(string))` | List of targets to scrape. | | yes |
34+
| `targets` | `list(map(string))` | List of external targets to scrape. | | no |
3635
| `disable_collectors` | `list(string)` | A list of collectors to disable from the default set. | | no |
3736
| `enable_collectors` | `list(string)` | A list of collectors to enable on top of the default set. | | no |
3837
| `exclude_schemas` | `list(string)` | A list of schemas to exclude from monitoring. | | no |
@@ -56,19 +55,20 @@ You can use the following blocks with `database_observability.mysql`:
5655

5756
{{< docs/alloy-config >}}
5857

59-
| Block | Description | Required |
60-
|--------------------------------------|---------------------------------------------------|----------|
61-
| [`cloud_provider`][cloud_provider] | Provide Cloud Provider information. | no |
62-
| `cloud_provider` > [`aws`][aws] | Provide AWS database host information. | no |
63-
| `cloud_provider` > [`azure`][azure] | Provide Azure database host information. | no |
64-
| [`setup_consumers`][setup_consumers] | Configure the `setup_consumers` collector. | no |
65-
| [`setup_actors`][setup_actors] | Configure the `setup_actors` collector. | no |
66-
| [`query_details`][query_details] | Configure the queries collector. | no |
67-
| [`schema_details`][schema_details] | Configure the schema and table details collector. | no |
68-
| [`explain_plans`][explain_plans] | Configure the explain plans collector. | no |
69-
| [`locks`][locks] | Configure the locks collector. | no |
70-
| [`query_samples`][query_samples] | Configure the query samples collector. | no |
71-
| [`health_check`][health_check] | Configure the health check collector. | no |
58+
| Block | Description | Required |
59+
|--------------------------------------------------|---------------------------------------------------|----------|
60+
| [`cloud_provider`][cloud_provider] | Provide Cloud Provider information. | no |
61+
| `cloud_provider` > [`aws`][aws] | Provide AWS database host information. | no |
62+
| `cloud_provider` > [`azure`][azure] | Provide Azure database host information. | no |
63+
| [`setup_consumers`][setup_consumers] | Configure the `setup_consumers` collector. | no |
64+
| [`setup_actors`][setup_actors] | Configure the `setup_actors` collector. | no |
65+
| [`query_details`][query_details] | Configure the queries collector. | no |
66+
| [`schema_details`][schema_details] | Configure the schema and table details collector. | no |
67+
| [`explain_plans`][explain_plans] | Configure the explain plans collector. | no |
68+
| [`locks`][locks] | Configure the locks collector. | no |
69+
| [`query_samples`][query_samples] | Configure the query samples collector. | no |
70+
| [`health_check`][health_check] | Configure the health check collector. | no |
71+
| [`prometheus_exporter`][prometheus_exporter] | Configure the embedded mysqld_exporter. | no |
7272

7373
[cloud_provider]: #cloud_provider
7474
[aws]: #aws
@@ -81,6 +81,7 @@ You can use the following blocks with `database_observability.mysql`:
8181
[query_samples]: #query_samples
8282
[setup_actors]: #setup_actors
8383
[health_check]: #health_check
84+
[prometheus_exporter]: #prometheus_exporter
8485

8586
{{< /docs/alloy-config >}}
8687

@@ -166,12 +167,18 @@ The `azure` block supplies the identifying information for the database being mo
166167
| `collect_interval` | `duration` | How frequently to check if `setup_actors` are configured correctly. | `"1h"` | no |
167168

168169

169-
### `health_checks`
170+
### `health_check`
170171

171172
| Name | Type | Description | Default | Required |
172173
| -------------------------- | ---------- | ---------------------------------------------------------------------- | ------- | -------- |
173174
| `collect_interval` | `duration` | How frequently to run health checks. | `"1h"` | no |
174175

176+
### `prometheus_exporter`
177+
178+
The `prometheus_exporter` block configures the embedded mysqld_exporter scrapers.
179+
The `data_source_name` is inherited from the parent block.
180+
181+
Refer to [`prometheus.exporter.mysql`](../../prometheus/prometheus.exporter.mysql/) docs for the full list of supported arguments and sub-blocks.
175182

176183
## Example
177184

docs/sources/reference/components/database_observability/database_observability.postgres.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ It forwards this data as log entries to Loki receivers and exports targets for P
2020
database_observability.postgres "<LABEL>" {
2121
data_source_name = <DATA_SOURCE_NAME>
2222
forward_to = [<LOKI_RECEIVERS>]
23-
targets = "<TARGET_LIST>"
2423
}
2524
```
2625

@@ -32,7 +31,7 @@ You can use the following arguments with `database_observability.postgres`:
3231
|----------------------|----------------------|-------------------------------------------------------------|---------|----------|
3332
| `data_source_name` | `secret` | [Data Source Name][] for the Postgres server to connect to. | | yes |
3433
| `forward_to` | `list(LogsReceiver)` | Where to forward log entries after processing. | | yes |
35-
| `targets` | `list(map(string))` | List of targets to scrape. | | yes |
34+
| `targets` | `list(map(string))` | List of external targets to scrape for Prometheus metrics. | | no |
3635
| `disable_collectors` | `list(string)` | A list of collectors to disable from the default set. | | no |
3736
| `enable_collectors` | `list(string)` | A list of collectors to enable on top of the default set. | | no |
3837
| `exclude_databases` | `list(string)` | A list of databases to exclude from monitoring. | | no |
@@ -76,7 +75,8 @@ You can use the following blocks with `database_observability.postgres`:
7675
| [`query_samples`][query_samples] | Configure the query samples collector. | no |
7776
| [`schema_details`][schema_details] | Configure the schema and table details collector. | no |
7877
| [`explain_plans`][explain_plans] | Configure the explain plans collector. | no |
79-
| [`health_check`][health_check] | Configure the health check collector. | no |
78+
| [`health_check`][health_check] | Configure the health check collector. | no |
79+
| [`prometheus_exporter`][prometheus_exporter] | Configure the embedded postgres_exporter. | no |
8080

8181
[cloud_provider]: #cloud_provider
8282
[aws]: #aws
@@ -86,6 +86,7 @@ You can use the following blocks with `database_observability.postgres`:
8686
[schema_details]: #schema_details
8787
[explain_plans]: #explain_plans
8888
[health_check]: #health_check
89+
[prometheus_exporter]: #prometheus_exporter
8990

9091
{{< /docs/alloy-config >}}
9192

@@ -152,6 +153,13 @@ The `azure` block supplies the identifying information for the database being mo
152153
|--------------------|------------|------------------------------------------------------|---------|----------|
153154
| `collect_interval` | `duration` | How frequently to collect information from database. | `"1h"` | no |
154155

156+
### `prometheus_exporter`
157+
158+
The `prometheus_exporter` block configures the embedded postgres_exporter scrapers.
159+
The `data_source_name` is inherited from the parent block.
160+
161+
Refer to [`prometheus.exporter.postgres`](../../prometheus/prometheus.exporter.postgres/) docs for the full list of supported arguments and sub-blocks.
162+
155163
## `logs` collector
156164

157165
The `logs` collector processes PostgreSQL logs received through the `logs_receiver` entry point and exports Prometheus metrics for query and server errors.

internal/component/database_observability/mysql/component.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ var (
6262
type Arguments struct {
6363
DataSourceName alloytypes.Secret `alloy:"data_source_name,attr"`
6464
ForwardTo []loki.LogsReceiver `alloy:"forward_to,attr"`
65-
Targets []discovery.Target `alloy:"targets,attr"`
65+
Targets []discovery.Target `alloy:"targets,attr,optional"`
6666
EnableCollectors []string `alloy:"enable_collectors,attr,optional"`
6767
DisableCollectors []string `alloy:"disable_collectors,attr,optional"`
6868
ExcludeSchemas []string `alloy:"exclude_schemas,attr,optional"`
@@ -460,7 +460,11 @@ func (c *Component) connectAndStartCollectors(ctx context.Context) error {
460460
c.exporterCollector = nil
461461
}
462462

463-
if c.args.PrometheusExporter != nil {
463+
if len(c.args.Targets) == 0 {
464+
if c.args.PrometheusExporter == nil {
465+
d := PrometheusExporterArguments(exporter_mysql.DefaultArguments)
466+
c.args.PrometheusExporter = &d
467+
}
464468
exporterArgs := exporter_mysql.Arguments(*c.args.PrometheusExporter)
465469
exporterCfg := exporterArgs.Convert()
466470
scrapers := mysqld_exporter.GetScrapers(exporterCfg)

internal/component/database_observability/mysql/component_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,6 @@ func Test_PrometheusExporterBlock(t *testing.T) {
564564
cfg := `
565565
data_source_name = ""
566566
forward_to = []
567-
targets = []
568567
`
569568
var args Arguments
570569
err := syntax.Unmarshal([]byte(cfg), &args)
@@ -576,7 +575,6 @@ func Test_PrometheusExporterBlock(t *testing.T) {
576575
cfg := `
577576
data_source_name = ""
578577
forward_to = []
579-
targets = []
580578
prometheus_exporter {}
581579
`
582580
var args Arguments
@@ -587,11 +585,10 @@ func Test_PrometheusExporterBlock(t *testing.T) {
587585
assert.Equal(t, 2, exporterArgs.LockWaitTimeout) // default value
588586
})
589587

590-
t.Run("present with defaults when empty block", func(t *testing.T) {
588+
t.Run("present with custom collectors", func(t *testing.T) {
591589
cfg := `
592590
data_source_name = ""
593591
forward_to = []
594-
targets = []
595592
prometheus_exporter {
596593
enable_collectors = ["perf_schema.eventsstatements", "perf_schema.eventswaits"]
597594
}

internal/component/database_observability/postgres/component.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ var (
6767
type Arguments struct {
6868
DataSourceName alloytypes.Secret `alloy:"data_source_name,attr"`
6969
ForwardTo []loki.LogsReceiver `alloy:"forward_to,attr"`
70-
Targets []discovery.Target `alloy:"targets,attr"`
70+
Targets []discovery.Target `alloy:"targets,attr,optional"`
7171
EnableCollectors []string `alloy:"enable_collectors,attr,optional"`
7272
DisableCollectors []string `alloy:"disable_collectors,attr,optional"`
7373
ExcludeDatabases []string `alloy:"exclude_databases,attr,optional"`
@@ -400,7 +400,11 @@ func (c *Component) connectAndStartCollectors(ctx context.Context) error {
400400
}
401401
c.exporterCollectors = nil
402402

403-
if c.args.PrometheusExporter != nil {
403+
if len(c.args.Targets) == 0 {
404+
if c.args.PrometheusExporter == nil {
405+
d := PrometheusExporterArguments(exporter_postgres.DefaultArguments)
406+
c.args.PrometheusExporter = &d
407+
}
404408
exporterArgs := exporter_postgres.Arguments(*c.args.PrometheusExporter)
405409
slogLogger := slog.New(logging.NewSlogGoKitHandler(c.opts.Logger))
406410
dsn := string(c.args.DataSourceName)

internal/component/database_observability/postgres/component_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,6 @@ func Test_PrometheusExporterBlock(t *testing.T) {
741741
cfg := `
742742
data_source_name = "postgresql://user:pass@localhost:5432/db"
743743
forward_to = []
744-
targets = []
745744
`
746745
var args Arguments
747746
err := syntax.Unmarshal([]byte(cfg), &args)
@@ -753,7 +752,6 @@ func Test_PrometheusExporterBlock(t *testing.T) {
753752
cfg := `
754753
data_source_name = "postgresql://user:pass@localhost:5432/db"
755754
forward_to = []
756-
targets = []
757755
prometheus_exporter {}
758756
`
759757
var args Arguments
@@ -769,7 +767,6 @@ func Test_PrometheusExporterBlock(t *testing.T) {
769767
cfg := `
770768
data_source_name = "postgresql://user:pass@localhost:5432/db"
771769
forward_to = []
772-
targets = []
773770
prometheus_exporter {
774771
disable_settings_metrics = true
775772
}

0 commit comments

Comments
 (0)