Skip to content

Issue 4632: fix exemplars calculation #5115

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

Conversation

ruslan-mikhailov
Copy link
Contributor

What this PR does:

  • Request at least 1 exemplar per shard
  • Correct exemplars calculation: with totalRecords > pages, page should be the numerator and total records the denominator
  • Correct calculation of required pages per request (fix for case small block)

Which issue(s) this PR fixes:
Fixes #4632

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]
@ruslan-mikhailov ruslan-mikhailov requested a review from Copilot May 7, 2025 15:33
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes issue 4632 by correcting the exemplars calculation and ensuring at least one exemplar is requested per shard.

  • Adjusted pagesPerRequest logic to search the entire block when its size is below the request threshold.
  • Corrected the exemplarsPerShard computation and division order in backend request building to use pages as the numerator and totalRecords as the denominator.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
modules/frontend/search_sharder.go Added early return for small blocks in page calculation.
modules/frontend/metrics_query_range_sharder.go Updated exemplarsPerShard and backendRequests exemplar calculation.
modules/frontend/metrics_query_range_sharder_test.go Added tests for various block and exemplar configurations.
Comments suppressed due to low confidence (2)

modules/frontend/metrics_query_range_sharder.go:172

  • The exemplarsPerShard calculation now correctly returns at least one exemplar per shard by applying the max function, but please verify that integer division does not inadvertently truncate values beyond the intended behavior.
return max(uint32(math.Ceil(float64(exemplars)*1.2))/total, 1) // require at least 1 exemplar per shard

modules/frontend/metrics_query_range_sharder.go:247

  • The adjustment in the backendRequests function now correctly uses pages as the numerator and totalRecords as the denominator; please ensure that this division reflects the intended exemplar scaling for each block.
exemplars = max(uint32(float64(exemplars)*float64(pages)/float64(m.TotalRecords)), 1)
@ruslan-mikhailov ruslan-mikhailov force-pushed the issue-4632_exemplars-bugfix branch 2 times, most recently from 7937c11 to 3a0617f Compare May 7, 2025 16:01
@ruslan-mikhailov ruslan-mikhailov marked this pull request as ready for review May 7, 2025 16:04
"github.com/grafana/tempo/tempodb/backend"
)

func FuzzExemplarsPerShard(f *testing.F) {
Copy link
Contributor

Choose a reason for hiding this comment

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

+1 for the fuzz.

Copy link
Contributor

@zalegrala zalegrala left a comment

Choose a reason for hiding this comment

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

This looks correct to me. Nice work on the tests.

},
}
tenantID := "test-tenant"
targetBytesPerRequest := 1000
Copy link
Contributor

Choose a reason for hiding this comment

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

Leaving this static I suppose is desired because the test cases have blocksizes and records above and below this number to test each side of the calculation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exactly

Copy link
Contributor

@mapno mapno left a comment

Choose a reason for hiding this comment

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

LGTM

@ruslan-mikhailov ruslan-mikhailov force-pushed the issue-4632_exemplars-bugfix branch from 3a0617f to 9dd34fe Compare May 19, 2025 07:27
@ruslan-mikhailov
Copy link
Contributor Author

+ rebase from latest master to resolve merge conflicts

@ruslan-mikhailov ruslan-mikhailov merged commit 17e20a4 into grafana:main May 19, 2025
19 checks passed
@ruslan-mikhailov ruslan-mikhailov deleted the issue-4632_exemplars-bugfix branch May 19, 2025 08:05
knylander-grafana pushed a commit to knylander-grafana/tempo-doc-work that referenced this pull request Jun 2, 2025
* [bugfix] Request at least 1 exemplar per shard

In case exemplars * 1.2 is less than total
it could lead to dropping all exemplars

* Test buildBackendRequests

* [bugfix] Correct exemplars calculation

* [bugfix] Correct calculation of required pages per request

Search the entire block if it's small

* Changelog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants