grafana/pkg/infra/metrics/service.go
Serge Zaitsev 2f8ee38c5e
Chore: Let wire inject prometheus.Registerer (#62195)
let wire inject prometheus.Registerer
2023-02-28 16:12:23 +01:00

60 lines
1.3 KiB
Go

package metrics
import (
"context"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics/graphitebridge"
"github.com/grafana/grafana/pkg/setting"
"github.com/prometheus/client_golang/prometheus"
)
var metricsLogger log.Logger = log.New("metrics")
type logWrapper struct {
logger log.Logger
}
func (lw *logWrapper) Println(v ...interface{}) {
lw.logger.Info("graphite metric bridge", v...)
}
func init() {
initMetricVars()
initFrontendMetrics()
}
func ProvideService(cfg *setting.Cfg) (*InternalMetricsService, error) {
s := &InternalMetricsService{
Cfg: cfg,
}
return s, s.readSettings()
}
type InternalMetricsService struct {
Cfg *setting.Cfg
intervalSeconds int64
graphiteCfg *graphitebridge.Config
}
func (im *InternalMetricsService) Run(ctx context.Context) error {
// Start Graphite Bridge
if im.graphiteCfg != nil {
bridge, err := graphitebridge.NewBridge(im.graphiteCfg)
if err != nil {
metricsLogger.Error("failed to create graphite bridge", "error", err)
} else {
go bridge.Run(ctx)
}
}
MInstanceStart.Inc()
<-ctx.Done()
return ctx.Err()
}
func ProvideRegisterer() prometheus.Registerer { return prometheus.DefaultRegisterer }
func ProvideRegistererForTest() prometheus.Registerer { return prometheus.NewRegistry() }