Skip to content

Commit 60ac3f3

Browse files
committed
PMM-13477 a bit of refactoring.
1 parent 1a22fdc commit 60ac3f3

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

exporter/exporter.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,6 @@ func (e *Exporter) makeRegistry(ctx context.Context, client *mongo.Client, topol
145145
gc := newGeneralCollector(ctx, client, nodeType, e.opts.Logger)
146146
registry.MustRegister(gc)
147147

148-
if client == nil {
149-
return registry
150-
}
151-
152148
// Enable collectors like collstats and indexstats depending on the number of collections
153149
// present in the database.
154150
limitsOk := false
@@ -340,13 +336,18 @@ func (e *Exporter) Handler() http.Handler {
340336
gatherers = append(gatherers, prometheus.DefaultGatherer)
341337
}
342338

339+
var registry *prometheus.Registry
343340
var ti *topologyInfo
344341
if client != nil {
345342
// Topology can change between requests, so we need to get it every time.
346343
ti = newTopologyInfo(ctx, client, e.logger)
344+
registry = e.makeRegistry(ctx, client, ti, requestOpts)
345+
} else {
346+
registry = prometheus.NewRegistry()
347+
gc := newGeneralCollector(ctx, client, "", e.opts.Logger)
348+
registry.MustRegister(gc)
347349
}
348350

349-
registry := e.makeRegistry(ctx, client, ti, requestOpts)
350351
gatherers = append(gatherers, registry)
351352

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

exporter/general_collector.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,17 @@ func mongodbUpMetric(ctx context.Context, client *mongo.Client, nodeType mongoDB
6060
if client != nil {
6161
if err := client.Ping(ctx, readpref.PrimaryPreferred()); err == nil {
6262
value = 1
63+
switch nodeType { //nolint:exhaustive
64+
case typeMongos:
65+
clusterRole = typeMongos
66+
case typeArbiter:
67+
clusterRole = typeArbiter
68+
default:
69+
clusterRole = typeMongod
70+
}
6371
} else {
6472
log.Errorf("error while checking mongodb connection: %s. mongo_up is set to 0", err.Error())
6573
}
66-
67-
switch nodeType { //nolint:exhaustive
68-
case typeMongos:
69-
clusterRole = typeMongos
70-
case typeArbiter:
71-
clusterRole = typeArbiter
72-
default:
73-
clusterRole = typeMongod
74-
}
7574
}
7675

7776
labels := map[string]string{"cluster_role": string(clusterRole)}

exporter/server.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,24 @@ func OverallTargetsHandler(exporters []*Exporter, logger *logrus.Logger) http.Ha
147147
}()
148148
}
149149

150+
var registry *prometheus.Registry
150151
var ti *topologyInfo
151152
if client != nil {
152153
// Topology can change between requests, so we need to get it every time.
153154
ti = newTopologyInfo(ctx, client, e.logger)
155+
registry = e.makeRegistry(ctx, client, ti, requestOpts)
156+
} else {
157+
registry = prometheus.NewRegistry()
158+
gc := newGeneralCollector(ctx, client, "", e.opts.Logger)
159+
registry.MustRegister(gc)
154160
}
155161

156162
hostlabels := prometheus.Labels{
157163
"instance": e.opts.NodeName,
158164
}
159165

160-
registry := NewGathererWrapper(e.makeRegistry(ctx, client, ti, requestOpts), hostlabels)
161-
gatherers = append(gatherers, registry)
166+
gw := NewGathererWrapper(registry, hostlabels)
167+
gatherers = append(gatherers, gw)
162168
}
163169

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

0 commit comments

Comments
 (0)