Skip to content

Commit 38631c6

Browse files
author
Arthur Silva Sens
authored
chore: Move VersionCollector to collectors directory (#1427)
* collectors/version: Move to collectors directory and remove unrelated code. Signed-off-by: Arthur Silva Sens <[email protected]> * examples: Add Version Collector example Signed-off-by: Arthur Silva Sens <[email protected]> --------- Signed-off-by: Arthur Silva Sens <[email protected]>
1 parent b7b88fa commit 38631c6

File tree

5 files changed

+110
-169
lines changed

5 files changed

+110
-169
lines changed

examples/versioncollector/main.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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/version"
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+
// Build using ldflags, for example:
34+
// go build -ldflags "-X github.com/prometheus/common/version.Version=1.0.0 -X github.com/prometheus/common/version.Branch=abc123" .
35+
func main() {
36+
flag.Parse()
37+
38+
// Create a new registry.
39+
reg := prometheus.NewRegistry()
40+
41+
// Register version collector.
42+
reg.MustRegister(version.NewCollector("example"))
43+
44+
// Expose the registered metrics via HTTP.
45+
http.Handle("/metrics", promhttp.HandlerFor(
46+
reg,
47+
promhttp.HandlerOpts{},
48+
))
49+
fmt.Println("Hello world from new Version Collector!")
50+
log.Fatal(http.ListenAndServe(*addr, nil))
51+
}

go.mod

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/davecgh/go-spew v1.1.1
99
github.com/json-iterator/go v1.1.12
1010
github.com/prometheus/client_model v0.5.0
11-
github.com/prometheus/common v0.45.0
11+
github.com/prometheus/common v0.46.0
1212
github.com/prometheus/procfs v0.12.0
1313
golang.org/x/sys v0.16.0
1414
google.golang.org/protobuf v1.32.0
@@ -18,13 +18,12 @@ require (
1818
github.com/golang/protobuf v1.5.3 // indirect
1919
github.com/jpillora/backoff v1.0.0 // indirect
2020
github.com/kr/pretty v0.3.1 // indirect
21-
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
2221
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
2322
github.com/modern-go/reflect2 v1.0.2 // indirect
2423
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
25-
golang.org/x/net v0.17.0 // indirect
26-
golang.org/x/oauth2 v0.12.0 // indirect
27-
golang.org/x/text v0.13.0 // indirect
24+
golang.org/x/net v0.20.0 // indirect
25+
golang.org/x/oauth2 v0.16.0 // indirect
26+
golang.org/x/text v0.14.0 // indirect
2827
google.golang.org/appengine v1.6.7 // indirect
2928
gopkg.in/yaml.v2 v2.4.0 // indirect
3029
)

go.sum

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
2121
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
2222
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
2323
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
24-
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg=
25-
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k=
2624
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
2725
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
2826
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -35,8 +33,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
3533
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3634
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
3735
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
38-
github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lneoxM=
39-
github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY=
36+
github.com/prometheus/common v0.46.0 h1:doXzt5ybi1HBKpsZOL0sSkaNHJJqkyfEWZGGqqScV0Y=
37+
github.com/prometheus/common v0.46.0/go.mod h1:Tp0qkxpb9Jsg54QMe+EAmqXkSV7Evdy1BTn+g2pa/hQ=
4038
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
4139
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
4240
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
@@ -46,17 +44,17 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0
4644
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
4745
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
4846
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
49-
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
50-
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
51-
golang.org/x/oauth2 v0.12.0 h1:smVPGxink+n1ZI5pkQa8y6fZT0RW0MgCO5bFpepy4B4=
52-
golang.org/x/oauth2 v0.12.0/go.mod h1:A74bZ3aGXgCY0qaIC9Ahg6Lglin4AMAco8cIv9baba4=
47+
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
48+
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
49+
golang.org/x/oauth2 v0.16.0 h1:aDkGMBSYxElaoP81NpoUoz2oo2R2wHdZpGToUxfyQrQ=
50+
golang.org/x/oauth2 v0.16.0/go.mod h1:hqZ+0LWXsiVoZpeld6jVt06P3adbS2Uu911W1SsJv2o=
5351
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
5452
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
5553
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
5654
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
5755
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
58-
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
59-
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
56+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
57+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
6058
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
6159
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
6260
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2016 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+
package version
15+
16+
import (
17+
"fmt"
18+
19+
"github.com/prometheus/common/version"
20+
21+
"github.com/prometheus/client_golang/prometheus"
22+
)
23+
24+
// NewCollector returns a collector that exports metrics about current version
25+
// information.
26+
func NewCollector(program string) prometheus.Collector {
27+
return prometheus.NewGaugeFunc(
28+
prometheus.GaugeOpts{
29+
Namespace: program,
30+
Name: "build_info",
31+
Help: fmt.Sprintf(
32+
"A metric with a constant '1' value labeled by version, revision, branch, goversion from which %s was built, and the goos and goarch for the build.",
33+
program,
34+
),
35+
ConstLabels: prometheus.Labels{
36+
"version": version.Version,
37+
"revision": version.GetRevision(),
38+
"branch": version.Branch,
39+
"goversion": version.GoVersion,
40+
"goos": version.GoOS,
41+
"goarch": version.GoArch,
42+
"tags": version.GetTags(),
43+
},
44+
},
45+
func() float64 { return 1 },
46+
)
47+
}

version/version.go

Lines changed: 0 additions & 154 deletions
This file was deleted.

0 commit comments

Comments
 (0)