Skip to content

Commit

Permalink
fix parsing issues in prometheus when value contains empty car
Browse files Browse the repository at this point in the history
  • Loading branch information
Björn Wenzel committed Jul 10, 2020
1 parent 9e48888 commit 5880c7e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
7 changes: 4 additions & 3 deletions pkg/endpoints/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
3 changes: 2 additions & 1 deletion pkg/endpoints/endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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",
Expand Down

0 comments on commit 5880c7e

Please sign in to comment.