Prechádzať zdrojové kódy

fix(metrics): replaces . with _ in instance name

closes #5739
bergquist 9 rokov pred
rodič
commit
5b57210aeb

+ 1 - 1
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

+ 1 - 1
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.
 

+ 4 - 4
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

+ 7 - 2
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
 }

+ 31 - 0
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")
+	})
+}