metrics_cloudwatch_embedded
Purpose
Provide a backend for the metrics facade crate,
to emit metrics in CloudWatch Embedded Metrics Format
Simple Example
let metrics = new
.cloudwatch_namespace
.init
.unwrap;
counter!.increment;
metrics
.set_property
.flush;
AWS Lambda Example
The Lambda Runtime intergration feature handles flushing metrics
after each invoke via either run() alternatives or MetricService which implements the
tower::Service trait.
It also provides optional helpers for:
- emiting a metric on cold starts
- wrapping cold starts in a
tracingspan - decorating metric documents with request id and/or x-ray trace id
In your Cargo.toml add:
= "0.24"
= { = "0.7.0", = ["lambda"] }
= { = "0.3", = false, = ["fmt", "env-filter", "json"] }
main.rs:
use ;
use run;
use ;
use ;
async
async
CloudWatch log after a single invoke (cold start):
INIT_START Runtime Version: provided:al2.v19 Runtime Version ARN: arn:aws:lambda:us-west-2::runtime:d1007133cb0d993d9a42f9fc10442cede0efec65d732c7943b51ebb979b8f3f8
{"level":"INFO","fields":{"message":"Hello from main"},"spans":[{"name":"cold start"}]}
START RequestId: fce53486-160d-41e8-b8c3-8ef0fd0f4051 Version: $LATEST
{"_aws":{"Timestamp":1688294472338,"CloudWatchMetrics":[{"Namespace":"MetricsTest","Dimensions":[["Function"]],"Metrics":[{"Name":"ColdStart","Unit":"Count"}]}]},"Function":"MetricsTest","RequestId":"fce53486-160d-41e8-b8c3-8ef0fd0f4051","ColdStart":1}
{"level":"INFO","fields":{"message":"Hello from function_handler"},"spans":[{"name":"cold start"},{"requestId":"fce53486-160d-41e8-b8c3-8ef0fd0f4051","xrayTraceId":"Root=1-64a15448-4aa914a00d66aa066325d7e3;Parent=60a7d0c22fb2f001;Sampled=0;Lineage=16f3a795:0","name":"Lambda runtime invoke"}]}
{"_aws":{"Timestamp":1688294472338,"CloudWatchMetrics":[{"Namespace":"MetricsTest","Dimensions":[["Function","Method"]],"Metrics":[{"Name":"requests"}]}]},"Function":"MetricsTest","Method":"Default","RequestId":"fce53486-160d-41e8-b8c3-8ef0fd0f4051","requests":1}
END RequestId: fce53486-160d-41e8-b8c3-8ef0fd0f4051
REPORT RequestId: fce53486-160d-41e8-b8c3-8ef0fd0f4051 Duration: 1.22 ms Billed Duration: 11 ms Memory Size: 128 MB Max Memory Used: 13 MB Init Duration: 8.99 ms
Limitations
- Histograms retain up to 100 values (the maximum for a single metric document) between calls to
collector::Collector::flush, overflow will report an error via thetracingcrate - Dimensions set at initialization via
Builder::with_dimension(...)may not overlap with metriclabels - Only the subset of metric units in
metrics::Unitare supported https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_MetricDatum.html - Registering different metric types with the same
metrics::Keywill fail with an error via thetracingcrate - The Embedded Metric Format supports a maximum of 30 dimensions per metric, attempting to register a metric with
more than 30 dimensions/labels will fail with an error via the
tracingcrate
Supported Rust Versions (MSRV)
This crate requires a minimum of Rust 1.82, and is not guaranteed to build on compiler versions earlier than that.
License
This project is licensed under the Apache-2.0 License. Apache-2.0 was chosen to match the Lambda Runtime
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be licensed as above, without any additional terms or conditions.
Thanks
- Simon Andersson (ramn) and contributors - For the metrics_cloudwatch crate I used as a reference
- Toby Lawrence (tobz) - For answering my metrics crate questions before I even had something working