Skip to content

PMM-15198 Indexstats fix. - #1321

Open
JiriCtvrtka wants to merge 26 commits into
mainfrom
PMM-15198-indexstats
Open

PMM-15198 Indexstats fix.#1321
JiriCtvrtka wants to merge 26 commits into
mainfrom
PMM-15198-indexstats

Conversation

@JiriCtvrtka

@JiriCtvrtka JiriCtvrtka commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

PMM-15198

FB: Percona-Lab/pmm-submodules#4507

Summary
mongodb_indexstats_* metrics are silently dropped when the exporter scrapes a sharded cluster through mongos.

Steps to reproduce
Run a sharded MongoDB cluster (2+ shards) with a sharded collection that has at least one index.
Point mongodb_exporter at mongos with --collector.indexstats enabled.
Scrape /metrics.

Actual result
Only one shard's index statistics are exposed. The exporter logs duplicate-metric errors from the Prometheus registry, and the remaining shards' data is discarded.

Expected result
One series per index per shard, each distinguished by a shard label.

Root cause
When scraping via mongos, the $indexStats aggregation returns one document per shard for every index. The indexstats collector built its label set per document but never read the shard field, so every shard produced an identical metric descriptor. Prometheus rejects duplicate descriptors, so all but one shard was thrown away.

A related defect exists in the collstats collector: the labels map is allocated once before the document loop, so a shard value from one document leaks into subsequent documents that report no shard.

Fix
Add a shared setShardLabel() helper in exporter/common.go that sets the shard label unconditionally (empty string when absent), so all series of a metric family carry identical label dimensions and no registration conflict occurs.
Call it from both the indexstats and collstats collectors.
Move the collstats label map allocation inside the document loop to stop cross-document leakage.
Impact on existing series

Non-sharded deployments see no change — Prometheus drops empty label values at ingestion. Sharded deployments gain one series per shard per index instead of losing them.

Testing
Unit tests for setShardLabel() (shard present / empty / missing / non-string). Integration tests TestIndexStatsCollectorSharded and TestCollStatsCollectorSharded assert metrics from ≥2 shards with no duplicates. Note: these integration tests currently skip because the compose test cluster registers no shards — a separate test-infrastructure issue, tracked independently of this fix.

@JiriCtvrtka

Copy link
Copy Markdown
Contributor Author

@copilot review

Copilot AI commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

@copilot review

Reviewed the current PR changes in the indexstats area; I don’t see additional actionable fixes to apply right now. I also re-ran targeted tests for the updated helper logic (TestIndexStatsLabels, TestIndexStatsLabelsWithoutShard, TestSanitize) and they pass.

@JiriCtvrtka

Copy link
Copy Markdown
Contributor Author

@copilot review

@JiriCtvrtka
JiriCtvrtka marked this pull request as ready for review July 22, 2026 08:54
@JiriCtvrtka
JiriCtvrtka requested a review from a team as a code owner July 22, 2026 08:54
@JiriCtvrtka
JiriCtvrtka requested review from 4nte, ademidoff and maxkondr and removed request for a team July 22, 2026 08:54

@ademidoff ademidoff left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minor suggestions, nothing blocking.

Comment thread exporter/indexstats_collector.go Outdated
Comment thread exporter/indexstats_collector_test.go Outdated
Comment thread exporter/indexstats_collector_test.go Outdated
@JiriCtvrtka

Copy link
Copy Markdown
Contributor Author

@copilot review

Comment thread exporter/collstats_collector.go Outdated
@JiriCtvrtka

Copy link
Copy Markdown
Contributor Author

@copilot review

@JiriCtvrtka

Copy link
Copy Markdown
Contributor Author

@copilot review

@JiriCtvrtka
JiriCtvrtka requested a review from ademidoff July 27, 2026 11:52

@ademidoff ademidoff left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things that don't map to a line:

Stale PR description. The second paragraph ("raises the go test timeout from 30s to 1m in the Makefile") no longer applies: main already carries -timeout 1m (Makefile:124, :127, :130) and no Makefile change appears in this diff. Commit bf207f6b is in the branch history but contributes nothing against the current base. Worth trimming so reviewers aren't hunting for it.

Cardinality, for the release notes. The duplicate indexstats series were previously deduplicated silently by Gather; now they're all exposed. Scraping mongos in discovering mode goes from collections x indexes to collections x indexes x shards series. That's the correct data, but it's a real increase users should be told about.

Comment thread exporter/common.go
Comment thread exporter/indexstats_collector.go Outdated
Comment thread exporter/indexstats_collector_test.go Outdated
Comment thread exporter/indexstats_collector_test.go Outdated
@JiriCtvrtka

Copy link
Copy Markdown
Contributor Author

@copilot review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants