2016-06-02 07:32:17 -05:00
|
|
|
package metrics
|
|
|
|
|
2017-09-05 16:19:57 -05:00
|
|
|
import (
|
2018-05-02 12:54:07 -05:00
|
|
|
"fmt"
|
2017-09-05 16:19:57 -05:00
|
|
|
"strings"
|
|
|
|
"time"
|
2016-06-02 07:32:17 -05:00
|
|
|
|
2017-09-06 12:23:03 -05:00
|
|
|
"github.com/grafana/grafana/pkg/metrics/graphitebridge"
|
2017-09-05 16:19:57 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
2016-06-02 07:32:17 -05:00
|
|
|
|
2018-05-02 12:54:07 -05:00
|
|
|
func (im *InternalMetricsService) readSettings() error {
|
|
|
|
var section, err = im.Cfg.Raw.GetSection("metrics")
|
2016-06-02 07:32:17 -05:00
|
|
|
if err != nil {
|
2018-05-02 12:54:07 -05:00
|
|
|
return fmt.Errorf("Unable to find metrics config section %v", err)
|
2016-06-02 07:32:17 -05:00
|
|
|
}
|
|
|
|
|
2018-05-02 12:54:07 -05:00
|
|
|
im.intervalSeconds = section.Key("interval_seconds").MustInt64(10)
|
2016-06-02 07:32:17 -05:00
|
|
|
|
2018-05-02 12:54:07 -05:00
|
|
|
if err := im.parseGraphiteSettings(); err != nil {
|
|
|
|
return fmt.Errorf("Unable to parse metrics graphite section, %v", err)
|
|
|
|
}
|
2017-09-05 16:19:57 -05:00
|
|
|
|
2018-05-02 12:54:07 -05:00
|
|
|
return nil
|
2016-06-02 07:32:17 -05:00
|
|
|
}
|
2017-09-05 16:19:57 -05:00
|
|
|
|
2018-05-02 12:54:07 -05:00
|
|
|
func (im *InternalMetricsService) parseGraphiteSettings() error {
|
|
|
|
graphiteSection, err := im.Cfg.Raw.GetSection("metrics.graphite")
|
|
|
|
|
2017-09-05 16:19:57 -05:00
|
|
|
if err != nil {
|
2018-05-02 12:54:07 -05:00
|
|
|
return nil
|
2017-09-05 16:19:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
address := graphiteSection.Key("address").String()
|
|
|
|
if address == "" {
|
2018-05-02 12:54:07 -05:00
|
|
|
return nil
|
2017-09-05 16:19:57 -05:00
|
|
|
}
|
|
|
|
|
2018-05-02 12:54:07 -05:00
|
|
|
bridgeCfg := &graphitebridge.Config{
|
2017-09-05 16:19:57 -05:00
|
|
|
URL: address,
|
|
|
|
Prefix: graphiteSection.Key("prefix").MustString("prod.grafana.%(instance_name)s"),
|
|
|
|
CountersAsDelta: true,
|
|
|
|
Gatherer: prometheus.DefaultGatherer,
|
2018-05-02 12:54:07 -05:00
|
|
|
Interval: time.Duration(im.intervalSeconds) * time.Second,
|
2017-09-05 16:19:57 -05:00
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
Logger: &logWrapper{logger: metricsLogger},
|
2017-09-06 12:23:03 -05:00
|
|
|
ErrorHandling: graphitebridge.ContinueOnError,
|
2017-09-05 16:19:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
safeInstanceName := strings.Replace(setting.InstanceName, ".", "_", -1)
|
|
|
|
prefix := graphiteSection.Key("prefix").Value()
|
|
|
|
|
|
|
|
if prefix == "" {
|
|
|
|
prefix = "prod.grafana.%(instance_name)s."
|
|
|
|
}
|
|
|
|
|
2018-05-02 12:54:07 -05:00
|
|
|
bridgeCfg.Prefix = strings.Replace(prefix, "%(instance_name)s", safeInstanceName, -1)
|
|
|
|
|
|
|
|
im.graphiteCfg = bridgeCfg
|
|
|
|
return nil
|
2017-09-05 16:19:57 -05:00
|
|
|
}
|