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

Fix port derivation #651

Merged
merged 16 commits into from
Jan 11, 2022
21 changes: 18 additions & 3 deletions pkg/collector/parser/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package parser

import (
"errors"
"fmt"
"regexp"
"strconv"
Expand Down Expand Up @@ -155,9 +156,23 @@ func portName(receiverName string, port int32) string {
}

func portFromEndpoint(endpoint string) (int32, error) {
i := strings.LastIndex(endpoint, ":") + 1
part := endpoint[i:]
port, err := strconv.ParseInt(part, 10, 32)
var err error
yuriolisa marked this conversation as resolved.
Show resolved Hide resolved
var port int64

yuriolisa marked this conversation as resolved.
Show resolved Hide resolved
r := regexp.MustCompile(":[0-9]+")

if r.MatchString(endpoint) {
port, err = strconv.ParseInt(strings.Replace(r.FindString(endpoint), ":", "", -1), 10, 32)

yuriolisa marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return 0, err
}
}

if port == 0 {
return 0, errors.New("Port should not be empty")
}

return int32(port), err
}

Expand Down
1 change: 1 addition & 0 deletions pkg/collector/parser/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func TestReceiverParsePortFromEndpoint(t *testing.T) {
errorExpected bool
}{
{"regular case", "http://localhost:1234", 1234, false},
{"absolute with path", "http://localhost:1234/server-status?auto", 1234, false},
{"no protocol", "0.0.0.0:1234", 1234, false},
{"just port", ":1234", 1234, false},
{"no port at all", "http://localhost", 0, true},
Expand Down