Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/go_modules/tools/github.com/daixi…
Browse files Browse the repository at this point in the history
…ang0/gci-0.11.0
  • Loading branch information
JiriCtvrtka authored Aug 17, 2023
2 parents da508d1 + 7312f7b commit 9801e2a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
13 changes: 11 additions & 2 deletions exporter/dbstats_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ type dbstatsCollector struct {
topologyInfo labelsGetter

databaseFilter []string

freeStorage bool
}

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

databaseFilter: databaseRegex,

freeStorage: freeStorage,
}
}

Expand Down Expand Up @@ -71,7 +75,12 @@ func (d *dbstatsCollector) collect(ch chan<- prometheus.Metric) {
logger.Debugf("getting stats for databases: %v", dbNames)
for _, db := range dbNames {
var dbStats bson.M
cmd := bson.D{{Key: "dbStats", Value: 1}, {Key: "scale", Value: 1}}
var cmd bson.D
if d.freeStorage {
cmd = bson.D{{Key: "dbStats", Value: 1}, {Key: "scale", Value: 1}, {Key: "freeStorage", Value: 1}}
} else {
cmd = bson.D{{Key: "dbStats", Value: 1}, {Key: "scale", Value: 1}}
}
r := client.Database(db).RunCommand(d.ctx, cmd)
err := r.Decode(&dbStats)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion exporter/dbstats_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestDBStatsCollector(t *testing.T) {

ti := labelsGetterMock{}

c := newDBStatsCollector(ctx, client, logrus.New(), false, ti, []string{dbName})
c := newDBStatsCollector(ctx, client, logrus.New(), false, ti, []string{dbName}, false)
expected := strings.NewReader(`
# HELP mongodb_dbstats_collections dbstats.
# TYPE mongodb_dbstats_collections untyped
Expand Down
19 changes: 11 additions & 8 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ type Opts struct {
DiscoveringMode bool
GlobalConnPool bool

CollectAll bool
EnableDBStats bool
EnableDiagnosticData bool
EnableReplicasetStatus bool
EnableTopMetrics bool
EnableIndexStats bool
EnableCollStats bool
CollectAll bool
EnableDBStats bool
EnableDBStatsFreeStorage bool
EnableDiagnosticData bool
EnableReplicasetStatus bool
EnableTopMetrics bool
EnableIndexStats bool
EnableCollStats bool

EnableOverrideDescendingIndex bool

Expand Down Expand Up @@ -164,6 +165,7 @@ func (e *Exporter) makeRegistry(ctx context.Context, client *mongo.Client, topol
}
e.opts.EnableDiagnosticData = true
e.opts.EnableDBStats = true
e.opts.EnableDBStatsFreeStorage = true
e.opts.EnableCollStats = true
e.opts.EnableTopMetrics = true
e.opts.EnableReplicasetStatus = true
Expand All @@ -173,6 +175,7 @@ func (e *Exporter) makeRegistry(ctx context.Context, client *mongo.Client, topol
// arbiter only have isMaster privileges
if isArbiter {
e.opts.EnableDBStats = false
e.opts.EnableDBStatsFreeStorage = false
e.opts.EnableCollStats = false
e.opts.EnableTopMetrics = false
e.opts.EnableReplicasetStatus = false
Expand Down Expand Up @@ -203,7 +206,7 @@ func (e *Exporter) makeRegistry(ctx context.Context, client *mongo.Client, topol

if e.opts.EnableDBStats && limitsOk && requestOpts.EnableDBStats {
cc := newDBStatsCollector(ctx, client, e.opts.Logger,
e.opts.CompatibleMode, topologyInfo, nil)
e.opts.CompatibleMode, topologyInfo, nil, e.opts.EnableDBStatsFreeStorage)
registry.MustRegister(cc)
}

Expand Down
26 changes: 14 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ type GlobalFlags struct {
TLSConfigPath string `name:"web.config" help:"Path to the file having Prometheus TLS config for basic auth"`
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"`

EnableDiagnosticData bool `name:"collector.diagnosticdata" help:"Enable collecting metrics from getDiagnosticData"`
EnableReplicasetStatus bool `name:"collector.replicasetstatus" help:"Enable collecting metrics from replSetGetStatus"`
EnableDBStats bool `name:"collector.dbstats" help:"Enable collecting metrics from dbStats"`
EnableTopMetrics bool `name:"collector.topmetrics" help:"Enable collecting metrics from top admin command"`
EnableIndexStats bool `name:"collector.indexstats" help:"Enable collecting metrics from $indexStats"`
EnableCollStats bool `name:"collector.collstats" help:"Enable collecting metrics from $collStats"`
EnableDiagnosticData bool `name:"collector.diagnosticdata" help:"Enable collecting metrics from getDiagnosticData"`
EnableReplicasetStatus bool `name:"collector.replicasetstatus" help:"Enable collecting metrics from replSetGetStatus"`
EnableDBStats bool `name:"collector.dbstats" help:"Enable collecting metrics from dbStats"`
EnableDBStatsFreeStorage bool `name:"collector.dbstatsfreestorage" help:"Enable collecting free space metrics from dbStats"`
EnableTopMetrics bool `name:"collector.topmetrics" help:"Enable collecting metrics from top admin command"`
EnableIndexStats bool `name:"collector.indexstats" help:"Enable collecting metrics from $indexStats"`
EnableCollStats bool `name:"collector.collstats" help:"Enable collecting metrics from $collStats"`

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

Expand Down Expand Up @@ -122,12 +123,13 @@ func buildExporter(opts GlobalFlags) *exporter.Exporter {
TLSConfigPath: opts.TLSConfigPath,
DirectConnect: opts.DirectConnect,

EnableDiagnosticData: opts.EnableDiagnosticData,
EnableReplicasetStatus: opts.EnableReplicasetStatus,
EnableTopMetrics: opts.EnableTopMetrics,
EnableDBStats: opts.EnableDBStats,
EnableIndexStats: opts.EnableIndexStats,
EnableCollStats: opts.EnableCollStats,
EnableDiagnosticData: opts.EnableDiagnosticData,
EnableReplicasetStatus: opts.EnableReplicasetStatus,
EnableTopMetrics: opts.EnableTopMetrics,
EnableDBStats: opts.EnableDBStats,
EnableDBStatsFreeStorage: opts.EnableDBStatsFreeStorage,
EnableIndexStats: opts.EnableIndexStats,
EnableCollStats: opts.EnableCollStats,

EnableOverrideDescendingIndex: opts.EnableOverrideDescendingIndex,

Expand Down

0 comments on commit 9801e2a

Please sign in to comment.