Skip to content

Commit 4f3568b

Browse files
authored
Provide old mongod metrics for dbstats (#337)
* Provide old mongod metrics for dbstats * Fix test case * Add Changelog
1 parent a1fd7ce commit 4f3568b

File tree

5 files changed

+90
-54
lines changed

5 files changed

+90
-54
lines changed

CHANGELOG

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
Release 0.20.8
2+
Improvement PMM-7424: Add dbStats metric
3+
Improvement : Add support for compatible mode for dbStats metric to provide old metrics
4+
15
Release 0.20.7 released 2021-08-10
26

37
Improvement : Upgraded dependencies
48
Fixed error PMM-6877: replsetGetStatus error on mongos
5-
Improvement PMM-7424: Add dbStats metric
69

710
Release 0.20.6 released 2021-06-26
811

exporter/dbstats_collector.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ func (d *dbstatsCollector) Collect(ch chan<- prometheus.Metric) {
5858
d.logger.Debugf("$dbStats metrics for %s", db)
5959
debugResult(d.logger, dbStats)
6060

61-
// Since all dbstats will have the same fields, we need to use a metric prefix (db)
62-
// to differentiate metrics between different databases. Labels are being set only to make it easier
63-
// to filter
64-
prefix := "dbstats_" + db
61+
prefix := "dbstats"
6562

6663
labels := d.topologyInfo.baseLabels()
64+
65+
// Since all dbstats will have the same fields, we need to use a label
66+
// to differentiate metrics between different databases.
6767
labels["database"] = db
6868

69-
for _, metric := range makeMetrics(prefix, dbStats, d.topologyInfo.baseLabels(), d.compatibleMode) {
69+
for _, metric := range makeMetrics(prefix, dbStats, labels, d.compatibleMode) {
7070
ch <- metric
7171
}
7272
}

exporter/dbstats_collector_test.go

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ package exporter
1919
import (
2020
"context"
2121
"fmt"
22-
"strings"
22+
"sort"
2323
"testing"
2424
"time"
2525

26-
"github.com/prometheus/client_golang/prometheus/testutil"
26+
"github.com/percona/exporter_shared/helpers"
2727
"github.com/sirupsen/logrus"
2828
"github.com/stretchr/testify/assert"
2929
"go.mongodb.org/mongo-driver/bson"
@@ -61,52 +61,41 @@ func TestDBStatsCollector(t *testing.T) {
6161
topologyInfo: ti,
6262
}
6363

64-
// The last \n at the end of this string is important
65-
expected := strings.NewReader(`
66-
# HELP mongodb_dbstats_testdb_avgObjSize dbstats_testdb.
67-
# TYPE mongodb_dbstats_testdb_avgObjSize untyped
68-
mongodb_dbstats_testdb_avgObjSize 40
69-
# HELP mongodb_dbstats_testdb_collections dbstats_testdb.
70-
# TYPE mongodb_dbstats_testdb_collections untyped
71-
mongodb_dbstats_testdb_collections 3
72-
# HELP mongodb_dbstats_testdb_dataSize dbstats_testdb.
73-
# TYPE mongodb_dbstats_testdb_dataSize untyped
74-
mongodb_dbstats_testdb_dataSize 1200
75-
# HELP mongodb_dbstats_testdb_indexSize dbstats_testdb.
76-
# TYPE mongodb_dbstats_testdb_indexSize untyped
77-
mongodb_dbstats_testdb_indexSize 12288
78-
# HELP mongodb_dbstats_testdb_indexes dbstats_testdb.
79-
# TYPE mongodb_dbstats_testdb_indexes untyped
80-
mongodb_dbstats_testdb_indexes 3
81-
# HELP mongodb_dbstats_testdb_objects dbstats_testdb.
82-
# TYPE mongodb_dbstats_testdb_objects untyped
83-
mongodb_dbstats_testdb_objects 30
84-
# HELP mongodb_dbstats_testdb_ok dbstats_testdb.
85-
# TYPE mongodb_dbstats_testdb_ok untyped
86-
mongodb_dbstats_testdb_ok 1
87-
# HELP mongodb_dbstats_testdb_storageSize dbstats_testdb.
88-
# TYPE mongodb_dbstats_testdb_storageSize untyped
89-
mongodb_dbstats_testdb_storageSize 12288
90-
# HELP mongodb_dbstats_testdb_views dbstats_testdb.
91-
# TYPE mongodb_dbstats_testdb_views untyped
92-
mongodb_dbstats_testdb_views 0` +
93-
"\n")
64+
expected := []string{
65+
"# HELP mongodb_dbstats_collections dbstats.",
66+
"# TYPE mongodb_dbstats_collections untyped",
67+
"mongodb_dbstats_collections{database=\"testdb\"} 3",
68+
"# HELP mongodb_dbstats_dataSize dbstats.",
69+
"# TYPE mongodb_dbstats_dataSize untyped",
70+
"mongodb_dbstats_dataSize{database=\"testdb\"} 1200",
71+
"# HELP mongodb_dbstats_indexSize dbstats.",
72+
"# TYPE mongodb_dbstats_indexSize untyped",
73+
"mongodb_dbstats_indexSize{database=\"testdb\"} 12288",
74+
"# HELP mongodb_dbstats_indexes dbstats.",
75+
"# TYPE mongodb_dbstats_indexes untyped",
76+
"mongodb_dbstats_indexes{database=\"testdb\"} 3",
77+
"# HELP mongodb_dbstats_objects dbstats.",
78+
"# TYPE mongodb_dbstats_objects untyped",
79+
"mongodb_dbstats_objects{database=\"testdb\"} 30",
80+
}
9481

95-
// Filter metrics for 2 reasons:
96-
// 1. The result is huge
97-
// 2. We need to check against know values. Don't use metrics that return counters like uptime
98-
// or counters like the number of transactions because they won't return a known value to compare
99-
filter := []string{
100-
"mongodb_dbstats_testdb_avgObjSize",
101-
"mongodb_dbstats_testdb_collections",
102-
"mongodb_dbstats_testdb_dataSize",
103-
"mongodb_dbstats_testdb_indexSize",
104-
"mongodb_dbstats_testdb_indexes",
105-
"mongodb_dbstats_testdb_objects",
106-
"mongodb_dbstats_testdb_views",
107-
"mongodb_dbstats_testdb_storageSize",
108-
"mongodb_dbstats_testdb_ok",
82+
metrics := helpers.CollectMetrics(c)
83+
actualMetrics := helpers.ReadMetrics(metrics)
84+
filters := []string{
85+
"mongodb_dbstats_collections",
86+
"mongodb_dbstats_dataSize",
87+
"mongodb_dbstats_indexSize",
88+
"mongodb_dbstats_indexes",
89+
"mongodb_dbstats_objects",
90+
}
91+
labels := map[string]string{
92+
"database": "testdb",
10993
}
110-
err := testutil.CollectAndCompare(c, expected, filter...)
111-
assert.NoError(t, err)
94+
actualMetrics = filterMetricsWithLabels(actualMetrics,
95+
filters,
96+
labels)
97+
actualLines := helpers.Format(helpers.WriteMetrics(actualMetrics))
98+
sort.Strings(actualLines)
99+
sort.Strings(expected)
100+
assert.Equal(t, expected, actualLines)
112101
}

exporter/utils_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,27 @@ func getMetricNames(lines []string) map[string]bool {
3535

3636
return names
3737
}
38+
39+
func filterMetricsWithLabels(metrics []*helpers.Metric, filters []string, labels map[string]string) []*helpers.Metric {
40+
res := make([]*helpers.Metric, 0, len(metrics))
41+
for _, m := range metrics {
42+
for _, filterName := range filters {
43+
if m.Name == filterName {
44+
validMetric := true
45+
for labelKey, labelValue := range labels {
46+
if m.Labels[labelKey] != labelValue {
47+
validMetric = false
48+
49+
break
50+
}
51+
}
52+
if validMetric {
53+
res = append(res, m)
54+
}
55+
56+
break
57+
}
58+
}
59+
}
60+
return res
61+
}

exporter/v1_compatibility.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,26 @@ func conversions() []conversion {
592592
oldName: "mongodb_ss_wt_cache_maximum_bytes_configured",
593593
newName: "mongodb_mongod_wiredtiger_cache_max_bytes",
594594
},
595+
{
596+
oldName: "mongodb_mongod_db_collections_total",
597+
newName: "mongodb_dbstats_collections",
598+
},
599+
{
600+
oldName: "mongodb_mongod_db_data_size_bytes",
601+
newName: "mongodb_dbstats_dataSize",
602+
},
603+
{
604+
oldName: "mongodb_mongod_db_index_size_bytes",
605+
newName: "mongodb_dbstats_indexSize",
606+
},
607+
{
608+
oldName: "mongodb_mongod_db_indexes_total",
609+
newName: "mongodb_dbstats_indexes",
610+
},
611+
{
612+
oldName: "mongodb_mongod_db_objects_total",
613+
newName: "mongodb_dbstats_objects",
614+
},
595615
}
596616
}
597617

0 commit comments

Comments
 (0)