feat(metrics): Prometheus app metrics (#42) #126

Merged
jmiller merged 1 commits from feat/prometheus-metrics into dev 2026-05-21 01:04:47 +00:00
2 changed files with 61 additions and 0 deletions
+21
View File
@@ -6,6 +6,7 @@ package activities
import (
"context"
actions_model "code.gitea.io/gitea/models/actions"
asymkey_model "code.gitea.io/gitea/models/asymkey"
"code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/db"
@@ -18,6 +19,7 @@ import (
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/optional"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
)
@@ -37,6 +39,11 @@ type Statistic struct {
Branches, Tags, CommitStatus int64
IssueByLabel []IssueByLabelCount
IssueByRepository []IssueByRepositoryCount
// MokoGitea extended metrics
ActiveUsers30d int64
ActionsQueueLength int64
ActionsRunningJobs int64
}
}
@@ -131,5 +138,19 @@ func GetStatistic(ctx context.Context) (stats Statistic) {
stats.Counter.Attachment, _ = e.Count(new(repo_model.Attachment))
stats.Counter.Project, _ = e.Count(new(project_model.Project))
stats.Counter.ProjectColumn, _ = e.Count(new(project_model.Column))
// MokoGitea extended metrics
// Active users in last 30 days (users who performed any action)
stats.Counter.ActiveUsers30d, _ = e.Where("last_login_unix > ?",
timeutil.TimeStampNow()-30*24*60*60).Count(new(user_model.User))
// Actions queue and running jobs (if actions enabled)
if setting.Actions.Enabled {
stats.Counter.ActionsQueueLength, _ = e.Where("status = ?", 1). // StatusWaiting
Count(new(actions_model.ActionRunJob))
stats.Counter.ActionsRunningJobs, _ = e.Where("status = ?", 2). // StatusRunning
Count(new(actions_model.ActionRunJob))
}
return stats
}
+40
View File
@@ -46,6 +46,11 @@ type Collector struct {
Users *prometheus.Desc
Watches *prometheus.Desc
Webhooks *prometheus.Desc
// MokoGitea extended metrics
ActiveUsers30d *prometheus.Desc
ActionsQueueLength *prometheus.Desc
ActionsRunningJobs *prometheus.Desc
}
// NewCollector returns a new Collector with all prometheus.Desc initialized
@@ -196,6 +201,21 @@ func NewCollector() Collector {
"Number of Webhooks",
nil, nil,
),
ActiveUsers30d: prometheus.NewDesc(
namespace+"active_users_30d",
"Number of active users in the last 30 days",
nil, nil,
),
ActionsQueueLength: prometheus.NewDesc(
namespace+"actions_queue_length",
"Number of actions jobs waiting to run",
nil, nil,
),
ActionsRunningJobs: prometheus.NewDesc(
namespace+"actions_running_jobs",
"Number of actions jobs currently running",
nil, nil,
),
}
}
@@ -229,6 +249,9 @@ func (c Collector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.Users
ch <- c.Watches
ch <- c.Webhooks
ch <- c.ActiveUsers30d
ch <- c.ActionsQueueLength
ch <- c.ActionsRunningJobs
}
// Collect returns the metrics with values
@@ -392,4 +415,21 @@ func (c Collector) Collect(ch chan<- prometheus.Metric) {
prometheus.GaugeValue,
float64(stats.Counter.Webhook),
)
// MokoGitea extended metrics
ch <- prometheus.MustNewConstMetric(
c.ActiveUsers30d,
prometheus.GaugeValue,
float64(stats.Counter.ActiveUsers30d),
)
ch <- prometheus.MustNewConstMetric(
c.ActionsQueueLength,
prometheus.GaugeValue,
float64(stats.Counter.ActionsQueueLength),
)
ch <- prometheus.MustNewConstMetric(
c.ActionsRunningJobs,
prometheus.GaugeValue,
float64(stats.Counter.ActionsRunningJobs),
)
}