From 24feb0ef8fbcad32939897f823c7835c6f3493cf Mon Sep 17 00:00:00 2001 From: Konstantin Kiess Date: Thu, 8 Oct 2020 18:58:20 +0200 Subject: [PATCH 1/2] added scheme to tag config option. will only parse if present so that backwards compability is given.. --- pkg/discovery/discovery.go | 16 +++++++++++---- pkg/discovery/discovery_test.go | 3 +-- pkg/endpoints/endpoints.go | 14 +++++++------ pkg/endpoints/endpoints_test.go | 25 +++++++++++++----------- pkg/libtesting/libtesting.go | 23 ++++++++++++---------- pkg/outputfile/outputfile.go | 2 +- pkg/outputkubernetes/outputkubernetes.go | 2 +- 7 files changed, 50 insertions(+), 35 deletions(-) diff --git a/pkg/discovery/discovery.go b/pkg/discovery/discovery.go index 8dff71f..481a80f 100644 --- a/pkg/discovery/discovery.go +++ b/pkg/discovery/discovery.go @@ -112,15 +112,23 @@ func parseMetricEndpoint(key string, value string, prefix string) (*endpoints.In parsedMetric := r.FindStringSubmatch(key) if len(parsedMetric) == 3 { - parsedPort, err := strconv.ParseInt(parsedMetric[1], 10, 64) + //TO BE FAIR, NONE OF THIS IS A GOOD IDEA. THIS IS AT BEST A DIRTY AND FUGLY QUICK FIX. THIS SHOULD BE REFACTORED TO DO SOMETHING REASONABLE + var scheme = "http" + parsedString := strings.Split(parsedMetric[1], ":") + if len(parsedString) == 2 { + scheme = parsedString[1] + } + + parsedPort, err := strconv.ParseInt(parsedString[0], 10, 64) if err != nil { return nil, err } return &endpoints.InstanceMetrics{ - Name: value, - Path: parsedMetric[2], - Port: parsedPort, + Name: value, + Path: parsedMetric[2], + Port: parsedPort, + Scheme: scheme, }, nil } diff --git a/pkg/discovery/discovery_test.go b/pkg/discovery/discovery_test.go index f46d222..f45983e 100644 --- a/pkg/discovery/discovery_test.go +++ b/pkg/discovery/discovery_test.go @@ -19,9 +19,8 @@ func TestDiscoveryHasCorrectEndpoints(t *testing.T) { if err != nil { t.Error("Failed to discover instances", err) } - + t.Log(returnedInstanceList) expectedInstanceList := libtesting.InstanceList() - if !reflect.DeepEqual(expectedInstanceList, returnedInstanceList.Instances) { t.Errorf("Expected instance list %v to equal returned instance list %v", expectedInstanceList, returnedInstanceList) } diff --git a/pkg/endpoints/endpoints.go b/pkg/endpoints/endpoints.go index f7a7665..dc8b50a 100644 --- a/pkg/endpoints/endpoints.go +++ b/pkg/endpoints/endpoints.go @@ -14,9 +14,10 @@ type Instance struct { } type InstanceMetrics struct { - Name string - Port int64 - Path string + Name string + Port int64 + Path string + Scheme string } type DiscoveredInstances struct { @@ -28,13 +29,13 @@ type outputFormat struct { Labels map[string]string `json:"labels"` } -func ToJsonString(d DiscoveredInstances) ([]byte, error) { +func ToJSONString(d DiscoveredInstances) ([]byte, error) { outputList := []outputFormat{} for _, instance := range d.Instances { for _, metric := range instance.Metrics { targetHost := fmt.Sprintf("%s:%d", instance.PrivateIP, metric.Port) - l := labels(instance.Tags, targetHost, metric.Path, metric.Name) + l := labels(instance.Tags, targetHost, metric.Path, metric.Name, metric.Scheme) output := outputFormat{Targets: []string{targetHost}, Labels: l} outputList = append(outputList, output) @@ -44,9 +45,10 @@ func ToJsonString(d DiscoveredInstances) ([]byte, error) { return json.Marshal(outputList) } -func labels(tags map[string]string, targetHost string, path string, metricName string) map[string]string { +func labels(tags map[string]string, targetHost string, path string, metricName string, scheme string) map[string]string { labels := standardizeKeys(tags) labels["__metrics_path__"] = path + labels["__scheme__"] = scheme labels["__address__"] = targetHost if val, ok := labels["name"]; ok { labels["instancename"] = val diff --git a/pkg/endpoints/endpoints_test.go b/pkg/endpoints/endpoints_test.go index ccc3835..d471fb3 100644 --- a/pkg/endpoints/endpoints_test.go +++ b/pkg/endpoints/endpoints_test.go @@ -8,8 +8,8 @@ func TestToJsonStringHasCorrectContent(t *testing.T) { instanceList := InstanceList() - returnedJSONContentBytes, _ := ToJsonString(DiscoveredInstances{Instances: instanceList}) - expectedJSONContent := "[{\"targets\":[\"127.0.0.1:9100\"],\"labels\":{\"__address__\":\"127.0.0.1:9100\",\"__metrics_path__\":\"/metrics\",\"billingnumber\":\"1111\",\"instancename\":\"Testinstance1\",\"name\":\"node_exporter\",\"spotprice\":\"123\"}},{\"targets\":[\"127.0.0.1:8080\"],\"labels\":{\"__address__\":\"127.0.0.1:8080\",\"__metrics_path__\":\"/metrics\",\"billingnumber\":\"1111\",\"instancename\":\"Testinstance1\",\"name\":\"blackbox_exporter\",\"spotprice\":\"123\"}},{\"targets\":[\"127.0.0.2:9100\"],\"labels\":{\"__address__\":\"127.0.0.2:9100\",\"__metrics_path__\":\"/metrics\",\"billingnumber\":\"2222\",\"instancename\":\"Testinstance2\",\"name\":\"node_exporter\"}}]" + returnedJSONContentBytes, _ := ToJSONString(DiscoveredInstances{Instances: instanceList}) + expectedJSONContent := "[{\"targets\":[\"127.0.0.1:9100\"],\"labels\":{\"__address__\":\"127.0.0.1:9100\",\"__metrics_path__\":\"/metrics\",\"__scheme__\":\"http\",\"billingnumber\":\"1111\",\"instancename\":\"Testinstance1\",\"name\":\"node_exporter\",\"spotprice\":\"123\"}},{\"targets\":[\"127.0.0.1:8080\"],\"labels\":{\"__address__\":\"127.0.0.1:8080\",\"__metrics_path__\":\"/metrics\",\"__scheme__\":\"https\",\"billingnumber\":\"1111\",\"instancename\":\"Testinstance1\",\"name\":\"blackbox_exporter\",\"spotprice\":\"123\"}},{\"targets\":[\"127.0.0.2:9100\"],\"labels\":{\"__address__\":\"127.0.0.2:9100\",\"__metrics_path__\":\"/metrics\",\"__scheme__\":\"http\",\"billingnumber\":\"2222\",\"instancename\":\"Testinstance2\",\"name\":\"node_exporter\"}}]" if expectedJSONContent != string(returnedJSONContentBytes) { t.Errorf("Expected json string with content %s, but got %s", expectedJSONContent, string(returnedJSONContentBytes)) @@ -27,14 +27,16 @@ func InstanceList() []Instance { tagsI1["Spot price"] = "123" metricsI1 := []InstanceMetrics{} m1I1 := InstanceMetrics{ - Name: "node_exporter", - Path: "/metrics", - Port: 9100, + Name: "node_exporter", + Path: "/metrics", + Port: 9100, + Scheme: "http", } m2I1 := InstanceMetrics{ - Name: "blackbox_exporter", - Path: "/metrics", - Port: 8080, + Name: "blackbox_exporter", + Path: "/metrics", + Port: 8080, + Scheme: "https", } metricsI1 = append(metricsI1, m1I1, m2I1) i1 := Instance{ @@ -52,9 +54,10 @@ func InstanceList() []Instance { tagsI2[" "] = "empty" metricsI2 := []InstanceMetrics{} m1I2 := InstanceMetrics{ - Name: "node_exporter", - Path: "/metrics", - Port: 9100, + Name: "node_exporter", + Path: "/metrics", + Port: 9100, + Scheme: "http", } metricsI2 = append(metricsI2, m1I2) i2 := Instance{ diff --git a/pkg/libtesting/libtesting.go b/pkg/libtesting/libtesting.go index aa34a13..60be5e9 100644 --- a/pkg/libtesting/libtesting.go +++ b/pkg/libtesting/libtesting.go @@ -15,14 +15,16 @@ func InstanceList() []endpoints.Instance { tagsI1["billingnumber"] = "1111" metricsI1 := []endpoints.InstanceMetrics{} m1I1 := endpoints.InstanceMetrics{ - Name: "node_exporter", - Path: "/metrics", - Port: 9100, + Name: "node_exporter", + Path: "/metrics", + Port: 9100, + Scheme: "http", } m2I1 := endpoints.InstanceMetrics{ - Name: "blackbox_exporter", - Path: "/metrics", - Port: 8080, + Name: "blackbox_exporter", + Path: "/metrics", + Port: 8080, + Scheme: "https", } metricsI1 = append(metricsI1, m1I1, m2I1) i1 := endpoints.Instance{ @@ -39,9 +41,10 @@ func InstanceList() []endpoints.Instance { tagsI2["billingnumber"] = "2222" metricsI2 := []endpoints.InstanceMetrics{} m1I2 := endpoints.InstanceMetrics{ - Name: "node_exporter", - Path: "/metrics", - Port: 9100, + Name: "node_exporter", + Path: "/metrics", + Port: 9100, + Scheme: "http", } metricsI2 = append(metricsI2, m1I2) i2 := endpoints.Instance{ @@ -64,7 +67,7 @@ func EC2InstanceList() []*ec2.Instance { Value: aws.String("node_exporter"), } t12 := ec2.Tag{ - Key: aws.String("prom/scrape:8080/metrics"), + Key: aws.String("prom/scrape:8080:https/metrics"), Value: aws.String("blackbox_exporter"), } nameTag1 := ec2.Tag{ diff --git a/pkg/outputfile/outputfile.go b/pkg/outputfile/outputfile.go index 2569430..19bbc4b 100644 --- a/pkg/outputfile/outputfile.go +++ b/pkg/outputfile/outputfile.go @@ -12,7 +12,7 @@ type OutputFile struct { } func (o OutputFile) Write(instances endpoints.DiscoveredInstances) error { - content, err := endpoints.ToJsonString(instances) + content, err := endpoints.ToJSONString(instances) if err != nil { log.Error("Failed to convert instances to json string with error", err) return err diff --git a/pkg/outputkubernetes/outputkubernetes.go b/pkg/outputkubernetes/outputkubernetes.go index b3fb055..446d48c 100644 --- a/pkg/outputkubernetes/outputkubernetes.go +++ b/pkg/outputkubernetes/outputkubernetes.go @@ -45,7 +45,7 @@ func NewOutputKubernetes(kubeconfig string, namespace string, cmName string, cmF } func (o OutputKubernetes) Write(instances endpoints.DiscoveredInstances) error { - output, err := endpoints.ToJsonString(instances) + output, err := endpoints.ToJSONString(instances) if err != nil { log.Error("Failed to convert instances to json string") return err From f79e90be5d6a80c0c9bd85d4bf81df58d66de423 Mon Sep 17 00:00:00 2001 From: Konstantin Kiess Date: Thu, 8 Oct 2020 19:25:11 +0200 Subject: [PATCH 2/2] removed debug line --- pkg/discovery/discovery_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/discovery/discovery_test.go b/pkg/discovery/discovery_test.go index f45983e..59eabda 100644 --- a/pkg/discovery/discovery_test.go +++ b/pkg/discovery/discovery_test.go @@ -19,7 +19,6 @@ func TestDiscoveryHasCorrectEndpoints(t *testing.T) { if err != nil { t.Error("Failed to discover instances", err) } - t.Log(returnedInstanceList) expectedInstanceList := libtesting.InstanceList() if !reflect.DeepEqual(expectedInstanceList, returnedInstanceList.Instances) { t.Errorf("Expected instance list %v to equal returned instance list %v", expectedInstanceList, returnedInstanceList)