diff --git a/pkg/collector/parser/receiver.go b/pkg/collector/parser/receiver.go index fb37ae9a56..156a6928b5 100644 --- a/pkg/collector/parser/receiver.go +++ b/pkg/collector/parser/receiver.go @@ -16,6 +16,7 @@ package parser import ( + "errors" "fmt" "regexp" "strconv" @@ -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 + var port int64 + + r := regexp.MustCompile(":[0-9]+") + + if r.MatchString(endpoint) { + port, err = strconv.ParseInt(strings.Replace(r.FindString(endpoint), ":", "", -1), 10, 32) + + if err != nil { + return 0, err + } + } + + if port == 0 { + return 0, errors.New("Port should not be empty") + } + return int32(port), err } diff --git a/pkg/collector/parser/receiver_test.go b/pkg/collector/parser/receiver_test.go index b706987ae6..47f741ea87 100644 --- a/pkg/collector/parser/receiver_test.go +++ b/pkg/collector/parser/receiver_test.go @@ -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},