Skip to content

Commit b1e7299

Browse files
author
beorn7
committed
Turned "le" and "quantile" label names into constants.
1 parent 3e50edd commit b1e7299

File tree

6 files changed

+43
-24
lines changed

6 files changed

+43
-24
lines changed

extraction/metricfamilyprocessor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func extractSummary(out Ingester, o *ProcessOptions, f *dto.MetricFamily) error
164164
metric[model.LabelName(p.GetName())] = model.LabelValue(p.GetValue())
165165
}
166166
// BUG(matt): Update other names to "quantile".
167-
metric[model.LabelName("quantile")] = model.LabelValue(fmt.Sprint(q.GetQuantile()))
167+
metric[model.LabelName(model.QuantileLabel)] = model.LabelValue(fmt.Sprint(q.GetQuantile()))
168168
metric[model.MetricNameLabel] = model.LabelValue(f.GetName())
169169
}
170170

@@ -259,7 +259,7 @@ func extractHistogram(out Ingester, o *ProcessOptions, f *dto.MetricFamily) erro
259259
for _, p := range m.Label {
260260
metric[model.LabelName(p.GetName())] = model.LabelValue(p.GetValue())
261261
}
262-
metric[model.LabelName("le")] = model.LabelValue(fmt.Sprint(q.GetUpperBound()))
262+
metric[model.LabelName(model.BucketLabel)] = model.LabelValue(fmt.Sprint(q.GetUpperBound()))
263263
metric[model.MetricNameLabel] = model.LabelValue(f.GetName() + "_bucket")
264264

265265
if math.IsInf(q.GetUpperBound(), +1) {
@@ -308,7 +308,7 @@ func extractHistogram(out Ingester, o *ProcessOptions, f *dto.MetricFamily) erro
308308
for _, p := range m.Label {
309309
metric[model.LabelName(p.GetName())] = model.LabelValue(p.GetValue())
310310
}
311-
metric[model.LabelName("le")] = model.LabelValue("+Inf")
311+
metric[model.LabelName(model.BucketLabel)] = model.LabelValue("+Inf")
312312
metric[model.MetricNameLabel] = model.LabelValue(f.GetName() + "_bucket")
313313
}
314314
}

model/labelname.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ const (
3333
// JobLabel is the label name indicating the job from which a timeseries
3434
// was scraped.
3535
JobLabel LabelName = "job"
36+
37+
// BucketLabel is used for the label that defines the upper bound of a
38+
// bucket of a histogram ("le" -> "less or equal").
39+
BucketLabel = "le"
40+
41+
// QuantileLabel is used for the label that defines the quantile in a
42+
// summary.
43+
QuantileLabel = "quantile"
3644
)
3745

3846
// A LabelName is a key for a LabelSet or Metric. It has a value associated

prometheus/histogram.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/golang/protobuf/proto"
2323

24+
"github.com/prometheus/client_golang/model"
2425
dto "github.com/prometheus/client_model/go"
2526
)
2627

@@ -33,7 +34,7 @@ import (
3334
//
3435
// Note that Histograms, in contrast to Summaries, can be aggregated with the
3536
// Prometheus query language (see the documentation for detailed
36-
// procedures). However, Histograms requires the user to pre-define suitable
37+
// procedures). However, Histograms require the user to pre-define suitable
3738
// buckets, and they are in general less accurate. The Observe method of a
3839
// Histogram has a very low performance overhead in comparison with the Observe
3940
// method of a Summary.
@@ -47,12 +48,16 @@ type Histogram interface {
4748
Observe(float64)
4849
}
4950

50-
// DefBuckets are the default Histogram buckets. The default buckets are
51-
// tailored to broadly measure response time in seconds for a typical online
52-
// serving system. Most likely, however, you will be required to define buckets
53-
// customized to your use case.
5451
var (
52+
// DefBuckets are the default Histogram buckets. The default buckets are
53+
// tailored to broadly measure response time in seconds for a typical online
54+
// serving system. Most likely, however, you will be required to define buckets
55+
// customized to your use case.
5556
DefBuckets = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10}
57+
58+
errBucketLabelNotAllowed = fmt.Errorf(
59+
"%q is not allowed as label name in histograms", model.BucketLabel,
60+
)
5661
)
5762

5863
// LinearBuckets creates 'count' buckets, each 'width' wide, where the lowest
@@ -165,13 +170,13 @@ func newHistogram(desc *Desc, opts HistogramOpts, labelValues ...string) Histogr
165170
}
166171

167172
for _, n := range desc.variableLabels {
168-
if n == "le" {
169-
panic("'le' is not allowed as label name in histograms")
173+
if n == model.BucketLabel {
174+
panic(errBucketLabelNotAllowed)
170175
}
171176
}
172177
for _, lp := range desc.constLabelPairs {
173-
if lp.GetName() == "le" {
174-
panic("'le' is not allowed as label name in histograms")
178+
if lp.GetName() == model.BucketLabel {
179+
panic(errBucketLabelNotAllowed)
175180
}
176181
}
177182

prometheus/summary.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
dto "github.com/prometheus/client_model/go"
2626

2727
"github.com/prometheus/client_golang/_vendor/perks/quantile"
28+
"github.com/prometheus/client_golang/model"
2829
)
2930

3031
// A Summary captures individual observations from an event or sample stream and
@@ -50,9 +51,13 @@ type Summary interface {
5051
Observe(float64)
5152
}
5253

53-
// DefObjectives are the default Summary quantile values.
5454
var (
55+
// DefObjectives are the default Summary quantile values.
5556
DefObjectives = map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}
57+
58+
errQuantileLabelNotAllowed = fmt.Errorf(
59+
"%q is not allowed as label name in summaries", model.QuantileLabel,
60+
)
5661
)
5762

5863
// Default values for SummaryOpts.
@@ -164,13 +169,13 @@ func newSummary(desc *Desc, opts SummaryOpts, labelValues ...string) Summary {
164169
}
165170

166171
for _, n := range desc.variableLabels {
167-
if n == "quantile" {
168-
panic("'quantile' is not allowed as label name in summaries")
172+
if n == model.QuantileLabel {
173+
panic(errQuantileLabelNotAllowed)
169174
}
170175
}
171176
for _, lp := range desc.constLabelPairs {
172-
if lp.GetName() == "quantile" {
173-
panic("'quantile' is not allowed as label name in summaries")
177+
if lp.GetName() == model.QuantileLabel {
178+
panic(errQuantileLabelNotAllowed)
174179
}
175180
}
176181

text/create.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"math"
2828
"strings"
2929

30+
"github.com/prometheus/client_golang/model"
3031
dto "github.com/prometheus/client_model/go"
3132
)
3233

@@ -117,7 +118,7 @@ func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (int, error) {
117118
for _, q := range metric.Summary.Quantile {
118119
n, err = writeSample(
119120
name, metric,
120-
"quantile", fmt.Sprint(q.GetQuantile()),
121+
model.QuantileLabel, fmt.Sprint(q.GetQuantile()),
121122
q.GetValue(),
122123
out,
123124
)
@@ -150,7 +151,7 @@ func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (int, error) {
150151
for _, q := range metric.Histogram.Bucket {
151152
n, err = writeSample(
152153
name+"_bucket", metric,
153-
"le", fmt.Sprint(q.GetUpperBound()),
154+
model.BucketLabel, fmt.Sprint(q.GetUpperBound()),
154155
float64(q.GetCumulativeCount()),
155156
out,
156157
)
@@ -165,7 +166,7 @@ func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (int, error) {
165166
if !infSeen {
166167
n, err = writeSample(
167168
name+"_bucket", metric,
168-
"le", "+Inf",
169+
model.BucketLabel, "+Inf",
169170
float64(metric.Histogram.GetSampleCount()),
170171
out,
171172
)

text/parse.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ func (p *Parser) startLabelName() stateFn {
274274
}
275275
// Special summary/histogram treatment. Don't add 'quantile' and 'le'
276276
// labels to 'real' labels.
277-
if !(p.currentMF.GetType() == dto.MetricType_SUMMARY && p.currentLabelPair.GetName() == "quantile") &&
278-
!(p.currentMF.GetType() == dto.MetricType_HISTOGRAM && p.currentLabelPair.GetName() == "le") {
277+
if !(p.currentMF.GetType() == dto.MetricType_SUMMARY && p.currentLabelPair.GetName() == model.QuantileLabel) &&
278+
!(p.currentMF.GetType() == dto.MetricType_HISTOGRAM && p.currentLabelPair.GetName() == model.BucketLabel) {
279279
p.currentMetric.Label = append(p.currentMetric.Label, p.currentLabelPair)
280280
}
281281
if p.skipBlankTabIfCurrentBlankTab(); p.err != nil {
@@ -306,7 +306,7 @@ func (p *Parser) startLabelValue() stateFn {
306306
// - Quantile labels are special, will result in dto.Quantile later.
307307
// - Other labels have to be added to currentLabels for signature calculation.
308308
if p.currentMF.GetType() == dto.MetricType_SUMMARY {
309-
if p.currentLabelPair.GetName() == "quantile" {
309+
if p.currentLabelPair.GetName() == model.QuantileLabel {
310310
if p.currentQuantile, p.err = strconv.ParseFloat(p.currentLabelPair.GetValue(), 64); p.err != nil {
311311
// Create a more helpful error message.
312312
p.parseError(fmt.Sprintf("expected float as value for 'quantile' label, got %q", p.currentLabelPair.GetValue()))
@@ -318,7 +318,7 @@ func (p *Parser) startLabelValue() stateFn {
318318
}
319319
// Similar special treatment of histograms.
320320
if p.currentMF.GetType() == dto.MetricType_HISTOGRAM {
321-
if p.currentLabelPair.GetName() == "le" {
321+
if p.currentLabelPair.GetName() == model.BucketLabel {
322322
if p.currentBucket, p.err = strconv.ParseFloat(p.currentLabelPair.GetValue(), 64); p.err != nil {
323323
// Create a more helpful error message.
324324
p.parseError(fmt.Sprintf("expected float as value for 'le' label, got %q", p.currentLabelPair.GetValue()))

0 commit comments

Comments
 (0)