From 3f56ff9de85702d586ebf6c6dac4fbd90ac69732 Mon Sep 17 00:00:00 2001 From: Irina Date: Tue, 17 Jan 2023 17:21:11 +0000 Subject: [PATCH] [exporter/loki] Add Config QueueSettings validation (#16855) Description: Added QueueStettings validation into Config Validate method Link to tracking Issue: #7841 --- ...iexporter-add-queue-settings-validate.yaml | 16 ++++++++ exporter/lokiexporter/config.go | 4 ++ exporter/lokiexporter/config_test.go | 41 +++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 .chloggen/lokiexporter-add-queue-settings-validate.yaml diff --git a/.chloggen/lokiexporter-add-queue-settings-validate.yaml b/.chloggen/lokiexporter-add-queue-settings-validate.yaml new file mode 100644 index 000000000000..9f0ca5fa13f4 --- /dev/null +++ b/.chloggen/lokiexporter-add-queue-settings-validate.yaml @@ -0,0 +1,16 @@ +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: lokiexporter + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Added QueueSettings validation into Config Validate method + +# One or more tracking issues related to the change +issues: [7841] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: diff --git a/exporter/lokiexporter/config.go b/exporter/lokiexporter/config.go index 59d8466df5e6..82fe97b356a9 100644 --- a/exporter/lokiexporter/config.go +++ b/exporter/lokiexporter/config.go @@ -51,6 +51,10 @@ type Config struct { } func (c *Config) Validate() error { + if err := c.QueueSettings.Validate(); err != nil { + return fmt.Errorf("queue settings has invalid configuration: %w", err) + } + if _, err := url.Parse(c.Endpoint); c.Endpoint == "" || err != nil { return fmt.Errorf("\"endpoint\" must be a valid URL") } diff --git a/exporter/lokiexporter/config_test.go b/exporter/lokiexporter/config_test.go index d9f2c6ca756f..fb06c16ccd1c 100644 --- a/exporter/lokiexporter/config_test.go +++ b/exporter/lokiexporter/config_test.go @@ -15,6 +15,7 @@ package lokiexporter import ( + "fmt" "path/filepath" "testing" "time" @@ -164,3 +165,43 @@ func TestIsLegacy(t *testing.T) { func stringp(str string) *string { return &str } + +func TestConfigValidate(t *testing.T) { + testCases := []struct { + desc string + cfg *Config + err error + }{ + { + desc: "QueueSettings are invalid", + cfg: &Config{QueueSettings: exporterhelper.QueueSettings{QueueSize: -1, Enabled: true}}, + err: fmt.Errorf("queue settings has invalid configuration"), + }, + { + desc: "Endpoint is invalid", + cfg: &Config{}, + err: fmt.Errorf("\"endpoint\" must be a valid URL"), + }, + { + desc: "Config is valid", + cfg: &Config{ + HTTPClientSettings: confighttp.HTTPClientSettings{ + Endpoint: "https://loki.example.com", + }, + }, + err: nil, + }, + } + + for _, tc := range testCases { + t.Run(tc.desc, func(t *testing.T) { + err := tc.cfg.Validate() + if tc.err != nil { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.err.Error()) + } else { + require.NoError(t, err) + } + }) + } +}