Skip to content

Add numerical app version metric #18454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"os"
"runtime"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -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()

Expand Down
13 changes: 13 additions & 0 deletions modules/metrics/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package metrics

import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/setting"

"github.com/prometheus/client_golang/prometheus"
)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down