Skip to content

feat: add prometheus metric for tracking user statuses #15281

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

Merged
merged 5 commits into from
Oct 30, 2024
Merged

Conversation

coadler
Copy link
Contributor

@coadler coadler commented Oct 30, 2024

Adds the ability to track total users in the platform by status. This can help for seeing how license seat usage is being used over time.

Closes #15278

@coadler coadler requested review from dannykopping and sreya October 30, 2024 02:03
@coadler coadler self-assigned this Oct 30, 2024
Copy link
Contributor

@dannykopping dannykopping left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, a few nits.

@coadler coadler enabled auto-merge (squash) October 30, 2024 18:22
@coadler coadler merged commit 3de98c2 into main Oct 30, 2024
26 checks passed
@coadler coadler deleted the colin/user-prom branch October 30, 2024 18:41
@github-actions github-actions bot locked and limited conversation to collaborators Oct 30, 2024
}, testutil.WaitShort, testutil.IntervalSlow)
}

require.Eventually(t, checkFn, testutil.WaitShort, testutil.IntervalFast)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hhmm, do we need require.Eventually with quartz now implemented (thank you for doing this!)?

Copy link
Contributor Author

@coadler coadler Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was my assumption that AdvanceNext().MustWait() only waits for the ticker to fire, then the actual gathering code runs async. I guess I could technically make it block until the end using AfterFunc, but that felt kinda janky. My assumption could definitely be wrong tho, in my testing the metrics seemed to always be ready on the first try but I didn't want to risk it.

Comment on lines +97 to +122
ctx, cancelFunc := context.WithCancel(ctx)
done := make(chan struct{})
ticker := clk.NewTicker(duration)
go func() {
defer close(done)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
}

gauge.Reset()
//nolint:gocritic // This is a system service that needs full access
//to the users table.
users, err := db.GetUsers(dbauthz.AsSystemRestricted(ctx), database.GetUsersParams{})
if err != nil {
logger.Error(ctx, "get all users for prometheus metrics", slog.Error(err))
continue
}

for _, user := range users {
gauge.WithLabelValues(string(user.Status)).Inc()
}
}
Copy link
Collaborator

@sreya sreya Oct 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing this you can just use clock.TickerFunc, it removes any timing problems in the tests as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that makes more sense

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prometheus metric breaking down total users by status
3 participants