lambda_extension/
events.rs

1use serde::Deserialize;
2
3/// Request tracing information
4#[derive(Debug, Default, Deserialize)]
5#[serde(rename_all = "camelCase")]
6pub struct Tracing {
7    /// The type of tracing exposed to the extension
8    pub r#type: String,
9    /// The span value
10    pub value: String,
11}
12/// Event received when there is a new Lambda invocation.
13#[derive(Debug, Deserialize)]
14#[serde(rename_all = "camelCase")]
15pub struct InvokeEvent {
16    /// The time that the function times out
17    pub deadline_ms: u64,
18    /// The ID assigned to the Lambda request
19    pub request_id: String,
20    /// The function's Amazon Resource Name
21    pub invoked_function_arn: String,
22    /// The request tracing information
23    #[serde(default)]
24    pub tracing: Tracing,
25}
26
27/// Event received when a Lambda function shuts down.
28#[derive(Debug, Deserialize)]
29#[serde(rename_all = "camelCase")]
30pub struct ShutdownEvent {
31    /// The reason why the function terminates
32    /// It can be SPINDOWN, TIMEOUT, or FAILURE
33    pub shutdown_reason: String,
34    /// The time that the function times out
35    pub deadline_ms: u64,
36}
37
38/// Event that the extension receives in
39/// either the INVOKE or SHUTDOWN phase
40#[derive(Debug, Deserialize)]
41#[serde(rename_all = "UPPERCASE", tag = "eventType")]
42pub enum NextEvent {
43    /// Payload when the event happens in the INVOKE phase
44    Invoke(InvokeEvent),
45    /// Payload when the event happens in the SHUTDOWN phase
46    Shutdown(ShutdownEvent),
47}
48
49impl NextEvent {
50    /// Return whether the event is a [`NextEvent::Invoke`] event or not
51    pub fn is_invoke(&self) -> bool {
52        matches!(self, NextEvent::Invoke(_))
53    }
54}
55
56/// Wrapper with information about the next
57/// event that the Lambda Runtime is going to process
58pub struct LambdaEvent {
59    /// Next incoming event
60    pub next: NextEvent,
61}
62
63impl LambdaEvent {
64    pub(crate) fn new(next: NextEvent) -> LambdaEvent {
65        LambdaEvent { next }
66    }
67}