Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ import (
type namedMetric struct {
name string
metric metric
isAux bool

// isAux indicates whether it is an auxiliary metric.
// Currently only set for quantileValue, as it is part of the Summary.
// This field affects sorting when quantileValue and Summary are compared.
isAux bool
}

type metric interface {
Expand Down
10 changes: 4 additions & 6 deletions set.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,10 @@ func (s *Set) WritePrometheus(w io.Writer) {
return fName1 < fName2
}

// Only summary and quantile(s) have different metric types.
// Sorting by metric type will stabilize the order for summary and quantile(s).
mType1 := s.a[i].metric.metricType()
mType2 := s.a[j].metric.metricType()
if mType1 != mType2 {
return mType1 < mType2
// if both has same family, check the auxiliary first.
// only summary quantile(s) is registered as auxiliary, and quantile(s) in summary should go first.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It would be nice to add comment to the isAux field in metric struct to note where and why it is used. So in future we can quickly figure out what it is for.

if s.a[i].isAux != s.a[j].isAux {
return s.a[i].isAux
}

// lastly by metric names, which is for quantiles and histogram buckets.
Expand Down
25 changes: 25 additions & 0 deletions set_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package metrics

import (
"bytes"
"fmt"
"sync"
"testing"
Expand Down Expand Up @@ -171,3 +172,27 @@ func TestRegisterUnregister(t *testing.T) {
}
wg.Wait()
}

func TestMetadataForAfterSummaryWindow(t *testing.T) {
ExposeMetadata(true)
defer ExposeMetadata(false)

s := NewSet()
var testSummaryQuantilesNoop = []float64{0.5, 0.9, 0.97, 0.99, 1}
testWindow := 50 * time.Millisecond
s.NewSummaryExt(`test_summary_expire_quick`, testWindow, testSummaryQuantilesNoop).Update(1)
time.Sleep(4 * testWindow)

var bb bytes.Buffer
s.WritePrometheus(&bb)

expect := `# HELP test_summary_expire_quick
# TYPE test_summary_expire_quick summary
test_summary_expire_quick_sum 1
test_summary_expire_quick_count 1
`

if bb.String() != expect {
t.Fatalf("unexpected summary metric names:\n%s", bb.String())
}
}
8 changes: 1 addition & 7 deletions summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,7 @@ func (sm *Summary) marshalTo(prefix string, w io.Writer) {
}

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

func splitMetricName(name string) (string, string) {
Expand Down
Loading