Skip to content

Commit 03f0bc4

Browse files
committed
fix(metrics): make metrics cfg backwards compatible
1 parent c5d0c9f commit 03f0bc4

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

conf/defaults.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ interval_seconds = 60
358358
# Send internal Grafana metrics to graphite
359359
; [metrics.graphite]
360360
; address = localhost:2003
361-
; prefix = service.grafana.%(instance_name)s.
361+
; prefix = service.grafana.%(instance_name)s
362362

363363
[grafana_net]
364364
url = https://grafana.net

conf/sample.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,10 @@ enabled = true
303303
# Publish interval
304304
;interval_seconds = 10
305305

306-
# Send internal metrics to Graphite. %instance_name% in prefix will be replaced with the value of instance_name
306+
# Send internal metrics to Graphite
307307
; [metrics.graphite]
308308
; address = localhost:2003
309-
; prefix = service.grafana.%instance_name%
309+
; prefix = service.grafana.%(instance_name)s
310310

311311
#################################### Internal Grafana Metrics ##########################
312312
# Url used to to import dashboards directly from Grafana.net

pkg/metrics/graphite.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ func CreateGraphitePublisher() (*GraphitePublisher, error) {
3030
publisher.address = graphiteSection.Key("address").MustString("localhost:2003")
3131

3232
safeInstanceName := strings.Replace(setting.InstanceName, ".", "_", -1)
33-
prefix := graphiteSection.Key("prefix").MustString("service.grafana.%instance_name%")
33+
prefix := graphiteSection.Key("prefix").Value()
3434

35-
publisher.prefix = strings.Replace(prefix, "%instance_name%", safeInstanceName, -1)
35+
if prefix == "" {
36+
prefix = "service.grafana.%(instance_name)s"
37+
}
38+
39+
publisher.prefix = strings.Replace(prefix, "%(instance_name)s", safeInstanceName, -1)
3640

3741
return publisher, nil
3842
}

pkg/metrics/graphite_test.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,46 @@ import (
1010

1111
func TestGraphitePublisher(t *testing.T) {
1212

13-
Convey("Test graphite prefix", t, func() {
14-
err := setting.NewConfigContext(&setting.CommandLineArgs{
13+
Convey("Test graphite prefix replacement", t, func() {
14+
var err error
15+
err = setting.NewConfigContext(&setting.CommandLineArgs{
1516
HomePath: "../../",
16-
Args: []string{
17-
"cfg:metrics.graphite.prefix=service.grafana.%instance_name%",
18-
"cfg:metrics.graphite.address=localhost:2003",
19-
},
2017
})
18+
19+
So(err, ShouldBeNil)
20+
21+
sec, err := setting.Cfg.NewSection("metrics.graphite")
22+
sec.NewKey("prefix", "service.grafana.%(instance_name)s")
23+
sec.NewKey("address", "localhost:2003")
24+
2125
So(err, ShouldBeNil)
2226

2327
setting.InstanceName = "hostname.with.dots.com"
24-
publisher, err2 := CreateGraphitePublisher()
28+
publisher, err := CreateGraphitePublisher()
29+
30+
So(err, ShouldBeNil)
31+
So(publisher, ShouldNotBeNil)
32+
33+
So(publisher.prefix, ShouldEqual, "service.grafana.hostname_with_dots_com")
34+
})
2535

26-
So(err2, ShouldBeNil)
36+
Convey("Test graphite publisher default values", t, func() {
37+
var err error
38+
err = setting.NewConfigContext(&setting.CommandLineArgs{
39+
HomePath: "../../",
40+
})
41+
42+
So(err, ShouldBeNil)
43+
44+
_, err = setting.Cfg.NewSection("metrics.graphite")
45+
46+
setting.InstanceName = "hostname.with.dots.com"
47+
publisher, err := CreateGraphitePublisher()
48+
49+
So(err, ShouldBeNil)
2750
So(publisher, ShouldNotBeNil)
2851

2952
So(publisher.prefix, ShouldEqual, "service.grafana.hostname_with_dots_com")
53+
So(publisher.address, ShouldEqual, "localhost:2003")
3054
})
3155
}

0 commit comments

Comments
 (0)