Skip to content

Commit

Permalink
PMM-13477 a bit of refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
BupycHuk committed Nov 4, 2024
1 parent 1a22fdc commit 60ac3f3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
11 changes: 6 additions & 5 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,6 @@ func (e *Exporter) makeRegistry(ctx context.Context, client *mongo.Client, topol
gc := newGeneralCollector(ctx, client, nodeType, e.opts.Logger)
registry.MustRegister(gc)

if client == nil {
return registry
}

// Enable collectors like collstats and indexstats depending on the number of collections
// present in the database.
limitsOk := false
Expand Down Expand Up @@ -340,13 +336,18 @@ func (e *Exporter) Handler() http.Handler {
gatherers = append(gatherers, prometheus.DefaultGatherer)
}

var registry *prometheus.Registry
var ti *topologyInfo
if client != nil {
// Topology can change between requests, so we need to get it every time.
ti = newTopologyInfo(ctx, client, e.logger)
registry = e.makeRegistry(ctx, client, ti, requestOpts)
} else {
registry = prometheus.NewRegistry()
gc := newGeneralCollector(ctx, client, "", e.opts.Logger)
registry.MustRegister(gc)
}

registry := e.makeRegistry(ctx, client, ti, requestOpts)
gatherers = append(gatherers, registry)

// Delegate http serving to Prometheus client library, which will call collector.Collect.
Expand Down
17 changes: 8 additions & 9 deletions exporter/general_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,17 @@ func mongodbUpMetric(ctx context.Context, client *mongo.Client, nodeType mongoDB
if client != nil {
if err := client.Ping(ctx, readpref.PrimaryPreferred()); err == nil {
value = 1
switch nodeType { //nolint:exhaustive
case typeMongos:
clusterRole = typeMongos
case typeArbiter:
clusterRole = typeArbiter
default:
clusterRole = typeMongod
}
} else {
log.Errorf("error while checking mongodb connection: %s. mongo_up is set to 0", err.Error())
}

switch nodeType { //nolint:exhaustive
case typeMongos:
clusterRole = typeMongos
case typeArbiter:
clusterRole = typeArbiter
default:
clusterRole = typeMongod
}
}

labels := map[string]string{"cluster_role": string(clusterRole)}
Expand Down
10 changes: 8 additions & 2 deletions exporter/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,24 @@ func OverallTargetsHandler(exporters []*Exporter, logger *logrus.Logger) http.Ha
}()
}

var registry *prometheus.Registry
var ti *topologyInfo
if client != nil {
// Topology can change between requests, so we need to get it every time.
ti = newTopologyInfo(ctx, client, e.logger)
registry = e.makeRegistry(ctx, client, ti, requestOpts)
} else {
registry = prometheus.NewRegistry()
gc := newGeneralCollector(ctx, client, "", e.opts.Logger)
registry.MustRegister(gc)
}

hostlabels := prometheus.Labels{
"instance": e.opts.NodeName,
}

registry := NewGathererWrapper(e.makeRegistry(ctx, client, ti, requestOpts), hostlabels)
gatherers = append(gatherers, registry)
gw := NewGathererWrapper(registry, hostlabels)
gatherers = append(gatherers, gw)
}

// Delegate http serving to Prometheus client library, which will call collector.Collect.
Expand Down

0 comments on commit 60ac3f3

Please sign in to comment.