From 8c5da5e0781a170dab088ed0c5603bd45a6a5fc8 Mon Sep 17 00:00:00 2001 From: Julien Levesy Date: Tue, 29 Nov 2022 15:24:07 +0100 Subject: [PATCH] feat(config): allow no remote write configs Signed-off-by: Julien Levesy --- config/config.go | 4 --- config/config_test.go | 27 +++++++++++++++++++ config/testdata/agent_mode.good.yml | 2 ++ .../agent_mode.with_alert_manager.yml | 6 +++++ .../agent_mode.with_alert_relabels.yml | 5 ++++ .../testdata/agent_mode.with_remote_reads.yml | 5 ++++ .../testdata/agent_mode.with_rule_files.yml | 3 +++ .../agent_mode.without_remote_writes.yml | 2 ++ 8 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 config/testdata/agent_mode.good.yml create mode 100644 config/testdata/agent_mode.with_alert_manager.yml create mode 100644 config/testdata/agent_mode.with_alert_relabels.yml create mode 100644 config/testdata/agent_mode.with_remote_reads.yml create mode 100644 config/testdata/agent_mode.with_rule_files.yml create mode 100644 config/testdata/agent_mode.without_remote_writes.yml diff --git a/config/config.go b/config/config.go index 8e8460d4c5..8f32f2d53e 100644 --- a/config/config.go +++ b/config/config.go @@ -112,10 +112,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 9ee8fe1a02..26f257ea23 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 0000000000..a16612095c --- /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 0000000000..9a3929957f --- /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 0000000000..67e70fc7f4 --- /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 0000000000..416676793b --- /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 0000000000..3aaa9ad471 --- /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 0000000000..1285bef674 --- /dev/null +++ b/config/testdata/agent_mode.without_remote_writes.yml @@ -0,0 +1,2 @@ +global: + scrape_interval: 15s