pub struct SimpleCloudWatch { /* private fields */ }Expand description
Standard implementation of the CloudWatch service using the AWS SDK.
§Design Considerations
AWS SDK for Rust is natively asynchronous. This implementation wraps the
asynchronous client and manages an internal Tokio [Runtime][tokio::runtime::Runtime] to provide
a synchronous (blocking) API.
§Performance Note
Initializing this struct has a high overhead due to the creation of a
multi-threaded Tokio [Runtime][tokio::runtime::Runtime]. It is recommended to initialize this service
once and reuse the instance throughout the application’s lifecycle.
§Alternatives
If high-performance logging is required without the overhead of an internal runtime, consider:
- Logging to
stdoutand using the CloudWatch Agent. - Logging to
stdoutwith aCloudWatchCoutservice and using the CloudWatch Agent. - Using AWS Lambda integrated logging.
Implementations§
Source§impl SimpleCloudWatch
impl SimpleCloudWatch
Sourcepub fn new(config: Config) -> Box<dyn CloudWatch + Send + Sync>
pub fn new(config: Config) -> Box<dyn CloudWatch + Send + Sync>
Initializes a new SimpleCloudWatch service using the StandardMessageFormatter.
This is a convenience wrapper around Self::new_formatted. It creates a
multi-threaded Tokio runtime to manage the asynchronous AWS SDK
synchronously under the hood.
§Arguments
config- AConfigobject containing AWS credentials and log group.
§Panics
Panics if the Tokio [Runtime][tokio::runtime::Runtime] fails to initialize or the AWS region is invalid.
§Example
let config = Config::new("access_key", "secret", "my-group", "us-east-1");
let service = SimpleCloudWatch::new(config);Sourcepub fn new_formatted<F>(
config: Config,
formatter: F,
) -> Box<dyn CloudWatch + Send + Sync>
pub fn new_formatted<F>( config: Config, formatter: F, ) -> Box<dyn CloudWatch + Send + Sync>
Initializes a new SimpleCloudWatch service with a custom MessageFormatter.
Use this method if you need to customize how logs are structured (e.g., adding extra fields, changing JSON keys, or using a non-JSON format).
§Arguments
config- AWS configuration and credentials.formatter- An implementation ofMessageFormatter.
§Type Parameters
F- The specific type of the formatter, which must beSend + Sync + 'static.
Sourcepub fn from_env(log_group: String) -> Box<dyn CloudWatch + Send + Sync>
pub fn from_env(log_group: String) -> Box<dyn CloudWatch + Send + Sync>
Initializes the service using AWS standard environment variables.
It looks for AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_REGION.
§Arguments
log_group- The name of the CloudWatch Log Group to use.
§Panics
Panics if the internal Tokio [Runtime][tokio::runtime::Runtime] fails to initialize.
§Example
// Assumes AWS_REGION and credentials are set in the environment
let cw_service = SimpleCloudWatch::from_env("my-app-logs".to_string());pub fn from_env_formatted<F>( log_group: String, formatter: F, ) -> Box<dyn CloudWatch + Send + Sync>
Trait Implementations§
Source§impl CloudWatch for SimpleCloudWatch
impl CloudWatch for SimpleCloudWatch
Source§fn status(&self) -> Status
fn status(&self) -> Status
Checks if the service is operational.
This performs a synchronous check against AWS to verify:
- Connectivity and Credentials.
- Existence of the target Log Group.
Source§fn work(&self, receiver: Receiver<CloudWatchMessage>)
fn work(&self, receiver: Receiver<CloudWatchMessage>)
This method runs in a dedicated thread and implements a greedy-drain /// batching strategy to optimize network I/O:
- Idle Efficiency: It uses a blocking
receiver.recv()to wait for the first message, ensuring the thread consumes zero CPU cycles when there is no logging activity. - Batch Formation: Once the first message is received, it performs
non-blocking
try_recv()calls to “drain” all currently pending messages in the channel into a single batch. - Synchronous Bridge: It utilizes the internal Tokio [
Runtime][tokio::runtime::Runtime] viablock_onto execute the asynchronous AWS SDKPutLogEventscall synchronously within the worker thread.
§Thread Safety
This loop is designed to run indefinitely until the receiver is disconnected
(usually when the application starts its shutdown sequence).
Auto Trait Implementations§
impl Freeze for SimpleCloudWatch
impl !RefUnwindSafe for SimpleCloudWatch
impl Send for SimpleCloudWatch
impl Sync for SimpleCloudWatch
impl Unpin for SimpleCloudWatch
impl !UnwindSafe for SimpleCloudWatch
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more