Skip to content

Commit a72b759

Browse files
committed
Add new Go collector example
1 parent 854d930 commit a72b759

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
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
*~

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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
// A minimal example of how to include Prometheus instrumentation.
15+
package main
16+
17+
import (
18+
"flag"
19+
"fmt"
20+
"log"
21+
"net/http"
22+
23+
"github.com/prometheus/client_golang/prometheus"
24+
"github.com/prometheus/client_golang/prometheus/collectors"
25+
"github.com/prometheus/client_golang/prometheus/promhttp"
26+
)
27+
28+
var addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
29+
30+
func main() {
31+
flag.Parse()
32+
33+
// Create a new registry.
34+
reg := prometheus.NewRegistry()
35+
36+
// Add Go module build info.
37+
reg.MustRegister(collectors.NewBuildInfoCollector())
38+
reg.MustRegister(collectors.NewGoCollector(
39+
collectors.WithGoCollections(collectors.GoRuntimeMemStatsCollection | collectors.GoRuntimeMetricsCollection),
40+
))
41+
42+
// Expose the registered metrics via HTTP.
43+
http.Handle("/metrics", promhttp.HandlerFor(
44+
reg,
45+
promhttp.HandlerOpts{
46+
// Opt into OpenMetrics to support exemplars.
47+
EnableOpenMetrics: true,
48+
},
49+
))
50+
fmt.Println("Hello world from new Go Collector!")
51+
log.Fatal(http.ListenAndServe(*addr, nil))
52+
}

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

0 commit comments

Comments
 (0)