Skip to content

Commit afb141d

Browse files
jiekunmakasim
andauthored
change the order of summary and quantile(s) (#105)
This is a follow-up to #99. Prior to #99, the order of summary metrics (`_sum` and `_count`) and quantiles could vary depending on the metric name. After #99, the order was fixed to show summaries first, followed by quantiles. This ordering was based on the author's tests, but at the time, the author was unaware that different orders were possible. In conclusion: 1. The order should be fixed, which was accomplished in the previous pull request. 2. The fixed order should match Prometheus or the most common ordering from versions before #99. - The current version (v1.40.1) fixed this order in an unusual way. Therefore, this pull request modifies the sorting logic for summaries and quantiles, enforcing that quantiles are printed first, followed by summaries. --------- Co-authored-by: Max Kotliar <[email protected]>
1 parent 5fb241f commit afb141d

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

set.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ func (s *Set) WritePrometheus(w io.Writer) {
4747
return fName1 < fName2
4848
}
4949

50+
// Only summary and quantile(s) have different metric types.
51+
// Sorting by metric type will stabilize the order for summary and quantile(s).
5052
mType1 := s.a[i].metric.metricType()
5153
mType2 := s.a[j].metric.metricType()
52-
53-
// stabilize the order for summary and quantiles.
5454
if mType1 != mType2 {
5555
return mType1 < mType2
5656
}

set_example_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,22 @@ func ExampleExposeMetadata() {
6969
// request_duration_seconds_count{path="/foo/bar"} 2
7070
// # HELP response_size_bytes
7171
// # TYPE response_size_bytes summary
72-
// response_size_bytes_sum 1
73-
// response_size_bytes_count 1
7472
// response_size_bytes{quantile="0.5"} 1
7573
// response_size_bytes{quantile="0.9"} 1
7674
// response_size_bytes{quantile="0.97"} 1
7775
// response_size_bytes{quantile="0.99"} 1
7876
// response_size_bytes{quantile="1"} 1
77+
// response_size_bytes_sum 1
78+
// response_size_bytes_count 1
7979
// # HELP response_size_bytes_extra_suffix
8080
// # TYPE response_size_bytes_extra_suffix summary
81-
// response_size_bytes_extra_suffix_sum 1
82-
// response_size_bytes_extra_suffix_count 1
8381
// response_size_bytes_extra_suffix{quantile="0.5"} 1
8482
// response_size_bytes_extra_suffix{quantile="0.9"} 1
8583
// response_size_bytes_extra_suffix{quantile="0.97"} 1
8684
// response_size_bytes_extra_suffix{quantile="0.99"} 1
8785
// response_size_bytes_extra_suffix{quantile="1"} 1
86+
// response_size_bytes_extra_suffix_sum 1
87+
// response_size_bytes_extra_suffix_count 1
8888
// # HELP set_counter
8989
// # TYPE set_counter counter
9090
// set_counter 1
@@ -97,11 +97,11 @@ func ExampleExposeMetadata() {
9797
// used_bytes{foo="baz"} 43
9898
// # HELP vm_request_duration_seconds
9999
// # TYPE vm_request_duration_seconds summary
100-
// vm_request_duration_seconds_sum{path="/api/v1/query_range"} 1
101-
// vm_request_duration_seconds_count{path="/api/v1/query_range"} 1
102100
// vm_request_duration_seconds{path="/api/v1/query_range",quantile="0.5"} 1
103101
// vm_request_duration_seconds{path="/api/v1/query_range",quantile="0.9"} 1
104102
// vm_request_duration_seconds{path="/api/v1/query_range",quantile="0.97"} 1
105103
// vm_request_duration_seconds{path="/api/v1/query_range",quantile="0.99"} 1
106104
// vm_request_duration_seconds{path="/api/v1/query_range",quantile="1"} 1
105+
// vm_request_duration_seconds_sum{path="/api/v1/query_range"} 1
106+
// vm_request_duration_seconds_count{path="/api/v1/query_range"} 1
107107
}

summary.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,13 @@ func (sm *Summary) marshalTo(prefix string, w io.Writer) {
120120
}
121121

122122
func (sm *Summary) metricType() string {
123-
return "summary"
123+
// this metric type should not be printed, because summary (sum and count)
124+
// of the same metric family will be printed after quantile(s).
125+
// If metadata is needed, the metadata from quantile(s) should be used.
126+
// quantile will be printed first, so its metrics type won't be printed as metadata.
127+
// Printing quantiles before sum and count aligns this code with Prometheus behavior.
128+
// See: https://github.com/VictoriaMetrics/metrics/pull/99
129+
return "unsupported"
124130
}
125131

126132
func splitMetricName(name string) (string, string) {
@@ -201,11 +207,7 @@ func (qv *quantileValue) marshalTo(prefix string, w io.Writer) {
201207
}
202208

203209
func (qv *quantileValue) metricType() string {
204-
// this metricsType should not be printed, because summary (sum and count) of the same metric family will be printed first,
205-
// and if metadata is needed, the metadata from summary should be used.
206-
// quantile will be printed later, so its metrics type won't be printed as metadata.
207-
// See: https://github.com/VictoriaMetrics/metrics/pull/99
208-
return "unsupported"
210+
return "summary"
209211
}
210212

211213
func addTag(name, tag string) string {

0 commit comments

Comments
 (0)