Skip to content

Commit 30f9038

Browse files
committed
Revert "PMM-10292 Remove unused collector (#547)"
This reverts commit cf4ca67.
1 parent acfa5ef commit 30f9038

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

exporter/serverstatus_collector.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// mongodb_exporter
2+
// Copyright (C) 2017 Percona LLC
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
package exporter
18+
19+
import (
20+
"context"
21+
22+
"github.com/prometheus/client_golang/prometheus"
23+
"github.com/sirupsen/logrus"
24+
"go.mongodb.org/mongo-driver/bson"
25+
"go.mongodb.org/mongo-driver/mongo"
26+
)
27+
28+
type serverStatusCollector struct {
29+
ctx context.Context
30+
base *baseCollector
31+
32+
compatibleMode bool
33+
topologyInfo labelsGetter
34+
}
35+
36+
// newServerStatusCollector creates a collector for statistics on server status.
37+
func newServerStatusCollector(ctx context.Context, client *mongo.Client, logger *logrus.Logger, compatible bool, topology labelsGetter) *serverStatusCollector {
38+
return &serverStatusCollector{
39+
ctx: ctx,
40+
base: newBaseCollector(client, logger),
41+
compatibleMode: compatible,
42+
topologyInfo: topology,
43+
}
44+
}
45+
46+
func (d *serverStatusCollector) Describe(ch chan<- *prometheus.Desc) {
47+
d.base.Describe(d.ctx, ch, d.collect)
48+
}
49+
50+
func (d *serverStatusCollector) Collect(ch chan<- prometheus.Metric) {
51+
d.base.Collect(ch)
52+
}
53+
54+
func (d *serverStatusCollector) collect(ch chan<- prometheus.Metric) {
55+
defer prometheus.MeasureCollectTime(ch, "mongodb", "serverstatus")()
56+
57+
logger := d.base.logger
58+
client := d.base.client
59+
60+
cmd := bson.D{{Key: "serverStatus", Value: "1"}}
61+
res := client.Database("admin").RunCommand(d.ctx, cmd)
62+
63+
var m bson.M
64+
if err := res.Decode(&m); err != nil {
65+
ch <- prometheus.NewInvalidMetric(prometheus.NewInvalidDesc(err), err)
66+
return
67+
}
68+
69+
logger.Debug("serverStatus result:")
70+
debugResult(logger, m)
71+
72+
for _, metric := range makeMetrics("", m, d.topologyInfo.baseLabels(), d.compatibleMode) {
73+
ch <- metric
74+
}
75+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// mongodb_exporter
2+
// Copyright (C) 2017 Percona LLC
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
package exporter
18+
19+
import (
20+
"context"
21+
"strings"
22+
"testing"
23+
"time"
24+
25+
"github.com/prometheus/client_golang/prometheus/testutil"
26+
"github.com/sirupsen/logrus"
27+
"github.com/stretchr/testify/assert"
28+
29+
"github.com/percona/mongodb_exporter/internal/tu"
30+
)
31+
32+
func TestServerStatusDataCollector(t *testing.T) {
33+
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
34+
defer cancel()
35+
36+
client := tu.DefaultTestClient(ctx, t)
37+
38+
ti := labelsGetterMock{}
39+
40+
c := newServerStatusCollector(ctx, client, logrus.New(), false, ti)
41+
42+
// The last \n at the end of this string is important
43+
expected := strings.NewReader(`
44+
# HELP mongodb_mem_bits mem.
45+
# TYPE mongodb_mem_bits untyped
46+
mongodb_mem_bits 64
47+
# HELP mongodb_metrics_commands_connPoolSync_failed metrics.commands.connPoolSync.
48+
# TYPE mongodb_metrics_commands_connPoolSync_failed untyped
49+
mongodb_metrics_commands_connPoolSync_failed 0` + "\n")
50+
// Filter metrics for 2 reasons:
51+
// 1. The result is huge
52+
// 2. We need to check against know values. Don't use metrics that return counters like uptime
53+
// or counters like the number of transactions because they won't return a known value to compare
54+
filter := []string{
55+
"mongodb_mem_bits",
56+
"mongodb_metrics_commands_connPoolSync_failed",
57+
}
58+
err := testutil.CollectAndCompare(c, expected, filter...)
59+
assert.NoError(t, err)
60+
}

0 commit comments

Comments
 (0)