Skip to main content

Crate metrique_writer_cloudwatch

Crate metrique_writer_cloudwatch 

Source
Expand description

CloudWatch Logs PutLogEvents (EMF) backend for metrique.

This crate provides CwLogsStream, an EntryIoStream implementation that serializes metric entries as EMF JSON and submits them directly to CloudWatch Logs via PutLogEvents.

§Architecture

App threads → BackgroundQueue (metrique) → std thread calls CwLogsStream::next(&entry)
  → EMF serialize (reuses metrique-writer-format-emf)
  → batch until size/count limit hit: try_send(batch) over bounded channel
  → single async task: recv batch → client.put_log_events (async HTTP)

§Runtime

The tokio_runtime feature is enabled by default and provides the default TaskSpawner::tokio() spawner. With --no-default-features, callers must provide a custom TaskSpawner with CwLogsStream::builder(). Custom spawners must detach or otherwise keep the submitted future running after the spawn callback returns.

For non-Tokio executors, configure the AWS client with a compatible AsyncSleep. SDK timeouts and retry delays use that sleep hook.

§Backpressure

When the submission channel is full (CloudWatch Logs is slow or down), batches are dropped at submission time with a rate-limited warning. Drops are reported through CwLogsStreamEvent::BatchDropped. Once the channel drains, normal submission resumes.

§Shutdown

CwLogsStream::builder() returns (CwLogsStream, CwLogsStreamHandle). Call CwLogsStreamHandle::shutdown() to await drain of in-flight batches.

§Example

use metrique_writer_cloudwatch::{CwLogsStream, CwLogsStreamConfig};
use metrique_writer_core::EntryIoStream;

let sdk_config = aws_config::load_from_env().await;
let client = aws_sdk_cloudwatchlogs::Client::new(&sdk_config);

let (stream, handle) = CwLogsStream::builder()
    .client(client)
    .log_group_name("/my-app/metrics".to_string())
    .log_stream_name("host-1".to_string())
    .namespace("MyApp".to_string())
    .default_dimensions(vec![vec![]])
    .build();

// Pass `stream` to metrique's BackgroundQueue or ServiceMetrics::attach_to_stream()
// ...

// Graceful shutdown:
handle.shutdown().await;

Structs§

CwLogsStream
An EntryIoStream that serializes entries as EMF JSON and submits them to CloudWatch Logs via PutLogEvents in a background async task.
CwLogsStreamBuilder
Use builder syntax to set the inputs and finish with build().
CwLogsStreamConfig
Configuration for CwLogsStream.
CwLogsStreamConfigBuilder
Use builder syntax to set the inputs and finish with build().
CwLogsStreamHandle
Handle for async shutdown of CwLogsStream.
NoOpObserver
Default no-op observer.
TaskSpawner
A wrapper around a task spawner function.

Enums§

CwLogsStreamEvent
Events emitted by CwLogsStream for observability.

Traits§

CwLogsStreamObserver
Observer for CwLogsStreamEvents. Implement this to collect internal metrics.

Type Aliases§

TaskSpawnerFn
Type alias for a task spawner function.