diff --git a/lib/promscrape/discovery/eureka/api.go b/lib/promscrape/discovery/eureka/api.go index e623bd323..f1a319bc3 100644 --- a/lib/promscrape/discovery/eureka/api.go +++ b/lib/promscrape/discovery/eureka/api.go @@ -16,17 +16,11 @@ type apiConfig struct { } func newAPIConfig(sdc *SDConfig, baseDir string) (*apiConfig, error) { - var ( - ba *promauth.BasicAuthConfig - token string - ) + token := "" if sdc.Token != nil { token = *sdc.Token } - port := 80 - if sdc.Port == nil { - sdc.Port = &port - } + var ba *promauth.BasicAuthConfig if len(sdc.Username) > 0 { ba = &promauth.BasicAuthConfig{ Username: sdc.Username, diff --git a/lib/promscrape/discovery/eureka/eureka.go b/lib/promscrape/discovery/eureka/eureka.go index df5324d97..5dca81937 100644 --- a/lib/promscrape/discovery/eureka/eureka.go +++ b/lib/promscrape/discovery/eureka/eureka.go @@ -27,6 +27,7 @@ type SDConfig struct { // refresh_interval is obtained from `-promscrape.ec2SDCheckInterval` command-line option. Port *int `yaml:"port,omitempty"` } + type applications struct { Applications []Application `xml:"application"` } @@ -93,41 +94,42 @@ func GetLabels(sdc *SDConfig, baseDir string) ([]map[string]string, error) { if err != nil { return nil, err } - return addInstanceLabels(apps, *sdc.Port), nil + port := 80 + if sdc.Port != nil { + port = *sdc.Port + } + return addInstanceLabels(apps, port), nil } -func addInstanceLabels(applications *applications, port int) []map[string]string { +func addInstanceLabels(apps *applications, port int) []map[string]string { var ms []map[string]string - for _, app := range applications.Applications { + for _, app := range apps.Applications { for _, instance := range app.Instances { - var targetAddress string - if instance.Port.Port > 0 { - targetAddress = discoveryutils.JoinHostPort(instance.HostName, instance.Port.Port) - } else { - targetAddress = discoveryutils.JoinHostPort(instance.HostName, port) + instancePort := port + if instance.Port.Port != 0 { + instancePort = instance.Port.Port } + targetAddress := discoveryutils.JoinHostPort(instance.HostName, instancePort) m := map[string]string{ - "__address__": targetAddress, - "instance": instance.InstanceID, - "__meta_eureka_app_instance_app_nanem": app.Name, - "__meta_eureka_app_instance_hostname": instance.HostName, - "__meta_eureka_app_instance_homepage_url": instance.HomePageURL, - "__meta_eureka_app_instance_statuspage_url": instance.StatusPageURL, - "__meta_eureka_app_instance_healthcheck_url": instance.HealthCheckURL, - "__meta_eureka_app_instance_ip_addr": instance.IPAddr, - "__meta_eureka_app_instance_vip_address": instance.VipAddress, - "__meta_eureka_app_instance_status": instance.Status, - "__meta_eureka_app_instance_country_id": strconv.Itoa(instance.CountryID), - "__meta_eureka_app_instance_id": instance.InstanceID, + "__address__": targetAddress, + "instance": instance.InstanceID, + "__meta_eureka_app_name": app.Name, + "__meta_eureka_app_instance_hostname": instance.HostName, + "__meta_eureka_app_instance_homepage_url": instance.HomePageURL, + "__meta_eureka_app_instance_statuspage_url": instance.StatusPageURL, + "__meta_eureka_app_instance_healthcheck_url": instance.HealthCheckURL, + "__meta_eureka_app_instance_ip_addr": instance.IPAddr, + "__meta_eureka_app_instance_vip_address": instance.VipAddress, + "__meta_eureka_app_instance_secure_vip_address": instance.SecureVipAddress, + "__meta_eureka_app_instance_status": instance.Status, + "__meta_eureka_app_instance_country_id": strconv.Itoa(instance.CountryID), + "__meta_eureka_app_instance_id": instance.InstanceID, } - if len(instance.SecureVipAddress) > 0 { - m["__meta_eureka_app_instance_secure_vip_address"] = instance.SecureVipAddress - } - if instance.Port.Port > 0 { + if instance.Port.Port != 0 { m["__meta_eureka_app_instance_port"] = strconv.Itoa(instance.Port.Port) m["__meta_eureka_app_instance_port_enabled"] = strconv.FormatBool(instance.Port.Enabled) } - if instance.SecurePort.Port > 0 { + if instance.SecurePort.Port != 0 { m["__meta_eureka_app_instance_secure_port"] = strconv.Itoa(instance.SecurePort.Port) m["__meta_eureka_app_instance_secure_port_enabled"] = strconv.FormatBool(instance.SecurePort.Enabled) diff --git a/lib/promscrape/discovery/eureka/eureka_test.go b/lib/promscrape/discovery/eureka/eureka_test.go index 9974118ef..8b5091648 100644 --- a/lib/promscrape/discovery/eureka/eureka_test.go +++ b/lib/promscrape/discovery/eureka/eureka_test.go @@ -51,19 +51,20 @@ func Test_addInstanceLabels(t *testing.T) { }, want: [][]prompbmarshal.Label{ discoveryutils.GetSortedLabels(map[string]string{ - "__address__": "host-1:9100", - "instance": "some-id", - "__meta_eureka_app_instance_hostname": "host-1", - "__meta_eureka_app_instance_app_nanem": "test-app", - "__meta_eureka_app_instance_healthcheck_url": "some-url", - "__meta_eureka_app_instance_ip_addr": "10.15.11.11", - "__meta_eureka_app_instance_vip_address": "10.15.11.11", - "__meta_eureka_app_instance_country_id": "5", - "__meta_eureka_app_instance_homepage_url": "some-home-url", - "__meta_eureka_app_instance_statuspage_url": "some-status-url", - "__meta_eureka_app_instance_id": "some-id", - "__meta_eureka_app_instance_metadata_key_1": "value-1", - "__meta_eureka_app_instance_status": "Ok", + "__address__": "host-1:9100", + "instance": "some-id", + "__meta_eureka_app_instance_hostname": "host-1", + "__meta_eureka_app_name": "test-app", + "__meta_eureka_app_instance_healthcheck_url": "some-url", + "__meta_eureka_app_instance_ip_addr": "10.15.11.11", + "__meta_eureka_app_instance_vip_address": "10.15.11.11", + "__meta_eureka_app_instance_secure_vip_address": "", + "__meta_eureka_app_instance_country_id": "5", + "__meta_eureka_app_instance_homepage_url": "some-home-url", + "__meta_eureka_app_instance_statuspage_url": "some-status-url", + "__meta_eureka_app_instance_id": "some-id", + "__meta_eureka_app_instance_metadata_key_1": "value-1", + "__meta_eureka_app_instance_status": "Ok", }), }, },