From f7a9102523a202b9090ff8761d87630b9f0f4b47 Mon Sep 17 00:00:00 2001 From: bergquist Date: Mon, 15 Aug 2016 09:37:24 +0200 Subject: [PATCH] feat(metrics): improve graphite settings Fixes the broken support for overriding settings with ENV variables. closes #5769 --- conf/defaults.ini | 7 ++++--- conf/sample.ini | 9 +++++---- pkg/metrics/graphite.go | 7 ++++++- pkg/metrics/graphite_test.go | 28 ++++++++++++++++++++++++---- 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/conf/defaults.ini b/conf/defaults.ini index 8c3fb36dded..5e7f138690c 100644 --- a/conf/defaults.ini +++ b/conf/defaults.ini @@ -353,9 +353,10 @@ enabled = true interval_seconds = 60 # Send internal Grafana metrics to graphite -; [metrics.graphite] -; address = localhost:2003 -; prefix = service.grafana.%(instance_name)s. +[metrics.graphite] +# Enable by setting the address setting (ex localhost:2003) +address = +prefix = service.grafana.%(instance_name)s. [grafana_net] url = https://grafana.net diff --git a/conf/sample.ini b/conf/sample.ini index d82412fa26e..4ebf2d46fef 100644 --- a/conf/sample.ini +++ b/conf/sample.ini @@ -300,12 +300,13 @@ check_for_updates = true enabled = true # Publish interval -;interval_seconds = 10 +interval_seconds = 10 # Send internal metrics to Graphite -; [metrics.graphite] -; address = localhost:2003 -; prefix = service.grafana.%(instance_name)s. +[metrics.graphite] +# Enable by setting the address setting (ex localhost:2003) +address = +prefix = service.grafana.%(instance_name)s. #################################### 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 bbd5696c85d..17031ed2931 100644 --- a/pkg/metrics/graphite.go +++ b/pkg/metrics/graphite.go @@ -24,10 +24,15 @@ func CreateGraphitePublisher() (*GraphitePublisher, error) { return nil, nil } + address := graphiteSection.Key("address").String() + if address == "" { + return nil, nil + } + publisher := &GraphitePublisher{} publisher.prevCounts = make(map[string]int64) publisher.protocol = "tcp" - publisher.address = graphiteSection.Key("address").MustString("localhost:2003") + publisher.address = address safeInstanceName := strings.Replace(setting.InstanceName, ".", "_", -1) prefix := graphiteSection.Key("prefix").Value() diff --git a/pkg/metrics/graphite_test.go b/pkg/metrics/graphite_test.go index 8ecee9449a5..320aa3be82b 100644 --- a/pkg/metrics/graphite_test.go +++ b/pkg/metrics/graphite_test.go @@ -34,6 +34,29 @@ func TestGraphitePublisher(t *testing.T) { So(publisher.address, ShouldEqual, "localhost:2001") }) + Convey("Test graphite publisher default prefix", t, func() { + var err error + err = setting.NewConfigContext(&setting.CommandLineArgs{ + HomePath: "../../", + }) + + So(err, ShouldBeNil) + + sec, err := setting.Cfg.NewSection("metrics.graphite") + sec.NewKey("address", "localhost:2001") + + So(err, ShouldBeNil) + + setting.InstanceName = "hostname.with.dots.com" + publisher, err := CreateGraphitePublisher() + + So(err, ShouldBeNil) + So(publisher, ShouldNotBeNil) + + So(publisher.prefix, ShouldEqual, "service.grafana.hostname_with_dots_com.") + So(publisher.address, ShouldEqual, "localhost:2001") + }) + Convey("Test graphite publisher default values", t, func() { var err error err = setting.NewConfigContext(&setting.CommandLineArgs{ @@ -48,9 +71,6 @@ func TestGraphitePublisher(t *testing.T) { publisher, err := CreateGraphitePublisher() So(err, ShouldBeNil) - So(publisher, ShouldNotBeNil) - - So(publisher.prefix, ShouldEqual, "service.grafana.hostname_with_dots_com.") - So(publisher.address, ShouldEqual, "localhost:2003") + So(publisher, ShouldBeNil) }) }