Skip to content

Commit 7312f7b

Browse files
hgghAlex TymchukBupycHuk
authored
add option --collector.dbstatsfreestorage to receive freeStorage stats from dbstats (#666)
* add option --collector.dbstatsfreestorage to receive freeStorage stats from dbstats #617 * do not add freeStorage option per default. not suppored at mongodb 4.4 --------- Co-authored-by: Alex Tymchuk <[email protected]> Co-authored-by: Nurlan Moldomurov <[email protected]>
1 parent 9896754 commit 7312f7b

File tree

4 files changed

+37
-23
lines changed

4 files changed

+37
-23
lines changed

exporter/dbstats_collector.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ type dbstatsCollector struct {
3232
topologyInfo labelsGetter
3333

3434
databaseFilter []string
35+
36+
freeStorage bool
3537
}
3638

3739
// newDBStatsCollector creates a collector for statistics on database storage.
38-
func newDBStatsCollector(ctx context.Context, client *mongo.Client, logger *logrus.Logger, compatible bool, topology labelsGetter, databaseRegex []string) *dbstatsCollector {
40+
func newDBStatsCollector(ctx context.Context, client *mongo.Client, logger *logrus.Logger, compatible bool, topology labelsGetter, databaseRegex []string, freeStorage bool) *dbstatsCollector {
3941
return &dbstatsCollector{
4042
ctx: ctx,
4143
base: newBaseCollector(client, logger),
@@ -44,6 +46,8 @@ func newDBStatsCollector(ctx context.Context, client *mongo.Client, logger *logr
4446
topologyInfo: topology,
4547

4648
databaseFilter: databaseRegex,
49+
50+
freeStorage: freeStorage,
4751
}
4852
}
4953

@@ -71,7 +75,12 @@ func (d *dbstatsCollector) collect(ch chan<- prometheus.Metric) {
7175
logger.Debugf("getting stats for databases: %v", dbNames)
7276
for _, db := range dbNames {
7377
var dbStats bson.M
74-
cmd := bson.D{{Key: "dbStats", Value: 1}, {Key: "scale", Value: 1}}
78+
var cmd bson.D
79+
if d.freeStorage {
80+
cmd = bson.D{{Key: "dbStats", Value: 1}, {Key: "scale", Value: 1}, {Key: "freeStorage", Value: 1}}
81+
} else {
82+
cmd = bson.D{{Key: "dbStats", Value: 1}, {Key: "scale", Value: 1}}
83+
}
7584
r := client.Database(db).RunCommand(d.ctx, cmd)
7685
err := r.Decode(&dbStats)
7786
if err != nil {

exporter/dbstats_collector_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestDBStatsCollector(t *testing.T) {
5858

5959
ti := labelsGetterMock{}
6060

61-
c := newDBStatsCollector(ctx, client, logrus.New(), false, ti, []string{dbName})
61+
c := newDBStatsCollector(ctx, client, logrus.New(), false, ti, []string{dbName}, false)
6262
expected := strings.NewReader(`
6363
# HELP mongodb_dbstats_collections dbstats.
6464
# TYPE mongodb_dbstats_collections untyped

exporter/exporter.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@ type Opts struct {
6060
DiscoveringMode bool
6161
GlobalConnPool bool
6262

63-
CollectAll bool
64-
EnableDBStats bool
65-
EnableDiagnosticData bool
66-
EnableReplicasetStatus bool
67-
EnableTopMetrics bool
68-
EnableIndexStats bool
69-
EnableCollStats bool
63+
CollectAll bool
64+
EnableDBStats bool
65+
EnableDBStatsFreeStorage bool
66+
EnableDiagnosticData bool
67+
EnableReplicasetStatus bool
68+
EnableTopMetrics bool
69+
EnableIndexStats bool
70+
EnableCollStats bool
7071

7172
EnableOverrideDescendingIndex bool
7273

@@ -164,6 +165,7 @@ func (e *Exporter) makeRegistry(ctx context.Context, client *mongo.Client, topol
164165
}
165166
e.opts.EnableDiagnosticData = true
166167
e.opts.EnableDBStats = true
168+
e.opts.EnableDBStatsFreeStorage = true
167169
e.opts.EnableCollStats = true
168170
e.opts.EnableTopMetrics = true
169171
e.opts.EnableReplicasetStatus = true
@@ -173,6 +175,7 @@ func (e *Exporter) makeRegistry(ctx context.Context, client *mongo.Client, topol
173175
// arbiter only have isMaster privileges
174176
if isArbiter {
175177
e.opts.EnableDBStats = false
178+
e.opts.EnableDBStatsFreeStorage = false
176179
e.opts.EnableCollStats = false
177180
e.opts.EnableTopMetrics = false
178181
e.opts.EnableReplicasetStatus = false
@@ -203,7 +206,7 @@ func (e *Exporter) makeRegistry(ctx context.Context, client *mongo.Client, topol
203206

204207
if e.opts.EnableDBStats && limitsOk && requestOpts.EnableDBStats {
205208
cc := newDBStatsCollector(ctx, client, e.opts.Logger,
206-
e.opts.CompatibleMode, topologyInfo, nil)
209+
e.opts.CompatibleMode, topologyInfo, nil, e.opts.EnableDBStatsFreeStorage)
207210
registry.MustRegister(cc)
208211
}
209212

main.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ type GlobalFlags struct {
4444
TLSConfigPath string `name:"web.config" help:"Path to the file having Prometheus TLS config for basic auth"`
4545
LogLevel string `name:"log.level" help:"Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal]" enum:"debug,info,warn,error,fatal" default:"error"`
4646

47-
EnableDiagnosticData bool `name:"collector.diagnosticdata" help:"Enable collecting metrics from getDiagnosticData"`
48-
EnableReplicasetStatus bool `name:"collector.replicasetstatus" help:"Enable collecting metrics from replSetGetStatus"`
49-
EnableDBStats bool `name:"collector.dbstats" help:"Enable collecting metrics from dbStats"`
50-
EnableTopMetrics bool `name:"collector.topmetrics" help:"Enable collecting metrics from top admin command"`
51-
EnableIndexStats bool `name:"collector.indexstats" help:"Enable collecting metrics from $indexStats"`
52-
EnableCollStats bool `name:"collector.collstats" help:"Enable collecting metrics from $collStats"`
47+
EnableDiagnosticData bool `name:"collector.diagnosticdata" help:"Enable collecting metrics from getDiagnosticData"`
48+
EnableReplicasetStatus bool `name:"collector.replicasetstatus" help:"Enable collecting metrics from replSetGetStatus"`
49+
EnableDBStats bool `name:"collector.dbstats" help:"Enable collecting metrics from dbStats"`
50+
EnableDBStatsFreeStorage bool `name:"collector.dbstatsfreestorage" help:"Enable collecting free space metrics from dbStats"`
51+
EnableTopMetrics bool `name:"collector.topmetrics" help:"Enable collecting metrics from top admin command"`
52+
EnableIndexStats bool `name:"collector.indexstats" help:"Enable collecting metrics from $indexStats"`
53+
EnableCollStats bool `name:"collector.collstats" help:"Enable collecting metrics from $collStats"`
5354

5455
EnableOverrideDescendingIndex bool `name:"metrics.overridedescendingindex" help:"Enable descending index name override to replace -1 with _DESC"`
5556

@@ -122,12 +123,13 @@ func buildExporter(opts GlobalFlags) *exporter.Exporter {
122123
TLSConfigPath: opts.TLSConfigPath,
123124
DirectConnect: opts.DirectConnect,
124125

125-
EnableDiagnosticData: opts.EnableDiagnosticData,
126-
EnableReplicasetStatus: opts.EnableReplicasetStatus,
127-
EnableTopMetrics: opts.EnableTopMetrics,
128-
EnableDBStats: opts.EnableDBStats,
129-
EnableIndexStats: opts.EnableIndexStats,
130-
EnableCollStats: opts.EnableCollStats,
126+
EnableDiagnosticData: opts.EnableDiagnosticData,
127+
EnableReplicasetStatus: opts.EnableReplicasetStatus,
128+
EnableTopMetrics: opts.EnableTopMetrics,
129+
EnableDBStats: opts.EnableDBStats,
130+
EnableDBStatsFreeStorage: opts.EnableDBStatsFreeStorage,
131+
EnableIndexStats: opts.EnableIndexStats,
132+
EnableCollStats: opts.EnableCollStats,
131133

132134
EnableOverrideDescendingIndex: opts.EnableOverrideDescendingIndex,
133135

0 commit comments

Comments
 (0)