From b5a694a7a454ed0a395158a863a5c5812038c554 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sun, 6 Aug 2017 12:39:16 +0200 Subject: [PATCH] [BUGFIX] Fixed Graphite naming (#72) * Bugfix: Graphite compatibility with meshviewer * Bugfix: Bad message: Router type not sanitized * Refactoring: Implemented replaceInvalidChars function for Graphite --- database/graphite/global.go | 2 +- database/graphite/node.go | 3 +-- database/graphite/utils.go | 11 +++++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 database/graphite/utils.go diff --git a/database/graphite/global.go b/database/graphite/global.go index 00f2d31..648d5c7 100644 --- a/database/graphite/global.go +++ b/database/graphite/global.go @@ -27,7 +27,7 @@ func GlobalStatsFields(stats *runtime.GlobalStats) []graphigo.Metric { func (c *Connection) addCounterMap(name string, m runtime.CounterMap, t time.Time) { var fields []graphigo.Metric for key, count := range m { - fields = append(fields, graphigo.Metric{Name: name + `.` + key + `.count`, Value: count, Timestamp: t}) + fields = append(fields, graphigo.Metric{Name: name + `.` + replaceInvalidChars(key) + `.count`, Value: count, Timestamp: t}) } c.addPoint(fields) } diff --git a/database/graphite/node.go b/database/graphite/node.go index 2b04e9a..817f562 100644 --- a/database/graphite/node.go +++ b/database/graphite/node.go @@ -1,7 +1,6 @@ package graphite import ( - "strings" "time" "github.com/FreifunkBremen/yanic/runtime" @@ -25,7 +24,7 @@ func (c *Connection) InsertNode(node *runtime.Node) { return } - node_prefix := MeasurementNode + `.` + stats.NodeID + `.` + strings.Replace(nodeinfo.Hostname, ".", "__", -1) + node_prefix := MeasurementNode + `.` + stats.NodeID + `.` + replaceInvalidChars(nodeinfo.Hostname) addField := func(name string, value interface{}) { fields = append(fields, graphigo.Metric{Name: node_prefix + "." + name, Value: value}) diff --git a/database/graphite/utils.go b/database/graphite/utils.go new file mode 100644 index 0000000..7e678ca --- /dev/null +++ b/database/graphite/utils.go @@ -0,0 +1,11 @@ +package graphite + +import ( + "regexp" +) + +var reInvalidChars = regexp.MustCompile("(?i)[^a-z0-9\\-]") + +func replaceInvalidChars(name string) string { + return reInvalidChars.ReplaceAllString(name, "_") +}