2016-06-02 07:32:17 -05:00
|
|
|
package metrics
|
|
|
|
|
2017-09-05 16:19:57 -05:00
|
|
|
import (
|
|
|
|
"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"
|
|
|
|
ini "gopkg.in/ini.v1"
|
|
|
|
)
|
2016-06-02 07:32:17 -05:00
|
|
|
|
|
|
|
type MetricSettings struct {
|
2017-09-05 16:19:57 -05:00
|
|
|
Enabled bool
|
|
|
|
IntervalSeconds int64
|
2017-09-06 12:23:03 -05:00
|
|
|
GraphiteBridgeConfig *graphitebridge.Config
|
2016-06-02 07:32:17 -05:00
|
|
|
}
|
|
|
|
|
2017-09-05 16:19:57 -05:00
|
|
|
func ReadSettings(file *ini.File) *MetricSettings {
|
2016-06-02 07:32:17 -05:00
|
|
|
var settings = &MetricSettings{
|
2017-09-05 16:19:57 -05:00
|
|
|
Enabled: false,
|
2016-06-02 07:32:17 -05:00
|
|
|
}
|
|
|
|
|
2017-09-05 16:19:57 -05:00
|
|
|
var section, err = file.GetSection("metrics")
|
2016-06-02 07:32:17 -05:00
|
|
|
if err != nil {
|
2016-06-07 02:29:47 -05:00
|
|
|
metricsLogger.Crit("Unable to find metrics config section", "error", err)
|
2016-06-02 07:32:17 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
settings.Enabled = section.Key("enabled").MustBool(false)
|
|
|
|
settings.IntervalSeconds = section.Key("interval_seconds").MustInt64(10)
|
|
|
|
|
|
|
|
if !settings.Enabled {
|
|
|
|
return settings
|
|
|
|
}
|
|
|
|
|
2017-09-05 16:19:57 -05:00
|
|
|
cfg, err := parseGraphiteSettings(settings, file)
|
|
|
|
if err != nil {
|
|
|
|
metricsLogger.Crit("Unable to parse metrics graphite section", "error", err)
|
|
|
|
return nil
|
2016-06-02 07:32:17 -05:00
|
|
|
}
|
|
|
|
|
2017-09-05 16:19:57 -05:00
|
|
|
settings.GraphiteBridgeConfig = cfg
|
|
|
|
|
2016-06-02 07:32:17 -05:00
|
|
|
return settings
|
|
|
|
}
|
2017-09-05 16:19:57 -05:00
|
|
|
|
2017-09-06 12:23:03 -05:00
|
|
|
func parseGraphiteSettings(settings *MetricSettings, file *ini.File) (*graphitebridge.Config, error) {
|
2018-04-30 09:21:04 -05:00
|
|
|
graphiteSection, err := setting.Raw.GetSection("metrics.graphite")
|
2017-09-05 16:19:57 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
address := graphiteSection.Key("address").String()
|
|
|
|
if address == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-09-06 12:23:03 -05:00
|
|
|
cfg := &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,
|
|
|
|
Interval: time.Duration(settings.IntervalSeconds) * time.Second,
|
|
|
|
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."
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Prefix = strings.Replace(prefix, "%(instance_name)s", safeInstanceName, -1)
|
|
|
|
return cfg, nil
|
|
|
|
}
|