Skip to content

Commit 0524d0c

Browse files
committed
fix(metrics): replaces . with _ in instance name
closes grafana#5739
1 parent 2b2e015 commit 0524d0c

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

circle.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test:
2323
# GO VET
2424
- go vet ./pkg/...
2525
# Go test
26-
- godep go test -v ./pkg/...
26+
- godep go test ./pkg/...
2727
# js tests
2828
- npm test
2929
- npm run coveralls

conf/defaults.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ enabled = true
353353
interval_seconds = 60
354354

355355
# Send internal Grafana metrics to graphite
356-
; [metrics.graphite]
356+
[metrics.graphite]
357357
; address = localhost:2003
358358
; prefix = prod.grafana.%(instance_name)s.
359359

conf/sample.ini

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,15 +297,15 @@ check_for_updates = true
297297
# Metrics available at HTTP API Url /api/metrics
298298
[metrics]
299299
# Disable / Enable internal metrics
300-
;enabled = true
300+
enabled = true
301301

302302
# Publish interval
303303
;interval_seconds = 10
304304

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

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

pkg/metrics/graphite.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"net"
7+
"strings"
78
"time"
89

910
"github.com/grafana/grafana/pkg/log"
@@ -20,14 +21,18 @@ type GraphitePublisher struct {
2021
func CreateGraphitePublisher() (*GraphitePublisher, error) {
2122
graphiteSection, err := setting.Cfg.GetSection("metrics.graphite")
2223
if err != nil {
23-
return nil, nil
24+
return nil, err
2425
}
2526

2627
publisher := &GraphitePublisher{}
2728
publisher.prevCounts = make(map[string]int64)
2829
publisher.protocol = "tcp"
2930
publisher.address = graphiteSection.Key("address").MustString("localhost:2003")
30-
publisher.prefix = graphiteSection.Key("prefix").MustString("service.grafana.%(instance_name)s")
31+
32+
safeInstanceName := strings.Replace(setting.InstanceName, ".", "_", -1)
33+
prefix := graphiteSection.Key("prefix").MustString("service.grafana.%instance_name%")
34+
35+
publisher.prefix = strings.Replace(prefix, "%instance_name%", safeInstanceName, -1)
3136

3237
return publisher, nil
3338
}

pkg/metrics/graphite_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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", t, func() {
14+
err := setting.NewConfigContext(&setting.CommandLineArgs{
15+
HomePath: "../../",
16+
Args: []string{
17+
"cfg:metrics.graphite.prefix=service.grafana.%instance_name%",
18+
"cfg:metrics.graphite.address=localhost:2003",
19+
},
20+
})
21+
So(err, ShouldBeNil)
22+
23+
setting.InstanceName = "hostname.with.dots.com"
24+
publisher, err2 := CreateGraphitePublisher()
25+
26+
So(err2, ShouldBeNil)
27+
So(publisher, ShouldNotBeNil)
28+
29+
So(publisher.prefix, ShouldEqual, "service.grafana.hostname_with_dots_com")
30+
})
31+
}

0 commit comments

Comments
 (0)