44// SPDX-License-Identifier: AGPL-3.0-only
55// Please see LICENSE in the repository root for full details.
66
7+ use std:: sync:: LazyLock ;
8+
79use axum:: {
810 Form ,
911 extract:: { Path , State } ,
@@ -26,6 +28,7 @@ use mas_storage::{
2628} ;
2729use mas_templates:: { FormPostContext , Templates } ;
2830use oauth2_types:: { errors:: ClientErrorCode , requests:: AccessTokenRequest } ;
31+ use opentelemetry:: { Key , KeyValue , metrics:: Counter } ;
2932use serde:: { Deserialize , Serialize } ;
3033use serde_json:: json;
3134use thiserror:: Error ;
@@ -37,7 +40,18 @@ use super::{
3740 client_credentials_for_provider,
3841 template:: { AttributeMappingContext , environment} ,
3942} ;
40- use crate :: { PreferredLanguage , impl_from_error_for_route, upstream_oauth2:: cache:: MetadataCache } ;
43+ use crate :: {
44+ METER , PreferredLanguage , impl_from_error_for_route, upstream_oauth2:: cache:: MetadataCache ,
45+ } ;
46+
47+ static CALLBACK_COUNTER : LazyLock < Counter < u64 > > = LazyLock :: new ( || {
48+ METER
49+ . u64_counter ( "mas.upstream_oauth2.callback" )
50+ . with_description ( "Number of requests to the upstream OAuth2 callback endpoint" )
51+ . build ( )
52+ } ) ;
53+ const PROVIDER : Key = Key :: from_static_str ( "provider" ) ;
54+ const RESULT : Key = Key :: from_static_str ( "result" ) ;
4155
4256#[ derive( Serialize , Deserialize ) ]
4357pub struct Params {
@@ -216,6 +230,14 @@ pub(crate) async fn handler(
216230 }
217231
218232 if let Some ( error) = params. error {
233+ CALLBACK_COUNTER . add (
234+ 1 ,
235+ & [
236+ KeyValue :: new ( PROVIDER , provider_id. to_string ( ) ) ,
237+ KeyValue :: new ( RESULT , "error" ) ,
238+ ] ,
239+ ) ;
240+
219241 return Err ( RouteError :: ClientError {
220242 error,
221243 error_description : params. error_description . clone ( ) ,
@@ -256,6 +278,14 @@ pub(crate) async fn handler(
256278 return Err ( RouteError :: MissingCode ) ;
257279 } ;
258280
281+ CALLBACK_COUNTER . add (
282+ 1 ,
283+ & [
284+ KeyValue :: new ( PROVIDER , provider_id. to_string ( ) ) ,
285+ KeyValue :: new ( RESULT , "success" ) ,
286+ ] ,
287+ ) ;
288+
259289 let mut lazy_metadata = LazyProviderInfos :: new ( & metadata_cache, & provider, & client) ;
260290
261291 // Figure out the client credentials
0 commit comments