From 5b57210aeb06fc555c4604c145420d82d00d5f9c Mon Sep 17 00:00:00 2001 From: bergquist Date: Tue, 9 Aug 2016 11:07:26 +0200 Subject: [PATCH] fix(metrics): replaces . with _ in instance name closes #5739 --- circle.yml | 2 +- conf/defaults.ini | 2 +- conf/sample.ini | 8 ++++---- pkg/metrics/graphite.go | 9 +++++++-- pkg/metrics/graphite_test.go | 31 +++++++++++++++++++++++++++++++ 5 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 pkg/metrics/graphite_test.go diff --git a/circle.yml b/circle.yml index ee19b50ee46..28c36763d9e 100644 --- a/circle.yml +++ b/circle.yml @@ -23,7 +23,7 @@ test: # GO VET - go vet ./pkg/... # Go test - - godep go test -v ./pkg/... + - godep go test ./pkg/... # js tests - npm test - npm run coveralls diff --git a/conf/defaults.ini b/conf/defaults.ini index 5233fe89722..77279c0f68b 100644 --- a/conf/defaults.ini +++ b/conf/defaults.ini @@ -356,7 +356,7 @@ enabled = true interval_seconds = 60 # Send internal Grafana metrics to graphite -; [metrics.graphite] +[metrics.graphite] ; address = localhost:2003 ; prefix = prod.grafana.%(instance_name)s. diff --git a/conf/sample.ini b/conf/sample.ini index 6abc8ba416d..859ba1675dd 100644 --- a/conf/sample.ini +++ b/conf/sample.ini @@ -298,15 +298,15 @@ check_for_updates = true # Metrics available at HTTP API Url /api/metrics [metrics] # Disable / Enable internal metrics -;enabled = true +enabled = true # Publish interval ;interval_seconds = 10 -# Send internal metrics to Graphite -; [metrics.graphite] +# Send internal metrics to Graphite. %instance_name% in prefix will be replaced with the value of instance_name +[metrics.graphite] ; address = localhost:2003 -; prefix = prod.grafana.%(instance_name)s. +; prefix = service.grafana.%instance_name% #################################### Internal Grafana Metrics ########################## # Url used to to import dashboards directly from Grafana.net diff --git a/pkg/metrics/graphite.go b/pkg/metrics/graphite.go index a232b97905e..a384b7f9d28 100644 --- a/pkg/metrics/graphite.go +++ b/pkg/metrics/graphite.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "net" + "strings" "time" "github.com/grafana/grafana/pkg/log" @@ -20,14 +21,18 @@ type GraphitePublisher struct { func CreateGraphitePublisher() (*GraphitePublisher, error) { graphiteSection, err := setting.Cfg.GetSection("metrics.graphite") if err != nil { - return nil, nil + return nil, err } publisher := &GraphitePublisher{} publisher.prevCounts = make(map[string]int64) publisher.protocol = "tcp" publisher.address = graphiteSection.Key("address").MustString("localhost:2003") - publisher.prefix = graphiteSection.Key("prefix").MustString("service.grafana.%(instance_name)s") + + safeInstanceName := strings.Replace(setting.InstanceName, ".", "_", -1) + prefix := graphiteSection.Key("prefix").MustString("service.grafana.%instance_name%") + + publisher.prefix = strings.Replace(prefix, "%instance_name%", safeInstanceName, -1) return publisher, nil } diff --git a/pkg/metrics/graphite_test.go b/pkg/metrics/graphite_test.go new file mode 100644 index 00000000000..764214fcb10 --- /dev/null +++ b/pkg/metrics/graphite_test.go @@ -0,0 +1,31 @@ +package metrics + +import ( + "testing" + + "github.com/grafana/grafana/pkg/setting" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestGraphitePublisher(t *testing.T) { + + Convey("Test graphite prefix", t, func() { + err := setting.NewConfigContext(&setting.CommandLineArgs{ + HomePath: "../../", + Args: []string{ + "cfg:metrics.graphite.prefix=service.grafana.%instance_name%", + "cfg:metrics.graphite.address=localhost:2003", + }, + }) + So(err, ShouldBeNil) + + setting.InstanceName = "hostname.with.dots.com" + publisher, err2 := CreateGraphitePublisher() + + So(err2, ShouldBeNil) + So(publisher, ShouldNotBeNil) + + So(publisher.prefix, ShouldEqual, "service.grafana.hostname_with_dots_com") + }) +}