Skip to content

Commit

Permalink
[receiver/httpcheck] fail fast on endpoint missing scheme (#23808)
Browse files Browse the repository at this point in the history
Fixes #23020
  • Loading branch information
andrzej-stencel authored Jul 11, 2023
1 parent 2f91717 commit 676559a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
24 changes: 24 additions & 0 deletions .chloggen/httpcheck-parse-scheme.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Use this changelog template to create an entry for release notes.
# If your change doesn't affect end users, such as a test fix or a tooling change,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: breaking

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: receiver/httpcheck

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fail fast on endpoint missing scheme

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [23020]

# (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: |
Previously, when configured with an endpoint without HTTP/HTTPS scheme like "opentelemetry.io",
the receiver would start correctly, but fail to check the endpoint, producing the `httpcheck.error`
metric on every collection interval. After this change, the receiver fails to start, writing
an error log saying that you need to provide a scheme in the endpoint.
12 changes: 6 additions & 6 deletions receiver/httpcheckreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
// Predefined error responses for configuration validation failures
var (
errMissingEndpoint = errors.New(`"endpoint" must be specified`)
errInvalidEndpoint = errors.New(`"endpoint" must be in the form of <scheme>://<hostname>:<port>`)
errInvalidEndpoint = errors.New(`"endpoint" must be in the form of <scheme>://<hostname>[:<port>]`)
)

// Config defines the configuration for the various elements of the receiver agent.
Expand All @@ -39,11 +39,11 @@ func (cfg *targetConfig) Validate() error {

if cfg.Endpoint == "" {
err = multierr.Append(err, errMissingEndpoint)
}
_, parseErr := url.Parse(cfg.Endpoint)
if parseErr != nil {
wrappedErr := fmt.Errorf("%s: %w", errInvalidEndpoint.Error(), parseErr)
err = multierr.Append(err, wrappedErr)
} else {
_, parseErr := url.ParseRequestURI(cfg.Endpoint)
if parseErr != nil {
err = multierr.Append(err, fmt.Errorf("%s: %w", errInvalidEndpoint.Error(), parseErr))
}
}

return err
Expand Down
20 changes: 20 additions & 0 deletions receiver/httpcheckreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ func TestValidate(t *testing.T) {
fmt.Errorf("%w: %s", errInvalidEndpoint, `parse "invalid://endpoint: 12efg": invalid port ": 12efg" after host`),
),
},
{
desc: "missing scheme",
cfg: &Config{
Targets: []*targetConfig{
{
HTTPClientSettings: confighttp.HTTPClientSettings{
Endpoint: "www.opentelemetry.io/docs",
},
},
},
},
expectedErr: multierr.Combine(
fmt.Errorf("%w: %s", errInvalidEndpoint, `parse "www.opentelemetry.io/docs": invalid URI for request`),
),
},
{
desc: "valid config",
cfg: &Config{
Expand All @@ -75,6 +90,11 @@ func TestValidate(t *testing.T) {
Endpoint: "https://opentelemetry.io",
},
},
{
HTTPClientSettings: confighttp.HTTPClientSettings{
Endpoint: "https://opentelemetry.io:80/docs",
},
},
},
},
expectedErr: nil,
Expand Down

0 comments on commit 676559a

Please sign in to comment.