diff --git a/main.go b/main.go index 19b9dd63275fc..18b7413db2186 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "fmt" "os" "runtime" + "strconv" "strings" "time" @@ -39,7 +40,15 @@ var ( ) func init() { + shortVer := strings.Split(Version, "+")[0] + shortVer = strings.ReplaceAll(shortVer, ".", "") + numVer, err := strconv.Atoi(shortVer) + if err != nil { + numVer = -1 + } + setting.AppVer = Version + setting.NumAppVer = numVer setting.AppBuiltWith = formatBuiltWith() setting.AppStartTime = time.Now().UTC() diff --git a/modules/metrics/collector.go b/modules/metrics/collector.go index 069633a565a20..02d95477f9f6d 100755 --- a/modules/metrics/collector.go +++ b/modules/metrics/collector.go @@ -6,6 +6,7 @@ package metrics import ( "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/setting" "github.com/prometheus/client_golang/prometheus" ) @@ -41,6 +42,7 @@ type Collector struct { Teams *prometheus.Desc UpdateTasks *prometheus.Desc Users *prometheus.Desc + Version *prometheus.Desc Watches *prometheus.Desc Webhooks *prometheus.Desc } @@ -178,6 +180,11 @@ func NewCollector() Collector { "Number of Users", nil, nil, ), + Version: prometheus.NewDesc( + namespace+"version", + "Gitea version", + nil, nil, + ), Watches: prometheus.NewDesc( namespace+"watches", "Number of Watches", @@ -219,6 +226,7 @@ func (c Collector) Describe(ch chan<- *prometheus.Desc) { ch <- c.Teams ch <- c.UpdateTasks ch <- c.Users + ch <- c.Version ch <- c.Watches ch <- c.Webhooks } @@ -363,6 +371,11 @@ func (c Collector) Collect(ch chan<- prometheus.Metric) { prometheus.GaugeValue, float64(stats.Counter.User), ) + ch <- prometheus.MustNewConstMetric( + c.Version, + prometheus.GaugeValue, + float64(setting.NumAppVer), + ) ch <- prometheus.MustNewConstMetric( c.Watches, prometheus.GaugeValue, diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 5b8683f578382..2c715f1ee0e0d 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -67,6 +67,8 @@ const ( var ( // AppVer is the version of the current build of Gitea. It is set in main.go from main.Version. AppVer string + // NumAppVer is the version of the current build of Gitea in numerical form. It is set in main.go from main.Version. + NumAppVer int // AppBuiltWith represents a human readable version go runtime build version and build tags. (See main.go formatBuiltWith().) AppBuiltWith string // AppStartTime store time gitea has started