oauth2_broker/
obs.rs

1//! Optional observability helpers for broker flows.
2//!
3//! # Feature Flags
4//!
5//! - Enable `tracing` to emit structured spans named `oauth2_broker.flow` with the `flow` (grant)
6//!   and `stage` (call site) fields.
7//! - Enable `metrics` to increment the `oauth2_broker_flow_total` counter for every
8//!   attempt/success/failure, labeled by `flow` + `outcome`.
9
10mod metrics;
11mod tracing;
12
13pub use metrics::*;
14pub use tracing::*;
15
16// self
17use crate::_prelude::*;
18
19/// OAuth flow kinds observed by the broker.
20#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
21pub enum FlowKind {
22	/// Authorization Code + PKCE grant helpers.
23	AuthorizationCode,
24	/// Refresh token flow.
25	Refresh,
26	/// Client Credentials flow.
27	ClientCredentials,
28}
29impl FlowKind {
30	/// Returns a stable label suitable for span or metric fields.
31	pub const fn as_str(self) -> &'static str {
32		match self {
33			FlowKind::AuthorizationCode => "authorization_code",
34			FlowKind::Refresh => "refresh",
35			FlowKind::ClientCredentials => "client_credentials",
36		}
37	}
38}
39impl Display for FlowKind {
40	fn fmt(&self, f: &mut Formatter) -> FmtResult {
41		f.write_str(self.as_str())
42	}
43}
44
45/// Outcome labels recorded for each attempt.
46#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
47pub enum FlowOutcome {
48	/// Entry to a broker helper.
49	Attempt,
50	/// Successful completion.
51	Success,
52	/// Failure propagated back to the caller.
53	Failure,
54}
55impl FlowOutcome {
56	/// Returns a stable label suitable for span or metric fields.
57	pub const fn as_str(self) -> &'static str {
58		match self {
59			FlowOutcome::Attempt => "attempt",
60			FlowOutcome::Success => "success",
61			FlowOutcome::Failure => "failure",
62		}
63	}
64}
65impl Display for FlowOutcome {
66	fn fmt(&self, f: &mut Formatter) -> FmtResult {
67		f.write_str(self.as_str())
68	}
69}