Skip to content

Commit

Permalink
[receiver/apache] Invalid endpoint should not cause panic (open-telem…
Browse files Browse the repository at this point in the history
…etry#34992)

**Description:**
The receiver crashes if it fails to parse the endpoint.

**Link to tracking Issue:**
N/A

**Testing:**
Add a test covering this case.

**Documentation:**
Changelog updated.
  • Loading branch information
pjanotti authored and f7o committed Sep 12, 2024
1 parent 2b2c916 commit e4c673f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
27 changes: 27 additions & 0 deletions .chloggen/apachereceiver_fix_panic_on_invalid_endpoint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

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

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix panic on invalid endpoint configuration

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

# (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:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
4 changes: 4 additions & 0 deletions receiver/apachereceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func createDefaultConfig() component.Config {

func parseResourceAttributes(endpoint string) (string, string, error) {
u, err := url.Parse(endpoint)
if err != nil {
return "", "", err
}

serverName := u.Hostname()
port := u.Port()

Expand Down
14 changes: 13 additions & 1 deletion receiver/apachereceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func TestPortValidate(t *testing.T) {
desc string
endpoint string
expectedPort string
expectError bool
}{
{
desc: "http with specified port",
Expand Down Expand Up @@ -80,14 +81,25 @@ func TestPortValidate(t *testing.T) {
endpoint: "abc://localhost/server-status?auto",
expectedPort: "",
},
{
desc: "invalid endpoint",
endpoint: ":missing-schema",
expectedPort: "",
expectError: true,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
cfg := NewFactory().CreateDefaultConfig().(*Config)
cfg.Endpoint = tc.endpoint
_, port, err := parseResourceAttributes(tc.endpoint)

require.NoError(t, err)
if tc.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
}

require.Equal(t, tc.expectedPort, port)
})
}
Expand Down

0 comments on commit e4c673f

Please sign in to comment.