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