|
| 1 | +package metrics |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/grafana/grafana/pkg/setting" |
| 7 | + |
| 8 | + . "github.com/smartystreets/goconvey/convey" |
| 9 | +) |
| 10 | + |
| 11 | +func TestGraphitePublisher(t *testing.T) { |
| 12 | + |
| 13 | + Convey("Test graphite prefix replacement", t, func() { |
| 14 | + var err error |
| 15 | + err = setting.NewConfigContext(&setting.CommandLineArgs{ |
| 16 | + HomePath: "../../", |
| 17 | + }) |
| 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:2001") |
| 24 | + |
| 25 | + So(err, ShouldBeNil) |
| 26 | + |
| 27 | + setting.InstanceName = "hostname.with.dots.com" |
| 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 | + So(publisher.address, ShouldEqual, "localhost:2001") |
| 35 | + }) |
| 36 | + |
| 37 | + Convey("Test graphite publisher default prefix", t, func() { |
| 38 | + var err error |
| 39 | + err = setting.NewConfigContext(&setting.CommandLineArgs{ |
| 40 | + HomePath: "../../", |
| 41 | + }) |
| 42 | + |
| 43 | + So(err, ShouldBeNil) |
| 44 | + |
| 45 | + sec, err := setting.Cfg.NewSection("metrics.graphite") |
| 46 | + sec.NewKey("address", "localhost:2001") |
| 47 | + |
| 48 | + So(err, ShouldBeNil) |
| 49 | + |
| 50 | + setting.InstanceName = "hostname.with.dots.com" |
| 51 | + publisher, err := CreateGraphitePublisher() |
| 52 | + |
| 53 | + So(err, ShouldBeNil) |
| 54 | + So(publisher, ShouldNotBeNil) |
| 55 | + |
| 56 | + So(publisher.prefix, ShouldEqual, "service.grafana.hostname_with_dots_com.") |
| 57 | + So(publisher.address, ShouldEqual, "localhost:2001") |
| 58 | + }) |
| 59 | + |
| 60 | + Convey("Test graphite publisher default values", t, func() { |
| 61 | + var err error |
| 62 | + err = setting.NewConfigContext(&setting.CommandLineArgs{ |
| 63 | + HomePath: "../../", |
| 64 | + }) |
| 65 | + |
| 66 | + So(err, ShouldBeNil) |
| 67 | + |
| 68 | + _, err = setting.Cfg.NewSection("metrics.graphite") |
| 69 | + |
| 70 | + setting.InstanceName = "hostname.with.dots.com" |
| 71 | + publisher, err := CreateGraphitePublisher() |
| 72 | + |
| 73 | + So(err, ShouldBeNil) |
| 74 | + So(publisher, ShouldBeNil) |
| 75 | + }) |
| 76 | +} |
0 commit comments