Skip to content

Commit 86a1261

Browse files
committed
Record metrics for upstream OAuth 2.0 callbacks
1 parent f7919e9 commit 86a1261

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

crates/handlers/src/upstream_oauth2/callback.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
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+
79
use axum::{
810
Form,
911
extract::{Path, State},
@@ -26,6 +28,7 @@ use mas_storage::{
2628
};
2729
use mas_templates::{FormPostContext, Templates};
2830
use oauth2_types::{errors::ClientErrorCode, requests::AccessTokenRequest};
31+
use opentelemetry::{Key, KeyValue, metrics::Counter};
2932
use serde::{Deserialize, Serialize};
3033
use serde_json::json;
3134
use 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)]
4357
pub 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

Comments
 (0)