From b1e7299877c31cec61356b8e8774420548715d9f Mon Sep 17 00:00:00 2001 From: beorn7 Date: Thu, 19 Feb 2015 15:31:43 +0100 Subject: [PATCH] Turned "le" and "quantile" label names into constants. --- extraction/metricfamilyprocessor.go | 6 +++--- model/labelname.go | 8 ++++++++ prometheus/histogram.go | 23 ++++++++++++++--------- prometheus/summary.go | 15 ++++++++++----- text/create.go | 7 ++++--- text/parse.go | 8 ++++---- 6 files changed, 43 insertions(+), 24 deletions(-) diff --git a/extraction/metricfamilyprocessor.go b/extraction/metricfamilyprocessor.go index 608e786..5edb49c 100644 --- a/extraction/metricfamilyprocessor.go +++ b/extraction/metricfamilyprocessor.go @@ -164,7 +164,7 @@ func extractSummary(out Ingester, o *ProcessOptions, f *dto.MetricFamily) error metric[model.LabelName(p.GetName())] = model.LabelValue(p.GetValue()) } // BUG(matt): Update other names to "quantile". - metric[model.LabelName("quantile")] = model.LabelValue(fmt.Sprint(q.GetQuantile())) + metric[model.LabelName(model.QuantileLabel)] = model.LabelValue(fmt.Sprint(q.GetQuantile())) metric[model.MetricNameLabel] = model.LabelValue(f.GetName()) } @@ -259,7 +259,7 @@ func extractHistogram(out Ingester, o *ProcessOptions, f *dto.MetricFamily) erro for _, p := range m.Label { metric[model.LabelName(p.GetName())] = model.LabelValue(p.GetValue()) } - metric[model.LabelName("le")] = model.LabelValue(fmt.Sprint(q.GetUpperBound())) + metric[model.LabelName(model.BucketLabel)] = model.LabelValue(fmt.Sprint(q.GetUpperBound())) metric[model.MetricNameLabel] = model.LabelValue(f.GetName() + "_bucket") if math.IsInf(q.GetUpperBound(), +1) { @@ -308,7 +308,7 @@ func extractHistogram(out Ingester, o *ProcessOptions, f *dto.MetricFamily) erro for _, p := range m.Label { metric[model.LabelName(p.GetName())] = model.LabelValue(p.GetValue()) } - metric[model.LabelName("le")] = model.LabelValue("+Inf") + metric[model.LabelName(model.BucketLabel)] = model.LabelValue("+Inf") metric[model.MetricNameLabel] = model.LabelValue(f.GetName() + "_bucket") } } diff --git a/model/labelname.go b/model/labelname.go index 047e756..75b2e79 100644 --- a/model/labelname.go +++ b/model/labelname.go @@ -33,6 +33,14 @@ const ( // JobLabel is the label name indicating the job from which a timeseries // was scraped. JobLabel LabelName = "job" + + // BucketLabel is used for the label that defines the upper bound of a + // bucket of a histogram ("le" -> "less or equal"). + BucketLabel = "le" + + // QuantileLabel is used for the label that defines the quantile in a + // summary. + QuantileLabel = "quantile" ) // A LabelName is a key for a LabelSet or Metric. It has a value associated diff --git a/prometheus/histogram.go b/prometheus/histogram.go index a02fd53..7f3da39 100644 --- a/prometheus/histogram.go +++ b/prometheus/histogram.go @@ -21,6 +21,7 @@ import ( "github.com/golang/protobuf/proto" + "github.com/prometheus/client_golang/model" dto "github.com/prometheus/client_model/go" ) @@ -33,7 +34,7 @@ import ( // // Note that Histograms, in contrast to Summaries, can be aggregated with the // Prometheus query language (see the documentation for detailed -// procedures). However, Histograms requires the user to pre-define suitable +// procedures). However, Histograms require the user to pre-define suitable // buckets, and they are in general less accurate. The Observe method of a // Histogram has a very low performance overhead in comparison with the Observe // method of a Summary. @@ -47,12 +48,16 @@ type Histogram interface { Observe(float64) } -// DefBuckets are the default Histogram buckets. The default buckets are -// tailored to broadly measure response time in seconds for a typical online -// serving system. Most likely, however, you will be required to define buckets -// customized to your use case. var ( + // DefBuckets are the default Histogram buckets. The default buckets are + // tailored to broadly measure response time in seconds for a typical online + // serving system. Most likely, however, you will be required to define buckets + // customized to your use case. DefBuckets = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10} + + errBucketLabelNotAllowed = fmt.Errorf( + "%q is not allowed as label name in histograms", model.BucketLabel, + ) ) // LinearBuckets creates 'count' buckets, each 'width' wide, where the lowest @@ -165,13 +170,13 @@ func newHistogram(desc *Desc, opts HistogramOpts, labelValues ...string) Histogr } for _, n := range desc.variableLabels { - if n == "le" { - panic("'le' is not allowed as label name in histograms") + if n == model.BucketLabel { + panic(errBucketLabelNotAllowed) } } for _, lp := range desc.constLabelPairs { - if lp.GetName() == "le" { - panic("'le' is not allowed as label name in histograms") + if lp.GetName() == model.BucketLabel { + panic(errBucketLabelNotAllowed) } } diff --git a/prometheus/summary.go b/prometheus/summary.go index 2d4bc55..f93cb35 100644 --- a/prometheus/summary.go +++ b/prometheus/summary.go @@ -25,6 +25,7 @@ import ( dto "github.com/prometheus/client_model/go" "github.com/prometheus/client_golang/_vendor/perks/quantile" + "github.com/prometheus/client_golang/model" ) // A Summary captures individual observations from an event or sample stream and @@ -50,9 +51,13 @@ type Summary interface { Observe(float64) } -// DefObjectives are the default Summary quantile values. var ( + // DefObjectives are the default Summary quantile values. DefObjectives = map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001} + + errQuantileLabelNotAllowed = fmt.Errorf( + "%q is not allowed as label name in summaries", model.QuantileLabel, + ) ) // Default values for SummaryOpts. @@ -164,13 +169,13 @@ func newSummary(desc *Desc, opts SummaryOpts, labelValues ...string) Summary { } for _, n := range desc.variableLabels { - if n == "quantile" { - panic("'quantile' is not allowed as label name in summaries") + if n == model.QuantileLabel { + panic(errQuantileLabelNotAllowed) } } for _, lp := range desc.constLabelPairs { - if lp.GetName() == "quantile" { - panic("'quantile' is not allowed as label name in summaries") + if lp.GetName() == model.QuantileLabel { + panic(errQuantileLabelNotAllowed) } } diff --git a/text/create.go b/text/create.go index 1b1cbd3..4430459 100644 --- a/text/create.go +++ b/text/create.go @@ -27,6 +27,7 @@ import ( "math" "strings" + "github.com/prometheus/client_golang/model" dto "github.com/prometheus/client_model/go" ) @@ -117,7 +118,7 @@ func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (int, error) { for _, q := range metric.Summary.Quantile { n, err = writeSample( name, metric, - "quantile", fmt.Sprint(q.GetQuantile()), + model.QuantileLabel, fmt.Sprint(q.GetQuantile()), q.GetValue(), out, ) @@ -150,7 +151,7 @@ func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (int, error) { for _, q := range metric.Histogram.Bucket { n, err = writeSample( name+"_bucket", metric, - "le", fmt.Sprint(q.GetUpperBound()), + model.BucketLabel, fmt.Sprint(q.GetUpperBound()), float64(q.GetCumulativeCount()), out, ) @@ -165,7 +166,7 @@ func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (int, error) { if !infSeen { n, err = writeSample( name+"_bucket", metric, - "le", "+Inf", + model.BucketLabel, "+Inf", float64(metric.Histogram.GetSampleCount()), out, ) diff --git a/text/parse.go b/text/parse.go index eaff592..e317d68 100644 --- a/text/parse.go +++ b/text/parse.go @@ -274,8 +274,8 @@ func (p *Parser) startLabelName() stateFn { } // Special summary/histogram treatment. Don't add 'quantile' and 'le' // labels to 'real' labels. - if !(p.currentMF.GetType() == dto.MetricType_SUMMARY && p.currentLabelPair.GetName() == "quantile") && - !(p.currentMF.GetType() == dto.MetricType_HISTOGRAM && p.currentLabelPair.GetName() == "le") { + if !(p.currentMF.GetType() == dto.MetricType_SUMMARY && p.currentLabelPair.GetName() == model.QuantileLabel) && + !(p.currentMF.GetType() == dto.MetricType_HISTOGRAM && p.currentLabelPair.GetName() == model.BucketLabel) { p.currentMetric.Label = append(p.currentMetric.Label, p.currentLabelPair) } if p.skipBlankTabIfCurrentBlankTab(); p.err != nil { @@ -306,7 +306,7 @@ func (p *Parser) startLabelValue() stateFn { // - Quantile labels are special, will result in dto.Quantile later. // - Other labels have to be added to currentLabels for signature calculation. if p.currentMF.GetType() == dto.MetricType_SUMMARY { - if p.currentLabelPair.GetName() == "quantile" { + if p.currentLabelPair.GetName() == model.QuantileLabel { if p.currentQuantile, p.err = strconv.ParseFloat(p.currentLabelPair.GetValue(), 64); p.err != nil { // Create a more helpful error message. 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 { } // Similar special treatment of histograms. if p.currentMF.GetType() == dto.MetricType_HISTOGRAM { - if p.currentLabelPair.GetName() == "le" { + if p.currentLabelPair.GetName() == model.BucketLabel { if p.currentBucket, p.err = strconv.ParseFloat(p.currentLabelPair.GetValue(), 64); p.err != nil { // Create a more helpful error message. p.parseError(fmt.Sprintf("expected float as value for 'le' label, got %q", p.currentLabelPair.GetValue()))