Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replSetGetConfigCollector #295

Merged
merged 29 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
31dca05
replSetGetConfigCollector. it just works
hiroshi Jun 12, 2021
ff968d7
Use member_idx for host field to match with replStatus
hiroshi Jun 13, 2021
0837f71
Add --disable.replicasetconfig option flag
hiroshi Jun 13, 2021
3bab659
Merge branch 'main' into replSetGetConfigCollector
hiroshi Jun 22, 2021
03c4922
Merge branch 'main' into replSetGetConfigCollector
JiriCtvrtka Jul 26, 2021
c5ba8b6
Merge branch 'main' into replSetGetConfigCollector
denisok Jul 27, 2021
f38e30b
Merge branch 'main' into replSetGetConfigCollector
JiriCtvrtka Jul 28, 2021
296e11b
Merge branch 'main' into replSetGetConfigCollector
JiriCtvrtka Aug 25, 2021
be17763
Merge branch 'main' into replSetGetConfigCollector
hiroshi Sep 19, 2021
54ded8c
Use errors.As() instead of type assertion.
hiroshi Sep 20, 2021
22b9fe3
Add replset_config_collector_test.
hiroshi Sep 20, 2021
678e125
Revert "Use errors.As() instead of type assertion."
hiroshi Sep 20, 2021
bb9ecc8
nolint - errors.As seems to cause segfault.
hiroshi Sep 21, 2021
bd5ca57
Merge remote-tracking branch 'origin/main' into replSetGetConfigColle…
hiroshi Feb 24, 2023
529c27c
Merge branch 'main' into replSetGetConfigCollector
BupycHuk Nov 11, 2024
1b2fe08
Update exporter/replset_config_collector.go
BupycHuk Nov 11, 2024
dc4899c
Update exporter/replset_config_collector_test.go
BupycHuk Nov 11, 2024
22cf389
Update exporter/replset_config_collector.go
BupycHuk Nov 11, 2024
8d051b0
Update exporter/replset_config_collector_test.go
BupycHuk Nov 11, 2024
ec5c37a
Update exporter/replset_config_collector.go
BupycHuk Nov 11, 2024
f5b0c15
Update exporter/exporter.go
BupycHuk Nov 11, 2024
58da23b
Update exporter/exporter.go
BupycHuk Nov 11, 2024
75ecc85
Update exporter/replset_config_collector_test.go
BupycHuk Nov 11, 2024
6578e64
Update replset_config_collector.go
BupycHuk Nov 11, 2024
0857d3d
Update replset_config_collector_test.go
BupycHuk Nov 11, 2024
08d54d2
Update exporter/replset_config_collector_test.go
BupycHuk Nov 11, 2024
12469dd
Update exporter/replset_config_collector.go
BupycHuk Nov 11, 2024
d4c588d
Update replset_config_collector.go
BupycHuk Nov 11, 2024
36c5423
Merge branch 'main' into replSetGetConfigCollector
BupycHuk Nov 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Currently, these metric sources are implemented:
|\-\-log.level|Only log messages with the given severity or above. Valid levels: [debug, info, warn, error]|\-\-log.level="error"|
|\-\-disable.diagnosticdata|Disable collecting metrics from getDiagnosticData||
|\-\-disable.replicasetstatus|Disable collecting metrics from replSetGetStatus||
|\-\-disable.replicasetconfig|Disable collecting metrics from replSetGetConfig||
|--version|Show version and exit|

### Build the exporter
Expand Down
12 changes: 12 additions & 0 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type Opts struct {
Logger *logrus.Logger
DisableDiagnosticData bool
DisableReplicasetStatus bool
DisableReplicasetConfig bool
}

var (
Expand Down Expand Up @@ -153,6 +154,17 @@ func (e *Exporter) makeRegistry(ctx context.Context, client *mongo.Client, topol
registry.MustRegister(&rsgsc)
}

if !e.opts.DisableReplicasetConfig {
rsgcc := replSetGetConfigCollector{
ctx: ctx,
client: client,
compatibleMode: e.opts.CompatibleMode,
logger: e.opts.Logger,
topologyInfo: topologyInfo,
}
registry.MustRegister(&rsgcc)
}

return registry
}

Expand Down
3 changes: 3 additions & 0 deletions exporter/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ func processSlice(prefix, k string, v []interface{}, commonLabels map[string]str
if state, ok := s["stateStr"].(string); ok {
labels["member_state"] = state
}
if host, ok := s["host"].(string); ok {
labels["member_idx"] = host
}

metrics = append(metrics, makeMetrics(prefix+k, s, labels, compatibleMode)...)
}
Expand Down
80 changes: 80 additions & 0 deletions exporter/replset_config_collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// mongodb_exporter
// Copyright (C) 2017 Percona LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package exporter

import (
"context"

"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)

// const (
// replicationNotEnabled = 76
// replicationNotYetInitialized = 94
// )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we keeping this for purpose?

BupycHuk marked this conversation as resolved.
Show resolved Hide resolved
BupycHuk marked this conversation as resolved.
Show resolved Hide resolved

type replSetGetConfigCollector struct {
ctx context.Context
client *mongo.Client
compatibleMode bool
logger *logrus.Logger
topologyInfo labelsGetter
}

func (d *replSetGetConfigCollector) Describe(ch chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(d, ch)
}

func (d *replSetGetConfigCollector) Collect(ch chan<- prometheus.Metric) {
cmd := bson.D{{Key: "replSetGetConfig", Value: "1"}}
res := d.client.Database("admin").RunCommand(d.ctx, cmd)

var m bson.M

if err := res.Decode(&m); err != nil {
if e, ok := err.(mongo.CommandError); ok {
if e.Code == replicationNotYetInitialized || e.Code == replicationNotEnabled {
return
}
}
d.logger.Errorf("cannot get replSetGetConfig: %s", err)

return
}

config, ok := m["config"].(bson.M)
if !ok {
err := errors.Wrapf(errUnexpectedDataType, "%T for data field", m["config"])
d.logger.Errorf("cannot decode getDiagnosticData: %s", err)

return
}
m = config

d.logger.Debug("replSetGetConfig result:")
debugResult(d.logger, m)

for _, metric := range makeMetrics("cfg", m, d.topologyInfo.baseLabels(), d.compatibleMode) {
BupycHuk marked this conversation as resolved.
Show resolved Hide resolved
ch <- metric
}
}

var _ prometheus.Collector = (*replSetGetConfigCollector)(nil)
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type GlobalFlags struct {

DisableDiagnosticData bool `name:"disable.diagnosticdata" help:"Disable collecting metrics from getDiagnosticData"`
DisableReplicasetStatus bool `name:"disable.replicasetstatus" help:"Disable collecting metrics from replSetGetStatus"`
DisableReplicasetConfig bool `name:"disable.replicasetconfig" help:"Disable collecting metrics from replSetGetConfig"`

DiscoveringMode bool `name:"discovering-mode" help:"Enable autodiscover collections"`
CompatibleMode bool `name:"compatible-mode" help:"Enable old mongodb-exporter compatible metrics"`
Expand Down Expand Up @@ -116,6 +117,7 @@ func buildExporter(opts GlobalFlags) (*exporter.Exporter, error) {
WebListenAddress: opts.WebListenAddress,
DisableDiagnosticData: opts.DisableDiagnosticData,
DisableReplicasetStatus: opts.DisableReplicasetStatus,
DisableReplicasetConfig: opts.DisableReplicasetConfig,
DirectConnect: opts.DirectConnect,
}

Expand Down
1 change: 1 addition & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestBuildExporter(t *testing.T) {

DisableDiagnosticData: true,
DisableReplicasetStatus: true,
DisableReplicasetConfig: true,

CompatibleMode: true,
}
Expand Down