diff --git a/config/config.go b/config/config.go index 8eb88eb51..8bc4bf34a 100644 --- a/config/config.go +++ b/config/config.go @@ -113,10 +113,6 @@ func LoadFile(filename string, agentMode, expandExternalLabels bool, logger log. } if agentMode { - if len(cfg.RemoteWriteConfigs) == 0 { - return nil, errors.New("at least one remote_write target must be specified in agent mode") - } - if len(cfg.AlertingConfig.AlertmanagerConfigs) > 0 || len(cfg.AlertingConfig.AlertRelabelConfigs) > 0 { return nil, errors.New("field alerting is not allowed in agent mode") } diff --git a/config/config_test.go b/config/config_test.go index 9ee8fe1a0..26f257ea2 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1745,6 +1745,33 @@ func TestExpandExternalLabels(t *testing.T) { require.Equal(t, labels.FromStrings("bar", "foo", "baz", "fooTestValuebar", "foo", "TestValue", "qux", "foo${TEST}", "xyz", "foo$bar"), c.GlobalConfig.ExternalLabels) } +func TestAgentMode(t *testing.T) { + _, err := LoadFile("testdata/agent_mode.with_alert_manager.yml", true, false, log.NewNopLogger()) + require.ErrorContains(t, err, "field alerting is not allowed in agent mode") + + _, err = LoadFile("testdata/agent_mode.with_alert_relabels.yml", true, false, log.NewNopLogger()) + require.ErrorContains(t, err, "field alerting is not allowed in agent mode") + + _, err = LoadFile("testdata/agent_mode.with_rule_files.yml", true, false, log.NewNopLogger()) + require.ErrorContains(t, err, "field rule_files is not allowed in agent mode") + + _, err = LoadFile("testdata/agent_mode.with_remote_reads.yml", true, false, log.NewNopLogger()) + require.ErrorContains(t, err, "field remote_read is not allowed in agent mode") + + c, err := LoadFile("testdata/agent_mode.without_remote_writes.yml", true, false, log.NewNopLogger()) + require.NoError(t, err) + require.Len(t, c.RemoteWriteConfigs, 0) + + c, err = LoadFile("testdata/agent_mode.good.yml", true, false, log.NewNopLogger()) + require.NoError(t, err) + require.Len(t, c.RemoteWriteConfigs, 1) + require.Equal( + t, + "http://remote1/push", + c.RemoteWriteConfigs[0].URL.String(), + ) +} + func TestEmptyGlobalBlock(t *testing.T) { c, err := Load("global:\n", false, log.NewNopLogger()) require.NoError(t, err) diff --git a/config/testdata/agent_mode.good.yml b/config/testdata/agent_mode.good.yml new file mode 100644 index 000000000..a16612095 --- /dev/null +++ b/config/testdata/agent_mode.good.yml @@ -0,0 +1,2 @@ +remote_write: + - url: http://remote1/push diff --git a/config/testdata/agent_mode.with_alert_manager.yml b/config/testdata/agent_mode.with_alert_manager.yml new file mode 100644 index 000000000..9a3929957 --- /dev/null +++ b/config/testdata/agent_mode.with_alert_manager.yml @@ -0,0 +1,6 @@ +alerting: + alertmanagers: + - scheme: https + static_configs: + - targets: + - "1.2.3.4:9093" diff --git a/config/testdata/agent_mode.with_alert_relabels.yml b/config/testdata/agent_mode.with_alert_relabels.yml new file mode 100644 index 000000000..67e70fc7f --- /dev/null +++ b/config/testdata/agent_mode.with_alert_relabels.yml @@ -0,0 +1,5 @@ +alerting: + alert_relabel_configs: + - action: uppercase + source_labels: [instance] + target_label: instance diff --git a/config/testdata/agent_mode.with_remote_reads.yml b/config/testdata/agent_mode.with_remote_reads.yml new file mode 100644 index 000000000..416676793 --- /dev/null +++ b/config/testdata/agent_mode.with_remote_reads.yml @@ -0,0 +1,5 @@ +remote_read: + - url: http://remote1/read + read_recent: true + name: default + enable_http2: false diff --git a/config/testdata/agent_mode.with_rule_files.yml b/config/testdata/agent_mode.with_rule_files.yml new file mode 100644 index 000000000..3aaa9ad47 --- /dev/null +++ b/config/testdata/agent_mode.with_rule_files.yml @@ -0,0 +1,3 @@ +rule_files: + - "first.rules" + - "my/*.rules" diff --git a/config/testdata/agent_mode.without_remote_writes.yml b/config/testdata/agent_mode.without_remote_writes.yml new file mode 100644 index 000000000..1285bef67 --- /dev/null +++ b/config/testdata/agent_mode.without_remote_writes.yml @@ -0,0 +1,2 @@ +global: + scrape_interval: 15s