Skip to content

Commit f251146

Browse files
authored
prometheus: Fix convention violating names for generated collector metrics (#1048)
* Fix convention violating names for generated collector metrics Signed-off-by: Kemal Akkoyun <[email protected]> * Add new Go collector example Signed-off-by: Kemal Akkoyun <[email protected]>
1 parent 589b2ea commit f251146

10 files changed

+82
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ WORKDIR /go/src/github.com/prometheus/client_golang/examples/random
2121
RUN CGO_ENABLED=0 GOOS=linux go build -a -tags netgo -ldflags '-w'
2222
WORKDIR /go/src/github.com/prometheus/client_golang/examples/simple
2323
RUN CGO_ENABLED=0 GOOS=linux go build -a -tags netgo -ldflags '-w'
24+
WORKDIR /go/src/github.com/prometheus/client_golang/examples/gocollector
25+
RUN CGO_ENABLED=0 GOOS=linux go build -a -tags netgo -ldflags '-w'
2426

2527
# Final image.
2628
FROM quay.io/prometheus/busybox:latest
2729
LABEL maintainer="The Prometheus Authors <[email protected]>"
2830
COPY --from=builder /go/src/github.com/prometheus/client_golang/examples/random \
29-
/go/src/github.com/prometheus/client_golang/examples/simple ./
31+
/go/src/github.com/prometheus/client_golang/examples/simple \
32+
/go/src/github.com/prometheus/client_golang/examples/gocollector ./
3033
EXPOSE 8080
31-
CMD ["echo", "Please run an example. Either /random or /simple"]
34+
CMD ["echo", "Please run an example. Either /random, /simple or /gocollector"]

examples/gocollector/main.go

Lines changed: 55 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ func main() {
3939
}
4040
toolVersion := runtime.Version()
4141
mtv := majorVersion(toolVersion)
42-
mv != majorVersion(os.Args[1])
42+
mv := majorVersion(os.Args[1])
4343
if mtv != mv {
4444
log.Fatalf("using Go version %q but expected Go version %q", mtv, mv)
4545
}
46-
version, err := parseVersion(os.Args[1])
46+
version, err := parseVersion(mv)
4747
if err != nil {
4848
log.Fatalf("parsing Go version: %v", err)
4949
}

prometheus/go_collector_latest_test.go

Lines changed: 3 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

prometheus/internal/go_runtime_metrics.go

Lines changed: 7 additions & 7 deletions
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)