Skip to content
Open
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
49 changes: 43 additions & 6 deletions crates/handlers/src/views/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ static PASSWORD_LOGIN_COUNTER: LazyLock<Counter<u64>> = LazyLock::new(|| {
.build()
});
const RESULT: Key = Key::from_static_str("result");
const REASON: Key = Key::from_static_str("reason");

#[derive(Debug, Deserialize, Serialize)]
pub(crate) struct LoginForm {
Expand Down Expand Up @@ -209,7 +210,13 @@ pub(crate) async fn post(

if !form_state.is_valid() {
tracing::warn!("Invalid login form: {form_state:?}");
PASSWORD_LOGIN_COUNTER.add(1, &[KeyValue::new(RESULT, "error")]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think to avoid breaking the existing metric, I would rather add a new label? like {result="error", reason="form-invalid"}? Most dashboard and alerting rules should anyway sum on (result) … or similar since MAS scales horizontally and usually has multiple instances, so adding cardinality with a new label isn't a breaking change

@reivilibre reivilibre Dec 1, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fair enough, though note result=mismatch is already a potential label. Having the split reason is going to feel a little bit inconsistent

PASSWORD_LOGIN_COUNTER.add(
1,
&[
KeyValue::new(RESULT, "error"),
KeyValue::new(REASON, "form-invalid"),
],
);
return render(
locale,
cookie_jar,
Expand All @@ -236,7 +243,13 @@ pub(crate) async fn post(
else {
tracing::warn!(username, "User not found");
let form_state = form_state.with_error_on_form(FormError::InvalidCredentials);
PASSWORD_LOGIN_COUNTER.add(1, &[KeyValue::new(RESULT, "error")]);
PASSWORD_LOGIN_COUNTER.add(
1,
&[
KeyValue::new(RESULT, "error"),
KeyValue::new(REASON, "user-not-found"),
],
);
return render(
locale,
cookie_jar,
Expand All @@ -257,7 +270,13 @@ pub(crate) async fn post(
if let Err(e) = limiter.check_password(requester, &user) {
tracing::warn!(error = &e as &dyn std::error::Error, "ratelimit exceeded");
let form_state = form_state.with_error_on_form(FormError::RateLimitExceeded);
PASSWORD_LOGIN_COUNTER.add(1, &[KeyValue::new(RESULT, "error")]);
PASSWORD_LOGIN_COUNTER.add(
1,
&[
KeyValue::new(RESULT, "error"),
KeyValue::new(REASON, "ratelimited"),
],
);
return render(
locale,
cookie_jar,
Expand All @@ -280,7 +299,13 @@ pub(crate) async fn post(
// a generic 'invalid credentials' error instead
tracing::warn!(username, "No password for user");
let form_state = form_state.with_error_on_form(FormError::InvalidCredentials);
PASSWORD_LOGIN_COUNTER.add(1, &[KeyValue::new(RESULT, "error")]);
PASSWORD_LOGIN_COUNTER.add(
1,
&[
KeyValue::new(RESULT, "error"),
KeyValue::new(REASON, "no-password-for-user"),
],
);
return render(
locale,
cookie_jar,
Expand Down Expand Up @@ -350,7 +375,13 @@ pub(crate) async fn post(
// the sign-in button on the interstitial resumes the flow the user started.
if user.deactivated_at.is_some() {
tracing::warn!(username, "User is deactivated");
PASSWORD_LOGIN_COUNTER.add(1, &[KeyValue::new(RESULT, "error")]);
PASSWORD_LOGIN_COUNTER.add(
1,
&[
KeyValue::new(RESULT, "error"),
KeyValue::new(REASON, "deactivated"),
],
);
let (cookie_jar, response) = render_account_inactive(
&templates,
&locale,
Expand All @@ -365,7 +396,13 @@ pub(crate) async fn post(

if user.locked_at.is_some() {
tracing::warn!(username, "User is locked");
PASSWORD_LOGIN_COUNTER.add(1, &[KeyValue::new(RESULT, "error")]);
PASSWORD_LOGIN_COUNTER.add(
1,
&[
KeyValue::new(RESULT, "error"),
KeyValue::new(REASON, "locked"),
],
);
let (cookie_jar, response) = render_account_inactive(
&templates,
&locale,
Expand Down
Loading