-
Notifications
You must be signed in to change notification settings - Fork 450
replSetGetConfigCollector #295
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
Changes from 22 commits
31dca05
ff968d7
0837f71
3bab659
03c4922
c5ba8b6
f38e30b
296e11b
be17763
54ded8c
22b9fe3
678e125
bb9ecc8
bd5ca57
529c27c
1b2fe08
dc4899c
22cf389
8d051b0
ec5c37a
f5b0c15
58da23b
75ecc85
6578e64
0857d3d
08d54d2
12469dd
d4c588d
36c5423
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| // 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 | ||
| // ) | ||
BupycHuk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
BupycHuk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| type replSetGetConfigCollector struct { | ||
| ctx context.Context | ||
| base *baseCollector | ||
|
|
||
| compatibleMode bool | ||
| topologyInfo labelsGetter | ||
| } | ||
|
|
||
| // newReplicationSetConfigCollector creates a collector for configuration of replication set. | ||
| func newReplicationSetConfigCollector(ctx context.Context, client *mongo.Client, logger *logrus.Logger, compatible bool, topology labelsGetter) *replSetGetConfigCollector { | ||
| return &replSetGetConfigCollector{ | ||
| ctx: ctx, | ||
| base: newBaseCollector(client, logger.WithFields(logrus.Fields{"collector": "replset_config"})), | ||
|
|
||
| compatibleMode: compatible, | ||
| topologyInfo: topology, | ||
| } | ||
| } | ||
|
|
||
| func (d *replSetGetConfigCollector) Describe(ch chan<- *prometheus.Desc) { | ||
| d.base.Describe(d.ctx, ch, d.collect) | ||
| } | ||
|
|
||
| func (d *replSetGetConfigCollector) Collect(ch chan<- prometheus.Metric) { | ||
| d.base.Collect(ch) | ||
| } | ||
|
|
||
| func (d *replSetGetConfigCollector) collect(ch chan<- prometheus.Metric) { | ||
| defer measureCollectTime(ch, "mongodb", "replset_config")() | ||
|
|
||
| logger := d.base.logger | ||
| client := d.base.client | ||
|
|
||
| cmd := bson.D{{Key: "replSetGetConfig", Value: "1"}} | ||
| res := 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 { //nolint // https://github.com/percona/mongodb_exporter/pull/295#issuecomment-922874632 | ||
| if e.Code == replicationNotYetInitialized || e.Code == replicationNotEnabled { | ||
| return | ||
| } | ||
| } | ||
| 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"]) | ||
| logger.Errorf("cannot decode getDiagnosticData: %s", err) | ||
|
|
||
| return | ||
| } | ||
| m = config | ||
|
|
||
| logger.Debug("replSetGetConfig result:") | ||
| debugResult(logger, m) | ||
ademidoff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| for _, metric := range makeMetrics("rs_cfg", m, d.topologyInfo.baseLabels(), d.compatibleMode) { | ||
| ch <- metric | ||
| } | ||
| } | ||
|
|
||
| var _ prometheus.Collector = (*replSetGetConfigCollector)(nil) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // 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" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus/testutil" | ||
| "github.com/sirupsen/logrus" | ||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| "github.com/percona/mongodb_exporter/internal/tu" | ||
| ) | ||
|
|
||
| func TestReplsetConfigCollector(t *testing.T) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) | ||
| defer cancel() | ||
|
|
||
| client := tu.DefaultTestClient(ctx, t) | ||
|
|
||
| ti := labelsGetterMock{} | ||
|
|
||
| c := newReplicationSetConfigCollector(ctx, client, logrus.New(), false, ti) | ||
|
|
||
| // The last \n at the end of this string is important | ||
| expected := strings.NewReader(` | ||
| # HELP mongodb_rs_cfg_protocolVersion cfg. | ||
BupycHuk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # TYPE mongodb_rs_cfg_protocolVersion untyped | ||
| mongodb_rs_cfg_protocolVersion 1` + "\n") | ||
| // Filter metrics for 2 reasons: | ||
| // 1. The result is huge | ||
| // 2. We need to check against know values. Don't use metrics that return counters like uptime | ||
| // or counters like the number of transactions because they won't return a known value to compare | ||
| filter := []string{ | ||
| "mongodb_rs_cfg_protocolVersion", | ||
| } | ||
| err := testutil.CollectAndCompare(c, expected, filter...) | ||
| assert.NoError(t, err) | ||
| } | ||
|
|
||
| func TestReplsetConfigCollectorNoSharding(t *testing.T) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) | ||
| defer cancel() | ||
|
|
||
| client := tu.TestClient(ctx, tu.MongoDBStandAlonePort, t) | ||
|
|
||
| ti := labelsGetterMock{} | ||
|
|
||
| c := &replSetGetConfigCollector{ | ||
| ctx: ctx, | ||
| client: client, | ||
|
Check failure on line 68 in exporter/replset_config_collector_test.go
|
||
| topologyInfo: ti, | ||
| } | ||
BupycHuk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Replication set metrics should not be generated for unsharded server | ||
| count := testutil.CollectAndCount(c) | ||
|
|
||
| metaMetricCount := 1 | ||
| assert.Equal(t, metaMetricCount, count, "Mismatch in metric count for collector run on unsharded server") | ||
| } | ||
There was a problem hiding this comment.
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?