@@ -19,11 +19,11 @@ package exporter
1919import (
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}
0 commit comments