Skip to content

Commit 0951da9

Browse files
committed
Merge branch 'v3.1.x' of github.com:grafana/grafana into v3.1.x
2 parents a5f0f50 + 3195209 commit 0951da9

File tree

7 files changed

+153
-45
lines changed

7 files changed

+153
-45
lines changed

circle.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@ dependencies:
1818

1919
test:
2020
override:
21-
# FMT
2221
- test -z "$(gofmt -s -l . | grep -v Godeps/_workspace/src/ | tee /dev/stderr)"
23-
# GO VET
2422
- go vet ./pkg/...
25-
# Go test
26-
- godep go test -v ./pkg/...
27-
# js tests
23+
# JS tests
2824
- npm test
2925
- npm run coveralls
26+
# GO tests
27+
- godep go test -v ./pkg/...
3028

3129
deployment:
3230
master:

conf/defaults.ini

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,10 @@ enabled = true
353353
interval_seconds = 60
354354

355355
# Send internal Grafana metrics to graphite
356-
; [metrics.graphite]
357-
; address = localhost:2003
358-
; prefix = prod.grafana.%(instance_name)s.
356+
[metrics.graphite]
357+
# Enable by setting the address setting (ex localhost:2003)
358+
address =
359+
prefix = service.grafana.%(instance_name)s.
359360

360361
[grafana_net]
361362
url = https://grafana.net

conf/sample.ini

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,15 +297,16 @@ 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
303-
;interval_seconds = 10
303+
interval_seconds = 10
304304

305305
# Send internal metrics to Graphite
306-
; [metrics.graphite]
307-
; address = localhost:2003
308-
; prefix = prod.grafana.%(instance_name)s.
306+
[metrics.graphite]
307+
# Enable by setting the address setting (ex localhost:2003)
308+
address =
309+
prefix = service.grafana.%(instance_name)s.
309310

310311
#################################### Internal Grafana Metrics ##########################
311312
# Url used to to import dashboards directly from Grafana.net

pkg/cmd/grafana-cli/main.go

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,11 @@ import (
88
"github.com/codegangsta/cli"
99
"github.com/grafana/grafana/pkg/cmd/grafana-cli/commands"
1010
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
11+
"github.com/grafana/grafana/pkg/cmd/grafana-cli/utils"
1112
)
1213

1314
var version = "master"
1415

15-
func getGrafanaPluginDir() string {
16-
currentOS := runtime.GOOS
17-
defaultNix := "/var/lib/grafana/plugins"
18-
19-
if currentOS == "windows" {
20-
return "../data/plugins"
21-
}
22-
23-
pwd, err := os.Getwd()
24-
25-
if err != nil {
26-
logger.Error("Could not get current path. using default")
27-
return defaultNix
28-
}
29-
30-
if isDevenvironment(pwd) {
31-
return "../data/plugins"
32-
}
33-
34-
return defaultNix
35-
}
36-
37-
func isDevenvironment(pwd string) bool {
38-
// if ../conf/defaults.ini exists, grafana is not installed as package
39-
// that its in development environment.
40-
_, err := os.Stat("../conf/defaults.ini")
41-
return err == nil
42-
}
43-
4416
func main() {
4517
setupLogging()
4618

@@ -54,7 +26,7 @@ func main() {
5426
cli.StringFlag{
5527
Name: "pluginsDir",
5628
Usage: "path to the grafana plugin directory",
57-
Value: getGrafanaPluginDir(),
29+
Value: utils.GetGrafanaPluginDir(runtime.GOOS),
5830
EnvVar: "GF_PLUGIN_DIR",
5931
},
6032
cli.StringFlag{
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package utils
2+
3+
import (
4+
"os"
5+
6+
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
7+
)
8+
9+
func GetGrafanaPluginDir(currentOS string) string {
10+
//currentOS := runtime.GOOS
11+
12+
if currentOS == "windows" {
13+
return returnOsDefault(currentOS)
14+
}
15+
16+
pwd, err := os.Getwd()
17+
18+
if err != nil {
19+
logger.Error("Could not get current path. using default")
20+
return returnOsDefault(currentOS)
21+
}
22+
23+
if isDevenvironment(pwd) {
24+
return "../data/plugins"
25+
}
26+
27+
return returnOsDefault(currentOS)
28+
}
29+
30+
func isDevenvironment(pwd string) bool {
31+
// if ../conf/defaults.ini exists, grafana is not installed as package
32+
// that its in development environment.
33+
_, err := os.Stat("../conf/defaults.ini")
34+
return err == nil
35+
}
36+
37+
func returnOsDefault(currentOs string) string {
38+
switch currentOs {
39+
case "windows":
40+
return "../data/plugins"
41+
case "darwin":
42+
return "/usr/local/var/lib/grafana/plugins"
43+
default: //"linux"
44+
return "/var/lib/grafana/plugins"
45+
}
46+
}

pkg/metrics/graphite.go

Lines changed: 16 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"
@@ -23,11 +24,24 @@ func CreateGraphitePublisher() (*GraphitePublisher, error) {
2324
return nil, nil
2425
}
2526

27+
address := graphiteSection.Key("address").String()
28+
if address == "" {
29+
return nil, nil
30+
}
31+
2632
publisher := &GraphitePublisher{}
2733
publisher.prevCounts = make(map[string]int64)
2834
publisher.protocol = "tcp"
29-
publisher.address = graphiteSection.Key("address").MustString("localhost:2003")
30-
publisher.prefix = graphiteSection.Key("prefix").MustString("service.grafana.%(instance_name)s")
35+
publisher.address = address
36+
37+
safeInstanceName := strings.Replace(setting.InstanceName, ".", "_", -1)
38+
prefix := graphiteSection.Key("prefix").Value()
39+
40+
if prefix == "" {
41+
prefix = "service.grafana.%(instance_name)s."
42+
}
43+
44+
publisher.prefix = strings.Replace(prefix, "%(instance_name)s", safeInstanceName, -1)
3145

3246
return publisher, nil
3347
}

pkg/metrics/graphite_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)