SimpleCloudWatch

Struct SimpleCloudWatch 

Source
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 stdout and using the CloudWatch Agent.
  • Logging to stdout with a CloudWatchCout service and using the CloudWatch Agent.
  • Using AWS Lambda integrated logging.

Implementations§

Source§

impl SimpleCloudWatch

Source

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 - A Config object 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);
Source

pub fn new_formatted<F>( config: Config, formatter: F, ) -> Box<dyn CloudWatch + Send + Sync>
where F: MessageFormatter + Send + Sync + 'static,

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 of MessageFormatter.
§Type Parameters
  • F - The specific type of the formatter, which must be Send + Sync + 'static.
Source

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());
Source

pub fn from_env_formatted<F>( log_group: String, formatter: F, ) -> Box<dyn CloudWatch + Send + Sync>
where F: MessageFormatter + Send + Sync + 'static,

Source

pub fn log_group(&self) -> &str

Returns the configured Log Group name.

Source

pub fn get_data(&self) -> Arc<Data>

Returns the internal data

Trait Implementations§

Source§

impl CloudWatch for SimpleCloudWatch

Source§

fn status(&self) -> Status

Checks if the service is operational.

This performs a synchronous check against AWS to verify:

  1. Connectivity and Credentials.
  2. Existence of the target Log Group.
Source§

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:

  1. 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.
  2. 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.
  3. Synchronous Bridge: It utilizes the internal Tokio [Runtime][tokio::runtime::Runtime] via block_on to execute the asynchronous AWS SDK PutLogEvents call 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).

Source§

impl Fallback for SimpleCloudWatch

Source§

fn fallback(&self, _error: &ServiceError, msg: &Message)

Handles a message that could not be delivered to the primary service. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more