Check quantile label during SummaryVec construction

Also, document the existing behavior more clearly.

Signed-off-by: beorn7 <beorn@soundcloud.com>
This commit is contained in:
beorn7 2018-07-13 13:43:21 +02:00
parent ee1c9d7e23
commit e064aa97f1
2 changed files with 39 additions and 0 deletions

View File

@ -105,6 +105,11 @@ type SummaryOpts struct {
// with the same fully-qualified name must have the same label names in
// their ConstLabels.
//
// Due to the way a Summary is represented in the Prometheus text format
// and how it is handled by the Prometheus server internally, “quantile”
// is an illegal label name. Construction of a Summary or SummaryVec
// will panic if this label name is used in ConstLabels.
//
// ConstLabels are only used rarely. In particular, do not use them to
// attach the same labels to all your metrics. Those use cases are
// better covered by target labels set by the scraping Prometheus
@ -402,7 +407,16 @@ type SummaryVec struct {
// NewSummaryVec creates a new SummaryVec based on the provided SummaryOpts and
// partitioned by the given label names.
//
// Due to the way a Summary is represented in the Prometheus text format and how
// it is handled by the Prometheus server internally, “quantile” is an illegal
// label name. NewSummaryVec will panic if this label name is used.
func NewSummaryVec(opts SummaryOpts, labelNames []string) *SummaryVec {
for _, ln := range labelNames {
if ln == quantileLabel {
panic(errQuantileLabelNotAllowed)
}
}
desc := NewDesc(
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
opts.Help,

View File

@ -64,6 +64,31 @@ func TestSummaryWithoutObjectives(t *testing.T) {
}
}
func TestSummaryWithQuantileLabel(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("Attempt to create Summary with 'quantile' label did not panic.")
}
}()
_ = NewSummary(SummaryOpts{
Name: "test_summary",
Help: "less",
ConstLabels: Labels{"quantile": "test"},
})
}
func TestSummaryVecWithQuantileLabel(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("Attempt to create SummaryVec with 'quantile' label did not panic.")
}
}()
_ = NewSummaryVec(SummaryOpts{
Name: "test_summary",
Help: "less",
}, []string{"quantile"})
}
func benchmarkSummaryObserve(w int, b *testing.B) {
b.StopTimer()