cdk_prometheus/lib.rs
1//! # CDK Prometheus
2
3/// Error definitions
4pub mod error;
5/// Metrics definitions
6pub mod metrics;
7/// Server implementation
8pub mod server;
9
10#[cfg(feature = "system-metrics")]
11/// System metrics
12pub mod process;
13
14// Re-exports for convenience
15pub use error::{PrometheusError, Result};
16pub use metrics::{global, CdkMetrics, METRICS};
17#[cfg(feature = "system-metrics")]
18pub use process::SystemMetrics;
19// Re-export prometheus crate for custom metrics
20pub use prometheus;
21pub use server::{PrometheusBuilder, PrometheusConfig, PrometheusServer};
22
23/// Macro for recording metrics with optional fallback to global instance
24///
25/// Usage:
26/// ```rust
27/// use cdk_prometheus::record_metrics;
28///
29/// // With optional metrics instance
30/// record_metrics!(metrics_option => {
31/// dec_in_flight_requests("operation");
32/// record_mint_operation("operation", true);
33/// });
34///
35/// // Direct global calls
36/// record_metrics!({
37/// dec_in_flight_requests("operation");
38/// record_mint_operation("operation", true);
39/// });
40/// ```
41#[macro_export]
42macro_rules! record_metrics {
43 // Pattern for using optional metrics with fallback to global
44 ($metrics_opt:expr => { $($method:ident($($arg:expr),*));* $(;)? }) => {
45 #[cfg(feature = "prometheus")]
46 {
47 if let Some(metrics) = $metrics_opt.as_ref() {
48 $(
49 metrics.$method($($arg),*);
50 )*
51 } else {
52 $(
53 $crate::global::$method($($arg),*);
54 )*
55 }
56 }
57 };
58
59 // Pattern for using global metrics directly
60 ({ $($method:ident($($arg:expr),*));* $(;)? }) => {
61 #[cfg(feature = "prometheus")]
62 {
63 $(
64 $crate::global::$method($($arg),*);
65 )*
66 }
67 };
68}
69
70/// Convenience function to create a new CDK metrics instance
71///
72/// # Errors
73/// Returns an error if any of the metrics cannot be created or registered
74pub fn create_cdk_metrics() -> Result<CdkMetrics> {
75 CdkMetrics::new()
76}
77
78/// Convenience function to start a Prometheus server with specific metrics
79///
80/// # Errors
81/// Returns an error if the server cannot be created or started
82pub async fn start_default_server_with_metrics(
83 shutdown_signal: impl std::future::Future<Output = ()> + Send + 'static,
84) -> Result<()> {
85 let server = PrometheusBuilder::new().build_with_cdk_metrics()?;
86
87 server.start(shutdown_signal).await
88}