Skip to content

nh support for metrics aggregation#7359

Merged
yeya24 merged 9 commits intocortexproject:masterfrom
Shvejan:expose-nh-metrics
Mar 25, 2026
Merged

nh support for metrics aggregation#7359
yeya24 merged 9 commits intocortexproject:masterfrom
Shvejan:expose-nh-metrics

Conversation

@Shvejan
Copy link
Contributor

@Shvejan Shvejan commented Mar 18, 2026

What this PR does:

  • Add native histogram support to metrics aggregation helper, the change also supports dual-format histograms that expose both native and classic bucket formats.
  • few methods and structs like makeBucketsFromMap and nativeHistogramMetric are copied from Prometheus implementation to follow the same patterns.

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

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

Signed-off-by: Shvejan Mutheboyina <shvejan@amazon.com>
Shvejan and others added 2 commits March 18, 2026 01:57
Signed-off-by: Shvejan <49836470+Shvejan@users.noreply.github.com>
Signed-off-by: Shvejan Mutheboyina <shvejan@amazon.com>
@Shvejan Shvejan force-pushed the expose-nh-metrics branch from 3b84c49 to 4740331 Compare March 19, 2026 19:39
Signed-off-by: Friedrich Gonzalez <1517449+friedrichg@users.noreply.github.com>
friedrichg and others added 2 commits March 20, 2026 13:39
Co-authored-by: Friedrich Gonzalez <1517449+friedrichg@users.noreply.github.com>
Signed-off-by: Friedrich Gonzalez <1517449+friedrichg@users.noreply.github.com>
Signed-off-by: Shvejan <49836470+Shvejan@users.noreply.github.com>
Comment on lines +1082 to +1089
if isNative(h1) || isNative(h2) {
// Use schema/threshold from whichever side has native data (they should match).
if !isNative(h1) {
schema := h2.GetSchema()
h1.Schema = &schema
zt := h2.GetZeroThreshold()
h1.ZeroThreshold = &zt
}
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if h1 and h2 always have the same schema.

Copy link
Contributor

Choose a reason for hiding this comment

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

For this usecase for aggregating in SendSumOfHistograms, they should have the same schema as we aggregate the same histogram exposed by prometheus client go library.

Copy link
Contributor

@yeya24 yeya24 left a comment

Choose a reason for hiding this comment

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

I'd like to also see an integration test that validates this code could by exposing both classic and NH metrics. Unit tests alone is not sufficient here


// Sentinel span for native histograms with no observations.
// This is required to distinguish an empty native histogram from a classic histogram.
// This matches the prometheus behavior (histogram.go:1958)
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add the code link instead of just file and line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines +1082 to +1089
if isNative(h1) || isNative(h2) {
// Use schema/threshold from whichever side has native data (they should match).
if !isNative(h1) {
schema := h2.GetSchema()
h1.Schema = &schema
zt := h2.GetZeroThreshold()
h1.ZeroThreshold = &zt
}
Copy link
Contributor

Choose a reason for hiding this comment

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

For this usecase for aggregating in SendSumOfHistograms, they should have the same schema as we aggregate the same histogram exposed by prometheus client go library.

// spans+deltas encoding used by the native histogram proto representation.
//
// This implementation is the same as makeBucketsFromMap from prometheus
// (histogram.go:2006) to include the gap-filling optimization
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

}

// TestHistogramData_AddHistogram_Multiple tests merging multiple histograms
func TestHistogramData_AddHistogram_Multiple(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we consolidate all TestHistogramData_AddHistogram tests? Seems they test similar things. Let's simplify the tests by consolidating them

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}

// TestMergeHistogram tests the mergeHistogram function
func TestMergeHistogram(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We need more coverage for this test case like merging native and classic or merging only classic ones

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added some unit tests to merge classic + native metrics

Shvejan and others added 2 commits March 25, 2026 19:33
Signed-off-by: Shvejan Mutheboyina <shvejan@amazon.com>
Signed-off-by: Shvejan <49836470+Shvejan@users.noreply.github.com>
@Shvejan Shvejan force-pushed the expose-nh-metrics branch 2 times, most recently from a554ea2 to 9d15b5c Compare March 25, 2026 20:34
// Testing Native histogram fields
require.Equal(t, int32(3), h.GetSchema())
require.NotNil(t, h.GetZeroThreshold())
require.Equal(t, uint64(0), h.GetZeroCount())
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we validate the actual spans and buckets of native histogram? Validating these fields might not be useful

Signed-off-by: Shvejan Mutheboyina <shvejan@amazon.com>
@Shvejan Shvejan force-pushed the expose-nh-metrics branch from 9d15b5c to c62f5c6 Compare March 25, 2026 22:29
Copy link
Contributor

@yeya24 yeya24 left a comment

Choose a reason for hiding this comment

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

Thanks!

@yeya24 yeya24 merged commit 8fbfe5f into cortexproject:master Mar 25, 2026
33 of 35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SendSumOfHistograms should support native histograms

4 participants