Skip to content

Commit 049d0fe

Browse files
committedMay 13, 2022
prometheus: Fix convention violating names for generated collector metrics (#1048)
* Fix convention violating names for generated collector metrics Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com> * Add new Go collector example Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
1 parent 7eb9d11 commit 049d0fe

10 files changed

+126
-17
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Examples
22
examples/simple/simple
33
examples/random/random
4+
examples/gocollector/gocollector
45

56
# Typical backup/temporary files of editors
67
*~

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* [CHANGE] Minimum required Go version is now 1.16.
44
* [CHANGE] Added `collectors.WithGoCollections` that allows to choose what collection of Go runtime metrics user wants: Equivalent of [`MemStats` structure](https://pkg.go.dev/runtime#MemStats) configured using `GoRuntimeMemStatsCollection`, new based on dedicated [runtime/metrics](https://pkg.go.dev/runtime/metrics) metrics represented by `GoRuntimeMetricsCollection` option, or both by specifying `GoRuntimeMemStatsCollection | GoRuntimeMetricsCollection` flag.
55
* [CHANGE] :warning: Change in `collectors.NewGoCollector` metrics: Reverting addition of new ~80 runtime metrics by default. You can enable this back with `GoRuntimeMetricsCollection` option or `GoRuntimeMemStatsCollection | GoRuntimeMetricsCollection` for smooth transition.
6+
* [BUGFIX] Fix the bug that causes generated histogram metric names to end with `_total`. `go_gc_heap_allocs_by_size_bytes_total` -> `go_gc_heap_allocs_by_size_bytes`, `go_gc_heap_frees_by_size_bytes_total` -> `go_gc_heap_allocs_by_size_bytes` and`go_gc_pauses_seconds_total` -> `go_gc_pauses_seconds`.
67

78
## 1.12.1 / 2022-01-29
89

‎Dockerfile

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ WORKDIR /go/src/github.com/prometheus/client_golang/examples/random
1313
RUN CGO_ENABLED=0 GOOS=linux go build -a -tags netgo -ldflags '-w'
1414
WORKDIR /go/src/github.com/prometheus/client_golang/examples/simple
1515
RUN CGO_ENABLED=0 GOOS=linux go build -a -tags netgo -ldflags '-w'
16+
WORKDIR /go/src/github.com/prometheus/client_golang/examples/gocollector
17+
RUN CGO_ENABLED=0 GOOS=linux go build -a -tags netgo -ldflags '-w'
1618

1719
# Final image.
1820
FROM quay.io/prometheus/busybox:latest
1921
LABEL maintainer="The Prometheus Authors <prometheus-developers@googlegroups.com>"
2022
COPY --from=builder /go/src/github.com/prometheus/client_golang/examples/random \
21-
/go/src/github.com/prometheus/client_golang/examples/simple ./
23+
/go/src/github.com/prometheus/client_golang/examples/simple \
24+
/go/src/github.com/prometheus/client_golang/examples/gocollector ./
2225
EXPOSE 8080
23-
CMD ["echo", "Please run an example. Either /random or /simple"]
26+
CMD ["echo", "Please run an example. Either /random, /simple or /gocollector"]

‎examples/gocollector/main.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2022 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
//go:build go1.17
15+
// +build go1.17
16+
17+
// A minimal example of how to include Prometheus instrumentation.
18+
package main
19+
20+
import (
21+
"flag"
22+
"fmt"
23+
"log"
24+
"net/http"
25+
26+
"github.com/prometheus/client_golang/prometheus"
27+
"github.com/prometheus/client_golang/prometheus/collectors"
28+
"github.com/prometheus/client_golang/prometheus/promhttp"
29+
)
30+
31+
var addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
32+
33+
func main() {
34+
flag.Parse()
35+
36+
// Create a new registry.
37+
reg := prometheus.NewRegistry()
38+
39+
// Add Go module build info.
40+
reg.MustRegister(collectors.NewBuildInfoCollector())
41+
reg.MustRegister(collectors.NewGoCollector(
42+
collectors.WithGoCollections(collectors.GoRuntimeMemStatsCollection | collectors.GoRuntimeMetricsCollection),
43+
))
44+
45+
// Expose the registered metrics via HTTP.
46+
http.Handle("/metrics", promhttp.HandlerFor(
47+
reg,
48+
promhttp.HandlerOpts{
49+
// Opt into OpenMetrics to support exemplars.
50+
EnableOpenMetrics: true,
51+
},
52+
))
53+
fmt.Println("Hello world from new Go Collector!")
54+
log.Fatal(http.ListenAndServe(*addr, nil))
55+
}

‎prometheus/collectors/go_collector_latest.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ const (
7272
//
7373
// The current default is GoRuntimeMemStatsCollection, so the compatibility mode with
7474
// client_golang pre v1.12 (move to runtime/metrics).
75-
func WithGoCollections(flags uint32) goOption {
75+
func WithGoCollections(flags GoCollectionOption) goOption {
7676
return func(o *goOptions) {
77-
o.EnabledCollections = flags
77+
o.EnabledCollections = uint32(flags)
7878
}
7979
}
8080

‎prometheus/gen_go_collector_metrics_set.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,17 @@ func main() {
3838
log.Fatal("requires Go version (e.g. go1.17) as an argument")
3939
}
4040
toolVersion := runtime.Version()
41+
<<<<<<< HEAD
4142
if majorVersion := toolVersion[:strings.LastIndexByte(toolVersion, '.')]; majorVersion != os.Args[1] {
4243
log.Fatalf("using Go version %q but expected Go version %q", majorVersion, os.Args[1])
44+
=======
45+
mtv := majorVersion(toolVersion)
46+
mv := majorVersion(os.Args[1])
47+
if mtv != mv {
48+
log.Fatalf("using Go version %q but expected Go version %q", mtv, mv)
49+
>>>>>>> f251146 (prometheus: Fix convention violating names for generated collector metrics (#1048))
4350
}
44-
version, err := parseVersion(os.Args[1])
51+
version, err := parseVersion(mv)
4552
if err != nil {
4653
log.Fatalf("parsing Go version: %v", err)
4754
}

‎prometheus/go_collector_latest_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ import (
2424
"sync"
2525
"testing"
2626

27-
"github.com/prometheus/client_golang/prometheus/internal"
2827
dto "github.com/prometheus/client_model/go"
28+
29+
"github.com/prometheus/client_golang/prometheus/internal"
2930
)
3031

3132
func TestRmForMemStats(t *testing.T) {
@@ -121,7 +122,7 @@ func TestBatchHistogram(t *testing.T) {
121122

122123
var mhist Metric
123124
for _, m := range goMetrics {
124-
if m.Desc().fqName == "go_gc_heap_allocs_by_size_bytes_total" {
125+
if m.Desc().fqName == "go_gc_heap_allocs_by_size_bytes" {
125126
mhist = m
126127
break
127128
}

‎prometheus/go_collector_metrics_go117_test.go

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎prometheus/go_collector_metrics_go118_test.go

+41
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎prometheus/internal/go_runtime_metrics.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func RuntimeMetricsToProm(d *metrics.Description) (string, string, string, bool)
6262
// other data.
6363
name = strings.ReplaceAll(name, "-", "_")
6464
name = name + "_" + unit
65-
if d.Cumulative {
65+
if d.Cumulative && d.Kind != metrics.KindFloat64Histogram {
6666
name = name + "_total"
6767
}
6868

@@ -84,12 +84,12 @@ func RuntimeMetricsToProm(d *metrics.Description) (string, string, string, bool)
8484
func RuntimeMetricsBucketsForUnit(buckets []float64, unit string) []float64 {
8585
switch unit {
8686
case "bytes":
87-
// Rebucket as powers of 2.
88-
return rebucketExp(buckets, 2)
87+
// Re-bucket as powers of 2.
88+
return reBucketExp(buckets, 2)
8989
case "seconds":
90-
// Rebucket as powers of 10 and then merge all buckets greater
90+
// Re-bucket as powers of 10 and then merge all buckets greater
9191
// than 1 second into the +Inf bucket.
92-
b := rebucketExp(buckets, 10)
92+
b := reBucketExp(buckets, 10)
9393
for i := range b {
9494
if b[i] <= 1 {
9595
continue
@@ -103,11 +103,11 @@ func RuntimeMetricsBucketsForUnit(buckets []float64, unit string) []float64 {
103103
return buckets
104104
}
105105

106-
// rebucketExp takes a list of bucket boundaries (lower bound inclusive) and
106+
// reBucketExp takes a list of bucket boundaries (lower bound inclusive) and
107107
// downsamples the buckets to those a multiple of base apart. The end result
108108
// is a roughly exponential (in many cases, perfectly exponential) bucketing
109109
// scheme.
110-
func rebucketExp(buckets []float64, base float64) []float64 {
110+
func reBucketExp(buckets []float64, base float64) []float64 {
111111
bucket := buckets[0]
112112
var newBuckets []float64
113113
// We may see a -Inf here, in which case, add it and skip it

0 commit comments

Comments
 (0)