-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add exemplars for native histograms #1686
Open
shivanthzen
wants to merge
3
commits into
prometheus:main
Choose a base branch
from
shivanthzen:nativehistogram_exemplars
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -187,6 +187,12 @@ func (m *withExemplarsMetric) Write(pb *dto.Metric) error { | |||||
pb.Counter.Exemplar = m.exemplars[len(m.exemplars)-1] | ||||||
case pb.Histogram != nil: | ||||||
for _, e := range m.exemplars { | ||||||
if pb.Histogram.Schema != nil { | ||||||
if *pb.Histogram.Schema > math.MinInt32 && e.GetTimestamp() != nil { | ||||||
pb.Histogram.Exemplars = append(pb.Histogram.Exemplars, e) | ||||||
} | ||||||
continue | ||||||
} | ||||||
// pb.Histogram.Bucket are sorted by UpperBound. | ||||||
i := sort.Search(len(pb.Histogram.Bucket), func(i int) bool { | ||||||
return pb.Histogram.Bucket[i].GetUpperBound() >= e.GetValue() | ||||||
|
@@ -227,6 +233,7 @@ type Exemplar struct { | |||||
// Only last applicable exemplar is injected from the list. | ||||||
// For example for Counter it means last exemplar is injected. | ||||||
// For Histogram, it means last applicable exemplar for each bucket is injected. | ||||||
// Note that for a nativeHistogram, all valid exemplars are injected. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// | ||||||
// NewMetricWithExemplars works best with MustNewConstMetric and | ||||||
// MustNewConstHistogram, see example. | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,10 +16,12 @@ package prometheus | |
import ( | ||
"math" | ||
"testing" | ||
"time" | ||
|
||
dto "github.com/prometheus/client_model/go" | ||
|
||
"google.golang.org/protobuf/proto" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
func TestBuildFQName(t *testing.T) { | ||
|
@@ -90,3 +92,29 @@ func TestWithExemplarsMetric(t *testing.T) { | |
} | ||
}) | ||
} | ||
|
||
func TestWithExemplarsNativeHistogramMetric(t *testing.T) { | ||
t.Run("histogram", func(t *testing.T) { | ||
// Create a constant histogram from values we got from a 3rd party telemetry system. | ||
h := MustNewConstNativeHistogram( | ||
NewDesc("http_request_duration_seconds", "A histogram of the HTTP request durations.", nil, nil), | ||
10, 12.1, map[int]int64{1: 7, 2: 1, 3: 2}, map[int]int64{}, 0, 2, 0.2, time.Date( | ||
2009, 11, 17, 20, 34, 58, 651387237, time.UTC)) | ||
m := &withExemplarsMetric{Metric: h, exemplars: []*dto.Exemplar{ | ||
{Value: proto.Float64(2000.0), Timestamp: timestamppb.New(time.Date(2009, 11, 17, 20, 34, 58, 3243244, time.UTC))}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also test the following cases:
|
||
}} | ||
metric := dto.Metric{} | ||
if err := m.Write(&metric); err != nil { | ||
t.Fatal(err) | ||
} | ||
if want, got := 1, len(metric.GetHistogram().Exemplars); want != got { | ||
t.Errorf("want %v, got %v", want, got) | ||
} | ||
|
||
for _, b := range metric.GetHistogram().Bucket { | ||
if b.Exemplar != nil { | ||
t.Error("Not expecting exemplar for bucket") | ||
} | ||
} | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a classic and a native histogram is exposed at the same time (for the scraper to pick), we should have the exemplars on both. Maybe the following works for that:
We do have to check if there are already buckets defined, because otherwise there will be an Inf bucket created just for the histograms.