Skip to content

Commit f72ff85

Browse files
committed
Record metrics for upstream OAuth 2.0 logins and registrations
1 parent 86a1261 commit f72ff85

File tree

1 file changed

+29
-2
lines changed
  • crates/handlers/src/upstream_oauth2

1 file changed

+29
-2
lines changed

crates/handlers/src/upstream_oauth2/link.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// SPDX-License-Identifier: AGPL-3.0-only
55
// Please see LICENSE in the repository root for full details.
66

7-
use std::sync::Arc;
7+
use std::sync::{Arc, LazyLock};
88

99
use axum::{
1010
Form,
@@ -35,6 +35,7 @@ use mas_templates::{
3535
ToFormState, UpstreamExistingLinkContext, UpstreamRegister, UpstreamSuggestLink,
3636
};
3737
use minijinja::Environment;
38+
use opentelemetry::{Key, KeyValue, metrics::Counter};
3839
use serde::{Deserialize, Serialize};
3940
use thiserror::Error;
4041
use tracing::warn;
@@ -45,10 +46,26 @@ use super::{
4546
template::{AttributeMappingContext, environment},
4647
};
4748
use crate::{
48-
BoundActivityTracker, PreferredLanguage, SiteConfig, impl_from_error_for_route,
49+
BoundActivityTracker, METER, PreferredLanguage, SiteConfig, impl_from_error_for_route,
4950
views::shared::OptionalPostAuthAction,
5051
};
5152

53+
static LOGIN_COUNTER: LazyLock<Counter<u64>> = LazyLock::new(|| {
54+
METER
55+
.u64_counter("mas.upstream_oauth2.login")
56+
.with_description("Successful upstream OAuth 2.0 login to existing accounts")
57+
.with_unit("{login}")
58+
.build()
59+
});
60+
static REGISTRATION_COUNTER: LazyLock<Counter<u64>> = LazyLock::new(|| {
61+
METER
62+
.u64_counter("mas.upstream_oauth2.registration")
63+
.with_description("Successful upstream OAuth 2.0 registration")
64+
.with_unit("{registration}")
65+
.build()
66+
});
67+
const PROVIDER: Key = Key::from_static_str("provider");
68+
5269
const DEFAULT_LOCALPART_TEMPLATE: &str = "{{ user.preferred_username }}";
5370
const DEFAULT_DISPLAYNAME_TEMPLATE: &str = "{{ user.name }}";
5471
const DEFAULT_EMAIL_TEMPLATE: &str = "{{ user.email }}";
@@ -340,6 +357,14 @@ pub(crate) async fn get(
340357

341358
repo.save().await?;
342359

360+
LOGIN_COUNTER.add(
361+
1,
362+
&[KeyValue::new(
363+
PROVIDER,
364+
upstream_session.provider_id.to_string(),
365+
)],
366+
);
367+
343368
post_auth_action.go_next(&url_builder).into_response()
344369
}
345370

@@ -805,6 +830,8 @@ pub(crate) async fn post(
805830
.into_response());
806831
}
807832

833+
REGISTRATION_COUNTER.add(1, &[KeyValue::new(PROVIDER, provider.id.to_string())]);
834+
808835
// Now we can create the user
809836
let user = repo.user().add(&mut rng, &clock, username).await?;
810837

0 commit comments

Comments
 (0)