Skip to content

PMM-13602 Improve performance #987

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

Merged
merged 7 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ linters:
- unused
- testpackage
- wsl
- exhaustivestruct
- exhaustruct
- varnamelen
- gochecknoglobals # we need globals for better performance, we use them for mapping, and we don't mutate them, so it's safe.
- maligned #deprecated
- scopelint #deprecated
- golint #deprecated
Expand Down
8 changes: 5 additions & 3 deletions exporter/collstats_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ type collstatsCollector struct {
}

// newCollectionStatsCollector creates a collector for statistics about collections.
func newCollectionStatsCollector(ctx context.Context, client *mongo.Client, logger *logrus.Logger, compatible, discovery bool, topology labelsGetter, collections []string) *collstatsCollector {
func newCollectionStatsCollector(ctx context.Context, client *mongo.Client, logger *logrus.Logger, discovery bool, topology labelsGetter, collections []string) *collstatsCollector {
return &collstatsCollector{
ctx: ctx,
base: newBaseCollector(client, logger.WithFields(logrus.Fields{"collector": "collstats"})),

compatibleMode: compatible,
compatibleMode: false, // there are no compatible metrics for this collector.
discoveringMode: discovery,
topologyInfo: topology,

Expand Down Expand Up @@ -108,7 +108,9 @@ func (d *collstatsCollector) collect(ch chan<- prometheus.Metric) {
},
}

cursor, err := client.Database(database).Collection(collection).Aggregate(d.ctx, mongo.Pipeline{aggregation})
pipeline := mongo.Pipeline{aggregation}

cursor, err := client.Database(database).Collection(collection).Aggregate(d.ctx, pipeline)
if err != nil {
logger.Errorf("cannot get $collstats cursor for collection %s.%s: %s", database, collection, err)

Expand Down
2 changes: 1 addition & 1 deletion exporter/collstats_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestCollStatsCollector(t *testing.T) {

collection := []string{"testdb.testcol_00", "testdb.testcol_01", "testdb.testcol_02"}
logger := logrus.New()
c := newCollectionStatsCollector(ctx, client, logger, false, false, ti, collection)
c := newCollectionStatsCollector(ctx, client, logger, false, ti, collection)

// The last \n at the end of this string is important
expected := strings.NewReader(`
Expand Down
3 changes: 2 additions & 1 deletion exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func (e *Exporter) makeRegistry(ctx context.Context, client *mongo.Client, topol
e.getTotalCollectionsCount() <= e.opts.CollStatsLimit {
limitsOk = true
}
limitsOk = true

if e.opts.CollectAll {
if len(e.opts.CollStatsNamespaces) == 0 {
Expand Down Expand Up @@ -191,7 +192,7 @@ func (e *Exporter) makeRegistry(ctx context.Context, client *mongo.Client, topol
// If we manually set the collection names we want or auto discovery is set.
if (len(e.opts.CollStatsNamespaces) > 0 || e.opts.DiscoveringMode) && e.opts.EnableCollStats && limitsOk && requestOpts.EnableCollStats {
cc := newCollectionStatsCollector(ctx, client, e.opts.Logger,
e.opts.CompatibleMode, e.opts.DiscoveringMode,
e.opts.DiscoveringMode,
topologyInfo, e.opts.CollStatsNamespaces)
registry.MustRegister(cc)
}
Expand Down
106 changes: 52 additions & 54 deletions exporter/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func makeMetrics(prefix string, m bson.M, labels map[string]string, compatibleMo

metrics := []*rawMetric{rm}

if renamedMetrics := metricRenameAndLabel(rm, specialConversions()); renamedMetrics != nil {
if renamedMetrics := metricRenameAndLabel(rm, specialConversions); renamedMetrics != nil {
metrics = renamedMetrics
}

Expand Down Expand Up @@ -435,62 +435,60 @@ func metricRenameAndLabel(rm *rawMetric, convs []conversion) []*rawMetric {

// specialConversions returns a list of special conversions we want to implement.
// See: https://jira.percona.com/browse/PMM-6506
func specialConversions() []conversion {
return []conversion{
{
oldName: "mongodb_ss_opLatencies_ops",
prefix: "mongodb_ss_opLatencies",
suffixLabel: "op_type",
suffixMapping: map[string]string{
"commands_ops": "commands",
"reads_ops": "reads",
"transactions_ops": "transactions",
"writes_ops": "writes",
},
var specialConversions = []conversion{ //nolint:gochecknoglobals
{
oldName: "mongodb_ss_opLatencies_ops",
prefix: "mongodb_ss_opLatencies",
suffixLabel: "op_type",
suffixMapping: map[string]string{
"commands_ops": "commands",
"reads_ops": "reads",
"transactions_ops": "transactions",
"writes_ops": "writes",
},
{
oldName: "mongodb_ss_opLatencies_latency",
prefix: "mongodb_ss_opLatencies",
suffixLabel: "op_type",
suffixMapping: map[string]string{
"commands_latency": "commands",
"reads_latency": "reads",
"transactions_latency": "transactions",
"writes_latency": "writes",
},
},
{
oldName: "mongodb_ss_opLatencies_latency",
prefix: "mongodb_ss_opLatencies",
suffixLabel: "op_type",
suffixMapping: map[string]string{
"commands_latency": "commands",
"reads_latency": "reads",
"transactions_latency": "transactions",
"writes_latency": "writes",
},
// mongodb_ss_wt_concurrentTransactions_read_out
// mongodb_ss_wt_concurrentTransactions_write_out
{
oldName: "mongodb_ss_wt_concurrentTransactions_out",
prefix: "mongodb_ss_wt_concurrentTransactions",
suffixLabel: "txn_rw",
suffixMapping: map[string]string{
"read_out": "read",
"write_out": "write",
},
},
// mongodb_ss_wt_concurrentTransactions_read_out
// mongodb_ss_wt_concurrentTransactions_write_out
{
oldName: "mongodb_ss_wt_concurrentTransactions_out",
prefix: "mongodb_ss_wt_concurrentTransactions",
suffixLabel: "txn_rw",
suffixMapping: map[string]string{
"read_out": "read",
"write_out": "write",
},
// mongodb_ss_wt_concurrentTransactions_read_available
// mongodb_ss_wt_concurrentTransactions_write_available
{
oldName: "mongodb_ss_wt_concurrentTransactions_available",
prefix: "mongodb_ss_wt_concurrentTransactions",
suffixLabel: "txn_rw",
suffixMapping: map[string]string{
"read_available": "read",
"write_available": "write",
},
},
// mongodb_ss_wt_concurrentTransactions_read_available
// mongodb_ss_wt_concurrentTransactions_write_available
{
oldName: "mongodb_ss_wt_concurrentTransactions_available",
prefix: "mongodb_ss_wt_concurrentTransactions",
suffixLabel: "txn_rw",
suffixMapping: map[string]string{
"read_available": "read",
"write_available": "write",
},
// mongodb_ss_wt_concurrentTransactions_read_totalTickets
// mongodb_ss_wt_concurrentTransactions_write_totalTickets
{
oldName: "mongodb_ss_wt_concurrentTransactions_totalTickets",
prefix: "mongodb_ss_wt_concurrentTransactions",
suffixLabel: "txn_rw",
suffixMapping: map[string]string{
"read_totalTickets": "read",
"write_totalTickets": "write",
},
},
// mongodb_ss_wt_concurrentTransactions_read_totalTickets
// mongodb_ss_wt_concurrentTransactions_write_totalTickets
{
oldName: "mongodb_ss_wt_concurrentTransactions_totalTickets",
prefix: "mongodb_ss_wt_concurrentTransactions",
suffixLabel: "txn_rw",
suffixMapping: map[string]string{
"read_totalTickets": "read",
"write_totalTickets": "write",
},
}
},
}
2 changes: 1 addition & 1 deletion exporter/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func TestRawToCompatibleRawMetric(t *testing.T) {
}

for _, tc := range testCases {
m := metricRenameAndLabel(tc.in, specialConversions())
m := metricRenameAndLabel(tc.in, specialConversions)
assert.Equal(t, m[0], tc.want)
}
}
2 changes: 1 addition & 1 deletion exporter/top_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func newTopCollector(ctx context.Context, client *mongo.Client, logger *logrus.L
return &topCollector{
ctx: ctx,
base: newBaseCollector(client, logger.WithFields(logrus.Fields{"collector": "top"})),
compatibleMode: compatible,
compatibleMode: false, // there are no compatible metrics for this collector.
topologyInfo: topology,
}
}
Expand Down
Loading
Loading