Skip to content

Commit b8b56b5

Browse files
authored
Merge pull request #595 from prometheus/beorn7/go-collector
Add a simple buildInfoCollector
2 parents 7ed96b3 + 8576729 commit b8b56b5

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

examples/random/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ func init() {
6363
// Register the summary and the histogram with Prometheus's default registry.
6464
prometheus.MustRegister(rpcDurations)
6565
prometheus.MustRegister(rpcDurationsHistogram)
66+
// Add Go module build info.
67+
prometheus.MustRegister(prometheus.NewBuildInfoCollector())
6668
}
6769

6870
func main() {

prometheus/build_info.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2019 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+
// +build go1.12
15+
16+
package prometheus
17+
18+
import "runtime/debug"
19+
20+
// readBuildInfo is a wrapper around debug.ReadBuildInfo for Go 1.12+.
21+
func readBuildInfo() (path, version, sum string) {
22+
path, version, sum = "unknown", "unknown", "unknown"
23+
if bi, ok := debug.ReadBuildInfo(); ok {
24+
path = bi.Main.Path
25+
version = bi.Main.Version
26+
sum = bi.Main.Sum
27+
}
28+
return
29+
}

prometheus/build_info_pre_1.12.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2019 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+
// +build !go1.12
15+
16+
package prometheus
17+
18+
// readBuildInfo is a wrapper around debug.ReadBuildInfo for Go versions before
19+
// 1.12. Remove this whole file once the minimum supported Go version is 1.12.
20+
func readBuildInfo() (path, version, sum string) {
21+
return "unknown", "unknown", "unknown"
22+
}

prometheus/go_collector.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type goCollector struct {
3636
msMaxAge time.Duration // Maximum allowed age of old memstats.
3737
}
3838

39-
// NewGoCollector returns a collector which exports metrics about the current Go
39+
// NewGoCollector returns a collector that exports metrics about the current Go
4040
// process. This includes memory stats. To collect those, runtime.ReadMemStats
4141
// is called. This requires to “stop the world”, which usually only happens for
4242
// garbage collection (GC). Take the following implications into account when
@@ -364,3 +364,33 @@ type memStatsMetrics []struct {
364364
eval func(*runtime.MemStats) float64
365365
valType ValueType
366366
}
367+
368+
// NewBuildInfoCollector returns a collector collecting a single metric
369+
// "go_build_info" with the constant value 1 and three labels "path", "version",
370+
// and "checksum". Their label values contain the main module path, version, and
371+
// checksum, respectively. The labels will only have meaningful values if the
372+
// binary is built with Go module support and from source code retrieved from
373+
// the source repository (rather than the local file system). This is usually
374+
// accomplished by building from outside of GOPATH, specifying the full address
375+
// of the main package, e.g. "GO111MODULE=on go run
376+
// github.com/prometheus/client_golang/examples/random". If built without Go
377+
// module support, all label values will be "unknown". If built with Go module
378+
// support but using the source code from the local file system, the "path" will
379+
// be set appropriately, but "checksum" will be empty and "version" will be
380+
// "(devel)".
381+
//
382+
// This collector uses only the build information for the main module. See
383+
// https://github.com/povilasv/prommod for an example of a collector for the
384+
// module dependencies.
385+
func NewBuildInfoCollector() Collector {
386+
path, version, sum := readBuildInfo()
387+
c := &selfCollector{MustNewConstMetric(
388+
NewDesc(
389+
"go_build_info",
390+
"Build information about the main Go module.",
391+
nil, Labels{"path": path, "version": version, "checksum": sum},
392+
),
393+
GaugeValue, 1)}
394+
c.init(c.self)
395+
return c
396+
}

0 commit comments

Comments
 (0)