2015-02-02 17:14:36 +03:00
|
|
|
// Copyright 2014 The Prometheus Authors
|
2014-05-07 22:08:33 +04:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-02-02 20:02:40 +03:00
|
|
|
"math"
|
2014-05-07 22:08:33 +04:00
|
|
|
"sort"
|
2015-02-02 20:02:40 +03:00
|
|
|
"sync/atomic"
|
2016-11-18 18:29:59 +03:00
|
|
|
"time"
|
2014-05-07 22:08:33 +04:00
|
|
|
|
2015-02-27 18:12:59 +03:00
|
|
|
dto "github.com/prometheus/client_model/go"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
2014-05-07 22:08:33 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ValueType is an enumeration of metric types that represent a simple value.
|
|
|
|
type ValueType int
|
|
|
|
|
|
|
|
// Possible values for the ValueType enum.
|
|
|
|
const (
|
|
|
|
_ ValueType = iota
|
|
|
|
CounterValue
|
|
|
|
GaugeValue
|
|
|
|
UntypedValue
|
|
|
|
)
|
|
|
|
|
|
|
|
// value is a generic metric for simple values. It implements Metric, Collector,
|
|
|
|
// Counter, Gauge, and Untyped. Its effective type is determined by
|
|
|
|
// ValueType. This is a low-level building block used by the library to back the
|
|
|
|
// implementations of Counter, Gauge, and Untyped.
|
|
|
|
type value struct {
|
2017-03-22 16:29:18 +03:00
|
|
|
// valBits contains the bits of the represented float64 value. It has
|
2015-05-21 13:19:38 +03:00
|
|
|
// to go first in the struct to guarantee alignment for atomic
|
|
|
|
// operations. http://golang.org/pkg/sync/atomic/#pkg-note-BUG
|
|
|
|
valBits uint64
|
2017-12-15 22:03:24 +03:00
|
|
|
// valInt is used to store values that are exact integers
|
|
|
|
valInt int64
|
|
|
|
|
2016-08-03 02:09:27 +03:00
|
|
|
selfCollector
|
2014-05-07 22:08:33 +04:00
|
|
|
|
|
|
|
desc *Desc
|
|
|
|
valType ValueType
|
|
|
|
labelPairs []*dto.LabelPair
|
|
|
|
}
|
|
|
|
|
2014-06-23 16:15:35 +04:00
|
|
|
// newValue returns a newly allocated value with the given Desc, ValueType,
|
2014-05-07 22:08:33 +04:00
|
|
|
// sample value and label values. It panics if the number of label
|
|
|
|
// values is different from the number of variable labels in Desc.
|
|
|
|
func newValue(desc *Desc, valueType ValueType, val float64, labelValues ...string) *value {
|
|
|
|
if len(labelValues) != len(desc.variableLabels) {
|
|
|
|
panic(errInconsistentCardinality)
|
|
|
|
}
|
|
|
|
result := &value{
|
|
|
|
desc: desc,
|
|
|
|
valType: valueType,
|
2015-02-02 20:02:40 +03:00
|
|
|
valBits: math.Float64bits(val),
|
2014-05-07 22:08:33 +04:00
|
|
|
labelPairs: makeLabelPairs(desc, labelValues),
|
|
|
|
}
|
2016-08-03 02:09:27 +03:00
|
|
|
result.init(result)
|
2014-05-07 22:08:33 +04:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *value) Desc() *Desc {
|
|
|
|
return v.desc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *value) Set(val float64) {
|
2015-02-02 20:02:40 +03:00
|
|
|
atomic.StoreUint64(&v.valBits, math.Float64bits(val))
|
2014-05-07 22:08:33 +04:00
|
|
|
}
|
|
|
|
|
2016-11-18 18:29:59 +03:00
|
|
|
func (v *value) SetToCurrentTime() {
|
|
|
|
v.Set(float64(time.Now().UnixNano()) / 1e9)
|
|
|
|
}
|
|
|
|
|
2017-12-15 22:03:24 +03:00
|
|
|
// add adjusts the underlying int64
|
|
|
|
func (v *value) add(delta int64) {
|
|
|
|
atomic.AddInt64(&v.valInt, delta)
|
|
|
|
}
|
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
func (v *value) Inc() {
|
2017-12-15 22:03:24 +03:00
|
|
|
v.add(1)
|
2014-05-07 22:08:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *value) Dec() {
|
2017-12-15 22:03:24 +03:00
|
|
|
v.add(-1)
|
2014-05-07 22:08:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *value) Add(val float64) {
|
2017-12-15 22:00:37 +03:00
|
|
|
ival := int64(val)
|
|
|
|
if float64(ival) == val {
|
|
|
|
v.add(ival)
|
2017-12-15 22:03:24 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-02 20:02:40 +03:00
|
|
|
for {
|
|
|
|
oldBits := atomic.LoadUint64(&v.valBits)
|
|
|
|
newBits := math.Float64bits(math.Float64frombits(oldBits) + val)
|
|
|
|
if atomic.CompareAndSwapUint64(&v.valBits, oldBits, newBits) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2014-05-07 22:08:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *value) Sub(val float64) {
|
|
|
|
v.Add(val * -1)
|
|
|
|
}
|
|
|
|
|
Allow error reporting during metrics collection and simplify Register().
Both are interface changes I want to get in before public
announcement. They only break rare usage cases, and are always easy to
fix, but still we want to avoid breaking changes after a wider
announcement of the project.
The change of Register() simply removes the return of the Collector,
which nobody was using in practice. It was just bloating the call
syntax. Note that this is different from RegisterOrGet(), which is
used at various occasions where you want to register something that
might or might not be registered already, but if it is, you want the
previously registered Collector back (because that's the relevant
one).
WRT error reporting: I first tried the obvious way of letting the
Collector methods Describe() and Collect() return error. However, I
had to conclude that that bloated _many_ calls and their handling in
very obnoxious ways. On the other hand, the case where you actually
want to report errors during registration or collection is very
rare. Hence, this approach has the wrong trade-off. The approach taken
here might at first appear clunky but is in practice quite handy,
mostly because there is almost no change for the "normal" case of "no
special error handling", but also because it plays well with the way
descriptors and metrics are handled (via channels).
Explaining the approach in more detail:
- During registration / describe: Error handling was actually already
in place (for invalid descriptors, which carry an error anyway). I
only added a convenience function to create an invalid descriptor
with a given error on purpose.
- Metrics are now treated in a similar way. The Write method returns
an error now (the only change in interface). An "invalid metric" is
provided that can be sent via the channel to signal that that metric
could not be collected. It alse transports an error.
NON-GOALS OF THIS COMMIT:
This is NOT yet the major improvement of the whole registry part,
where we want a public Registry interface and plenty of modular
configurations (for error handling, various auto-metrics, http
instrumentation, testing, ...). However, we can do that whole thing
without breaking existing interfaces. For now (which is a significant
issue) any error during collection will either cause a 500 HTTP
response or a panic (depending on registry config). Later, we
definitely want to have a possibility to skip (and only report
somehow) non-collectible metrics instead of aborting the whole scrape.
2015-01-12 21:16:09 +03:00
|
|
|
func (v *value) Write(out *dto.Metric) error {
|
2017-12-15 22:03:24 +03:00
|
|
|
fval := math.Float64frombits(atomic.LoadUint64(&v.valBits))
|
|
|
|
ival := atomic.LoadInt64(&v.valInt)
|
|
|
|
val := fval + float64(ival)
|
|
|
|
|
Allow error reporting during metrics collection and simplify Register().
Both are interface changes I want to get in before public
announcement. They only break rare usage cases, and are always easy to
fix, but still we want to avoid breaking changes after a wider
announcement of the project.
The change of Register() simply removes the return of the Collector,
which nobody was using in practice. It was just bloating the call
syntax. Note that this is different from RegisterOrGet(), which is
used at various occasions where you want to register something that
might or might not be registered already, but if it is, you want the
previously registered Collector back (because that's the relevant
one).
WRT error reporting: I first tried the obvious way of letting the
Collector methods Describe() and Collect() return error. However, I
had to conclude that that bloated _many_ calls and their handling in
very obnoxious ways. On the other hand, the case where you actually
want to report errors during registration or collection is very
rare. Hence, this approach has the wrong trade-off. The approach taken
here might at first appear clunky but is in practice quite handy,
mostly because there is almost no change for the "normal" case of "no
special error handling", but also because it plays well with the way
descriptors and metrics are handled (via channels).
Explaining the approach in more detail:
- During registration / describe: Error handling was actually already
in place (for invalid descriptors, which carry an error anyway). I
only added a convenience function to create an invalid descriptor
with a given error on purpose.
- Metrics are now treated in a similar way. The Write method returns
an error now (the only change in interface). An "invalid metric" is
provided that can be sent via the channel to signal that that metric
could not be collected. It alse transports an error.
NON-GOALS OF THIS COMMIT:
This is NOT yet the major improvement of the whole registry part,
where we want a public Registry interface and plenty of modular
configurations (for error handling, various auto-metrics, http
instrumentation, testing, ...). However, we can do that whole thing
without breaking existing interfaces. For now (which is a significant
issue) any error during collection will either cause a 500 HTTP
response or a panic (depending on registry config). Later, we
definitely want to have a possibility to skip (and only report
somehow) non-collectible metrics instead of aborting the whole scrape.
2015-01-12 21:16:09 +03:00
|
|
|
return populateMetric(v.valType, val, v.labelPairs, out)
|
2014-05-07 22:08:33 +04:00
|
|
|
}
|
|
|
|
|
2014-06-23 16:15:35 +04:00
|
|
|
// valueFunc is a generic metric for simple values retrieved on collect time
|
|
|
|
// from a function. It implements Metric and Collector. Its effective type is
|
|
|
|
// determined by ValueType. This is a low-level building block used by the
|
|
|
|
// library to back the implementations of CounterFunc, GaugeFunc, and
|
|
|
|
// UntypedFunc.
|
|
|
|
type valueFunc struct {
|
2016-08-03 02:09:27 +03:00
|
|
|
selfCollector
|
2014-06-23 16:15:35 +04:00
|
|
|
|
|
|
|
desc *Desc
|
|
|
|
valType ValueType
|
|
|
|
function func() float64
|
|
|
|
labelPairs []*dto.LabelPair
|
|
|
|
}
|
|
|
|
|
|
|
|
// newValueFunc returns a newly allocated valueFunc with the given Desc and
|
|
|
|
// ValueType. The value reported is determined by calling the given function
|
|
|
|
// from within the Write method. Take into account that metric collection may
|
|
|
|
// happen concurrently. If that results in concurrent calls to Write, like in
|
|
|
|
// the case where a valueFunc is directly registered with Prometheus, the
|
|
|
|
// provided function must be concurrency-safe.
|
|
|
|
func newValueFunc(desc *Desc, valueType ValueType, function func() float64) *valueFunc {
|
|
|
|
result := &valueFunc{
|
|
|
|
desc: desc,
|
|
|
|
valType: valueType,
|
|
|
|
function: function,
|
|
|
|
labelPairs: makeLabelPairs(desc, nil),
|
|
|
|
}
|
2016-08-03 02:09:27 +03:00
|
|
|
result.init(result)
|
2014-06-23 16:15:35 +04:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *valueFunc) Desc() *Desc {
|
|
|
|
return v.desc
|
|
|
|
}
|
|
|
|
|
Allow error reporting during metrics collection and simplify Register().
Both are interface changes I want to get in before public
announcement. They only break rare usage cases, and are always easy to
fix, but still we want to avoid breaking changes after a wider
announcement of the project.
The change of Register() simply removes the return of the Collector,
which nobody was using in practice. It was just bloating the call
syntax. Note that this is different from RegisterOrGet(), which is
used at various occasions where you want to register something that
might or might not be registered already, but if it is, you want the
previously registered Collector back (because that's the relevant
one).
WRT error reporting: I first tried the obvious way of letting the
Collector methods Describe() and Collect() return error. However, I
had to conclude that that bloated _many_ calls and their handling in
very obnoxious ways. On the other hand, the case where you actually
want to report errors during registration or collection is very
rare. Hence, this approach has the wrong trade-off. The approach taken
here might at first appear clunky but is in practice quite handy,
mostly because there is almost no change for the "normal" case of "no
special error handling", but also because it plays well with the way
descriptors and metrics are handled (via channels).
Explaining the approach in more detail:
- During registration / describe: Error handling was actually already
in place (for invalid descriptors, which carry an error anyway). I
only added a convenience function to create an invalid descriptor
with a given error on purpose.
- Metrics are now treated in a similar way. The Write method returns
an error now (the only change in interface). An "invalid metric" is
provided that can be sent via the channel to signal that that metric
could not be collected. It alse transports an error.
NON-GOALS OF THIS COMMIT:
This is NOT yet the major improvement of the whole registry part,
where we want a public Registry interface and plenty of modular
configurations (for error handling, various auto-metrics, http
instrumentation, testing, ...). However, we can do that whole thing
without breaking existing interfaces. For now (which is a significant
issue) any error during collection will either cause a 500 HTTP
response or a panic (depending on registry config). Later, we
definitely want to have a possibility to skip (and only report
somehow) non-collectible metrics instead of aborting the whole scrape.
2015-01-12 21:16:09 +03:00
|
|
|
func (v *valueFunc) Write(out *dto.Metric) error {
|
|
|
|
return populateMetric(v.valType, v.function(), v.labelPairs, out)
|
2014-06-23 16:15:35 +04:00
|
|
|
}
|
|
|
|
|
2014-05-07 22:08:33 +04:00
|
|
|
// NewConstMetric returns a metric with one fixed value that cannot be
|
|
|
|
// changed. Users of this package will not have much use for it in regular
|
|
|
|
// operations. However, when implementing custom Collectors, it is useful as a
|
|
|
|
// throw-away metric that is generated on the fly to send it to Prometheus in
|
|
|
|
// the Collect method. NewConstMetric returns an error if the length of
|
|
|
|
// labelValues is not consistent with the variable labels in Desc.
|
|
|
|
func NewConstMetric(desc *Desc, valueType ValueType, value float64, labelValues ...string) (Metric, error) {
|
2017-08-25 18:58:59 +03:00
|
|
|
if err := validateLabelValues(labelValues, len(desc.variableLabels)); err != nil {
|
2017-08-19 23:57:48 +03:00
|
|
|
return nil, err
|
2014-05-07 22:08:33 +04:00
|
|
|
}
|
|
|
|
return &constMetric{
|
|
|
|
desc: desc,
|
|
|
|
valType: valueType,
|
|
|
|
val: value,
|
|
|
|
labelPairs: makeLabelPairs(desc, labelValues),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustNewConstMetric is a version of NewConstMetric that panics where
|
|
|
|
// NewConstMetric would have returned an error.
|
|
|
|
func MustNewConstMetric(desc *Desc, valueType ValueType, value float64, labelValues ...string) Metric {
|
|
|
|
m, err := NewConstMetric(desc, valueType, value, labelValues...)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
type constMetric struct {
|
|
|
|
desc *Desc
|
|
|
|
valType ValueType
|
|
|
|
val float64
|
|
|
|
labelPairs []*dto.LabelPair
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *constMetric) Desc() *Desc {
|
|
|
|
return m.desc
|
|
|
|
}
|
|
|
|
|
Allow error reporting during metrics collection and simplify Register().
Both are interface changes I want to get in before public
announcement. They only break rare usage cases, and are always easy to
fix, but still we want to avoid breaking changes after a wider
announcement of the project.
The change of Register() simply removes the return of the Collector,
which nobody was using in practice. It was just bloating the call
syntax. Note that this is different from RegisterOrGet(), which is
used at various occasions where you want to register something that
might or might not be registered already, but if it is, you want the
previously registered Collector back (because that's the relevant
one).
WRT error reporting: I first tried the obvious way of letting the
Collector methods Describe() and Collect() return error. However, I
had to conclude that that bloated _many_ calls and their handling in
very obnoxious ways. On the other hand, the case where you actually
want to report errors during registration or collection is very
rare. Hence, this approach has the wrong trade-off. The approach taken
here might at first appear clunky but is in practice quite handy,
mostly because there is almost no change for the "normal" case of "no
special error handling", but also because it plays well with the way
descriptors and metrics are handled (via channels).
Explaining the approach in more detail:
- During registration / describe: Error handling was actually already
in place (for invalid descriptors, which carry an error anyway). I
only added a convenience function to create an invalid descriptor
with a given error on purpose.
- Metrics are now treated in a similar way. The Write method returns
an error now (the only change in interface). An "invalid metric" is
provided that can be sent via the channel to signal that that metric
could not be collected. It alse transports an error.
NON-GOALS OF THIS COMMIT:
This is NOT yet the major improvement of the whole registry part,
where we want a public Registry interface and plenty of modular
configurations (for error handling, various auto-metrics, http
instrumentation, testing, ...). However, we can do that whole thing
without breaking existing interfaces. For now (which is a significant
issue) any error during collection will either cause a 500 HTTP
response or a panic (depending on registry config). Later, we
definitely want to have a possibility to skip (and only report
somehow) non-collectible metrics instead of aborting the whole scrape.
2015-01-12 21:16:09 +03:00
|
|
|
func (m *constMetric) Write(out *dto.Metric) error {
|
|
|
|
return populateMetric(m.valType, m.val, m.labelPairs, out)
|
2014-05-07 22:08:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func populateMetric(
|
|
|
|
t ValueType,
|
|
|
|
v float64,
|
|
|
|
labelPairs []*dto.LabelPair,
|
|
|
|
m *dto.Metric,
|
Allow error reporting during metrics collection and simplify Register().
Both are interface changes I want to get in before public
announcement. They only break rare usage cases, and are always easy to
fix, but still we want to avoid breaking changes after a wider
announcement of the project.
The change of Register() simply removes the return of the Collector,
which nobody was using in practice. It was just bloating the call
syntax. Note that this is different from RegisterOrGet(), which is
used at various occasions where you want to register something that
might or might not be registered already, but if it is, you want the
previously registered Collector back (because that's the relevant
one).
WRT error reporting: I first tried the obvious way of letting the
Collector methods Describe() and Collect() return error. However, I
had to conclude that that bloated _many_ calls and their handling in
very obnoxious ways. On the other hand, the case where you actually
want to report errors during registration or collection is very
rare. Hence, this approach has the wrong trade-off. The approach taken
here might at first appear clunky but is in practice quite handy,
mostly because there is almost no change for the "normal" case of "no
special error handling", but also because it plays well with the way
descriptors and metrics are handled (via channels).
Explaining the approach in more detail:
- During registration / describe: Error handling was actually already
in place (for invalid descriptors, which carry an error anyway). I
only added a convenience function to create an invalid descriptor
with a given error on purpose.
- Metrics are now treated in a similar way. The Write method returns
an error now (the only change in interface). An "invalid metric" is
provided that can be sent via the channel to signal that that metric
could not be collected. It alse transports an error.
NON-GOALS OF THIS COMMIT:
This is NOT yet the major improvement of the whole registry part,
where we want a public Registry interface and plenty of modular
configurations (for error handling, various auto-metrics, http
instrumentation, testing, ...). However, we can do that whole thing
without breaking existing interfaces. For now (which is a significant
issue) any error during collection will either cause a 500 HTTP
response or a panic (depending on registry config). Later, we
definitely want to have a possibility to skip (and only report
somehow) non-collectible metrics instead of aborting the whole scrape.
2015-01-12 21:16:09 +03:00
|
|
|
) error {
|
2014-05-07 22:08:33 +04:00
|
|
|
m.Label = labelPairs
|
|
|
|
switch t {
|
|
|
|
case CounterValue:
|
|
|
|
m.Counter = &dto.Counter{Value: proto.Float64(v)}
|
|
|
|
case GaugeValue:
|
|
|
|
m.Gauge = &dto.Gauge{Value: proto.Float64(v)}
|
|
|
|
case UntypedValue:
|
|
|
|
m.Untyped = &dto.Untyped{Value: proto.Float64(v)}
|
|
|
|
default:
|
Allow error reporting during metrics collection and simplify Register().
Both are interface changes I want to get in before public
announcement. They only break rare usage cases, and are always easy to
fix, but still we want to avoid breaking changes after a wider
announcement of the project.
The change of Register() simply removes the return of the Collector,
which nobody was using in practice. It was just bloating the call
syntax. Note that this is different from RegisterOrGet(), which is
used at various occasions where you want to register something that
might or might not be registered already, but if it is, you want the
previously registered Collector back (because that's the relevant
one).
WRT error reporting: I first tried the obvious way of letting the
Collector methods Describe() and Collect() return error. However, I
had to conclude that that bloated _many_ calls and their handling in
very obnoxious ways. On the other hand, the case where you actually
want to report errors during registration or collection is very
rare. Hence, this approach has the wrong trade-off. The approach taken
here might at first appear clunky but is in practice quite handy,
mostly because there is almost no change for the "normal" case of "no
special error handling", but also because it plays well with the way
descriptors and metrics are handled (via channels).
Explaining the approach in more detail:
- During registration / describe: Error handling was actually already
in place (for invalid descriptors, which carry an error anyway). I
only added a convenience function to create an invalid descriptor
with a given error on purpose.
- Metrics are now treated in a similar way. The Write method returns
an error now (the only change in interface). An "invalid metric" is
provided that can be sent via the channel to signal that that metric
could not be collected. It alse transports an error.
NON-GOALS OF THIS COMMIT:
This is NOT yet the major improvement of the whole registry part,
where we want a public Registry interface and plenty of modular
configurations (for error handling, various auto-metrics, http
instrumentation, testing, ...). However, we can do that whole thing
without breaking existing interfaces. For now (which is a significant
issue) any error during collection will either cause a 500 HTTP
response or a panic (depending on registry config). Later, we
definitely want to have a possibility to skip (and only report
somehow) non-collectible metrics instead of aborting the whole scrape.
2015-01-12 21:16:09 +03:00
|
|
|
return fmt.Errorf("encountered unknown type %v", t)
|
2014-05-07 22:08:33 +04:00
|
|
|
}
|
Allow error reporting during metrics collection and simplify Register().
Both are interface changes I want to get in before public
announcement. They only break rare usage cases, and are always easy to
fix, but still we want to avoid breaking changes after a wider
announcement of the project.
The change of Register() simply removes the return of the Collector,
which nobody was using in practice. It was just bloating the call
syntax. Note that this is different from RegisterOrGet(), which is
used at various occasions where you want to register something that
might or might not be registered already, but if it is, you want the
previously registered Collector back (because that's the relevant
one).
WRT error reporting: I first tried the obvious way of letting the
Collector methods Describe() and Collect() return error. However, I
had to conclude that that bloated _many_ calls and their handling in
very obnoxious ways. On the other hand, the case where you actually
want to report errors during registration or collection is very
rare. Hence, this approach has the wrong trade-off. The approach taken
here might at first appear clunky but is in practice quite handy,
mostly because there is almost no change for the "normal" case of "no
special error handling", but also because it plays well with the way
descriptors and metrics are handled (via channels).
Explaining the approach in more detail:
- During registration / describe: Error handling was actually already
in place (for invalid descriptors, which carry an error anyway). I
only added a convenience function to create an invalid descriptor
with a given error on purpose.
- Metrics are now treated in a similar way. The Write method returns
an error now (the only change in interface). An "invalid metric" is
provided that can be sent via the channel to signal that that metric
could not be collected. It alse transports an error.
NON-GOALS OF THIS COMMIT:
This is NOT yet the major improvement of the whole registry part,
where we want a public Registry interface and plenty of modular
configurations (for error handling, various auto-metrics, http
instrumentation, testing, ...). However, we can do that whole thing
without breaking existing interfaces. For now (which is a significant
issue) any error during collection will either cause a 500 HTTP
response or a panic (depending on registry config). Later, we
definitely want to have a possibility to skip (and only report
somehow) non-collectible metrics instead of aborting the whole scrape.
2015-01-12 21:16:09 +03:00
|
|
|
return nil
|
2014-05-07 22:08:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func makeLabelPairs(desc *Desc, labelValues []string) []*dto.LabelPair {
|
|
|
|
totalLen := len(desc.variableLabels) + len(desc.constLabelPairs)
|
|
|
|
if totalLen == 0 {
|
|
|
|
// Super fast path.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(desc.variableLabels) == 0 {
|
|
|
|
// Moderately fast path.
|
|
|
|
return desc.constLabelPairs
|
|
|
|
}
|
|
|
|
labelPairs := make([]*dto.LabelPair, 0, totalLen)
|
|
|
|
for i, n := range desc.variableLabels {
|
|
|
|
labelPairs = append(labelPairs, &dto.LabelPair{
|
|
|
|
Name: proto.String(n),
|
|
|
|
Value: proto.String(labelValues[i]),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, lp := range desc.constLabelPairs {
|
|
|
|
labelPairs = append(labelPairs, lp)
|
|
|
|
}
|
|
|
|
sort.Sort(LabelPairSorter(labelPairs))
|
|
|
|
return labelPairs
|
|
|
|
}
|