grafana/pkg/infra/metrics/settings.go

62 lines
1.6 KiB
Go
Raw Normal View History

package metrics
2017-09-05 16:19:57 -05:00
import (
"fmt"
2017-09-05 16:19:57 -05:00
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/grafana/grafana/pkg/infra/metrics/graphitebridge"
2017-09-05 16:19:57 -05:00
)
func (im *InternalMetricsService) readSettings() error {
var section, err = im.Cfg.Raw.GetSection("metrics")
if err != nil {
return fmt.Errorf("unable to find metrics config section: %w", err)
}
im.intervalSeconds = section.Key("interval_seconds").MustInt64(10)
if err := im.parseGraphiteSettings(); err != nil {
return fmt.Errorf("unable to parse metrics graphite section: %w", err)
}
2017-09-05 16:19:57 -05:00
return nil
}
2017-09-05 16:19:57 -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 {
return nil
2017-09-05 16:19:57 -05:00
}
address := graphiteSection.Key("address").String()
if address == "" {
return nil
2017-09-05 16:19:57 -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,
Interval: time.Duration(im.intervalSeconds) * time.Second,
2017-09-05 16:19:57 -05:00
Timeout: 10 * time.Second,
Logger: &logWrapper{logger: metricsLogger},
ErrorHandling: graphitebridge.ContinueOnError,
2017-09-05 16:19:57 -05:00
}
safeInstanceName := strings.ReplaceAll(im.Cfg.InstanceName, ".", "_")
2017-09-05 16:19:57 -05:00
prefix := graphiteSection.Key("prefix").Value()
if prefix == "" {
prefix = "prod.grafana.%(instance_name)s."
}
bridgeCfg.Prefix = strings.ReplaceAll(prefix, "%(instance_name)s", safeInstanceName)
im.graphiteCfg = bridgeCfg
return nil
2017-09-05 16:19:57 -05:00
}