Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add simple examples of using telemetry API
  • Loading branch information
RafalSumislawski committed Nov 14, 2022
commit 8a02a41c17aec05a96fe5bac775310ca83b8a4cb
20 changes: 20 additions & 0 deletions examples/extension-telemetry-basic/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "extension-telemetry-basic"
version = "0.1.0"
edition = "2021"


# Use cargo-edit(https://github.com/killercup/cargo-edit#installation)
# to manage dependencies.
# Running `cargo add DEPENDENCY_NAME` will
# add the latest version of a dependency to the list,
# and it will keep the alphabetic ordering for you.

[dependencies]
lambda-extension = { path = "../../lambda-extension" }
serde = "1.0.136"
tokio = { version = "1", features = ["macros", "rt"] }
tracing = { version = "0.1", features = ["log"] }
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt"] }


14 changes: 14 additions & 0 deletions examples/extension-telemetry-basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# AWS Lambda Telemetry extension example

## Build & Deploy

1. Install [cargo-lambda](https://github.com/cargo-lambda/cargo-lambda#installation)
2. Build the extension with `cargo lambda build --release --extension`
3. Deploy the extension as a layer with `cargo lambda deploy --extension`

The last command will give you an ARN for the extension layer that you can use in your functions.


## Build for ARM 64

Build the extension with `cargo lambda build --release --extension --arm64`
59 changes: 59 additions & 0 deletions examples/extension-telemetry-basic/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use lambda_extension::{service_fn, Error, Extension, LambdaTelemetry, LambdaTelemetryRecord, SharedService};
use tracing::info;

async fn handler(events: Vec<LambdaTelemetry>) -> Result<(), Error> {
for event in events {
match event.record {
LambdaTelemetryRecord::Function(record) => info!("[logs] [function] {}", record),
LambdaTelemetryRecord::PlatformInitStart {
initialization_type: _,
phase: _,
runtime_version: _,
runtime_version_arn: _,
} => info!("[platform] Initialization started"),
LambdaTelemetryRecord::PlatformInitRuntimeDone {
initialization_type: _,
phase: _,
status: _,
error_type: _,
spans: _,
} => info!("[platform] Initialization finished"),
LambdaTelemetryRecord::PlatformStart {
request_id,
version: _,
tracing: _,
} => info!("[platform] Handling of request {} started", request_id),
LambdaTelemetryRecord::PlatformRuntimeDone {
request_id,
status: _,
error_type: _,
metrics: _,
spans: _,
tracing: _,
} => info!("[platform] Handling of request {} finished", request_id),
_ => (),
}
}

Ok(())
}

#[tokio::main]
async fn main() -> Result<(), Error> {
// The runtime logging can be enabled here by initializing `tracing` with `tracing-subscriber`
// While `tracing` is used internally, `log` can be used as well if preferred.
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
// disabling time is handy because CloudWatch will add the ingestion time.
.without_time()
.init();

let telemetry_processor = SharedService::new(service_fn(handler));

Extension::new()
.with_telemetry_processor(telemetry_processor)
.run()
.await?;

Ok(())
}
41 changes: 40 additions & 1 deletion lambda-extension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

### Simple extension

The code below creates a simple extension that's registered to every `INVOKE` and `SHUTDOWN` events, and logs them in CloudWatch.
The code below creates a simple extension that's registered to every `INVOKE` and `SHUTDOWN` events.

```rust,no_run
use lambda_extension::{service_fn, Error, LambdaEvent, NextEvent};
Expand Down Expand Up @@ -75,6 +75,45 @@ async fn main() -> Result<(), Error> {

```

### Telemetry processor extension

```rust,no_run
use lambda_extension::{service_fn, Error, Extension, LambdaTelemetry, LambdaTelemetryRecord, SharedService};
use tracing::info;

async fn handler(events: Vec<LambdaTelemetry>) -> Result<(), Error> {
for event in events {
match event.record {
LambdaTelemetryRecord::Function(record) => {
// do something with the function log record
},
LambdaTelemetryRecord::PlatformInitStart {
initialization_type: _,
phase: _,
runtime_version: _,
runtime_version_arn: _,
} => {
// do something with the PlatformInitStart event
},
// more types of telemetry events are available
_ => (),
}
}

Ok(())
}

#[tokio::main]
async fn main() -> Result<(), Error> {
let telemetry_processor = SharedService::new(service_fn(handler));

Extension::new().with_telemetry_processor(telemetry_processor).run().await?;

Ok(())
}

```

## Deployment

Lambda extensions can be added to your functions either using [Lambda layers](https://docs.aws.amazon.com/lambda/latest/dg/using-extensions.html#using-extensions-config), or adding them to [containers images](https://docs.aws.amazon.com/lambda/latest/dg/using-extensions.html#invocation-extensions-images).
Expand Down