Skip to content

Commit 5ae07d4

Browse files
authored
include build info in Prometheus metrics (#22819)
Related to: #18061 This PR adds build info to the Prometheus metrics. This includes: - goarch: https://pkg.go.dev/runtime#GOARCH - goos: https://pkg.go.dev/runtime#pkg-constants - goversion: https://pkg.go.dev/runtime#Version - gitea version: just exposes the existing code.gitea.io/gitea/modules/setting.AppVer It's a similar approach to what some other Golang projects are doing, e.g. Prometheus: https://github.com/prometheus/common/blob/main/version/info.go example /metrics response from Prometheus: ``` # HELP prometheus_build_info A metric with a constant '1' value labeled by version, revision, branch, goversion from which prometheus was built, and the goos and goarch for the build. # TYPE prometheus_build_info gauge prometheus_build_info{branch="HEAD",goarch="amd64",goos="linux",goversion="go1.19.4",revision="c0d8a56c69014279464c0e15d8bfb0e153af0dab",version="2.41.0"} 1 ``` /metrics response from gitea with this PR: ``` # HELP gitea_build_info Build information # TYPE gitea_build_info gauge gitea_build_info{goarch="amd64",goos="linux",goversion="go1.20",version="2c6cc0b8c"} 1 ``` Signed-off-by: Michal Wasilewski <[email protected]> <!-- Please check the following: 1. Make sure you are targeting the `main` branch, pull requests on release branches are only allowed for bug fixes. 2. Read contributing guidelines: https://github.com/go-gitea/gitea/blob/main/CONTRIBUTING.md 3. Describe what your pull request does and which issue you're targeting (if any) --> Signed-off-by: Michal Wasilewski <[email protected]>
1 parent 7d3c4c3 commit 5ae07d4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Diff for: modules/metrics/collector.go

+24
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
package metrics
55

66
import (
7+
"runtime"
8+
79
activities_model "code.gitea.io/gitea/models/activities"
10+
"code.gitea.io/gitea/modules/setting"
811

912
"github.com/prometheus/client_golang/prometheus"
1013
)
@@ -17,6 +20,7 @@ type Collector struct {
1720
Accesses *prometheus.Desc
1821
Actions *prometheus.Desc
1922
Attachments *prometheus.Desc
23+
BuildInfo *prometheus.Desc
2024
Comments *prometheus.Desc
2125
Follows *prometheus.Desc
2226
HookTasks *prometheus.Desc
@@ -62,6 +66,16 @@ func NewCollector() Collector {
6266
"Number of Attachments",
6367
nil, nil,
6468
),
69+
BuildInfo: prometheus.NewDesc(
70+
namespace+"build_info",
71+
"Build information",
72+
[]string{
73+
"goarch",
74+
"goos",
75+
"goversion",
76+
"version",
77+
}, nil,
78+
),
6579
Comments: prometheus.NewDesc(
6680
namespace+"comments",
6781
"Number of Comments",
@@ -195,6 +209,7 @@ func (c Collector) Describe(ch chan<- *prometheus.Desc) {
195209
ch <- c.Accesses
196210
ch <- c.Actions
197211
ch <- c.Attachments
212+
ch <- c.BuildInfo
198213
ch <- c.Comments
199214
ch <- c.Follows
200215
ch <- c.HookTasks
@@ -241,6 +256,15 @@ func (c Collector) Collect(ch chan<- prometheus.Metric) {
241256
prometheus.GaugeValue,
242257
float64(stats.Counter.Attachment),
243258
)
259+
ch <- prometheus.MustNewConstMetric(
260+
c.BuildInfo,
261+
prometheus.GaugeValue,
262+
1,
263+
runtime.GOARCH,
264+
runtime.GOOS,
265+
runtime.Version(),
266+
setting.AppVer,
267+
)
244268
ch <- prometheus.MustNewConstMetric(
245269
c.Comments,
246270
prometheus.GaugeValue,

0 commit comments

Comments
 (0)