Skip to content

Commit

Permalink
feat: include user rank in prometheus metrics (resolve #566)
Browse files Browse the repository at this point in the history
  • Loading branch information
muety committed Dec 2, 2023
1 parent f82ab82 commit db90463
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func main() {
healthApiHandler := api.NewHealthApiHandler(db)
heartbeatApiHandler := api.NewHeartbeatApiHandler(userService, heartbeatService, languageMappingService)
summaryApiHandler := api.NewSummaryApiHandler(userService, summaryService)
metricsHandler := api.NewMetricsHandler(userService, summaryService, heartbeatService, keyValueService, metricsRepository)
metricsHandler := api.NewMetricsHandler(userService, summaryService, heartbeatService, leaderboardService, keyValueService, metricsRepository)
diagnosticsHandler := api.NewDiagnosticsApiHandler(userService, diagnosticsService)
avatarHandler := api.NewAvatarHandler()
activityHandler := api.NewActivityApiHandler(userService, activityService)
Expand Down
46 changes: 33 additions & 13 deletions routes/api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
DescOperatingSystems = "Total seconds for each operating system."
DescMachines = "Total seconds for each machine."
DescLabels = "Total seconds for each project label."
DescRank = "User's current rank in the public leaderboard."

DescAdminTotalTime = "Total seconds (all users, all time)."
DescAdminTotalHeartbeats = "Total number of tracked heartbeats (all users, all time)"
Expand All @@ -53,22 +54,24 @@ const (
)

type MetricsHandler struct {
config *conf.Config
userSrvc services.IUserService
summarySrvc services.ISummaryService
heartbeatSrvc services.IHeartbeatService
keyValueSrvc services.IKeyValueService
metricsRepo *repositories.MetricsRepository
config *conf.Config
userSrvc services.IUserService
summarySrvc services.ISummaryService
heartbeatSrvc services.IHeartbeatService
leaderboardSrvc services.ILeaderboardService
keyValueSrvc services.IKeyValueService
metricsRepo *repositories.MetricsRepository
}

func NewMetricsHandler(userService services.IUserService, summaryService services.ISummaryService, heartbeatService services.IHeartbeatService, keyValueService services.IKeyValueService, metricsRepo *repositories.MetricsRepository) *MetricsHandler {
func NewMetricsHandler(userService services.IUserService, summaryService services.ISummaryService, heartbeatService services.IHeartbeatService, leaderboardService services.ILeaderboardService, keyValueService services.IKeyValueService, metricsRepo *repositories.MetricsRepository) *MetricsHandler {
return &MetricsHandler{
userSrvc: userService,
summarySrvc: summaryService,
heartbeatSrvc: heartbeatService,
keyValueSrvc: keyValueService,
metricsRepo: metricsRepo,
config: conf.Get(),
userSrvc: userService,
summarySrvc: summaryService,
heartbeatSrvc: heartbeatService,
leaderboardSrvc: leaderboardService,
keyValueSrvc: keyValueService,
metricsRepo: metricsRepo,
config: conf.Get(),
}
}

Expand Down Expand Up @@ -149,6 +152,12 @@ func (h *MetricsHandler) getUserMetrics(user *models.User) (*mm.Metrics, error)
return nil, err
}

leaderboard, err := h.leaderboardSrvc.GetByIntervalAndUser(h.leaderboardSrvc.GetDefaultScope(), user.ID, false)
if err != nil {
conf.Log().Error("failed to fetch leaderboard for user '%s' for metric", user.ID)
return nil, err
}

// User Metrics

metrics = append(metrics, &mm.GaugeMetric{
Expand Down Expand Up @@ -226,6 +235,17 @@ func (h *MetricsHandler) getUserMetrics(user *models.User) (*mm.Metrics, error)
})
}

var userRank int64
if leaderboard.HasUser(user.ID) {
userRank = int64(leaderboard[0].Rank)
}
metrics = append(metrics, &mm.GaugeMetric{
Name: MetricsPrefix + "_rank",
Desc: DescRank,
Value: userRank,
Labels: nil,
})

// Runtime metrics
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
Expand Down

0 comments on commit db90463

Please sign in to comment.