Skip to content

Commit 111fae1

Browse files
committed
Merge branch 'main' into sparsehistogram
2 parents 25bc188 + 10b0550 commit 111fae1

File tree

6 files changed

+21
-15
lines changed

6 files changed

+21
-15
lines changed

examples/gocollector/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"log"
2424
"net/http"
25+
"regexp"
2526

2627
"github.com/prometheus/client_golang/prometheus"
2728
"github.com/prometheus/client_golang/prometheus/collectors"
@@ -39,7 +40,7 @@ func main() {
3940
// Add Go module build info.
4041
reg.MustRegister(collectors.NewBuildInfoCollector())
4142
reg.MustRegister(collectors.NewGoCollector(
42-
collectors.WithGoCollections(collectors.GoRuntimeMemStatsCollection | collectors.GoRuntimeMetricsCollection),
43+
collectors.WithGoCollectorRuntimeMetrics(collectors.GoRuntimeMetricsRule{Matcher: regexp.MustCompile("/.*")}),
4344
))
4445

4546
// Expose the registered metrics via HTTP.

prometheus/counter.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,13 @@ func (c *counter) get() float64 {
140140
}
141141

142142
func (c *counter) Write(out *dto.Metric) error {
143-
val := c.get()
144-
143+
// Read the Exemplar first and the value second. This is to avoid a race condition
144+
// where users see an exemplar for a not-yet-existing observation.
145145
var exemplar *dto.Exemplar
146146
if e := c.exemplar.Load(); e != nil {
147147
exemplar = e.(*dto.Exemplar)
148148
}
149+
val := c.get()
149150

150151
return populateMetric(CounterValue, val, c.labelPairs, exemplar, out)
151152
}

prometheus/histogram.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,7 @@ func (h *constHistogram) Write(out *dto.Metric) error {
11701170
// to send it to Prometheus in the Collect method.
11711171
//
11721172
// buckets is a map of upper bounds to cumulative counts, excluding the +Inf
1173-
// bucket.
1173+
// bucket. The +Inf bucket is implicit, and its value is equal to the provided count.
11741174
//
11751175
// NewConstHistogram returns an error if the length of labelValues is not
11761176
// consistent with the variable labels in Desc or if Desc is invalid.

prometheus/metric.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func (m *withExemplarsMetric) Write(pb *dto.Metric) error {
187187
} else {
188188
// The +Inf bucket should be explicitly added if there is an exemplar for it, similar to non-const histogram logic in https://github.com/prometheus/client_golang/blob/main/prometheus/histogram.go#L357-L365.
189189
b := &dto.Bucket{
190-
CumulativeCount: proto.Uint64(pb.Histogram.Bucket[len(pb.Histogram.GetBucket())-1].GetCumulativeCount()),
190+
CumulativeCount: proto.Uint64(pb.Histogram.GetSampleCount()),
191191
UpperBound: proto.Float64(math.Inf(1)),
192192
Exemplar: e,
193193
}

prometheus/metric_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,14 @@ func TestWithExemplarsMetric(t *testing.T) {
7979
}
8080
}
8181

82-
infBucket := metric.GetHistogram().Bucket[len(metric.GetHistogram().Bucket)-1].GetUpperBound()
82+
infBucket := metric.GetHistogram().Bucket[len(metric.GetHistogram().Bucket)-1]
8383

84-
if infBucket != math.Inf(1) {
85-
t.Errorf("want %v, got %v", math.Inf(1), infBucket)
84+
if want, got := math.Inf(1), infBucket.GetUpperBound(); want != got {
85+
t.Errorf("want %v, got %v", want, got)
86+
}
87+
88+
if want, got := uint64(4711), infBucket.GetCumulativeCount(); want != got {
89+
t.Errorf("want %v, got %v", want, got)
8690
}
8791
})
8892
}

prometheus/promauto/auto.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
// Package promauto provides alternative constructors for the fundamental
1515
// Prometheus metric types and their …Vec and …Func variants. The difference to
1616
// their counterparts in the prometheus package is that the promauto
17-
// constructors return Collectors that are already registered with a
18-
// registry. There are two sets of constructors. The constructors in the first
19-
// set are top-level functions, while the constructors in the other set are
20-
// methods of the Factory type. The top-level function return Collectors
21-
// registered with the global registry (prometheus.DefaultRegisterer), while the
22-
// methods return Collectors registered with the registry the Factory was
23-
// constructed with. All constructors panic if the registration fails.
17+
// constructors register the Collectors with a registry before returning them.
18+
// There are two sets of constructors. The constructors in the first set are
19+
// top-level functions, while the constructors in the other set are methods of
20+
// the Factory type. The top-level function return Collectors registered with
21+
// the global registry (prometheus.DefaultRegisterer), while the methods return
22+
// Collectors registered with the registry the Factory was constructed with. All
23+
// constructors panic if the registration fails.
2424
//
2525
// The following example is a complete program to create a histogram of normally
2626
// distributed random numbers from the math/rand package:

0 commit comments

Comments
 (0)