Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[exporter/loki] Add Config QueueSettings validation #16855

Merged
merged 2 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .chloggen/lokiexporter-add-queue-settings-validate.yaml
Original file line number Diff line number Diff line change
@@ -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:
4 changes: 4 additions & 0 deletions exporter/lokiexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, but I think calling out the failing config key here would be helpful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the failing key could be QueueSize only, and it will be mentioned in the inner error message: https://github.com/open-telemetry/opentelemetry-collector/blob/7d168dd20efd4ebf659ccf7c501fdf02d7ed6dfd/exporter/exporterhelper/queued_retry.go#L76

So the error message here will be:
queue settings has invalid configuration: queue size must be positive

I think the message identifies the failing key clearly. Or maybe I misunderstood your concern?

}

if _, err := url.Parse(c.Endpoint); c.Endpoint == "" || err != nil {
return fmt.Errorf("\"endpoint\" must be a valid URL")
}
Expand Down
41 changes: 41 additions & 0 deletions exporter/lokiexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package lokiexporter

import (
"fmt"
"path/filepath"
"testing"
"time"
Expand Down Expand Up @@ -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)
}
})
}
}