Skip to content
Snippets Groups Projects
Commit 81368d46 authored by Pavlo Strokov's avatar Pavlo Strokov
Browse files

prometheus: Avoid duplicated metrics registration

Praefect uses prometheus to export metrics from inside.
It relies on the defaults from the prometheus library
to gather set of metrics and register a new metrics.
Because of it the new metrics got registered on the
DefaultRegisterer - a global pre-configured registerer.
Because of that we can't call 'run' function multiple
times (for testing purposes) as it results to the metrics
registration error. To omit that problem the 'run' function
extended with prometheus.Registerer parameter that is used
to register praefect custom metrics. The production code
still uses the same DefaultRegisterer as it was before.
And the test code creates a new instance of the registerer
for each 'run' invocation, so there are no more duplicates.
parent 18ff3676
No related branches found
No related tags found
Loading
......@@ -149,7 +149,7 @@ func main() {
logger.Fatalf("unable to create a bootstrap: %v", err)
}
if err := run(starterConfigs, conf, b); err != nil {
if err := run(starterConfigs, conf, b, prometheus.DefaultRegisterer); err != nil {
logger.Fatalf("%v", err)
}
}
......@@ -192,18 +192,18 @@ func configure(conf config.Config) {
sentry.ConfigureSentry(version.GetVersion(), conf.Sentry)
}
func run(cfgs []starter.Config, conf config.Config, b bootstrap.Listener) error {
nodeLatencyHistogram, err := metrics.RegisterNodeLatency(conf.Prometheus)
func run(cfgs []starter.Config, conf config.Config, b bootstrap.Listener, promreg prometheus.Registerer) error {
nodeLatencyHistogram, err := metrics.RegisterNodeLatency(conf.Prometheus, promreg)
if err != nil {
return err
}
delayMetric, err := metrics.RegisterReplicationDelay(conf.Prometheus)
delayMetric, err := metrics.RegisterReplicationDelay(conf.Prometheus, promreg)
if err != nil {
return err
}
latencyMetric, err := metrics.RegisterReplicationLatency(conf.Prometheus)
latencyMetric, err := metrics.RegisterReplicationLatency(conf.Prometheus, promreg)
if err != nil {
return err
}
......@@ -390,11 +390,11 @@ func run(cfgs []starter.Config, conf config.Config, b bootstrap.Listener) error
)
metricsCollectors = append(metricsCollectors, transactionManager, coordinator, repl)
if db != nil {
prometheus.MustRegister(
promreg.MustRegister(
datastore.NewRepositoryStoreCollector(logger, conf.VirtualStorageNames(), db, conf.Prometheus.ScrapeTimeout),
)
}
prometheus.MustRegister(metricsCollectors...)
promreg.MustRegister(metricsCollectors...)
for _, cfg := range cfgs {
srv, err := srvFactory.Create(cfg.IsSecure())
......@@ -447,7 +447,7 @@ func run(cfgs []starter.Config, conf config.Config, b bootstrap.Listener) error
conf.StorageNames(),
conf.Reconciliation.HistogramBuckets,
)
prometheus.MustRegister(r)
promreg.MustRegister(r)
go r.Run(ctx, helper.NewTimerTicker(interval))
}
}
......
......@@ -9,6 +9,7 @@ import (
"testing"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v14/client"
......@@ -88,7 +89,7 @@ func TestListUntrackedRepositories_Exec(t *testing.T) {
bootstrapper := bootstrap.NewNoop()
go func() {
defer close(stopped)
assert.NoError(t, run(starterConfigs, conf, bootstrapper))
assert.NoError(t, run(starterConfigs, conf, bootstrapper, prometheus.NewRegistry()))
}()
cc, err := client.Dial("unix://"+conf.SocketPath, nil)
......
......@@ -7,6 +7,7 @@ import (
"testing"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
......@@ -111,7 +112,7 @@ func TestRemoveRepository_Exec(t *testing.T) {
bootstrapper := bootstrap.NewNoop()
go func() {
defer close(stopped)
assert.NoError(t, run(starterConfigs, conf, bootstrapper))
assert.NoError(t, run(starterConfigs, conf, bootstrapper, prometheus.NewRegistry()))
}()
cc, err := client.Dial("unix://"+conf.SocketPath, nil)
......
......@@ -9,7 +9,7 @@ import (
// RegisterReplicationDelay creates and registers a prometheus histogram
// to observe replication delay times
func RegisterReplicationDelay(conf gitalycfgprom.Config) (metrics.HistogramVec, error) {
func RegisterReplicationDelay(conf gitalycfgprom.Config, registerer prometheus.Registerer) (metrics.HistogramVec, error) {
replicationDelay := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "gitaly",
......@@ -20,12 +20,12 @@ func RegisterReplicationDelay(conf gitalycfgprom.Config) (metrics.HistogramVec,
[]string{"type"},
)
return replicationDelay, prometheus.Register(replicationDelay)
return replicationDelay, registerer.Register(replicationDelay)
}
// RegisterReplicationLatency creates and registers a prometheus histogram
// to observe replication latency times
func RegisterReplicationLatency(conf gitalycfgprom.Config) (metrics.HistogramVec, error) {
func RegisterReplicationLatency(conf gitalycfgprom.Config, registerer prometheus.Registerer) (metrics.HistogramVec, error) {
replicationLatency := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "gitaly",
......@@ -36,12 +36,12 @@ func RegisterReplicationLatency(conf gitalycfgprom.Config) (metrics.HistogramVec
[]string{"type"},
)
return replicationLatency, prometheus.Register(replicationLatency)
return replicationLatency, registerer.Register(replicationLatency)
}
// RegisterNodeLatency creates and registers a prometheus histogram to
// observe internal node latency
func RegisterNodeLatency(conf gitalycfgprom.Config) (metrics.HistogramVec, error) {
func RegisterNodeLatency(conf gitalycfgprom.Config, registerer prometheus.Registerer) (metrics.HistogramVec, error) {
nodeLatency := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "gitaly",
......@@ -51,7 +51,7 @@ func RegisterNodeLatency(conf gitalycfgprom.Config) (metrics.HistogramVec, error
}, []string{"gitaly_storage"},
)
return nodeLatency, prometheus.Register(nodeLatency)
return nodeLatency, registerer.Register(nodeLatency)
}
var MethodTypeCounter = promauto.NewCounterVec(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment