forked from awslabs/aws-lambda-rust-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension-trait.rs
45 lines (39 loc) · 1.36 KB
/
extension-trait.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use lambda_extension::{Error, Extension, NextEvent};
use std::{
future::{ready, Future},
pin::Pin,
};
use tracing::info;
#[derive(Default)]
struct MyExtension {
invoke_count: usize,
}
impl Extension for MyExtension {
type Fut = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
fn call(&mut self, event: NextEvent) -> Self::Fut {
match event {
NextEvent::Shutdown(e) => {
info!("[extension] Shutdown event received: {:?}", e);
}
NextEvent::Invoke(e) => {
self.invoke_count += 1;
info!("[extension] Request event {} received: {:?}", self.invoke_count, e);
}
}
Box::pin(ready(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)
// this needs to be set to false, otherwise ANSI color codes will
// show up in a confusing manner in CloudWatch logs.
.with_ansi(false)
// disabling time is handy because CloudWatch will add the ingestion time.
.without_time()
.init();
lambda_extension::run(MyExtension::default()).await
}