diff --git a/pkg/endpoints/endpoints.go b/pkg/endpoints/endpoints.go index fc203aa..fe46380 100644 --- a/pkg/endpoints/endpoints.go +++ b/pkg/endpoints/endpoints.go @@ -45,7 +45,7 @@ func ToJsonString(d DiscoveredInstances) ([]byte, error) { } func labels(tags map[string]string, targetHost string, path string, metricName string) map[string]string { - labels := lowerMapKeys(tags) + labels := standardizeKeys(tags) labels["__metrics_path__"] = path labels["__address__"] = targetHost if val, ok := labels["name"]; ok { @@ -55,10 +55,11 @@ func labels(tags map[string]string, targetHost string, path string, metricName s return labels } -func lowerMapKeys(tags map[string]string) map[string]string { +func standardizeKeys(tags map[string]string) map[string]string { lowerMap := map[string]string{} for key, value := range tags { - lowerMap[strings.ToLower(key)] = value + reducedByEmpty := strings.ReplaceAll(key, " ", "-") + lowerMap[strings.ToLower(reducedByEmpty)] = value } return lowerMap } diff --git a/pkg/endpoints/endpoints_test.go b/pkg/endpoints/endpoints_test.go index 65ca6ec..619a4d4 100644 --- a/pkg/endpoints/endpoints_test.go +++ b/pkg/endpoints/endpoints_test.go @@ -9,7 +9,7 @@ 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\"}},{\"targets\":[\"127.0.0.1:8080\"],\"labels\":{\"__address__\":\"127.0.0.1:8080\",\"__metrics_path__\":\"/metrics\",\"billingnumber\":\"1111\",\"instancename\":\"Testinstance1\",\"name\":\"blackbox_exporter\"}},{\"targets\":[\"127.0.0.2:9100\"],\"labels\":{\"__address__\":\"127.0.0.2:9100\",\"__metrics_path__\":\"/metrics\",\"billingnumber\":\"2222\",\"instancename\":\"Testinstance2\",\"name\":\"node_exporter\"}}]" + expectedJSONContent := "[{\"targets\":[\"127.0.0.1:9100\"],\"labels\":{\"__address__\":\"127.0.0.1:9100\",\"__metrics_path__\":\"/metrics\",\"billingnumber\":\"1111\",\"instancename\":\"Testinstance1\",\"name\":\"node_exporter\",\"spot-price\":\"123\"}},{\"targets\":[\"127.0.0.1:8080\"],\"labels\":{\"__address__\":\"127.0.0.1:8080\",\"__metrics_path__\":\"/metrics\",\"billingnumber\":\"1111\",\"instancename\":\"Testinstance1\",\"name\":\"blackbox_exporter\",\"spot-price\":\"123\"}},{\"targets\":[\"127.0.0.2:9100\"],\"labels\":{\"__address__\":\"127.0.0.2:9100\",\"__metrics_path__\":\"/metrics\",\"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)) @@ -24,6 +24,7 @@ func InstanceList() []Instance { tagsI1 := make(map[string]string) tagsI1["Name"] = "Testinstance1" tagsI1["billingnumber"] = "1111" + tagsI1["Spot price"] = "123" metricsI1 := []InstanceMetrics{} m1I1 := InstanceMetrics{ Name: "node_exporter",