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

Better URL comparison for matching Metrics endpoints #3862

Merged
merged 2 commits into from
Sep 17, 2019
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
40 changes: 37 additions & 3 deletions src/jetstream/plugins/metrics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,47 @@ func (m *MetricsSpecification) UpdateMetadata(info *interfaces.Info, userGUID st

func hasMetricsProvider(providers []MetricsMetadata, url string) (*MetricsMetadata, bool) {
for _, provider := range providers {
if provider.URL == url {
if compareURL(provider.URL, url) {
return &provider, true
}
}
return nil, false
}

// Compare two URLs, taking into account default HTTP/HTTPS ports and ignoring query string
func compareURL(a, b string) bool {

ua, err := url.Parse(a)
if err != nil {
return false
}

ub, err := url.Parse(b)
if err != nil {
return false
}

aPort := getPort(ua)
bPort := getPort(ub)
return ua.Scheme == ub.Scheme && ua.Hostname() == ub.Hostname() && aPort == bPort && ua.Path == ub.Path
}

func getPort(u *url.URL) string {
port := u.Port()
if len(port) == 0 {
switch u.Scheme {
case "http":
port = "80"
case "https":
port = "443"
default:
port = ""
}
}

return port
}

func (m *MetricsSpecification) getMetricsEndpoints(userGUID string, cnsiList []string) (map[string]EndpointMetricsRelation, error) {

metricsProviders := make([]MetricsMetadata, 0)
Expand Down Expand Up @@ -429,7 +463,7 @@ func (m *MetricsSpecification) getMetricsEndpoints(userGUID string, cnsiList []s
for _, metricProviderInfo := range metricsProviders {
for guid, info := range endpointsMap {
// Depends on the type
if info.CNSIType == metricProviderInfo.Type && info.DopplerLoggingEndpoint == metricProviderInfo.URL {
if info.CNSIType == metricProviderInfo.Type && compareURL(info.DopplerLoggingEndpoint, metricProviderInfo.URL) {
relate := EndpointMetricsRelation{}
relate.endpoint = info
// Make a copy
Expand All @@ -442,7 +476,7 @@ func (m *MetricsSpecification) getMetricsEndpoints(userGUID string, cnsiList []s
// K8s
log.Debugf("Processing endpoint: %+v", info)
log.Debugf("Processing endpoint Metrics provider: %+v", metricProviderInfo)
if info.APIEndpoint.String() == metricProviderInfo.URL {
if compareURL(info.APIEndpoint.String(), metricProviderInfo.URL) {
relate := EndpointMetricsRelation{}
relate.endpoint = info
relate.metrics = &metricProviderInfo
Expand Down
28 changes: 28 additions & 0 deletions src/jetstream/plugins/metrics/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package metrics

import (
"testing"

. "github.com/smartystreets/goconvey/convey"
)

func TestUrlComparision(t *testing.T) {
t.Parallel()

Convey("URL Comparision", t, func() {

So(compareURL("https://test.com", "https://test.com"), ShouldBeTrue)
So(compareURL("http://test.com", "http://test.com"), ShouldBeTrue)
So(compareURL("http://test3.com", "http://test.com"), ShouldBeFalse)
So(compareURL("https://test.com", "https://test.com:443"), ShouldBeTrue)
So(compareURL("http://test.com", "https://test.com:443"), ShouldBeFalse)
So(compareURL("http://test.com", "http://test.com:80"), ShouldBeTrue)
So(compareURL("http://test.com:80", "http://test.com:80"), ShouldBeTrue)
So(compareURL("http://test.com:80", "http://test.com"), ShouldBeTrue)
So(compareURL("http://test.com", "http://test2.com"), ShouldBeFalse)
So(compareURL("http://test.com/a", "http://test.com/a"), ShouldBeTrue)
So(compareURL("http://test.com/a?one=two", "http://test.com/a?two=one"), ShouldBeTrue)

})

}