diff --git a/src/jetstream/plugins/metrics/main.go b/src/jetstream/plugins/metrics/main.go index ed495973ef..725799302d 100644 --- a/src/jetstream/plugins/metrics/main.go +++ b/src/jetstream/plugins/metrics/main.go @@ -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) @@ -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 @@ -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 diff --git a/src/jetstream/plugins/metrics/main_test.go b/src/jetstream/plugins/metrics/main_test.go new file mode 100644 index 0000000000..7b57d89cab --- /dev/null +++ b/src/jetstream/plugins/metrics/main_test.go @@ -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) + + }) + +}